【golang】aws s3 go sdk

前端之家收集整理的这篇文章主要介绍了【golang】aws s3 go sdk前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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相关文章