diff --git a/s3/s3.go b/s3/s3.go index 0f7a3823e4575473ac9ea511b04612a30fc14d5d..0275af6d85a21c01e94d94d17a8a7d5190ef4928 100644 --- a/s3/s3.go +++ b/s3/s3.go @@ -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,27 @@ 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 = uuid.New().String() } - if settings.GenerateFileNameFromParts { - fileName = fmt.Sprintf("%s_%s.%s", settings.FilePrefix, uuid.New().String(), settings.FileExt) - } - - if settings.FilePath != "" { - filePath = settings.FilePath - } else { - filePath = fileName - } + 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 +194,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 +203,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 +214,7 @@ func (s SessionWithHelpers) UploadWithSettingsRevised(data []byte, bucket string response := S3UploadResponse{ URL: uploadUrl, - Filename: fileName, + Filename: fullFileName, Bucket: bucket, FileSize: fileSizeInBytes, }