Skip to content
Snippets Groups Projects
Commit 75d7999f authored by Johan de Klerk's avatar Johan de Klerk
Browse files

Added S3 UploadWithSettings function

parent cbbcb587
No related branches found
No related tags found
No related merge requests found
...@@ -27,6 +27,12 @@ type S3UploadResponse struct { ...@@ -27,6 +27,12 @@ type S3UploadResponse struct {
FileSize int `json:"file_size"` FileSize int `json:"file_size"`
} }
type S3UploadSettings struct {
MimeType MIMEType
RetrieveSignedUrl bool
ExpiryDuration *time.Duration
}
type MIMEType string type MIMEType string
const ( const (
...@@ -68,7 +74,7 @@ func NewSession(session *session.Session) *SessionWithHelpers { ...@@ -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) mimeType := getTypeForFilename(fileName)
putInput := &s3.PutObjectInput{ putInput := &s3.PutObjectInput{
Bucket: aws.String(bucket), Bucket: aws.String(bucket),
...@@ -89,40 +95,21 @@ func (s SessionWithHelpers) Upload(data []byte, bucket, fileName string, metadat ...@@ -89,40 +95,21 @@ func (s SessionWithHelpers) Upload(data []byte, bucket, fileName string, metadat
return response, nil return response, nil
} }
func (s SessionWithHelpers) UploadWith1DayExpiry(data []byte, bucket, fileName string, mimeType MIMEType, isDebug bool) (string, error) { func (s SessionWithHelpers) UploadWithSettings(data []byte, bucket, fileName string, settings S3UploadSettings) (string, error) {
if mimeType == "" { if settings.MimeType == "" {
mimeType = getTypeForFilename(fileName) settings.MimeType = getTypeForFilename(fileName)
} }
expiry := time.Now().Add(24 * time.Hour)
putInput := &s3.PutObjectInput{ putInput := &s3.PutObjectInput{
Bucket: aws.String(bucket), Bucket: aws.String(bucket),
Key: aws.String(fileName), Key: aws.String(fileName),
ContentType: aws.String(string(mimeType)), ContentType: aws.String(string(settings.MimeType)),
Body: bytes.NewReader(data), 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) if settings.ExpiryDuration != nil {
} expiry := time.Now().Add(*settings.ExpiryDuration)
putInput.Expires = &expiry
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) _, err := s.S3Session.PutObject(putInput)
...@@ -130,34 +117,33 @@ func (s SessionWithHelpers) UploadWithExpiry(data []byte, bucket, fileName strin ...@@ -130,34 +117,33 @@ func (s SessionWithHelpers) UploadWithExpiry(data []byte, bucket, fileName strin
return "", err return "", err
} }
return s.GetSignedDownloadURL(bucket, fileName, 24*time.Hour, isDebug) if settings.RetrieveSignedUrl {
return s.GetSignedDownloadURL(bucket, fileName, 24*time.Hour)
} }
// UploadTempFile upload a file to S3 that will be automatically deleted after the expireDate return "", nil
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 { func (s SessionWithHelpers) UploadWith1DayExpiry(data []byte, bucket, fileName string, mimeType MIMEType) (string, error) {
putInput.Metadata = *metadata if mimeType == "" {
mimeType = getTypeForFilename(fileName)
} }
response, 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 { if err != nil {
return nil, err return "", 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. // 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{ getInput := &s3.GetObjectInput{
Bucket: aws.String(bucket), Bucket: aws.String(bucket),
Key: aws.String(fileName), Key: aws.String(fileName),
...@@ -196,10 +182,15 @@ func (s SessionWithHelpers) FileExists(bucket string, fileName string) (bool, er ...@@ -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. // 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) 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 { if err != nil {
return nil, err return nil, err
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment