intial commit
This commit is contained in:
67
internal/backup/s3_upload.go
Normal file
67
internal/backup/s3_upload.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package backup
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"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/service/s3"
|
||||
)
|
||||
|
||||
func UploadParquetToSpaces(localFilePath, objectPath string) (string, error) {
|
||||
|
||||
region := os.Getenv("DO_SPACES_REGION")
|
||||
endpoint := os.Getenv("DO_SPACES_ENDPOINT")
|
||||
bucket := os.Getenv("DO_SPACES_BUCKET")
|
||||
accessKey := os.Getenv("DO_SPACES_ACCESS_KEY")
|
||||
secretKey := os.Getenv("DO_SPACES_SECRET_KEY")
|
||||
|
||||
cfg, err := config.LoadDefaultConfig(
|
||||
context.TODO(),
|
||||
config.WithRegion(region),
|
||||
config.WithCredentialsProvider(
|
||||
credentials.NewStaticCredentialsProvider(accessKey, secretKey, ""),
|
||||
),
|
||||
config.WithEndpointResolver(
|
||||
aws.EndpointResolverFunc(func(service, region string) (aws.Endpoint, error) {
|
||||
return aws.Endpoint{
|
||||
URL: "https://" + endpoint,
|
||||
SigningRegion: region,
|
||||
}, nil
|
||||
}),
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
client := s3.NewFromConfig(cfg)
|
||||
|
||||
file, err := os.Open(localFilePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = client.PutObject(context.TODO(), &s3.PutObjectInput{
|
||||
Bucket: aws.String(bucket),
|
||||
Key: aws.String(objectPath),
|
||||
Body: file,
|
||||
ContentType: aws.String("application/octet-stream"),
|
||||
ACL: "public-read",
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
cdnURL := fmt.Sprintf(
|
||||
"https://images.nearle.app/%s",
|
||||
filepath.ToSlash(objectPath),
|
||||
)
|
||||
|
||||
return cdnURL, nil
|
||||
}
|
||||
Reference in New Issue
Block a user