-
Notifications
You must be signed in to change notification settings - Fork 24
/
s3_client.go
115 lines (101 loc) · 2.66 KB
/
s3_client.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
112
113
114
115
/*
* Copyright 2022-present Kuei-chun Chen. All rights reserved.
* s3_client.go
*/
package hatchet
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
// S3Client provides methods to interact with an S3 service.
type S3Client struct {
service *s3.S3
}
func NewS3Client(profile string, params...string) (*S3Client, error) {
sess, err := session.NewSessionWithOptions(session.Options{
Profile: profile,
SharedConfigState: session.SharedConfigEnable,
Config: aws.Config{
CredentialsChainVerboseErrors: aws.Bool(true),
},
})
if err != nil {
return nil, err
}
config := aws.Config{
Region: aws.String(*sess.Config.Region),
S3ForcePathStyle: aws.Bool(true),
Credentials: sess.Config.Credentials,
HTTPClient: &http.Client{
Timeout: time.Second * 10,
},
}
if len(params) > 0 && params[0] != "" {
config.Endpoint = ¶ms[0]
}
if sess, err = session.NewSession(&config); err != nil {
return nil, errors.New("error creating session")
}
// Create a new S3 service client
service := s3.New(sess)
return &S3Client{service}, nil
}
// CreateBucket creates a new S3 bucket.
func (c *S3Client) CreateBucket(bucket string) error {
_, err := c.service.CreateBucket(&s3.CreateBucketInput{
Bucket: aws.String(bucket),
})
return err
}
// CreateBucket creates a new S3 bucket.
func (c *S3Client) DeleteBucket(bucket string) error {
_, err := c.service.DeleteBucket(&s3.DeleteBucketInput{
Bucket: aws.String(bucket),
})
return err
}
// DeleteObject deletes an object from S3.
func (c *S3Client) DeleteObject(bucket, key string) error {
_, err := c.service.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
return err
}
// PutObject uploads a file to S3.
func (c *S3Client) PutObject(bucket, key, filePath string) error {
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("error opening file: %v", err)
}
defer file.Close()
_, err = c.service.PutObject(&s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Body: file,
})
return err
}
// GetObject retrieves an object from S3 and returns its contents as a byte slice.
func (c *S3Client) GetObject(logname string) ([]byte, error) {
toks := strings.Split(logname, "/")
bucket := toks[0]
key := strings.Join(toks[1:], "/")
resp, err := c.service.GetObject(&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
return nil, fmt.Errorf("error retrieving S3 object: %v", err)
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}