Skip to content

Commit

Permalink
Add ACL flag to put, cp commands
Browse files Browse the repository at this point in the history
This adds the feature requested in #41
  • Loading branch information
rlmcpherson committed Nov 12, 2014
1 parent 7ca1367 commit 5d199b3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
8 changes: 4 additions & 4 deletions gof3r/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"

Expand All @@ -19,8 +18,8 @@ type CpArg struct {
type cpOpts struct {
DataOpts
CommonOpts
Header http.Header `long:"header" short:"m" description:"HTTP headers. May be used to set custom metadata, server-side encryption etc." ini-name:"header"`
CpArg `positional-args:"true" required:"true"`
CpArg `positional-args:"true" required:"true"`
UpOpts
}

var cp cpOpts
Expand Down Expand Up @@ -68,7 +67,8 @@ func (cp *cpOpts) Execute(args []string) (err error) {
if u.Host == "" {
return os.Create(u.Path)
}
return s3.Bucket(u.Host).PutWriter(u.Path, cp.Header, conf)

return s3.Bucket(u.Host).PutWriter(u.Path, ACL(cp.Header, cp.ACL), conf)
}(cp.Dest)
if err != nil {
return
Expand Down
17 changes: 16 additions & 1 deletion gof3r/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"log"
"net/http"
"os"
"os/exec"
"strings"
Expand All @@ -20,14 +21,20 @@ type CommonOpts struct {
Debug bool `long:"debug" description:"Enable debug logging." ini-name:"debug"`
}

// CommonOpts are Options common to cp, get, and put commands
// DataOpts are Options common to cp, get, and put commands
type DataOpts struct {
NoSSL bool `long:"no-ssl" description:"Do not use SSL for endpoint connection." ini-name:"no-ssl"`
NoMd5 bool `long:"no-md5" description:"Do not use md5 hash checking to ensure data integrity. By default, the md5 hash of is calculated concurrently during puts, stored at <bucket>.md5/<key>.md5, and verified on gets." ini-name:"no-md5"`
Concurrency int `long:"concurrency" short:"c" default:"10" description:"Concurrency of transfers" ini-name:"concurrency"`
PartSize int64 `long:"partsize" short:"s" description:"Initial size of concurrent parts, in bytes" default:"20971520" ini-name:"partsize"`
}

// UpOpts are Options for uploading common to cp and put commands
type UpOpts struct {
Header http.Header `long:"header" short:"m" description:"HTTP headers. May be used to set custom metadata, server-side encryption etc." ini-name:"header"`
ACL string `long:"acl" description:"canned acl to apply to the object"`
}

var appOpts struct {
Version func() `long:"version" short:"v" description:"Print version"`
Man func() `long:"manpage" short:"m" description:"Create gof3r.man man page in current directory"`
Expand Down Expand Up @@ -104,3 +111,11 @@ func homeDir() (string, error) {
}
return "", fmt.Errorf("home directory not found for current user")
}

// add canned acl to http.Header
func ACL(h http.Header, acl string) http.Header {
if acl != "" {
h.Set("x-amz-acl", acl)
}
return h
}
11 changes: 11 additions & 0 deletions gof3r/options_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package main

import (
"log"
"net/http"
"os"
"os/user"
"reflect"
"testing"
)

Expand All @@ -28,3 +31,11 @@ func TestHomeDir(t *testing.T) {
}

}

func TestACL(t *testing.T) {
h2 := http.Header{"X-Amz-Acl": []string{"public-read"}}
h3 := ACL(http.Header{}, "public-read")
if !reflect.DeepEqual(h3, h2) {
log.Fatalf("mismatch: %v, %v", h2, h3)
}
}
4 changes: 2 additions & 2 deletions gof3r/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type putOpts struct {
Path string `short:"p" long:"path" description:"Path to file. Defaults to standard output for streaming." no-ini:"true"`
DataOpts
CommonOpts
Header http.Header `long:"header" short:"m" description:"HTTP headers. May be used to set custom metadata, server-side encryption etc." ini-name:"header"`
UpOpts
}

var put putOpts
Expand Down Expand Up @@ -50,7 +50,7 @@ func (put *putOpts) Execute(args []string) (err error) {
}
}
defer r.Close()
w, err := b.PutWriter(put.Key, put.Header, conf)
w, err := b.PutWriter(put.Key, ACL(put.Header, put.ACL), conf)
if err != nil {
return
}
Expand Down

0 comments on commit 5d199b3

Please sign in to comment.