Merge pull request #15 from rhnvrm/develop
Support for Custom Metadata in FileUpload and fetching FileDetails
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
// LICENSE MIT
|
// LICENSE BSD-2-Clause-FreeBSD
|
||||||
// Copyright (c) 2018, Rohan Verma <hello@rohanverma.net>
|
// Copyright (c) 2018, Rohan Verma <hello@rohanverma.net>
|
||||||
// Copyright (c) 2017, L Campbell
|
// Copyright (c) 2017, L Campbell
|
||||||
// forked from: https://github.com/lye/s3/
|
// forked from: https://github.com/lye/s3/
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
// LICENSE MIT
|
// LICENSE BSD-2-Clause-FreeBSD
|
||||||
// Copyright (c) 2018, Rohan Verma <hello@rohanverma.net>
|
// Copyright (c) 2018, Rohan Verma <hello@rohanverma.net>
|
||||||
// Copyright (C) 2012 Blake Mizerany
|
// Copyright (C) 2012 Blake Mizerany
|
||||||
// contains code from: github.com/bmizerany/aws4
|
// contains code from: github.com/bmizerany/aws4
|
||||||
|
|||||||
+119
-10
@@ -1,4 +1,4 @@
|
|||||||
// LICENSE MIT
|
// LICENSE BSD-2-Clause-FreeBSD
|
||||||
// Copyright (c) 2018, Rohan Verma <hello@rohanverma.net>
|
// Copyright (c) 2018, Rohan Verma <hello@rohanverma.net>
|
||||||
|
|
||||||
package simples3
|
package simples3
|
||||||
@@ -25,6 +25,10 @@ import (
|
|||||||
const (
|
const (
|
||||||
securityCredentialsURL = "http://169.254.169.254/latest/meta-data/iam/security-credentials/"
|
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.
|
// S3 provides a wrapper around your S3 credentials.
|
||||||
type S3 struct {
|
type S3 struct {
|
||||||
@@ -38,12 +42,33 @@ type S3 struct {
|
|||||||
URIFormat string
|
URIFormat string
|
||||||
}
|
}
|
||||||
|
|
||||||
// DownloadInput is passed to FileUpload as a parameter.
|
// DownloadInput is passed to FileDownload as a parameter.
|
||||||
type DownloadInput struct {
|
type DownloadInput struct {
|
||||||
Bucket string
|
Bucket string
|
||||||
ObjectKey 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.
|
// UploadInput is passed to FileUpload as a parameter.
|
||||||
type UploadInput struct {
|
type UploadInput struct {
|
||||||
// essential fields
|
// essential fields
|
||||||
@@ -55,6 +80,7 @@ type UploadInput struct {
|
|||||||
// optional fields
|
// optional fields
|
||||||
ContentDisposition string
|
ContentDisposition string
|
||||||
ACL 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
|
Body io.ReadSeeker
|
||||||
}
|
}
|
||||||
@@ -62,12 +88,12 @@ type UploadInput struct {
|
|||||||
// UploadResponse receives the following XML
|
// UploadResponse receives the following XML
|
||||||
// in case of success, since we set a 201 response from S3.
|
// in case of success, since we set a 201 response from S3.
|
||||||
// Sample response:
|
// Sample response:
|
||||||
// <PostResponse>
|
// <PostResponse>
|
||||||
// <Location>https://s3.amazonaws.com/link-to-the-file</Location>
|
// <Location>https://s3.amazonaws.com/link-to-the-file</Location>
|
||||||
// <Bucket>s3-bucket</Bucket>
|
// <Bucket>s3-bucket</Bucket>
|
||||||
// <Key>development/8614bd40-691b-4668-9241-3b342c6cf429/image.jpg</Key>
|
// <Key>development/8614bd40-691b-4668-9241-3b342c6cf429/image.jpg</Key>
|
||||||
// <ETag>"32-bit-tag"</ETag>
|
// <ETag>"32-bit-tag"</ETag>
|
||||||
// </PostResponse>
|
// </PostResponse>
|
||||||
type UploadResponse struct {
|
type UploadResponse struct {
|
||||||
Location string `xml:"Location"`
|
Location string `xml:"Location"`
|
||||||
Bucket string `xml:"Bucket"`
|
Bucket string `xml:"Bucket"`
|
||||||
@@ -307,7 +333,8 @@ func (s3 *S3) FileUpload(u UploadInput) (UploadResponse, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return UploadResponse{}, err
|
return UploadResponse{}, err
|
||||||
}
|
}
|
||||||
policies, err := s3.CreateUploadPolicies(UploadConfig{
|
|
||||||
|
uc := UploadConfig{
|
||||||
UploadURL: s3.getURL(u.Bucket),
|
UploadURL: s3.getURL(u.Bucket),
|
||||||
BucketName: u.Bucket,
|
BucketName: u.Bucket,
|
||||||
ObjectKey: u.ObjectKey,
|
ObjectKey: u.ObjectKey,
|
||||||
@@ -318,8 +345,18 @@ func (s3 *S3) FileUpload(u UploadInput) (UploadResponse, error) {
|
|||||||
MetaData: map[string]string{
|
MetaData: map[string]string{
|
||||||
"success_action_status": "201", // returns XML doc on success
|
"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 {
|
if err != nil {
|
||||||
return UploadResponse{}, err
|
return UploadResponse{}, err
|
||||||
}
|
}
|
||||||
@@ -406,6 +443,78 @@ func (s3 *S3) FileDelete(u DeleteInput) error {
|
|||||||
return nil
|
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
|
// if object matches reserved string, no need to encode them
|
||||||
var reservedObjectNames = regexp.MustCompile("^[a-zA-Z0-9-_.~/]+$")
|
var reservedObjectNames = regexp.MustCompile("^[a-zA-Z0-9-_.~/]+$")
|
||||||
|
|
||||||
|
|||||||
+47
-4
@@ -39,10 +39,11 @@ func TestS3_FileUpload(t *testing.T) {
|
|||||||
u UploadInput
|
u UploadInput
|
||||||
}
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
fields tConfig
|
fields tConfig
|
||||||
args args
|
args args
|
||||||
wantErr bool
|
testDetails bool
|
||||||
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Upload test.txt",
|
name: "Upload test.txt",
|
||||||
@@ -63,6 +64,29 @@ func TestS3_FileUpload(t *testing.T) {
|
|||||||
},
|
},
|
||||||
wantErr: false,
|
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",
|
name: "Upload avatar.png",
|
||||||
fields: tConfig{
|
fields: tConfig{
|
||||||
@@ -111,10 +135,29 @@ func TestS3_FileUpload(t *testing.T) {
|
|||||||
if (err != nil) != tt.wantErr {
|
if (err != nil) != tt.wantErr {
|
||||||
t.Errorf("S3.FileUpload() error = %v, wantErr %v", err, 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
|
// check for empty response
|
||||||
if (resp == UploadResponse{}) {
|
if (resp == UploadResponse{}) {
|
||||||
t.Errorf("S3.FileUpload() returned empty response, %v", resp)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user