Skip to content

Commit

Permalink
Provide a new command, gof3r info, which fetches metadata about an ob…
Browse files Browse the repository at this point in the history
…ject.

The new command will print some small bits of metadata about an object on
success and will exit with a non-zero exit code on failure. The info command
can be used to query for a file's existence without downloading it, and
performs only a HEAD request.

Fixes #71
  • Loading branch information
reem committed Sep 25, 2015
1 parent 4ecf3c1 commit fe750d6
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
29 changes: 29 additions & 0 deletions getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,35 @@ func (g *getter) Close() error {
return nil
}

func GetInfo(b *Bucket, c *Config, key string, version string) (*http.Response, error) {
urlStr, err := b.url(key, c)

if err != nil {
return nil, err
}

for i := 0; i < c.NTry; i++ {
var req *http.Request
req, err := http.NewRequest("HEAD", urlStr.String(), nil)

if err != nil {
return nil, err
}

b.Sign(req)

resp, err := c.Client.Do(req)

if err != nil {
return nil, err
}

return resp, nil
}

return nil, fmt.Errorf("Could not get info.")
}

func (g *getter) checkMd5() (err error) {
calcMd5 := fmt.Sprintf("%x", g.md5.Sum(nil))
md5Path := fmt.Sprint(".md5", g.url.Path, ".md5")
Expand Down
59 changes: 59 additions & 0 deletions gof3r/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"log"

"github.com/rlmcpherson/s3gof3r"
)

var info infoOpts

type infoOpts struct {
Key string `long:"key" short:"k" description:"S3 object key" required:"true" no-ini:"true"`
Bucket string `long:"bucket" short:"b" description:"S3 bucket" required:"true" no-ini:"true"`
VersionID string `short:"v" long:"versionId" description:"Version ID of the object. Incompatible with md5 check (use --no-md5)." no-ini:"true"`
}

func (info *infoOpts) Execute(args []string) (err error) {
conf := new(s3gof3r.Config)
*conf = *s3gof3r.DefaultConfig
k, err := getAWSKeys()
if err != nil {
return err
}

s3 := s3gof3r.New(get.EndPoint, k)
b := s3.Bucket(info.Bucket)
conf.Concurrency = get.Concurrency

if get.NoSSL {
conf.Scheme = "http"
}

resp, err := s3gof3r.GetInfo(b, s3gof3r.DefaultConfig, info.Key, info.VersionID)

if err != nil {
return err
}

if resp.StatusCode >= 200 && resp.StatusCode < 300 {
log.Println("Found object:")
log.Println(" Status: ", resp.StatusCode)
log.Println(" Last-Modified: ", resp.Header["Last-Modified"])
log.Println(" Size: ", resp.Header["Content-Length"])
} else if resp.StatusCode == 403 {
log.Fatal("Access Denied")
} else {
log.Fatal("Non-2XX status code: ", resp.StatusCode)
}

return nil
}

func init() {
_, err := parser.AddCommand("info", "check info from S3", "get information about an object from S3", &info)

if err != nil {
log.Fatal(err)
}
}
9 changes: 9 additions & 0 deletions gof3r/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ var flagTests = []flagTest{
errors.New("expected argument for flag")},
{[]string{"gof3r", "get"},
errors.New("required flags")},
{[]string{"gof3r", "info"},
errors.New("required flags")},
{[]string{"gof3r", "info", "-b"},
errors.New("expected argument for flag")},
{[]string{"gof3r", "info", "-b", "fake-bucket", "-k", "test-key"},
errors.New("Access Denied")},
{[]string{"gof3r", "info", "-b", "fake-bucket", "-k", "key",
"-c", "1", "-s", "1024", "--debug", "--no-ssl", "--no-md5"},
errors.New("Access Denied")},
}

func TestFlags(t *testing.T) {
Expand Down

0 comments on commit fe750d6

Please sign in to comment.