From ba4ed6b92348ec5da534939b8c74fb60b0786c0b Mon Sep 17 00:00:00 2001 From: Cornel Rautenbach <corneliusr> Date: Thu, 20 Jan 2022 13:17:31 +0200 Subject: [PATCH] S3 changes --- s3/s3.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/s3/s3.go b/s3/s3.go index 9401c3b..ea0dc2b 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 } -- GitLab