Skip to content
Snippets Groups Projects
Commit 28121f29 authored by Francé Wilke's avatar Francé Wilke
Browse files

Merge branch '406-s3-header-download' into 'main'

Add param to apply content disposition header to s3 downloads

See merge request uafrica/go-utils!20
parents 503fc952 e49205d9
Branches
Tags v1.17.0
1 merge request!20Add param to apply content disposition header to s3 downloads
......@@ -31,6 +31,7 @@ type S3UploadSettings struct {
MimeType MIMEType
RetrieveSignedUrl bool
ExpiryDuration *time.Duration
AddContentDisposition bool
}
type MIMEType string
......@@ -121,7 +122,14 @@ func (s SessionWithHelpers) UploadWithSettings(data []byte, bucket, fileName str
}
if settings.RetrieveSignedUrl {
return s.GetSignedDownloadURL(bucket, fileName, 24*time.Hour)
var headers map[string]string
if settings.AddContentDisposition {
headers = map[string]string{
"content-disposition": "attachment; filename=\"" + fileName + "\"",
}
}
return s.GetSignedDownloadURL(bucket, fileName, 24*time.Hour, headers)
}
return "", nil
......@@ -146,11 +154,18 @@ func (s SessionWithHelpers) UploadWith1DayExpiry(data []byte, bucket, fileName s
}
// GetSignedDownloadURL gets a signed download URL for the duration. If scv is nil, a new session will be created.
func (s SessionWithHelpers) GetSignedDownloadURL(bucket string, fileName string, duration time.Duration) (string, error) {
func (s SessionWithHelpers) GetSignedDownloadURL(bucket string, fileName string, duration time.Duration, headers ...map[string]string) (string, error) {
getInput := &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(fileName),
}
if headers != nil {
if value, exists := headers[0]["content-disposition"]; exists {
getInput.ResponseContentDisposition = &value
}
}
getRequest, _ := s.S3Session.GetObjectRequest(getInput)
fileExists, err := s.FileExists(bucket, fileName)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment