idun/pkg/storage/s3.go
2023-10-14 15:13:04 +02:00

96 lines
2.2 KiB
Go

package storage
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
type S3FileSystem struct {
Config map[string]string
}
func (l S3FileSystem) ListFiles() ([]File, error) {
s3Config := &aws.Config{
Credentials: credentials.NewStaticCredentials(l.Config["keyid"], l.Config["secret"], ""),
Endpoint: aws.String(l.Config["endpoint"]),
Region: aws.String(l.Config["region"]),
S3ForcePathStyle: aws.Bool(true),
}
newSession, err := session.NewSession(s3Config)
if err != nil {
return nil, err
}
s3Client := s3.New(newSession)
lc := s3.ListObjectsInput{
Bucket: aws.String(l.Config["bucket"]),
Delimiter: nil,
EncodingType: nil,
ExpectedBucketOwner: nil,
Marker: nil,
MaxKeys: nil,
OptionalObjectAttributes: nil,
Prefix: aws.String(l.Config["prefix"]),
RequestPayer: nil,
}
objects, err := s3Client.ListObjects(&lc)
if err != nil {
return nil, err
}
fileList := []File{}
for _, o := range objects.Contents {
f := GeneralFile{
name: *o.Key,
time: *o.LastModified,
}
fileList = append(fileList, f)
}
return fileList, nil
}
func (I S3FileSystem) Delete(files []File) error {
s3Config := &aws.Config{
Credentials: credentials.NewStaticCredentials(I.Config["keyid"], I.Config["secret"], ""),
Endpoint: aws.String(I.Config["endpoint"]),
Region: aws.String(I.Config["region"]),
S3ForcePathStyle: aws.Bool(true),
}
newSession, err := session.NewSession(s3Config)
if err != nil {
return err
}
s3Client := s3.New(newSession)
for _, f := range files {
name, err := f.GetName()
if err != nil {
return err
}
doc := s3.DeleteObjectInput{
Bucket: aws.String(I.Config["bucket"]),
BypassGovernanceRetention: nil,
ExpectedBucketOwner: nil,
Key: aws.String(name),
MFA: nil,
RequestPayer: nil,
VersionId: nil,
}
_, err = s3Client.DeleteObject(&doc)
if err != nil {
return err
}
}
return nil
}