fix: decouple http client from library
This commit is contained in:
@@ -51,7 +51,6 @@ const (
|
|||||||
serviceName = "s3"
|
serviceName = "s3"
|
||||||
|
|
||||||
defaultUploadURLFormat = "http://%s.s3.amazonaws.com/" // <bucketName>
|
defaultUploadURLFormat = "http://%s.s3.amazonaws.com/" // <bucketName>
|
||||||
defaultBucketURLFormat = "%s.s3.amazonaws.com/" // <bucketName>
|
|
||||||
defaultExpirationHour = 1 * time.Hour
|
defaultExpirationHour = 1 * time.Hour
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
+23
-3
@@ -27,6 +27,7 @@ type S3 struct {
|
|||||||
AccessKey string
|
AccessKey string
|
||||||
SecretKey string
|
SecretKey string
|
||||||
Region string
|
Region string
|
||||||
|
Client *http.Client
|
||||||
|
|
||||||
URIFormat string
|
URIFormat string
|
||||||
}
|
}
|
||||||
@@ -135,6 +136,13 @@ func newUsingIAMImpl(baseURL, region string) (*S3, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s3 *S3) getClient() *http.Client {
|
||||||
|
if s3.Client == nil {
|
||||||
|
return http.DefaultClient
|
||||||
|
}
|
||||||
|
return s3.Client
|
||||||
|
}
|
||||||
|
|
||||||
func detectFileSize(body multipart.File) int64 {
|
func detectFileSize(body multipart.File) int64 {
|
||||||
switch r := body.(type) {
|
switch r := body.(type) {
|
||||||
case io.Seeker:
|
case io.Seeker:
|
||||||
@@ -150,6 +158,18 @@ func detectFileSize(body multipart.File) int64 {
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetClient can be used to set the http client to be
|
||||||
|
// used by the package. If client passed is nil,
|
||||||
|
// http.DefaultClient is used.
|
||||||
|
func (s3 *S3) SetClient(client *http.Client) *S3 {
|
||||||
|
if client != nil {
|
||||||
|
s3.Client = client
|
||||||
|
} else {
|
||||||
|
s3.Client = http.DefaultClient
|
||||||
|
}
|
||||||
|
return s3
|
||||||
|
}
|
||||||
|
|
||||||
// FileUpload makes a POST call with the file written as multipart
|
// FileUpload makes a POST call with the file written as multipart
|
||||||
// and on successful upload, checks for 200 OK.
|
// and on successful upload, checks for 200 OK.
|
||||||
func (s3 *S3) FileUpload(u UploadInput) (UploadResponse, error) {
|
func (s3 *S3) FileUpload(u UploadInput) (UploadResponse, error) {
|
||||||
@@ -172,7 +192,7 @@ func (s3 *S3) FileUpload(u UploadInput) (UploadResponse, error) {
|
|||||||
w := multipart.NewWriter(&b)
|
w := multipart.NewWriter(&b)
|
||||||
|
|
||||||
for k, v := range policies.Form {
|
for k, v := range policies.Form {
|
||||||
if err := w.WriteField(k, v); err != nil {
|
if err = w.WriteField(k, v); err != nil {
|
||||||
return UploadResponse{}, err
|
return UploadResponse{}, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -200,7 +220,7 @@ func (s3 *S3) FileUpload(u UploadInput) (UploadResponse, error) {
|
|||||||
req.Header.Set("Content-Type", w.FormDataContentType())
|
req.Header.Set("Content-Type", w.FormDataContentType())
|
||||||
|
|
||||||
// Submit the request
|
// Submit the request
|
||||||
client := &http.Client{}
|
client := s3.getClient()
|
||||||
res, err := client.Do(req)
|
res, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return UploadResponse{}, err
|
return UploadResponse{}, err
|
||||||
@@ -268,7 +288,7 @@ func (s3 *S3) FileDelete(u DeleteInput) error {
|
|||||||
req.Header.Set("Authorization", auth.String())
|
req.Header.Set("Authorization", auth.String())
|
||||||
|
|
||||||
// Submit the request
|
// Submit the request
|
||||||
client := &http.Client{}
|
client := s3.getClient()
|
||||||
res, err := client.Do(req)
|
res, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -127,18 +127,6 @@ func TestS3_FileDelete(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type MockClient struct {
|
|
||||||
DoFunc func(req *http.Request) (*http.Response, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockClient) Do(req *http.Request) (*http.Response, error) {
|
|
||||||
if m.DoFunc != nil {
|
|
||||||
return m.DoFunc(req)
|
|
||||||
}
|
|
||||||
// just in case you want default correct return value
|
|
||||||
return &http.Response{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestS3_NewUsingIAM(t *testing.T) {
|
func TestS3_NewUsingIAM(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
iam = `test-new-s3-using-iam`
|
iam = `test-new-s3-using-iam`
|
||||||
|
|||||||
Reference in New Issue
Block a user