diff --git a/s3/s3.go b/s3/s3.go index 682a932d0be4996df247e2f258ed0d8a41fc01f4..9784db880c32a3062afa80d14554fb49fc4e04de 100644 --- a/s3/s3.go +++ b/s3/s3.go @@ -27,6 +27,12 @@ type S3UploadResponse struct { FileSize int `json:"file_size"` } +type S3UploadSettings struct { + MimeType MIMEType + RetrieveSignedUrl bool + ExpiryDuration *time.Duration +} + type MIMEType string const ( @@ -68,7 +74,7 @@ func NewSession(session *session.Session) *SessionWithHelpers { } } -func (s SessionWithHelpers) Upload(data []byte, bucket, fileName string, metadata *map[string]*string, isDebug bool) (*s3.PutObjectOutput, error) { +func (s SessionWithHelpers) Upload(data []byte, bucket, fileName string, metadata *map[string]*string) (*s3.PutObjectOutput, error) { mimeType := getTypeForFilename(fileName) putInput := &s3.PutObjectInput{ Bucket: aws.String(bucket), @@ -89,18 +95,21 @@ func (s SessionWithHelpers) Upload(data []byte, bucket, fileName string, metadat return response, nil } -func (s SessionWithHelpers) UploadWith1DayExpiry(data []byte, bucket, fileName string, mimeType MIMEType, isDebug bool) (string, error) { - if mimeType == "" { - mimeType = getTypeForFilename(fileName) +func (s SessionWithHelpers) UploadWithSettings(data []byte, bucket, fileName string, settings S3UploadSettings) (string, error) { + if settings.MimeType == "" { + settings.MimeType = getTypeForFilename(fileName) } - expiry := time.Now().Add(24 * time.Hour) putInput := &s3.PutObjectInput{ Bucket: aws.String(bucket), Key: aws.String(fileName), - ContentType: aws.String(string(mimeType)), + ContentType: aws.String(string(settings.MimeType)), Body: bytes.NewReader(data), - Expires: &expiry, + } + + if settings.ExpiryDuration != nil { + expiry := time.Now().Add(*settings.ExpiryDuration) + putInput.Expires = &expiry } _, err := s.S3Session.PutObject(putInput) @@ -108,56 +117,33 @@ func (s SessionWithHelpers) UploadWith1DayExpiry(data []byte, bucket, fileName s return "", err } - return s.GetSignedDownloadURL(bucket, fileName, 24*time.Hour, isDebug) + if settings.RetrieveSignedUrl { + return s.GetSignedDownloadURL(bucket, fileName, 24*time.Hour) + } + + return "", nil } -func (s SessionWithHelpers) UploadWithExpiry(data []byte, bucket, fileName string, expiryDuration time.Duration, mimeType MIMEType, isDebug bool) (string, error) { +func (s SessionWithHelpers) UploadWith1DayExpiry(data []byte, bucket, fileName string, mimeType MIMEType) (string, error) { if mimeType == "" { mimeType = getTypeForFilename(fileName) } - expiry := time.Now().Add(expiryDuration) - putInput := &s3.PutObjectInput{ - Bucket: aws.String(bucket), - Key: aws.String(fileName), - ContentType: aws.String(string(mimeType)), - Body: bytes.NewReader(data), - Expires: &expiry, - } - - _, err := s.S3Session.PutObject(putInput) + expiry := 24 * time.Hour + signedUrl, err := s.UploadWithSettings(data, bucket, fileName, S3UploadSettings{ + MimeType: mimeType, + RetrieveSignedUrl: true, + ExpiryDuration: &expiry, + }) if err != nil { return "", err } - return s.GetSignedDownloadURL(bucket, fileName, 24*time.Hour, isDebug) -} - -// UploadTempFile upload a file to S3 that will be automatically deleted after the expireDate -func (s SessionWithHelpers) UploadTempFile(data []byte, bucket, fileName string, metadata *map[string]*string, expireDate *time.Time) (*s3.PutObjectOutput, error) { - mimeType := getTypeForFilename(fileName) - putInput := &s3.PutObjectInput{ - Bucket: aws.String(bucket), - Key: aws.String(fileName), - ContentType: aws.String(string(mimeType)), - Body: bytes.NewReader(data), - Expires: expireDate, - } - - if metadata != nil { - putInput.Metadata = *metadata - } - - response, err := s.S3Session.PutObject(putInput) - if err != nil { - return nil, err - } - - return response, nil + return signedUrl, nil } // 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, isDebug bool) (string, error) { +func (s SessionWithHelpers) GetSignedDownloadURL(bucket string, fileName string, duration time.Duration) (string, error) { getInput := &s3.GetObjectInput{ Bucket: aws.String(bucket), Key: aws.String(fileName), @@ -196,10 +182,15 @@ func (s SessionWithHelpers) FileExists(bucket string, fileName string) (bool, er } // UploadWithFileExtension will upload a file to S3 and return a standard S3UploadResponse. -func (s SessionWithHelpers) UploadWithFileExtension(data []byte, bucket, filePrefix string, fileExt string, mimeType MIMEType, isDebug bool) (*S3UploadResponse, error) { +func (s SessionWithHelpers) UploadWithFileExtension(data []byte, bucket, filePrefix, fileExt string, mimeType MIMEType) (*S3UploadResponse, error) { fileName := fmt.Sprintf("%s_%s.%s", filePrefix, uuid.New().String(), fileExt) - uploadUrl, err := s.UploadWith1DayExpiry(data, bucket, fileName, mimeType, isDebug) + duration := 24 * time.Hour + uploadUrl, err := s.UploadWithSettings(data, bucket, fileName, S3UploadSettings{ + MimeType: mimeType, + RetrieveSignedUrl: true, + ExpiryDuration: &duration, + }) if err != nil { return nil, err } @@ -278,9 +269,9 @@ func (s SessionWithHelpers) CopyObjectBucketToBucket(sourceBucket string, destin // copy the file copySource := url.QueryEscape(sourceBucket + "/" + sourceFileName) copyObjectInput := &s3.CopyObjectInput{ - Bucket: aws.String(destinationBucket), //destination bucket - CopySource: aws.String(copySource), //source path (ie: myBucket/myFile.csv) - Key: aws.String(destinationFilename), //filename on destination + Bucket: aws.String(destinationBucket), // destination bucket + CopySource: aws.String(copySource), // source path (ie: myBucket/myFile.csv) + Key: aws.String(destinationFilename), // filename on destination } _, err := s.S3Session.CopyObject(copyObjectInput) if err != nil {