-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.go
111 lines (98 loc) · 2.97 KB
/
aws.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package cloudcourier
import (
"fmt"
"io"
"github.com/aws/aws-sdk-go/aws"
"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"
)
type AwsManufacture struct {
Region string
Bucket string
}
func (a *AwsManufacture) GetProvider() cloudCourierProvider {
return AWS
}
func init() {
storeFunc[AWS] = newAWSClient
}
type AWSClient struct {
AwConfig *AwsManufacture
BucketName string
S3 *s3.S3
}
func newAWSClient(cbb Provider) (StorageClient, error) {
awConfig, ok := cbb.(*AwsManufacture)
if !ok {
return nil, fmt.Errorf("incorrect configuration")
}
sess, err := session.NewSession(&aws.Config{
Region: &awConfig.Region,
})
if err != nil {
return nil, fmt.Errorf("failed to create AWS session: %v", err)
}
s3Svc := s3.New(sess)
return &AWSClient{S3: s3Svc,
BucketName: awConfig.Bucket, AwConfig: awConfig}, nil
}
// AWSClient struct encapsulates the AWS S3 client to manage storage operations.
// type AWSClient struct {
// S3 *s3.S3 // The S3 service client
// BucketName string // The name of the bucket to operate on
// }
// NewAWSClient creates a new client for AWS S3 operations.
// It requires AWS credentials and a region to initialize the S3 service client.
// UploadFile uploads a file to S3 from an io.Reader.
func (client *AWSClient) UploadFile(filePath string, reader io.Reader) error {
uploader := s3manager.NewUploaderWithClient(client.S3)
_, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(client.BucketName),
Key: aws.String(filePath),
Body: reader,
})
if err != nil {
return fmt.Errorf("failed to upload file to S3: %v", err)
}
return nil
}
// DeleteFile deletes a file from S3 by its key.
func (client *AWSClient) DeleteFile(fileID string) error {
_, err := client.S3.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(client.BucketName),
Key: aws.String(fileID),
})
if err != nil {
return fmt.Errorf("failed to delete file from S3: %v", err)
}
return nil
}
// ListFiles lists all files in a specified directory of the S3 bucket.
func (client *AWSClient) ListFiles(directory string) ([]string, error) {
var fileNames []string
err := client.S3.ListObjectsV2Pages(&s3.ListObjectsV2Input{
Bucket: aws.String(client.BucketName),
Prefix: aws.String(directory),
}, func(page *s3.ListObjectsV2Output, lastPage bool) bool {
for _, obj := range page.Contents {
fileNames = append(fileNames, *obj.Key)
}
return true // continue paging
})
if err != nil {
return nil, fmt.Errorf("failed to list files in S3: %v", err)
}
return fileNames, nil
}
// GetFile retrieves a file as an io.Reader by its key from S3.
func (client *AWSClient) GetFile(fileID string) (io.Reader, error) {
output, err := client.S3.GetObject(&s3.GetObjectInput{
Bucket: aws.String(client.BucketName),
Key: aws.String(fileID),
})
if err != nil {
return nil, fmt.Errorf("failed to retrieve file from S3: %v", err)
}
return output.Body, nil
}