aws S3Client

This commit is contained in:
kkb0318
2024-03-28 21:41:09 +09:00
parent 022c561938
commit babd997d3b
6 changed files with 340 additions and 206 deletions
+53
View File
@@ -0,0 +1,53 @@
package client
import (
"bytes"
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
type AwsConfig struct {
config aws.Config
}
func NewAwsClient(ctx context.Context, region string) (*AwsConfig, error) {
cfg, err := config.LoadDefaultConfig(ctx,
config.WithRegion(region),
)
if err != nil {
return nil, fmt.Errorf("unable to load SDK config, %w", err)
}
return &AwsConfig{config: cfg}, nil
}
func (a *AwsConfig) S3Cient(bucketName string) *AwsS3Client {
return &AwsS3Client{
bucketName,
s3.NewFromConfig(a.config),
}
}
type AwsS3Client struct {
bucketName string
client *s3.Client
}
func (a *AwsS3Client) PutObject(ctx context.Context, key string, body []byte) error {
_, err := a.client.PutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String(a.bucketName),
Key: aws.String(key),
Body: bytes.NewReader(body),
})
return err
}
func (a *AwsS3Client) CreateBucket(ctx context.Context) error {
_, err := a.client.CreateBucket(ctx, &s3.CreateBucketInput{
Bucket: aws.String(a.bucketName),
})
return err
}