Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
75d7999f
Commit
75d7999f
authored
3 years ago
by
Johan de Klerk
Browse files
Options
Downloads
Patches
Plain Diff
Added S3 UploadWithSettings function
parent
cbbcb587
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
s3/s3.go
+40
-49
40 additions, 49 deletions
s3/s3.go
with
40 additions
and
49 deletions
s3/s3.go
+
40
−
49
View file @
75d7999f
...
@@ -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
)
UploadWith
1DayExpiry
(
data
[]
byte
,
bucket
,
fileName
string
,
mimeType
MIMEType
,
isDebug
bool
)
(
string
,
error
)
{
func
(
s
SessionWithHelpers
)
UploadWith
Settings
(
data
[]
byte
,
bucket
,
fileName
string
,
settings
S3UploadSettings
)
(
string
,
error
)
{
if
m
imeType
==
""
{
if
settings
.
M
imeType
==
""
{
m
imeType
=
getTypeForFilename
(
fileName
)
settings
.
M
imeType
=
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
(
m
imeType
)),
ContentType
:
aws
.
String
(
string
(
settings
.
M
imeType
)),
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
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment