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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
ac44bce0
Commit
ac44bce0
authored
3 years ago
by
Cornel Rautenbach
Browse files
Options
Downloads
Patches
Plain Diff
S3 to be more reusable
parent
22b236bd
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
+38
-69
38 additions, 69 deletions
s3/s3.go
with
38 additions
and
69 deletions
s3
_utils/s3_utils
.go
→
s3
/s3
.go
+
38
−
69
View file @
ac44bce0
package
s3
_utils
package
s3
import
(
"bytes"
...
...
@@ -11,13 +11,10 @@ import (
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/google/uuid"
)
var
s3Session
*
s3
.
S3
// S3UploadResponse defines the structure of a standard JSON response to a PDF/CSV/etc request.
type
S3UploadResponse
struct
{
URL
string
`json:"url"`
...
...
@@ -29,16 +26,16 @@ type S3UploadResponse struct {
type
MIMEType
string
const
(
// TypePDF defines the constant for the PDF MIME type.
//
MIME
TypePDF defines the constant for the PDF MIME type.
MIMETypePDF
MIMEType
=
"application/pdf"
// TypeCSV defines the constant for the CSV MIME type.
//
MIME
TypeCSV defines the constant for the CSV MIME type.
MIMETypeCSV
MIMEType
=
"text/csv"
// TypeZIP defines the constant for the ZIP MIME type.
//
MIME
TypeZIP defines the constant for the ZIP MIME type.
MIMETypeZIP
MIMEType
=
"application/zip"
// TypeJSON defines the constant for the JSON MIME type.
//
MIME
TypeJSON defines the constant for the JSON MIME type.
MIMETypeJSON
MIMEType
=
"application/json"
// MIMETypeText defines the constant for the Plain text MIME type.
...
...
@@ -47,11 +44,21 @@ const (
// MIMETypeImage defines the constant for the Image MIME type.
MIMETypeImage
MIMEType
=
"image/*"
// TypeDefault defines the constant for the default MIME type.
//
MIME
TypeDefault defines the constant for the default MIME type.
MIMETypeDefault
MIMEType
=
"application/octet-stream"
)
func
Upload
(
data
[]
byte
,
bucket
,
fileName
string
,
metadata
*
map
[
string
]
*
string
,
isDebug
bool
)
(
*
s3
.
PutObjectOutput
,
error
)
{
type
SessionWithHelpers
struct
{
S3Session
*
s3
.
S3
}
func
NewSession
(
session
*
s3
.
S3
)
*
SessionWithHelpers
{
return
&
SessionWithHelpers
{
S3Session
:
session
,
}
}
func
(
s
SessionWithHelpers
)
Upload
(
data
[]
byte
,
bucket
,
fileName
string
,
metadata
*
map
[
string
]
*
string
,
isDebug
bool
)
(
*
s3
.
PutObjectOutput
,
error
)
{
mimeType
:=
getTypeForFilename
(
fileName
)
putInput
:=
&
s3
.
PutObjectInput
{
Bucket
:
aws
.
String
(
bucket
),
...
...
@@ -64,7 +71,7 @@ func Upload(data []byte, bucket, fileName string, metadata *map[string]*string,
putInput
.
Metadata
=
*
metadata
}
response
,
err
:=
get
Session
(
isDebug
)
.
PutObject
(
putInput
)
response
,
err
:=
s
.
S3
Session
.
PutObject
(
putInput
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -72,7 +79,7 @@ func Upload(data []byte, bucket, fileName string, metadata *map[string]*string,
return
response
,
nil
}
func
UploadWithExpiry
(
data
[]
byte
,
bucket
,
fileName
string
,
mimeType
MIMEType
,
isDebug
bool
)
(
string
,
error
)
{
func
(
s
SessionWithHelpers
)
UploadWithExpiry
(
data
[]
byte
,
bucket
,
fileName
string
,
mimeType
MIMEType
,
isDebug
bool
)
(
string
,
error
)
{
if
mimeType
==
""
{
mimeType
=
getTypeForFilename
(
fileName
)
}
...
...
@@ -86,30 +93,30 @@ func UploadWithExpiry(data []byte, bucket, fileName string, mimeType MIMEType, i
Expires
:
&
expiry
,
}
_
,
err
:=
get
Session
(
isDebug
)
.
PutObject
(
putInput
)
_
,
err
:=
s
.
S3
Session
.
PutObject
(
putInput
)
if
err
!=
nil
{
return
""
,
err
}
return
GetSignedDownloadURL
(
bucket
,
fileName
,
24
*
time
.
Hour
,
isDebug
)
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
GetSignedDownloadURL
(
bucket
string
,
fileName
string
,
duration
time
.
Duration
,
isDebug
bool
)
(
string
,
error
)
{
func
(
s
SessionWithHelpers
)
GetSignedDownloadURL
(
bucket
string
,
fileName
string
,
duration
time
.
Duration
,
isDebug
bool
)
(
string
,
error
)
{
getInput
:=
&
s3
.
GetObjectInput
{
Bucket
:
aws
.
String
(
bucket
),
Key
:
aws
.
String
(
fileName
),
}
getRequest
,
_
:=
get
Session
(
isDebug
)
.
GetObjectRequest
(
getInput
)
getRequest
,
_
:=
s
.
S3
Session
.
GetObjectRequest
(
getInput
)
return
getRequest
.
Presign
(
duration
)
}
// UploadWithFileExtension will upload a file to S3 and return a standard S3UploadResponse.
func
UploadWithFileExtension
(
data
[]
byte
,
bucket
,
filePrefix
string
,
fileExt
string
,
mimeType
MIMEType
,
isDebug
bool
)
(
*
S3UploadResponse
,
error
)
{
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
:=
UploadWithExpiry
(
data
,
bucket
,
fileName
,
mimeType
,
isDebug
)
uploadUrl
,
err
:=
s
.
UploadWithExpiry
(
data
,
bucket
,
fileName
,
mimeType
,
isDebug
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -143,24 +150,24 @@ func getTypeForFilename(f string) MIMEType {
return
MIMETypeDefault
}
func
GetObject
(
bucket
string
,
fileName
string
,
isDebug
bool
)
(
*
s3
.
GetObjectOutput
,
error
)
{
func
(
s
SessionWithHelpers
)
GetObject
(
bucket
string
,
fileName
string
,
isDebug
bool
)
(
*
s3
.
GetObjectOutput
,
error
)
{
getInput
:=
&
s3
.
GetObjectInput
{
Bucket
:
aws
.
String
(
bucket
),
Key
:
aws
.
String
(
fileName
),
}
getObjectOutput
,
err
:=
get
Session
(
isDebug
)
.
GetObject
(
getInput
)
getObjectOutput
,
err
:=
s
.
S3
Session
.
GetObject
(
getInput
)
if
err
!=
nil
{
return
nil
,
err
}
return
getObjectOutput
,
nil
}
func
GetObjectMetadata
(
bucket
string
,
fileName
string
,
isDebug
bool
)
(
map
[
string
]
*
string
,
error
)
{
func
(
s
SessionWithHelpers
)
GetObjectMetadata
(
bucket
string
,
fileName
string
,
isDebug
bool
)
(
map
[
string
]
*
string
,
error
)
{
headObjectInput
:=
&
s3
.
HeadObjectInput
{
Bucket
:
aws
.
String
(
bucket
),
Key
:
aws
.
String
(
fileName
),
}
headObjectOutput
,
err
:=
get
Session
(
isDebug
)
.
HeadObject
(
headObjectInput
)
headObjectOutput
,
err
:=
s
.
S3
Session
.
HeadObject
(
headObjectInput
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -168,14 +175,14 @@ func GetObjectMetadata(bucket string, fileName string, isDebug bool) (map[string
}
// MoveObjectBucketToBucket - Move object from one S3 bucket to another
func
MoveObjectBucketToBucket
(
sourceBucket
string
,
destinationBucket
string
,
sourceFileName
string
,
destinationFileName
string
,
isDebug
bool
)
error
{
func
(
s
SessionWithHelpers
)
MoveObjectBucketToBucket
(
sourceBucket
string
,
destinationBucket
string
,
sourceFileName
string
,
destinationFileName
string
,
isDebug
bool
)
error
{
err
:=
CopyObjectBucketToBucket
(
sourceBucket
,
destinationBucket
,
sourceFileName
,
destinationFileName
,
isDebug
)
err
:=
s
.
CopyObjectBucketToBucket
(
sourceBucket
,
destinationBucket
,
sourceFileName
,
destinationFileName
,
isDebug
)
if
err
!=
nil
{
return
err
}
err
=
DeleteObjectFromBucket
(
sourceBucket
,
sourceFileName
,
isDebug
)
err
=
s
.
DeleteObjectFromBucket
(
sourceBucket
,
sourceFileName
,
isDebug
)
if
err
!=
nil
{
return
err
}
...
...
@@ -184,7 +191,7 @@ func MoveObjectBucketToBucket(sourceBucket string, destinationBucket string, sou
}
// CopyObjectBucketToBucket - Copy an object from one S3 bucket to another
func
CopyObjectBucketToBucket
(
sourceBucket
string
,
destinationBucket
string
,
sourceFileName
string
,
destinationFilename
string
,
isDebug
bool
)
error
{
func
(
s
SessionWithHelpers
)
CopyObjectBucketToBucket
(
sourceBucket
string
,
destinationBucket
string
,
sourceFileName
string
,
destinationFilename
string
,
isDebug
bool
)
error
{
// copy the file
copySource
:=
url
.
QueryEscape
(
sourceBucket
+
"/"
+
sourceFileName
)
copyObjectInput
:=
&
s3
.
CopyObjectInput
{
...
...
@@ -192,13 +199,13 @@ func CopyObjectBucketToBucket(sourceBucket string, destinationBucket string, sou
CopySource
:
aws
.
String
(
copySource
),
//source path (ie: myBucket/myFile.csv)
Key
:
aws
.
String
(
destinationFilename
),
//filename on destination
}
_
,
err
:=
get
Session
(
isDebug
)
.
CopyObject
(
copyObjectInput
)
_
,
err
:=
s
.
S3
Session
.
CopyObject
(
copyObjectInput
)
if
err
!=
nil
{
return
err
}
// wait to see if the file copied successfully
err
=
get
Session
(
isDebug
)
.
WaitUntilObjectExists
(
&
s3
.
HeadObjectInput
{
Bucket
:
aws
.
String
(
destinationBucket
),
Key
:
aws
.
String
(
destinationFilename
)})
err
=
s
.
S3
Session
.
WaitUntilObjectExists
(
&
s3
.
HeadObjectInput
{
Bucket
:
aws
.
String
(
destinationBucket
),
Key
:
aws
.
String
(
destinationFilename
)})
if
err
!=
nil
{
return
err
}
...
...
@@ -207,19 +214,19 @@ func CopyObjectBucketToBucket(sourceBucket string, destinationBucket string, sou
}
// DeleteObjectFromBucket - Delete an object from an S3 bucket
func
DeleteObjectFromBucket
(
bucket
string
,
fileName
string
,
isDebug
bool
)
error
{
func
(
s
SessionWithHelpers
)
DeleteObjectFromBucket
(
bucket
string
,
fileName
string
,
isDebug
bool
)
error
{
// delete the file
deleteObjectInput
:=
&
s3
.
DeleteObjectInput
{
Bucket
:
aws
.
String
(
bucket
),
Key
:
aws
.
String
(
fileName
),
}
_
,
err
:=
get
Session
(
isDebug
)
.
DeleteObject
(
deleteObjectInput
)
_
,
err
:=
s
.
S3
Session
.
DeleteObject
(
deleteObjectInput
)
if
err
!=
nil
{
return
err
}
// wait to see if the file deleted successfully
err
=
get
Session
(
isDebug
)
.
WaitUntilObjectNotExists
(
&
s3
.
HeadObjectInput
{
err
=
s
.
S3
Session
.
WaitUntilObjectNotExists
(
&
s3
.
HeadObjectInput
{
Bucket
:
aws
.
String
(
bucket
),
// the bucket we are deleting from
Key
:
aws
.
String
(
fileName
),
// the filename we are deleting
})
...
...
@@ -247,41 +254,3 @@ func GetS3FileKey(fileName string, folder string) string {
return
"/"
+
folder
+
"/"
+
fileName
}
func
ListObjects
(
bucketName
string
,
maxKeys
int64
,
function
func
(
*
s3
.
ListObjectsOutput
,
bool
)
bool
,
isDebug
bool
)
error
{
listObjectsInput
:=
&
s3
.
ListObjectsInput
{
Bucket
:
aws
.
String
(
bucketName
),
MaxKeys
:
aws
.
Int64
(
maxKeys
),
}
err
:=
getSession
(
isDebug
)
.
ListObjectsPages
(
listObjectsInput
,
function
)
if
err
!=
nil
{
return
err
}
return
nil
}
func
getSession
(
isDebug
bool
)
*
s3
.
S3
{
if
s3Session
==
nil
{
env
:=
os
.
Getenv
(
"ENVIRONMENT"
)
options
:=
session
.
Options
{
Config
:
aws
.
Config
{
Region
:
aws
.
String
(
"af-south-1"
),
},
}
if
isDebug
{
options
.
Profile
=
fmt
.
Sprintf
(
"shiplogic-%s"
,
env
)
}
sess
,
err
:=
session
.
NewSessionWithOptions
(
options
)
if
err
!=
nil
{
return
nil
}
s3Session
=
s3
.
New
(
sess
)
}
return
s3Session
}
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