docs: Add missing docstrings
This commit is contained in:
+24
@@ -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
|
||||
@@ -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.
|
||||
@@ -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 {
|
||||
|
||||
@@ -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++
|
||||
}
|
||||
|
||||
+7
-6
@@ -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)
|
||||
|
||||
+3
-3
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user