diff --git a/s3/s3.go b/s3/s3.go index b3665f0..a324ca3 100644 --- a/s3/s3.go +++ b/s3/s3.go @@ -152,6 +152,10 @@ func (client Client) GetStream(path string) (io.ReadCloser, error) { Key: aws.String(client.ToS3Key(path)), }) + if err != nil { + return nil, err + } + return getResponse.Body, err } @@ -240,7 +244,7 @@ func (client Client) List(path string) ([]*oss.Object, error) { if err == nil { for _, content := range listObjectsResponse.Contents { objects = append(objects, &oss.Object{ - Path: client.ToS3Key(*content.Key), + Path: "/" + client.ToS3Key(*content.Key), Name: filepath.Base(*content.Key), LastModified: content.LastModified, StorageInterface: client, @@ -307,9 +311,7 @@ func (client Client) ToRelativePath(urlPath string) string { // GetURL get public accessible URL func (client Client) GetURL(path string) (url string, err error) { if client.Config.Endpoint == "" { - if client.Config.ACL == types.ObjectCannedACLPrivate || client.Config.ACL == types.ObjectCannedACLAuthenticatedRead { - presignClient := s3.NewPresignClient(client.S3) presignedGetURL, err := presignClient.PresignGetObject(context.TODO(), &s3.GetObjectInput{ Bucket: aws.String(client.Config.Bucket), diff --git a/s3/s3_test.go b/s3/s3_test.go index 6827ed2..62c95da 100644 --- a/s3/s3_test.go +++ b/s3/s3_test.go @@ -58,6 +58,22 @@ func TestToRelativePath(t *testing.T) { } } +func TestToS3Key(t *testing.T) { + urlMap := map[string]string{ + "https://mybucket.s3.amazonaws.com/test/myobject.ext": "test/myobject.ext", + "https://qor-example.com/myobject.ext": "myobject.ext", + "//mybucket.s3.amazonaws.com/myobject.ext": "myobject.ext", + "http://mybucket.s3.amazonaws.com/myobject.ext": "myobject.ext", + "/test/myobject.ext": "test/myobject.ext", + } + + for url, path := range urlMap { + if client.ToS3Key(url) != path { + t.Errorf("%v's relative path should be %v, but got %v", url, path, client.ToRelativePath(url)) + } + } +} + func TestToRelativePathWithS3ForcePathStyle(t *testing.T) { urlMap := map[string]string{ "https://s3.amazonaws.com/mybucket/myobject.ext": "/myobject.ext",