Last active
April 4, 2019 09:33
-
-
Save soundkitchen/4ad609ba3b23b3b2b02bc1205ebd10ff to your computer and use it in GitHub Desktop.
aws-sdk-go で s3 をさわってみるメモ
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
$ go build ./download.go | |
$ ./download | |
spam! spam! spam! |
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
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/s3" | |
) | |
var sess *session.Session = session.Must(session.NewSession()) | |
var config *aws.Config = &aws.Config{ | |
Region: aws.String("ap-northeast-1"), | |
} | |
var s3Client *s3.S3 = s3.New(sess, config) | |
func main() { | |
input := &s3.GetObjectInput{ | |
Bucket: aws.String("example.com"), | |
Key: aws.String("spam.txt"), | |
} | |
res, err := s3Client.GetObject(input) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer res.Body.Close() | |
body, err := ioutil.ReadAll(res.Body) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(string(body)) | |
} |
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
$ mkdir spam | |
$ cd spam | |
$ export AWS_ACCESS_KEY_ID='my id here' | |
$ export AWS_SECRET_ACCESS_KEY='my secret here' | |
$ export GOPATH=`pwd` | |
$ go get github.com/aws/aws-sdk-go | |
$ ls | |
pkg src |
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
$ go build ./upload.go | |
$ ./upload | |
{ | |
ETag: "..." | |
} |
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
package main | |
import ( | |
"bytes" | |
"fmt" | |
"log" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/s3" | |
) | |
var sess *session.Session = session.Must(session.NewSession()) | |
var config *aws.Config = &aws.Config{ | |
Region: aws.String("ap-northeast-1"), | |
} | |
var s3Client *s3.S3 = s3.New(sess, config) | |
func main() { | |
input := &s3.PutObjectInput{ | |
Bucket: aws.String("example.com"), | |
Key: aws.String("spam.txt"), | |
Body: bytes.NewReader([]byte("spam! spam! spam!")), | |
ContentType: aws.String("text/plain"), | |
CacheControl: aws.String("max-age=86400"), | |
} | |
res, err := s3Client.PutObject(input) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(res) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment