From cbbcb587544ae16b3070ea4134add091c0e6cbd0 Mon Sep 17 00:00:00 2001
From: Johan de Klerk <jdeklerk00@gmail.com>
Date: Mon, 31 Jan 2022 09:15:28 +0200
Subject: [PATCH] Upload a temp file to s3

---
 s3/s3.go | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/s3/s3.go b/s3/s3.go
index f263941..682a932 100644
--- a/s3/s3.go
+++ b/s3/s3.go
@@ -133,6 +133,29 @@ func (s SessionWithHelpers) UploadWithExpiry(data []byte, bucket, fileName strin
 	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
+}
+
 // 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{
-- 
GitLab