diff --git a/s3/s3.go b/s3/s3.go index 9401c3bb8e3f4908f1890b5e42fb00c9ae367db0..ea0dc2bfc827ea4577202998a403b034ef5b2302 100644 --- a/s3/s3.go +++ b/s3/s3.go @@ -86,7 +86,7 @@ func (s SessionWithHelpers) Upload(data []byte, bucket, fileName string, metadat return response, nil } -func (s SessionWithHelpers) UploadWithExpiry(data []byte, bucket, fileName string, mimeType MIMEType, isDebug bool) (string, error) { +func (s SessionWithHelpers) UploadWith1DayExpiry(data []byte, bucket, fileName string, mimeType MIMEType, isDebug bool) (string, error) { if mimeType == "" { mimeType = getTypeForFilename(fileName) } @@ -108,6 +108,28 @@ func (s SessionWithHelpers) UploadWithExpiry(data []byte, bucket, fileName strin return s.GetSignedDownloadURL(bucket, fileName, 24*time.Hour, isDebug) } +func (s SessionWithHelpers) UploadWithExpiry(data []byte, bucket, fileName string, expiryDuration time.Duration, mimeType MIMEType, isDebug bool) (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) + if err != nil { + return "", err + } + + return s.GetSignedDownloadURL(bucket, fileName, 24*time.Hour, isDebug) +} + // 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) { getInput := &s3.GetObjectInput{ @@ -123,7 +145,7 @@ func (s SessionWithHelpers) GetSignedDownloadURL(bucket string, fileName string, func (s SessionWithHelpers) UploadWithFileExtension(data []byte, bucket, filePrefix string, fileExt string, mimeType MIMEType, isDebug bool) (*S3UploadResponse, error) { fileName := fmt.Sprintf("%s_%s.%s", filePrefix, uuid.New().String(), fileExt) - uploadUrl, err := s.UploadWithExpiry(data, bucket, fileName, mimeType, isDebug) + uploadUrl, err := s.UploadWith1DayExpiry(data, bucket, fileName, mimeType, isDebug) if err != nil { return nil, err }