Skip to content

Commit

Permalink
added s3 put functions with s3 tag and metadata support
Browse files Browse the repository at this point in the history
Signed-off-by: MenD32 <[email protected]>
  • Loading branch information
MenD32 committed Sep 14, 2024
1 parent 442fd11 commit 0d295b8
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ type S3Client interface {
// PutFile puts a single file to a bucket at the specified key
PutFile(bucket, key, path string) error

// PutFileWithMetadata puts a single file to a bucket at the specified key with specified metadata and tags
PutFileWithMetadata(bucket, key, path string, metadata, tags map[string]string) error

// PutDirectory puts a complete directory into a bucket key prefix, with each file in the directory
// a separate key in the bucket.
PutDirectory(bucket, key, path string) error

// PutDirectoryWithMetadata puts a complete directory into a bucket key prefix, with each file in the directory
// a separate key in the bucket with specified metadata and tags.
PutDirectoryWithMetadata(bucket, key, path string, metadata, tags map[string]string) error

// GetFile downloads a file to a local file path
GetFile(bucket, key, path string) error

Expand Down Expand Up @@ -225,6 +232,24 @@ func (s *s3client) PutFile(bucket, key, path string) error {
return nil
}

// PutFile puts a single file to a bucket at the specified key
func (s *s3client) PutFileWithMetadata(bucket, key, path string, metadata, tags map[string]string) error {
log.WithFields(log.Fields{"endpoint": s.Endpoint, "bucket": bucket, "key": key, "path": path}).Info("Saving file to s3")
// NOTE: minio will detect proper mime-type based on file extension

encOpts, err := s.EncryptOpts.buildServerSideEnc(bucket, key)

if err != nil {
return errors.WithStack(err)
}

_, err = s.minioClient.FPutObject(s.ctx, bucket, key, path, minio.PutObjectOptions{ServerSideEncryption: encOpts, UserMetadata: metadata, UserTags: tags})
if err != nil {
return errors.WithStack(err)
}
return nil
}

func (s *s3client) BucketExists(bucketName string) (bool, error) {
log.WithField("bucket", bucketName).Info("Checking if bucket exists")
result, err := s.minioClient.BucketExists(s.ctx, bucketName)
Expand Down Expand Up @@ -288,6 +313,16 @@ func (s *s3client) PutDirectory(bucket, key, path string) error {
return nil
}

func (s *s3client) PutDirectoryWithMetadata(bucket, key, path string, metadata, tags map[string]string) error {
for putTask := range generatePutTasks(key, path) {
err := s.PutFileWithMetadata(bucket, putTask.key, putTask.path, metadata, tags)
if err != nil {
return err
}
}
return nil
}

// GetFile downloads a file to a local file path
func (s *s3client) GetFile(bucket, key, path string) error {
log.WithFields(log.Fields{"endpoint": s.Endpoint, "bucket": bucket, "key": key, "path": path}).Info("Getting file from s3")
Expand Down

0 comments on commit 0d295b8

Please sign in to comment.