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 {
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,40 +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,
}
_, err := s.S3Session.PutObject(putInput)
if err != nil {
return "", err
}
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,
if settings.ExpiryDuration != nil {
expiry := time.Now().Add(*settings.ExpiryDuration)
putInput.Expires = &expiry
}
_, err := s.S3Session.PutObject(putInput)
......@@ -130,34 +117,33 @@ func (s SessionWithHelpers) UploadWithExpiry(data []byte, bucket, fileName strin
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
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,
return "", nil
}
if metadata != nil {
putInput.Metadata = *metadata
func (s SessionWithHelpers) UploadWith1DayExpiry(data []byte, bucket, fileName string, mimeType MIMEType) (string, error) {
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 {
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.
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
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment