-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
658b0f7
commit 8d1d654
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package blobstore | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
|
||
awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http" | ||
|
||
"github.com/aws/aws-sdk-go-v2/service/s3" | ||
) | ||
|
||
type S3BlobStore struct { | ||
client *s3.Client | ||
} | ||
|
||
func NewS3BlobStore(client *s3.Client) *S3BlobStore { | ||
return &S3BlobStore{client: client} | ||
} | ||
|
||
func (s *S3BlobStore) Put(ctx context.Context, bucket string, key string, blob Blob) error { | ||
_, err := s.client.PutObject(ctx, &s3.PutObjectInput{ | ||
Bucket: &bucket, | ||
Key: &key, | ||
Body: blob.Body, | ||
ContentType: blob.ContentType, | ||
Metadata: blob.Metadata, | ||
}) | ||
|
||
if err != nil { | ||
var httpResponseErr *awshttp.ResponseError | ||
if errors.As(err, &httpResponseErr) && | ||
(httpResponseErr.HTTPStatusCode() == http.StatusNotFound || | ||
httpResponseErr.HTTPStatusCode() == http.StatusForbidden) { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *S3BlobStore) Exist(ctx context.Context, bucket string, key string) (bool, error) { | ||
_, err := s.client.HeadObject(ctx, &s3.HeadObjectInput{ | ||
Bucket: &bucket, | ||
Key: &key, | ||
}) | ||
if err != nil { | ||
var httpResponseErr *awshttp.ResponseError | ||
if errors.As(err, &httpResponseErr) && | ||
(httpResponseErr.HTTPStatusCode() == http.StatusNotFound || | ||
httpResponseErr.HTTPStatusCode() == http.StatusForbidden) { | ||
return false, nil | ||
} | ||
return false, err | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func (s *S3BlobStore) Get(ctx context.Context, bucket string, key string) (Blob, error) { | ||
output, err := s.client.GetObject(ctx, &s3.GetObjectInput{ | ||
Bucket: &bucket, | ||
Key: &key, | ||
}) | ||
if err != nil { | ||
var httpResponseErr *awshttp.ResponseError | ||
if errors.As(err, &httpResponseErr) && | ||
(httpResponseErr.HTTPStatusCode() == http.StatusNotFound || | ||
httpResponseErr.HTTPStatusCode() == http.StatusForbidden) { | ||
return Blob{}, nil | ||
} | ||
return Blob{}, err | ||
} | ||
|
||
return Blob{ | ||
Body: output.Body, | ||
ContentType: output.ContentType, | ||
Metadata: output.Metadata, | ||
}, nil | ||
} |