-
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.
to #246 feat: add object_storage_client.go.
- Loading branch information
1 parent
c155b9d
commit 8f3add0
Showing
2 changed files
with
56 additions
and
44 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
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,54 @@ | ||
package storage | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/s3" | ||
"github.com/aws/smithy-go" | ||
"os" | ||
"time" | ||
) | ||
|
||
type BucketBasics struct { | ||
S3Client *s3.Client | ||
} | ||
|
||
func NewBucketBasics(config *aws.Config) *BucketBasics { | ||
return &BucketBasics{S3Client: s3.NewFromConfig(*config)} | ||
} | ||
|
||
func (basics *BucketBasics) UploadFile(ctx context.Context, bucketName string, objectKey string, fileName string) error { | ||
file, err := os.Open(fileName) | ||
if err != nil { | ||
return fmt.Errorf("Couldn't open file %v to upload. Here's why: %v\n", fileName, err) | ||
} else { | ||
defer file.Close() | ||
|
||
_, err = basics.S3Client.PutObject(ctx, &s3.PutObjectInput{ | ||
Bucket: aws.String(bucketName), | ||
Key: aws.String(objectKey), | ||
Body: file, | ||
}) | ||
|
||
if err != nil { | ||
var apiErr smithy.APIError | ||
if errors.As(err, &apiErr) && apiErr.ErrorCode() == "EntityTooLarge" { | ||
return fmt.Errorf("Error while uploading object to %s. The object is too large.\n"+ | ||
"To upload objects larger than 5GB, use the S3 console (160GB max)\n"+ | ||
"or the multipart upload API (5TB max).", bucketName) | ||
} else { | ||
return fmt.Errorf("Couldn't upload file %v to %v:%v. Here's why: %v\n", | ||
fileName, bucketName, objectKey, err) | ||
} | ||
} else { | ||
err = s3.NewObjectExistsWaiter(basics.S3Client).Wait( | ||
ctx, &s3.HeadObjectInput{Bucket: aws.String(bucketName), Key: aws.String(objectKey)}, time.Minute) | ||
if err != nil { | ||
return fmt.Errorf("Failed attempt to wait for object %s to exist.\n", objectKey) | ||
} | ||
} | ||
} | ||
return err | ||
} |