Skip to content
Snippets Groups Projects

Resolve "Extend new S3.UploadWithSettingsRevised to handle file path and file name separately"

+ 28
25
@@ -28,15 +28,14 @@ type S3UploadResponse struct {
}
type S3UploadSettings struct {
MimeType MIMEType
RetrieveSignedUrl bool
ExpiryDuration *time.Duration // Used to set expiry datetime of download links. NB: does not affect deletion of object from S3 bucket.
AddContentDisposition bool
FileName string // Used to specify the file name (excluding path) for content disposition; if FilePath is not present, it is used as the object key as well.
FilePath string // The complete file path (including name) used for the object key in S3.
GenerateFileNameFromParts bool // Whether the file extension needs to be specified. If true, supply FilePrefix and FileExt. Adds a UUID between the prefix and the extension.
FilePrefix string // Required when GenerateFileNameFromParts is true
FileExt string // Required when GenerateFileNameFromParts is true
MimeType MIMEType
RetrieveSignedUrl bool
ExpiryDuration *time.Duration // Used to set expiry datetime of download links. NB: does not affect deletion of object from S3 bucket.
AddContentDisposition bool
FilePath string
FileName string
FileExt string
InsertUUID bool
}
// Duration constants
@@ -155,35 +154,39 @@ func (s SessionWithHelpers) UploadWithSettings(data []byte, bucket, fileName str
// UploadWithSettingsRevised can be renamed to UploadWithSettings once original function has been deprecated.
func (s SessionWithHelpers) UploadWithSettingsRevised(data []byte, bucket string, settings S3UploadSettings) (S3UploadResponse, error) {
var fileName, filePath, uploadUrl string
var fullFileName, uploadUrl string
// fileName is used as the object key in S3
if settings.FileName != "" {
fileName = settings.FileName
uuidString := ""
if settings.InsertUUID {
uuidString = fmt.Sprintf("_%s", uuid.New().String())
}
if settings.GenerateFileNameFromParts {
fileName = fmt.Sprintf("%s_%s.%s", settings.FilePrefix, uuid.New().String(), settings.FileExt)
if len(settings.FileExt) > 0 {
if settings.FileExt[0] != '.' {
settings.FileExt = fmt.Sprintf(".%s", settings.FileExt)
}
}
if settings.FilePath != "" {
filePath = settings.FilePath
} else {
filePath = fileName
if len(settings.FilePath) > 0 {
if settings.FilePath[len(settings.FilePath)-1] != '/' {
settings.FilePath = fmt.Sprintf("%s/", settings.FilePath)
}
}
fullFileName = fmt.Sprintf("%s%s%s%s", settings.FilePath, settings.FileName, uuidString, settings.FileExt)
// Uploaded objects require a key
if fileName == "" {
if fullFileName == "" {
return S3UploadResponse{}, errors.Error("no file name supplied for upload")
}
if settings.MimeType == "" {
settings.MimeType = getTypeForFilename(fileName)
settings.MimeType = getTypeForFilename(fullFileName)
}
putInput := &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(filePath),
Key: aws.String(fullFileName),
ContentType: aws.String(string(settings.MimeType)),
Body: bytes.NewReader(data),
}
@@ -203,7 +206,7 @@ func (s SessionWithHelpers) UploadWithSettingsRevised(data []byte, bucket string
if settings.AddContentDisposition {
headers = map[string]string{
"content-disposition": "attachment; filename=\"" + fileName + "\"",
"content-disposition": fmt.Sprintf("attachment; filename=\"%s.%s\"", settings.FileName, settings.FileExt),
}
}
@@ -212,7 +215,7 @@ func (s SessionWithHelpers) UploadWithSettingsRevised(data []byte, bucket string
downloadUrlExpiry = *settings.ExpiryDuration
}
uploadUrl, err = s.GetSignedDownloadURL(bucket, filePath, downloadUrlExpiry, headers)
uploadUrl, err = s.GetSignedDownloadURL(bucket, fullFileName, downloadUrlExpiry, headers)
if err != nil {
return S3UploadResponse{}, err
}
@@ -223,7 +226,7 @@ func (s SessionWithHelpers) UploadWithSettingsRevised(data []byte, bucket string
response := S3UploadResponse{
URL: uploadUrl,
Filename: fileName,
Filename: fullFileName,
Bucket: bucket,
FileSize: fileSizeInBytes,
}
Loading