This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

core / spindle / engine / s3.go
1.4 kB 71 lines
1package engine 2 3import ( 4 "context" 5 "fmt" 6 "io" 7 "os" 8 "path/filepath" 9 10 "github.com/aws/aws-sdk-go-v2/aws" 11 "github.com/aws/aws-sdk-go-v2/config" 12 "github.com/aws/aws-sdk-go-v2/service/s3" 13) 14 15type S3 struct { 16 bucket string 17 client *s3.Client 18} 19 20const BASE_S3_PATH = "spindle/workflows" 21 22func NewS3(bucket string) (*S3, error) { 23 ctx := context.Background() 24 sdkConfig, err := config.LoadDefaultConfig(ctx) 25 26 if err != nil { 27 return nil, fmt.Errorf("error loading s3 config: %w", err) 28 } 29 s3Client := s3.NewFromConfig(sdkConfig) 30 31 return &S3{ 32 bucket: bucket, 33 client: s3Client, 34 }, nil 35} 36 37func (s *S3) WriteFile(ctx context.Context, path string) error { 38 s3_key := fmt.Sprintf("%s/%s", BASE_S3_PATH, filepath.Base(path)) 39 40 file, err := os.Open(path) 41 if err != nil { 42 return fmt.Errorf("error opening file %s: %w", path, err) 43 } 44 defer file.Close() 45 46 _, err = s.client.PutObject(ctx, &s3.PutObjectInput{ 47 Bucket: &s.bucket, 48 Key: &s3_key, 49 Body: file, 50 }) 51 52 if err != nil { 53 return fmt.Errorf("error writing to s3: %w", err) 54 } 55 56 return nil 57} 58 59func (s *S3) ReadFile(ctx context.Context, name string) ([]byte, error) { 60 res, err := s.client.GetObject(ctx, &s3.GetObjectInput{ 61 Bucket: &s.bucket, 62 Key: aws.String(name), 63 }) 64 65 if err != nil { 66 return nil, fmt.Errorf("error reading file %s: %w", name, err) 67 } 68 defer res.Body.Close() 69 70 return io.ReadAll(res.Body) 71}