Skip to content

Commit

Permalink
Added s3-upload
Browse files Browse the repository at this point in the history
  • Loading branch information
graphman65 committed Mar 11, 2020
1 parent 7d3f664 commit c1b310a
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,14 @@ builds:
- amd64
ldflags:
- -s -w -X github.com/hamstah/awstools/common.Version={{.Version}} -X github.com/hamstah/awstools/common.CommitHash={{.ShortCommit}}"
- env:
- CGO_ENABLED=0
main: ./s3/upload/
binary: s3-upload
goos:
- linux
- darwin
goarch:
- amd64
ldflags:
- -s -w -X github.com/hamstah/awstools/common.Version={{.Version}} -X github.com/hamstah/awstools/common.CommitHash={{.ShortCommit}}"
34 changes: 34 additions & 0 deletions s3/upload/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# s3-upload

Download a single file from S3.

```
usage: s3-upload --bucket=BUCKET --key=KEY --file=FILE [<flags>]
Upload a file to S3.
Flags:
--help Show context-sensitive help (also try --help-long and --help-man).
--bucket=BUCKET Name of the bucket
--key=KEY Key of the uploaded file
--file=FILE File to upload
--acl=ACL ACL of the uploaded file
--metadata=METADATA Metadata of the uploaded file
--assume-role-arn=ASSUME-ROLE-ARN
Role to assume
--assume-role-external-id=ASSUME-ROLE-EXTERNAL-ID
External ID of the role to assume
--assume-role-session-name=ASSUME-ROLE-SESSION-NAME
Role session name
--assume-role-policy=ASSUME-ROLE-POLICY
IAM policy to use when assuming the role
--region=REGION AWS Region
--mfa-serial-number=MFA-SERIAL-NUMBER
MFA Serial Number
--mfa-token-code=MFA-TOKEN-CODE
MFA Token Code
--session-duration=1h Session Duration
-v, --version Display the version
--log-level=warn Log level
--log-format=text Log format
```
56 changes: 56 additions & 0 deletions s3/upload/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"encoding/json"
"fmt"

"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/hamstah/awstools/common"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)

var (
bucket = kingpin.Flag("bucket", "Name of the bucket").Required().String()
key = kingpin.Flag("key", "Key of the uploaded file").Required().String()
file = kingpin.Flag("file", "File to upload").Required().File()
acl = kingpin.Flag("acl", "ACL of the uploaded file").String()
metadata = kingpin.Flag("metadata", "Metadata of the uploaded file (json)").String()
)

func main() {
kingpin.CommandLine.Name = "s3-upload"
kingpin.CommandLine.Help = "Upload a file to S3."
flags := common.HandleFlags()

session, conf := common.OpenSession(flags)

s3Client := s3.New(session, conf)
uploader := s3manager.NewUploaderWithClient(s3Client)

defer (*file).Close()

var parsedMetadata map[string]*string

if metadata != nil {
err := json.Unmarshal([]byte(*metadata), &parsedMetadata)
common.FatalOnErrorW(err, "Invalid metadata")
}

uploadInput := &s3manager.UploadInput{
Bucket: bucket,
Key: key,
Body: *file,
ACL: acl,
Metadata: parsedMetadata,
}

res, err := uploader.Upload(uploadInput)

if err != nil {
(*file).Close()
common.Fatalln(err.Error())
}

fmt.Println(res.Location)
}

0 comments on commit c1b310a

Please sign in to comment.