Skip to content

Commit

Permalink
Merge pull request #15 from rhnvrm/develop
Browse files Browse the repository at this point in the history
Support for Custom Metadata in FileUpload and fetching FileDetails
  • Loading branch information
rhnvrm authored Jul 19, 2021
2 parents 7977285 + eac235b commit b23aec1
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 16 deletions.
2 changes: 1 addition & 1 deletion policy.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// LICENSE MIT
// LICENSE BSD-2-Clause-FreeBSD
// Copyright (c) 2018, Rohan Verma <hello@rohanverma.net>
// Copyright (c) 2017, L Campbell
// forked from: https://github.com/lye/s3/
Expand Down
2 changes: 1 addition & 1 deletion sign.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// LICENSE MIT
// LICENSE BSD-2-Clause-FreeBSD
// Copyright (c) 2018, Rohan Verma <hello@rohanverma.net>
// Copyright (C) 2012 Blake Mizerany
// contains code from: github.com/bmizerany/aws4
Expand Down
129 changes: 119 additions & 10 deletions simples3.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// LICENSE MIT
// LICENSE BSD-2-Clause-FreeBSD
// Copyright (c) 2018, Rohan Verma <hello@rohanverma.net>

package simples3
Expand All @@ -25,6 +25,10 @@ import (
const (
securityCredentialsURL = "http://169.254.169.254/latest/meta-data/iam/security-credentials/"
)
const (
// AMZMetaPrefix to prefix metadata key.
AMZMetaPrefix = "x-amz-meta-"
)

// S3 provides a wrapper around your S3 credentials.
type S3 struct {
Expand All @@ -38,12 +42,33 @@ type S3 struct {
URIFormat string
}

// DownloadInput is passed to FileUpload as a parameter.
// DownloadInput is passed to FileDownload as a parameter.
type DownloadInput struct {
Bucket string
ObjectKey string
}

// DetailsInput is passed to FileDetails as a parameter.
type DetailsInput struct {
Bucket string
ObjectKey string
}

// DetailsResponse is returned by FileDetails.
type DetailsResponse struct {
ContentType string
ContentLength string
AcceptRanges string
Date string
Etag string
LastModified string
Server string
AmzID2 string
AmzRequestID string
AmzMeta map[string]string
ExtraHeaders map[string]string
}

// UploadInput is passed to FileUpload as a parameter.
type UploadInput struct {
// essential fields
Expand All @@ -55,19 +80,20 @@ type UploadInput struct {
// optional fields
ContentDisposition string
ACL string
CustomMetadata map[string]string // Setting key/value pairs adds user-defined metadata keys to the object, prefixed with AMZMetaPrefix.

Body io.ReadSeeker
}

// UploadResponse receives the following XML
// in case of success, since we set a 201 response from S3.
// Sample response:
// <PostResponse>
// <Location>https://s3.amazonaws.com/link-to-the-file</Location>
// <Bucket>s3-bucket</Bucket>
// <Key>development/8614bd40-691b-4668-9241-3b342c6cf429/image.jpg</Key>
// <ETag>"32-bit-tag"</ETag>
// </PostResponse>
// <PostResponse>
// <Location>https://s3.amazonaws.com/link-to-the-file</Location>
// <Bucket>s3-bucket</Bucket>
// <Key>development/8614bd40-691b-4668-9241-3b342c6cf429/image.jpg</Key>
// <ETag>"32-bit-tag"</ETag>
// </PostResponse>
type UploadResponse struct {
Location string `xml:"Location"`
Bucket string `xml:"Bucket"`
Expand Down Expand Up @@ -307,7 +333,8 @@ func (s3 *S3) FileUpload(u UploadInput) (UploadResponse, error) {
if err != nil {
return UploadResponse{}, err
}
policies, err := s3.CreateUploadPolicies(UploadConfig{

uc := UploadConfig{
UploadURL: s3.getURL(u.Bucket),
BucketName: u.Bucket,
ObjectKey: u.ObjectKey,
Expand All @@ -318,8 +345,18 @@ func (s3 *S3) FileUpload(u UploadInput) (UploadResponse, error) {
MetaData: map[string]string{
"success_action_status": "201", // returns XML doc on success
},
})
}

// Set custom metadata.
for k, v := range u.CustomMetadata {
if !strings.HasPrefix(k, AMZMetaPrefix) {
k = AMZMetaPrefix + k
}

uc.MetaData[k] = v
}

policies, err := s3.CreateUploadPolicies(uc)
if err != nil {
return UploadResponse{}, err
}
Expand Down Expand Up @@ -406,6 +443,78 @@ func (s3 *S3) FileDelete(u DeleteInput) error {
return nil
}

func (s3 *S3) FileDetails(u DetailsInput) (DetailsResponse, error) {
req, err := http.NewRequest(
http.MethodHead, s3.getURL(u.Bucket, u.ObjectKey), nil,
)
if err != nil {
return DetailsResponse{}, err
}

if err := s3.signRequest(req); err != nil {
return DetailsResponse{}, err
}

res, err := s3.getClient().Do(req)
if err != nil {
return DetailsResponse{}, err
}

if res.StatusCode != 200 {
return DetailsResponse{}, fmt.Errorf("status code: %s", res.Status)
}

var out DetailsResponse
for k, v := range res.Header {
lk := strings.ToLower(k)

switch lk {
case "content-type":
out.ContentType = getFirstString(v)
case "content-length":
out.ContentLength = getFirstString(v)
case "accept-ranges":
out.AcceptRanges = getFirstString(v)
case "date":
out.Date = getFirstString(v)
case "etag":
out.Etag = getFirstString(v)
case "last-modified":
out.LastModified = getFirstString(v)
case "server":
out.Server = getFirstString(v)
case "x-amz-id-2":
out.AmzID2 = getFirstString(v)
case "x-amz-request-id":
out.AmzRequestID = getFirstString(v)
default:
if strings.HasPrefix(lk, AMZMetaPrefix) {
if out.AmzMeta == nil {
out.AmzMeta = map[string]string{}
}

out.AmzMeta[k] = getFirstString(v)
} else {
if out.ExtraHeaders == nil {
out.ExtraHeaders = map[string]string{}
}

out.ExtraHeaders[k] = getFirstString(v)
}
}
}

return out, nil
}

func getFirstString(s []string) string {
if len(s) > 0 {
return s[0]
}

return ""
}

// if object matches reserved string, no need to encode them
var reservedObjectNames = regexp.MustCompile("^[a-zA-Z0-9-_.~/]+$")

Expand Down
51 changes: 47 additions & 4 deletions simples3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ func TestS3_FileUpload(t *testing.T) {
u UploadInput
}
tests := []struct {
name string
fields tConfig
args args
wantErr bool
name string
fields tConfig
args args
testDetails bool
wantErr bool
}{
{
name: "Upload test.txt",
Expand All @@ -63,6 +64,29 @@ func TestS3_FileUpload(t *testing.T) {
},
wantErr: false,
},
{
name: "Upload test.txt with custom metadata",
fields: tConfig{
AccessKey: os.Getenv("AWS_S3_ACCESS_KEY"),
SecretKey: os.Getenv("AWS_S3_SECRET_KEY"),
Endpoint: os.Getenv("AWS_S3_ENDPOINT"),
Region: os.Getenv("AWS_S3_REGION"),
},
args: args{
UploadInput{
Bucket: os.Getenv("AWS_S3_BUCKET"),
ObjectKey: "test_metadata.txt",
ContentType: "text/plain",
FileName: "test.txt",
Body: testTxt,
CustomMetadata: map[string]string{
"test-metadata": "foo-bar",
},
},
},
wantErr: false,
testDetails: true,
},
{
name: "Upload avatar.png",
fields: tConfig{
Expand Down Expand Up @@ -111,10 +135,29 @@ func TestS3_FileUpload(t *testing.T) {
if (err != nil) != tt.wantErr {
t.Errorf("S3.FileUpload() error = %v, wantErr %v", err, tt.wantErr)
}

// reset file, to reuse in further tests.
tt.args.u.Body.Seek(0, 0)

// check for empty response
if (resp == UploadResponse{}) {
t.Errorf("S3.FileUpload() returned empty response, %v", resp)
}

if tt.testDetails {
dResp, err := s3.FileDetails(DetailsInput{
Bucket: tt.args.u.Bucket,
ObjectKey: tt.args.u.ObjectKey,
})

if (err != nil) != tt.wantErr {
t.Errorf("S3.FileUpload() error = %v, wantErr %v", err, tt.wantErr)
}

if len(dResp.AmzMeta) != 1 {
t.Errorf("S3.FileDetails() returned incorrect metadata, got: %v", dResp.AmzMeta)
}
}
})
}
}
Expand Down

0 comments on commit b23aec1

Please sign in to comment.