From 157aa88554fa82e28e39459a4580c6a84f1f2b42 Mon Sep 17 00:00:00 2001 From: Rohan Verma Date: Thu, 6 Dec 2018 16:48:37 +0530 Subject: [PATCH] docs: Add missing docstrings --- .gitignore | 24 ++++++++++++++++++++++++ README.md | 17 ++++++++--------- policy.go | 17 +++++------------ sign.go | 2 +- simples3.go | 13 +++++++------ simples3_test.go | 6 +++--- 6 files changed, 48 insertions(+), 31 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..93b09ee --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +### Go ### +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +### Go Patch ### +/vendor/ +/Godeps/ + +### Code ### +# Visual Studio Code - https://code.visualstudio.com/ +.settings/ +.vscode/ +tsconfig.json +jsconfig.json \ No newline at end of file diff --git a/README.md b/README.md index 06b7ac0..29311d5 100644 --- a/README.md +++ b/README.md @@ -6,23 +6,23 @@ SimpleS3 is a golang library for uploading and deleting objects on S3 buckets us ## Install -``` +```sh go get github.com/rhnvrm/simples3 ``` ## Example -``` +```go testTxt, _ := os.Open("test.txt") defer testTxt.Close() s3 := simples3.New(Region, AWSAccessKey, AWSSecretKey) err := s3.FileUpload(simples3.UploadInput{ - Bucket: "zerodha-testbucket", - ObjectKey: "test.txt", - ContentType: "text/plain", - FileName: "test.txt", - Body: testTxt, + Bucket: "zerodha-testbucket", + ObjectKey: "test.txt", + ContentType: "text/plain", + FileName: "test.txt", + Body: testTxt, } ``` @@ -40,5 +40,4 @@ ToDo. ## License -MIT. - +MIT. \ No newline at end of file diff --git a/policy.go b/policy.go index cdca498..c70bd8d 100644 --- a/policy.go +++ b/policy.go @@ -18,6 +18,7 @@ import ( ) // UploadConfig generate policies from config +// for POST requests to S3 using Signing V4. type UploadConfig struct { // Required BucketName string @@ -30,15 +31,6 @@ type UploadConfig struct { MetaData map[string]string } -type DeleteConfig struct { - // Required - BucketName string - ObjectKey string - ContentType string - // Optional - Expiration time.Duration -} - // UploadPolicies Amazon s3 upload policies type UploadPolicies struct { URL string @@ -69,7 +61,8 @@ var NowTime = func() time.Time { var lf = []byte{'\n'} -// CreatePolicies create amazon s3 to upload policies return +// CreateUploadPolicies creates amazon s3 sigv4 compatible +// policy and signing keys with the signature returns the upload policy. // https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/API/sigv4-authentication-HTTPPOST.html func (s3 *S3) CreateUploadPolicies(uploadConfig UploadConfig) (UploadPolicies, error) { nowTime := NowTime() @@ -133,8 +126,8 @@ func buildUploadSign(nowTime time.Time, credential string, uploadConfig UploadCo }) } -func (c S3) buildCredential(nowTime time.Time) string { - return fmt.Sprintf("%s/%s/%s/%s/%s", c.AccessKey, nowTime.UTC().Format(shortTimeFormat), c.Region, serviceName, "aws4_request") +func (s3 S3) buildCredential(nowTime time.Time) string { + return fmt.Sprintf("%s/%s/%s/%s/%s", s3.AccessKey, nowTime.UTC().Format(shortTimeFormat), s3.Region, serviceName, "aws4_request") } func buildSignature(nowTime time.Time, secretAccessKey string, regionName string, serviceName string) []byte { diff --git a/sign.go b/sign.go index aa858d1..4d8b57e 100644 --- a/sign.go +++ b/sign.go @@ -116,7 +116,7 @@ func writeHeader(w io.Writer, r *http.Request) { func writeHeaderList(w io.Writer, r *http.Request) { i, a := 0, make([]string, len(r.Header)) - for k, _ := range r.Header { + for k := range r.Header { a[i] = strings.ToLower(k) i++ } diff --git a/simples3.go b/simples3.go index 59f2bee..ff025d0 100644 --- a/simples3.go +++ b/simples3.go @@ -13,7 +13,6 @@ import ( "log" "mime/multipart" "net/http" - "net/http/httputil" "time" ) @@ -26,6 +25,7 @@ type S3 struct { URIFormat string } +// UploadInput is passed to FileUpload as a parameter. type UploadInput struct { Bucket string ObjectKey string @@ -34,11 +34,13 @@ type UploadInput struct { Body multipart.File } +// DeleteInput is passed to FileDelete as a parameter. type DeleteInput struct { Bucket string ObjectKey string } +// New returns an instance of S3. func New(region, accessKey, secretKey string) *S3 { return &S3{ Region: region, @@ -64,6 +66,8 @@ func detectFileSize(body multipart.File) int64 { return -1 } +// FileUpload makes a POST call with the file written as multipart +// and on successful upload, checks for 200 OK. func (s3 *S3) FileUpload(u UploadInput) error { policies, err := s3.CreateUploadPolicies(UploadConfig{ UploadURL: fmt.Sprintf(s3.URIFormat, s3.Region, u.Bucket), @@ -84,7 +88,6 @@ func (s3 *S3) FileUpload(u UploadInput) error { w := multipart.NewWriter(&b) for k, v := range policies.Form { - log.Println(k, v) if err := w.WriteField(k, v); err != nil { return err } @@ -130,12 +133,13 @@ func (s3 *S3) FileUpload(u UploadInput) error { return nil } +// FileDelete makes a POST call with the file written as multipart +// and on successful upload, checks for 204 No Content. func (s3 *S3) FileDelete(u DeleteInput) error { url := fmt.Sprintf( s3.URIFormat+"/%s", s3.Region, u.Bucket, u.ObjectKey, ) - log.Println(url) req, err := http.NewRequest( "DELETE", url, nil, ) @@ -176,9 +180,6 @@ func (s3 *S3) FileDelete(u DeleteInput) error { req.Header.Set("Authorization", auth.String()) - dump, err := httputil.DumpRequest(req, true) - log.Printf("%q", dump) - // Submit the request client := &http.Client{} res, err := client.Do(req) diff --git a/simples3_test.go b/simples3_test.go index 40f10ae..8d362de 100644 --- a/simples3_test.go +++ b/simples3_test.go @@ -73,9 +73,9 @@ func TestS3_FileDelete(t *testing.T) { { name: "Delete test.txt", fields: fields{ - AccessKey: os.Getenv("MONGO_PASS"), - SecretKey: os.Getenv("AWS_S3_ACCESS_KEY"), - Region: os.Getenv("AWS_S3_ACCESS_KEY"), + AccessKey: os.Getenv("AWS_S3_ACCESS_KEY"), + SecretKey: os.Getenv("AWS_S3_SECRET_KEY"), + Region: os.Getenv("AWS_S3_REGION"), }, args: args{ DeleteInput{