Skip to content

Commit

Permalink
to #246 feat: add object_storage_client.go.
Browse files Browse the repository at this point in the history
  • Loading branch information
TianyuZhang1214 committed Dec 11, 2024
1 parent c155b9d commit 8f3add0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 44 deletions.
46 changes: 2 additions & 44 deletions storage/object_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@ package storage

import (
"context"
"errors"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/smithy-go"
"net/url"
"os"
"path"
"strings"
"time"
)

type ObjectStorageConfig struct {
Expand All @@ -34,25 +29,21 @@ var supportedProvider = map[string]struct{}{
"s3c": {},
}

type BucketBasics struct {
S3Client *s3.Client
}

func (storageConfig *ObjectStorageConfig) UploadLocalFile(localDir, localFile, remotePath string) (string, error) {
localFullPath := path.Join(localDir, localFile)
s3Cfg, err := storageConfig.buildConfig()
if err != nil {
return "", err
}

bucketBasics := &BucketBasics{S3Client: s3.NewFromConfig(*s3Cfg)}

// Parse the bucket and key from the remote path
bucket, key := parseBucketAndPath(remotePath)
if strings.HasSuffix(key, "/") {
key += localFile
}

bucketBasics := NewBucketBasics(s3Cfg)

err = bucketBasics.UploadFile(context.TODO(), bucket, key, localFullPath)
if err != nil {
return "", err
Expand All @@ -61,39 +52,6 @@ func (storageConfig *ObjectStorageConfig) UploadLocalFile(localDir, localFile, r
return fmt.Sprintf("File %q successfully uploaded to %s://%s/%s\n", localFullPath, storageConfig.Provider, bucket, key), nil
}

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
}

func (storageConfig *ObjectStorageConfig) buildConfig() (cfg *aws.Config, err error) {
var s3Cfg aws.Config
if storageConfig.Provider == "s3c" {
Expand Down
54 changes: 54 additions & 0 deletions storage/object_storage_client.go
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
}

0 comments on commit 8f3add0

Please sign in to comment.