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
|
## Install
|
||||||
|
|
||||||
```
|
```sh
|
||||||
go get github.com/rhnvrm/simples3
|
go get github.com/rhnvrm/simples3
|
||||||
```
|
```
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
```
|
```go
|
||||||
testTxt, _ := os.Open("test.txt")
|
testTxt, _ := os.Open("test.txt")
|
||||||
defer testTxt.Close()
|
defer testTxt.Close()
|
||||||
|
|
||||||
s3 := simples3.New(Region, AWSAccessKey, AWSSecretKey)
|
s3 := simples3.New(Region, AWSAccessKey, AWSSecretKey)
|
||||||
err := s3.FileUpload(simples3.UploadInput{
|
err := s3.FileUpload(simples3.UploadInput{
|
||||||
Bucket: "zerodha-testbucket",
|
Bucket: "zerodha-testbucket",
|
||||||
ObjectKey: "test.txt",
|
ObjectKey: "test.txt",
|
||||||
ContentType: "text/plain",
|
ContentType: "text/plain",
|
||||||
FileName: "test.txt",
|
FileName: "test.txt",
|
||||||
Body: testTxt,
|
Body: testTxt,
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -40,5 +40,4 @@ ToDo.
|
|||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
MIT.
|
MIT.
|
||||||
|
|
||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// UploadConfig generate policies from config
|
// UploadConfig generate policies from config
|
||||||
|
// for POST requests to S3 using Signing V4.
|
||||||
type UploadConfig struct {
|
type UploadConfig struct {
|
||||||
// Required
|
// Required
|
||||||
BucketName string
|
BucketName string
|
||||||
@@ -30,15 +31,6 @@ type UploadConfig struct {
|
|||||||
MetaData map[string]string
|
MetaData map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
type DeleteConfig struct {
|
|
||||||
// Required
|
|
||||||
BucketName string
|
|
||||||
ObjectKey string
|
|
||||||
ContentType string
|
|
||||||
// Optional
|
|
||||||
Expiration time.Duration
|
|
||||||
}
|
|
||||||
|
|
||||||
// UploadPolicies Amazon s3 upload policies
|
// UploadPolicies Amazon s3 upload policies
|
||||||
type UploadPolicies struct {
|
type UploadPolicies struct {
|
||||||
URL string
|
URL string
|
||||||
@@ -69,7 +61,8 @@ var NowTime = func() time.Time {
|
|||||||
|
|
||||||
var lf = []byte{'\n'}
|
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
|
// https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/API/sigv4-authentication-HTTPPOST.html
|
||||||
func (s3 *S3) CreateUploadPolicies(uploadConfig UploadConfig) (UploadPolicies, error) {
|
func (s3 *S3) CreateUploadPolicies(uploadConfig UploadConfig) (UploadPolicies, error) {
|
||||||
nowTime := NowTime()
|
nowTime := NowTime()
|
||||||
@@ -133,8 +126,8 @@ func buildUploadSign(nowTime time.Time, credential string, uploadConfig UploadCo
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c S3) buildCredential(nowTime time.Time) string {
|
func (s3 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")
|
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 {
|
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) {
|
func writeHeaderList(w io.Writer, r *http.Request) {
|
||||||
i, a := 0, make([]string, len(r.Header))
|
i, a := 0, make([]string, len(r.Header))
|
||||||
for k, _ := range r.Header {
|
for k := range r.Header {
|
||||||
a[i] = strings.ToLower(k)
|
a[i] = strings.ToLower(k)
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-6
@@ -13,7 +13,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -26,6 +25,7 @@ type S3 struct {
|
|||||||
URIFormat string
|
URIFormat string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UploadInput is passed to FileUpload as a parameter.
|
||||||
type UploadInput struct {
|
type UploadInput struct {
|
||||||
Bucket string
|
Bucket string
|
||||||
ObjectKey string
|
ObjectKey string
|
||||||
@@ -34,11 +34,13 @@ type UploadInput struct {
|
|||||||
Body multipart.File
|
Body multipart.File
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteInput is passed to FileDelete as a parameter.
|
||||||
type DeleteInput struct {
|
type DeleteInput struct {
|
||||||
Bucket string
|
Bucket string
|
||||||
ObjectKey string
|
ObjectKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New returns an instance of S3.
|
||||||
func New(region, accessKey, secretKey string) *S3 {
|
func New(region, accessKey, secretKey string) *S3 {
|
||||||
return &S3{
|
return &S3{
|
||||||
Region: region,
|
Region: region,
|
||||||
@@ -64,6 +66,8 @@ func detectFileSize(body multipart.File) int64 {
|
|||||||
return -1
|
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 {
|
func (s3 *S3) FileUpload(u UploadInput) error {
|
||||||
policies, err := s3.CreateUploadPolicies(UploadConfig{
|
policies, err := s3.CreateUploadPolicies(UploadConfig{
|
||||||
UploadURL: fmt.Sprintf(s3.URIFormat, s3.Region, u.Bucket),
|
UploadURL: fmt.Sprintf(s3.URIFormat, s3.Region, u.Bucket),
|
||||||
@@ -84,7 +88,6 @@ func (s3 *S3) FileUpload(u UploadInput) error {
|
|||||||
w := multipart.NewWriter(&b)
|
w := multipart.NewWriter(&b)
|
||||||
|
|
||||||
for k, v := range policies.Form {
|
for k, v := range policies.Form {
|
||||||
log.Println(k, v)
|
|
||||||
if err := w.WriteField(k, v); err != nil {
|
if err := w.WriteField(k, v); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -130,12 +133,13 @@ func (s3 *S3) FileUpload(u UploadInput) error {
|
|||||||
return nil
|
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 {
|
func (s3 *S3) FileDelete(u DeleteInput) error {
|
||||||
url := fmt.Sprintf(
|
url := fmt.Sprintf(
|
||||||
s3.URIFormat+"/%s",
|
s3.URIFormat+"/%s",
|
||||||
s3.Region, u.Bucket, u.ObjectKey,
|
s3.Region, u.Bucket, u.ObjectKey,
|
||||||
)
|
)
|
||||||
log.Println(url)
|
|
||||||
req, err := http.NewRequest(
|
req, err := http.NewRequest(
|
||||||
"DELETE", url, nil,
|
"DELETE", url, nil,
|
||||||
)
|
)
|
||||||
@@ -176,9 +180,6 @@ func (s3 *S3) FileDelete(u DeleteInput) error {
|
|||||||
|
|
||||||
req.Header.Set("Authorization", auth.String())
|
req.Header.Set("Authorization", auth.String())
|
||||||
|
|
||||||
dump, err := httputil.DumpRequest(req, true)
|
|
||||||
log.Printf("%q", dump)
|
|
||||||
|
|
||||||
// Submit the request
|
// Submit the request
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
res, err := client.Do(req)
|
res, err := client.Do(req)
|
||||||
|
|||||||
+3
-3
@@ -73,9 +73,9 @@ func TestS3_FileDelete(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "Delete test.txt",
|
name: "Delete test.txt",
|
||||||
fields: fields{
|
fields: fields{
|
||||||
AccessKey: os.Getenv("MONGO_PASS"),
|
AccessKey: os.Getenv("AWS_S3_ACCESS_KEY"),
|
||||||
SecretKey: os.Getenv("AWS_S3_ACCESS_KEY"),
|
SecretKey: os.Getenv("AWS_S3_SECRET_KEY"),
|
||||||
Region: os.Getenv("AWS_S3_ACCESS_KEY"),
|
Region: os.Getenv("AWS_S3_REGION"),
|
||||||
},
|
},
|
||||||
args: args{
|
args: args{
|
||||||
DeleteInput{
|
DeleteInput{
|
||||||
|
|||||||
Reference in New Issue
Block a user