-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.go
72 lines (65 loc) · 2.2 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
package utils
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/ku9nov/backup/configs"
"github.com/sirupsen/logrus"
)
type AWSS3StorageClient struct {
Client *s3.Client
}
func (c *AWSS3StorageClient) ListObjects(cfgValues configs.Config, isExtraClient bool) (interface{}, error) {
bucketName := getBucketName(cfgValues, isExtraClient)
output, err := c.Client.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{
Bucket: aws.String(bucketName),
})
if err != nil {
return nil, err
}
return output, nil
}
func (c *AWSS3StorageClient) UploadFileToS3(filename string, cfgValues configs.Config, dailyPrefix string, isExtraClient bool) error {
bucketName := getBucketName(cfgValues, isExtraClient)
file, err := os.Open(filename)
if err != nil {
return fmt.Errorf("failed to open file %s: %v", filename, err)
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
return fmt.Errorf("failed to get file info for %s: %v", filename, err)
}
_, err = c.Client.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(dailyPrefix + filepath.Base(filename)), // Adjust key to include daily, weekly, or monthly prefix
Body: file,
ContentType: aws.String("application/octet-stream"),
ContentLength: aws.Int64(fileInfo.Size()),
})
if err != nil {
return fmt.Errorf("failed to upload file %s to S3: %v", filename, err)
}
logrus.Infof("%s was successfully uploaded to S3.\n", filename)
return nil
}
func (c *AWSS3StorageClient) RemoveFileFromS3(filename string, cfgValues configs.Config, isExtraClient bool) error {
bucketName := getBucketName(cfgValues, isExtraClient)
_, err := c.Client.DeleteObjects(context.TODO(), &s3.DeleteObjectsInput{
Bucket: aws.String(bucketName),
Delete: &types.Delete{
Objects: []types.ObjectIdentifier{
{Key: aws.String(filename)},
},
},
})
if err != nil {
logrus.Errorf("Couldn't delete objects from bucket %v. Here's why: %v\n", cfgValues.Default.Bucket, err)
}
logrus.Infof("%s was successfully removed from AWS s3.\n", filename)
return nil
}