2 回答
TA贡献2036条经验 获得超8个赞
有几种方法可以设置凭据。
例如,您可以通过设置环境变量来指定它:
AWS_ACCESS_KEY = <your_access_key> AWS_SECRET_KEY = <your_secret_key>
credentials.NewEnvCredentials()
然后只需在您的配置实例中使用:
sess, _ := session.NewSession(&aws.Config{ Region: aws.String("us-east-1"), Credentials: credentials.NewEnvCredentials(), })
TA贡献1871条经验 获得超8个赞
只需设置AWS_ACCESS_KEY和AWS_SECRET_KEY环境变量(您不需要调用credentials.NewEnvCredentials()-session.NewSession您所做的是完美的):
func DownloadFromS3Bucket() {
os.Setenv("AWS_ACCESS_KEY","my-key")
os.Setenv("AWS_SECRET_KEY","my-secret")
bucket := "cellery-runtime-installation"
item := "hello-world.txt"
file, err := os.Create(item)
if err != nil {
fmt.Println(err)
}
defer file.Close()
sess, _ := session.NewSession(&aws.Config{Region: aws.String("us-east-1")})
downloader := s3manager.NewDownloader(sess)
numBytes, err := downloader.Download(file,
&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(item),
})
if err != nil {
fmt.Println(err)
}
fmt.Println("Downloaded", file.Name(), numBytes, "bytes")
}
- 2 回答
- 0 关注
- 143 浏览
添加回答
举报