Skip to content

Instantly share code, notes, and snippets.

@ruanbekker
Forked from CarterTsai/go
Created April 25, 2019 12:25
Show Gist options
  • Save ruanbekker/a7fcc96d010386258118d8b1efa6d599 to your computer and use it in GitHub Desktop.
Save ruanbekker/a7fcc96d010386258118d8b1efa6d599 to your computer and use it in GitHub Desktop.
golang s3 PutObject
package main
import (
"bytes"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
func main() {
bucket := aws.String("xxxxxxxxxxxxxx")
keyname := aws.String("test123.txt")
sess := session.Must(session.NewSession())
svc := s3.New(sess, &aws.Config{Region: aws.String("ap-northeast-1")})
params := &s3.PutObjectInput{
Bucket: bucket, // Required
Key: keyname, // Required
ACL: aws.String("bucket-owner-full-control"),
Body: bytes.NewReader([]byte("PAYLOAD")),
// CacheControl: aws.String("CacheControl"),
// ContentDisposition: aws.String("ContentDisposition"),
// ContentEncoding: aws.String("ContentEncoding"),
// ContentLanguage: aws.String("ContentLanguage"),
ContentLength: aws.Int64(7),
// ContentType: aws.String("ContentType"),
// Expires: aws.Time(time.Now()),
// GrantFullControl: aws.String("GrantFullControl"),
// GrantRead: aws.String("GrantRead"),
// GrantReadACP: aws.String("GrantReadACP"),
// GrantWriteACP: aws.String("GrantWriteACP"),
// RequestPayer: aws.String("RequestPayer"),
// SSECustomerAlgorithm: aws.String("SSECustomerAlgorithm"),
// SSECustomerKey: aws.String("SSECustomerKey"),
// SSECustomerKeyMD5: aws.String("SSECustomerKeyMD5"),
// SSEKMSKeyId: aws.String("SSEKMSKeyId"),
}
resp, err := svc.PutObject(params)
if err != nil {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
return
}
fmt.Println(resp)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment