Last active
August 29, 2015 14:23
-
-
Save funnythingz/ce0ba4307fdc35446624 to your computer and use it in GitHub Desktop.
AWS SDK for Go を使ってS3にファイルをアップロードする ref: http://qiita.com/funnythingz/items/1f69164df6759cc22a74
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/awsutil" | |
"github.com/aws/aws-sdk-go/aws/credentials" | |
"github.com/aws/aws-sdk-go/service/s3" | |
"log" | |
"os" | |
) | |
func main() { | |
fileTransfer := FileTransferToS3{ | |
AccessKeyId: "ACCESS_KEY_ID", | |
SecretAccessKey: "SECRET_ACCESS_KEY", | |
Region: "us-west-2", | |
BucketName: "bucket-name", | |
} | |
fileTransfer.PutToS3("./", "sample.jpg") | |
} | |
type FileTransferToS3 struct { | |
AccessKeyId string | |
SecretAccessKey string | |
Region string | |
BucketName string | |
} | |
func (f *FileTransferToS3) PutToS3(path string, filename string) { | |
file, err := os.Open(fmt.Sprintf("%s%s", path, filename)) | |
if err != nil { | |
log.Println(err.Error()) | |
} | |
defer file.Close() | |
cli := s3.New(&aws.Config{ | |
Credentials: credentials.NewStaticCredentials(f.AccessKeyId, f.SecretAccessKey, ""), | |
Region: f.Region, | |
}) | |
resp, err := cli.PutObject(&s3.PutObjectInput{ | |
Bucket: aws.String(f.BucketName), | |
Key: aws.String(filename), | |
Body: file, | |
}) | |
if err != nil { | |
log.Println(err.Error()) | |
} | |
log.Println(awsutil.StringValue(resp)) | |
} |
This file contains hidden or 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
{ | |
"AWSTemplateFormatVersion": "2010-09-09", | |
"Parameters": { | |
}, | |
"Resources": { | |
"DemoBucket" : { | |
"Type" : "AWS::S3::Bucket", | |
"Properties" : { | |
"AccessControl" : "PublicReadWrite", | |
"BucketName" : "sample-demo", | |
"Tags": [ | |
{ | |
"Key": "Name", | |
"Value": "demo" | |
}, | |
{ | |
"Key": "Role", | |
"Value": "s3" | |
} | |
] | |
} | |
}, | |
"DemoBucketPolicy" : { | |
"Type" : "AWS::S3::BucketPolicy", | |
"Properties" : { | |
"Bucket" : { "Ref" : "DemoBucket" }, | |
"PolicyDocument" : { | |
"Version": "2008-10-17", | |
"Statement": [ | |
{ | |
"Sid": "PublicReadGetObject", | |
"Effect": "Allow", | |
"Principal": { | |
"AWS": "*" | |
}, | |
"Action": "s3:GetObject", | |
"Resource": "arn:aws:s3:::sample-demo/*" | |
} | |
] | |
} | |
}, | |
"DependsOn": "DemoBucket" | |
} | |
}, | |
"Description": "s3" | |
} |
This file contains hidden or 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
{ | |
"AWSTemplateFormatVersion": "2010-09-09", | |
"Parameters": { | |
}, | |
"Resources": { | |
"DemoBucket" : { | |
"Type" : "AWS::S3::Bucket", | |
"Properties" : { | |
"AccessControl" : "PublicReadWrite", | |
"BucketName" : "sample-demo", | |
"Tags": [ | |
{ | |
"Key": "Name", | |
"Value": "demo" | |
}, | |
{ | |
"Key": "Role", | |
"Value": "s3" | |
} | |
] | |
} | |
}, | |
"DemoBucketPolicy" : { | |
"Type" : "AWS::S3::BucketPolicy", | |
"Properties" : { | |
"Bucket" : { "Ref" : "DemoBucket" }, | |
"PolicyDocument" : { | |
"Version": "2008-10-17", | |
"Statement": [ | |
{ | |
"Sid": "PublicReadGetObject", | |
"Effect": "Allow", | |
"Principal": { | |
"AWS": "*" | |
}, | |
"Action": "s3:GetObject", | |
"Resource": "arn:aws:s3:::sample-demo/*" | |
} | |
] | |
} | |
}, | |
"DependsOn": "DemoBucket" | |
} | |
}, | |
"Description": "s3" | |
} |
This file contains hidden or 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
$ go get github.com/mattn/gom |
This file contains hidden or 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
gom 'github.com/aws/aws-sdk-go/aws' | |
gom 'github.com/aws/aws-sdk-go/service/s3' |
This file contains hidden or 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
$ gom install |
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/awsutil" | |
"github.com/aws/aws-sdk-go/aws/credentials" | |
"github.com/aws/aws-sdk-go/service/s3" | |
"log" | |
"os" | |
) | |
func main() { | |
fileTransfer := FileTransferToS3{ | |
AccessKeyId: "ACCESS_KEY_ID", | |
SecretAccessKey: "SECRET_ACCESS_KEY", | |
Region: "us-west-2", | |
BucketName: "bucket-name", | |
} | |
fileTransfer.PutToS3("./", "sample.jpg") | |
} | |
type FileTransferToS3 struct { | |
AccessKeyId string | |
SecretAccessKey string | |
Region string | |
BucketName string | |
} | |
func (f *FileTransferToS3) PutToS3(path string, filename string) { | |
file, err := os.Open(fmt.Sprintf("%s%s", path, filename)) | |
if err != nil { | |
log.Println(err.Error()) | |
} | |
defer file.Close() | |
cli := s3.New(&aws.Config{ | |
Credentials: credentials.NewStaticCredentials(f.AccessKeyId, f.SecretAccessKey, ""), | |
Region: f.Region, | |
}) | |
resp, err := cli.PutObject(&s3.PutObjectInput{ | |
Bucket: aws.String(f.BucketName), | |
Key: aws.String(filename), | |
Body: file, | |
}) | |
if err != nil { | |
log.Println(err.Error()) | |
} | |
log.Println(awsutil.StringValue(resp)) | |
} |
This file contains hidden or 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
$ gom run app.go |
This file contains hidden or 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
$ go get github.com/mattn/gom |
This file contains hidden or 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
$ gom install |
This file contains hidden or 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
gom 'github.com/aws/aws-sdk-go/aws' | |
gom 'github.com/aws/aws-sdk-go/service/s3' |
This file contains hidden or 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
$ gom run app.go |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment