Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a new command, gof3r info, which fetches metadata about an object. #72

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
55 changes: 55 additions & 0 deletions gof3r/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"log"

"github.com/rlmcpherson/s3gof3r"
)

var info infoOpts

type infoOpts struct {
CommonOpts
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(info.EndPoint, k)
b := s3.Bucket(info.Bucket)

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