Skip to content

Instantly share code, notes, and snippets.

@hernanhrm
Created July 16, 2022 02:07
Show Gist options
  • Save hernanhrm/45d528aebcd94089a68f5ad90371e438 to your computer and use it in GitHub Desktop.
Save hernanhrm/45d528aebcd94089a68f5ad90371e438 to your computer and use it in GitHub Desktop.
const (
// PublicRead is used to set the permission of a file to public read
PublicRead = "public-read"
// Private is used to set the permission of a file to private
Private = "private"
)
// Upload takes a file and uploads it to the s3 bucket
func (s Service) Upload(fileBytes []byte, contentType, path string, isPublic bool) error {
permission := PublicRead
if !isPublic {
permission = Private
}
_, err := s.S3.PutObject(&s3.PutObjectInput{
Bucket: aws.String(s.Bucket),
Key: aws.String(path),
Body: bytes.NewReader(fileBytes),
ContentType: aws.String(contentType),
ACL: aws.String(permission),
})
if err != nil {
return fmt.Errorf("s3.Upload(): %v", err)
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment