diff --git a/README.md b/README.md index 919adba..6c4853e 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ go get github.com/rhnvrm/simples3 ## Example ```go -testTxt, _ := os.Open("test.txt") +testTxt, _ := os.Open("testdata/test.txt") defer testTxt.Close() // Create an instance of the package @@ -24,6 +24,9 @@ s3 := simples3.New(Region, AWSAccessKey, AWSSecretKey) // obtain credentials from IAM attached to the instance. s3 := simples3.NewUsingIAM(Region) +// You can also set a custom endpoint to a compatible s3 instance. +s3.SetEndpoint(CustomEndpoint) + // Note: Consider adding a testTxt.Seek(0, 0) // in case you have read // the body, as the pointer is shared by the library. diff --git a/go.mod b/go.mod index dda828e..99c1501 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,3 @@ module github.com/rhnvrm/simples3 + +go 1.13 diff --git a/policy.go b/policy.go index 33d0357..e8562cb 100644 --- a/policy.go +++ b/policy.go @@ -55,8 +55,8 @@ const ( defaultExpirationHour = 1 * time.Hour ) -// NowTime mockable time.Now() -var NowTime = func() time.Time { +// nowTime mockable time.Now() +var nowTime = func() time.Time { return time.Now().UTC() } @@ -66,7 +66,7 @@ var newLine = []byte{'\n'} // 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() + nowTime := nowTime() credential := string(s3.buildCredential(nowTime)) data, err := buildUploadSign(nowTime, credential, uploadConfig) if err != nil { diff --git a/presigned.go b/presigned.go index 3ead23e..ca5f3f2 100644 --- a/presigned.go +++ b/presigned.go @@ -33,7 +33,7 @@ type PresignedInput struct { // (https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html) func (s3 *S3) GeneratePresignedURL(in PresignedInput) string { var ( - nowTime = NowTime() + nowTime = nowTime() protocol = defaultProtocol endpoint = defaultPresignedHost diff --git a/presigned_test.go b/presigned_test.go index f4607f1..c03ff6b 100644 --- a/presigned_test.go +++ b/presigned_test.go @@ -42,7 +42,7 @@ func TestS3_GeneratePresignedURL_Personal(t *testing.T) { Endpoint: os.Getenv("AWS_S3_ENDPOINT"), ObjectKey: "test1.txt", Method: "GET", - Timestamp: NowTime(), + Timestamp: nowTime(), ExpirySeconds: 3600, }); got == dontwant { t.Errorf("S3.GeneratePresignedURL() = %v, dontwant %v", got, dontwant) @@ -63,7 +63,7 @@ func TestS3_GeneratePresignedURL_ExtraHeader(t *testing.T) { Endpoint: os.Getenv("AWS_S3_ENDPOINT"), ObjectKey: "test2.txt", Method: "GET", - Timestamp: NowTime(), + Timestamp: nowTime(), ExpirySeconds: 3600, ExtraHeaders: map[string]string{ "x-amz-meta-test": "test", @@ -87,7 +87,7 @@ func TestS3_GeneratePresignedURL_PUT(t *testing.T) { Endpoint: os.Getenv("AWS_S3_ENDPOINT"), ObjectKey: "test2.txt", Method: "PUT", - Timestamp: NowTime(), + Timestamp: nowTime(), ExpirySeconds: 3600, }); got == dontwant { t.Errorf("S3.GeneratePresignedURL() = %v, dontwant %v", got, dontwant) @@ -111,7 +111,7 @@ func BenchmarkS3_GeneratePresigned(b *testing.B) { Endpoint: os.Getenv("AWS_S3_ENDPOINT"), ObjectKey: "test.txt", Method: "GET", - Timestamp: NowTime(), + Timestamp: nowTime(), ExpirySeconds: 3600, }) } diff --git a/simples3.go b/simples3.go index e30641f..2774f0d 100644 --- a/simples3.go +++ b/simples3.go @@ -158,7 +158,8 @@ func (s3 *S3) getURL(bucket string, args ...string) (uri string) { return } -// SetEndpoint ... +// SetEndpoint can be used to the set a custom endpoint for +// using an alternate instance compatible with the s3 API. func (s3 *S3) SetEndpoint(uri string) *S3 { if len(uri) > 0 { s3.Endpoint = uri diff --git a/simples3_test.go b/simples3_test.go index 843eab8..fc6c160 100644 --- a/simples3_test.go +++ b/simples3_test.go @@ -16,7 +16,7 @@ type tConfig struct { } func TestS3_FileUpload(t *testing.T) { - testTxt, err := os.Open("test.txt") + testTxt, err := os.Open("testdata/test.txt") if err != nil { return } @@ -75,7 +75,8 @@ func TestS3_FileUpload(t *testing.T) { wantErr: false, }, } - for _, tt := range tests { + for _, testcase := range tests { + tt := testcase t.Run(tt.name, func(t *testing.T) { s3 := New(tt.fields.Region, tt.fields.AccessKey, tt.fields.SecretKey) s3.SetEndpoint(tt.fields.Endpoint) @@ -118,7 +119,8 @@ func TestS3_FileDelete(t *testing.T) { wantErr: false, }, } - for _, tt := range tests { + for _, testcase := range tests { + tt := testcase t.Run(tt.name, func(t *testing.T) { s3 := New(tt.fields.Region, tt.fields.AccessKey, tt.fields.SecretKey) s3.SetEndpoint(tt.fields.Endpoint) diff --git a/test.txt b/testdata/test.txt similarity index 100% rename from test.txt rename to testdata/test.txt