-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d3f664
commit c1b310a
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |