Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added s3-upload #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Upload a single file to 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
```
52 changes: 52 additions & 0 deletions s3/upload/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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").Default("private").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()
defer (*file).Close()

session, conf := common.OpenSession(flags)

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

var parsedMetadata map[string]*string

if metadata != nil && len(*metadata) != 0 {
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)

graphman65 marked this conversation as resolved.
Show resolved Hide resolved
common.FatalOnError(err)

fmt.Println(res.Location)
}