105 lines
3.2 KiB
Go
105 lines
3.2 KiB
Go
package backup
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
awsCreds "github.com/aws/aws-sdk-go-v2/credentials"
|
|
awsConfig "github.com/aws/aws-sdk-go-v2/config" // Aliased AWS config package
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
|
|
backGoConfig "pixelridgesoftworks.com/BackGo/config" // Aliased your config package
|
|
)
|
|
|
|
// S3Client wraps the AWS S3 client
|
|
type S3Client struct {
|
|
Client *s3.Client
|
|
Bucket string
|
|
}
|
|
|
|
// NewS3Client initializes and returns a new S3 client using the provided application configuration
|
|
func NewS3Client(appConfig *backGoConfig.Config) (*S3Client, error) {
|
|
if !appConfig.S3Enabled {
|
|
return nil, fmt.Errorf("S3 storage is not enabled in the configuration")
|
|
}
|
|
|
|
// Split the S3AuthorizationInfo into AccessKeyID and SecretAccessKey
|
|
parts := strings.SplitN(appConfig.S3AuthorizationInfo, ":", 2)
|
|
if len(parts) != 2 {
|
|
return nil, fmt.Errorf("S3AuthorizationInfo format is invalid, expected 'AccessKeyID:SecretAccessKey'")
|
|
}
|
|
accessKeyID, secretAccessKey := parts[0], parts[1]
|
|
|
|
cfg, err := awsConfig.LoadDefaultConfig(context.TODO(),
|
|
awsConfig.WithRegion(appConfig.S3Region),
|
|
awsConfig.WithCredentialsProvider(awsCreds.NewStaticCredentialsProvider(accessKeyID, secretAccessKey, "")),
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unable to load AWS SDK config, %w", err)
|
|
}
|
|
|
|
s3Client := s3.NewFromConfig(cfg)
|
|
return &S3Client{
|
|
Client: s3Client,
|
|
Bucket: appConfig.S3BucketInfo,
|
|
}, nil
|
|
}
|
|
|
|
// UploadFile uploads a file to the specified S3 bucket
|
|
func (c *S3Client) UploadFile(key string, filePath string) error {
|
|
// Open the file for use
|
|
file, err := os.Open(filePath)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open file %q, %v", filePath, err)
|
|
}
|
|
defer file.Close()
|
|
|
|
// Upload the file to S3.
|
|
uploader := manager.NewUploader(c.Client)
|
|
_, err = uploader.Upload(context.TODO(), &s3.PutObjectInput{
|
|
Bucket: &c.Bucket,
|
|
Key: &key,
|
|
Body: file,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to upload file, %v", err)
|
|
}
|
|
fmt.Printf("File uploaded successfully: %s\n", key)
|
|
return nil
|
|
}
|
|
|
|
// DeleteFile deletes a file from the specified S3 bucket
|
|
func (c *S3Client) DeleteFile(key string) error {
|
|
_, err := c.Client.DeleteObject(context.TODO(), &s3.DeleteObjectInput{
|
|
Bucket: &c.Bucket,
|
|
Key: &key,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to delete file, %v", err)
|
|
}
|
|
fmt.Printf("File deleted successfully: %s\n", key)
|
|
return nil
|
|
}
|
|
|
|
// ListFiles lists all files (backups) in the specified S3 bucket
|
|
func (c *S3Client) ListFiles() ([]string, error) {
|
|
resp, err := c.Client.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{
|
|
Bucket: &c.Bucket,
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to list files, %v", err)
|
|
}
|
|
|
|
var files []string
|
|
for _, item := range resp.Contents {
|
|
files = append(files, *item.Key)
|
|
}
|
|
return files, nil
|
|
}
|
|
|
|
// SetBucket sets the S3 bucket for the client
|
|
func (c *S3Client) SetBucket(bucket string) {
|
|
c.Bucket = bucket
|
|
}
|