aws s3 go sdk 使用
引入包
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3"
创建 session
func newSession() (*session.Session,error){ creds := credentials.NewStaticCredentials(ak,sk,"") config := &aws.Config{ Region: aws.String(region), Endpoint: &endpoint, S3ForcePathStyle: aws.Bool(true), Credentials: creds, DisableSSL: &disableSSL, } return session.NewSession(config) }
创建 bucket
sess,err := newSession() if err != nil { fmt.Println("Failed to create session,",err) return } svc := s3.New(sess) params := &s3.CreateBucketInput{ Bucket: aws.String(bucket),} _,err = svc.CreateBucket(params)
列出所有 bucket
sess,err) return } svc := s3.New(sess) resp,err := svc.ListBuckets(&s3.ListBucketsInput{})
上传 object
sess,err) return } svc := s3.New(sess) params := &s3.PutObjectInput{ Bucket: aws.String(bucket),// required Key: aws.String(object),// required Body: bytes.NewReader([]byte(content)),err = svc.PutObject(params)
列出一个 bucket 的所有 object
sess,err) return } svc := s3.New(sess) params := &s3.ListObjectsInput{ Bucket: aws.String(bucket),// required } resp,err := svc.ListObjects(params)原文链接:/go/188116.html