mirror of
https://github.com/eduardolat/pgbackweb.git
synced 2026-02-19 11:58:31 -06:00
Migrate S3 integration to AWS SDK v2, updating client creation and method calls
This commit is contained in:
@@ -1,33 +1,50 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/config"
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/eduardolat/pgbackweb/internal/util/strutil"
|
||||
)
|
||||
|
||||
// createS3Client creates a new S3 client
|
||||
func createS3Client(
|
||||
accessKey, secretKey, region, endpoint string,
|
||||
) (*s3.S3, error) {
|
||||
sess, err := session.NewSession(&aws.Config{
|
||||
Credentials: credentials.NewStaticCredentials(accessKey, secretKey, ""),
|
||||
Region: aws.String(region),
|
||||
Endpoint: aws.String(endpoint),
|
||||
S3ForcePathStyle: aws.Bool(true),
|
||||
) (*s3.Client, error) {
|
||||
credentialsProvider := credentials.NewStaticCredentialsProvider(
|
||||
accessKey, secretKey, "",
|
||||
)
|
||||
|
||||
//nolint:all
|
||||
endpointResolver := aws.EndpointResolverFunc(func(
|
||||
_ string, _ string,
|
||||
) (aws.Endpoint, error) {
|
||||
return aws.Endpoint{
|
||||
HostnameImmutable: true,
|
||||
URL: endpoint,
|
||||
}, nil
|
||||
})
|
||||
|
||||
//nolint:all
|
||||
conf, err := config.LoadDefaultConfig(
|
||||
context.TODO(),
|
||||
config.WithRegion(region),
|
||||
config.WithEndpointResolver(endpointResolver),
|
||||
config.WithCredentialsProvider(credentialsProvider),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create aws session: %w", err)
|
||||
return nil, fmt.Errorf("error initializing storage config: %w", err)
|
||||
}
|
||||
|
||||
return s3.New(sess), nil
|
||||
s3Client := s3.NewFromConfig(conf)
|
||||
return s3Client, nil
|
||||
}
|
||||
|
||||
// S3Test tests the connection to S3
|
||||
@@ -41,9 +58,12 @@ func (Client) S3Test(
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = s3Client.HeadBucket(&s3.HeadBucketInput{
|
||||
Bucket: aws.String(bucketName),
|
||||
})
|
||||
_, err = s3Client.HeadBucket(
|
||||
context.TODO(),
|
||||
&s3.HeadBucketInput{
|
||||
Bucket: aws.String(bucketName),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to test S3 bucket: %w", err)
|
||||
}
|
||||
@@ -68,25 +88,35 @@ func (Client) S3Upload(
|
||||
key = strutil.RemoveLeadingSlash(key)
|
||||
contentType := strutil.GetContentTypeFromFileName(key)
|
||||
|
||||
uploader := s3manager.NewUploaderWithClient(s3Client)
|
||||
_, err = uploader.Upload(&s3manager.UploadInput{
|
||||
Bucket: aws.String(bucketName),
|
||||
Key: aws.String(key),
|
||||
Body: fileReader,
|
||||
ContentType: aws.String(contentType),
|
||||
})
|
||||
uploader := manager.NewUploader(s3Client)
|
||||
_, err = uploader.Upload(
|
||||
context.TODO(),
|
||||
&s3.PutObjectInput{
|
||||
Bucket: aws.String(bucketName),
|
||||
Key: aws.String(key),
|
||||
Body: fileReader,
|
||||
ContentType: aws.String(contentType),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to upload file to S3: %w", err)
|
||||
}
|
||||
|
||||
fileHead, err := s3Client.HeadObject(&s3.HeadObjectInput{
|
||||
Bucket: aws.String(bucketName),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
fileHead, err := s3Client.HeadObject(
|
||||
context.TODO(),
|
||||
&s3.HeadObjectInput{
|
||||
Bucket: aws.String(bucketName),
|
||||
Key: aws.String(key),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to get uploaded file info from S3: %w", err)
|
||||
}
|
||||
fileSize := *fileHead.ContentLength
|
||||
|
||||
var fileSize int64
|
||||
if fileHead.ContentLength != nil {
|
||||
fileSize = *fileHead.ContentLength
|
||||
}
|
||||
|
||||
return fileSize, nil
|
||||
}
|
||||
@@ -104,10 +134,13 @@ func (Client) S3Delete(
|
||||
|
||||
key = strutil.RemoveLeadingSlash(key)
|
||||
|
||||
_, err = s3Client.DeleteObject(&s3.DeleteObjectInput{
|
||||
Bucket: aws.String(bucketName),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
_, err = s3Client.DeleteObject(
|
||||
context.TODO(),
|
||||
&s3.DeleteObjectInput{
|
||||
Bucket: aws.String(bucketName),
|
||||
Key: aws.String(key),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete file from S3: %w", err)
|
||||
}
|
||||
@@ -124,19 +157,20 @@ func (Client) S3GetDownloadLink(
|
||||
accessKey, secretKey, region, endpoint,
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", fmt.Errorf("failed to create S3 client: %w", err)
|
||||
}
|
||||
|
||||
key = strutil.RemoveLeadingSlash(key)
|
||||
req, _ := s3Client.GetObjectRequest(&s3.GetObjectInput{
|
||||
Bucket: aws.String(bucketName),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
|
||||
url, err := req.Presign(expiration)
|
||||
presigned, err := s3.NewPresignClient(s3Client).PresignGetObject(
|
||||
context.TODO(),
|
||||
&s3.GetObjectInput{
|
||||
Bucket: aws.String(bucketName),
|
||||
Key: aws.String(key),
|
||||
},
|
||||
s3.WithPresignExpires(expiration),
|
||||
)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to generate presigned URL: %w", err)
|
||||
}
|
||||
|
||||
return url, nil
|
||||
return presigned.URL, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user