I am writing a go function to download a file from AWS S3 bucket.
func DownloadFromS3Bucket() {
bucket := "cellery-runtime-installation"
item := "hello-world.txt"
file, err := os.Create(item)
if err != nil {
fmt.Println(err)
}
defer file.Close()
// Initialize a session in us-west-2 that the SDK will use to load
// credentials from the shared credentials file ~/.aws/credentials.
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")
}
However, I am getting an error message asking for credentials.
NoCredentialProviders: no valid providers in chain. Deprecated. For verbose messaging see aws.Config.CredentialsChainVerboseErrors
The documentation does not specifically say how to set the credentials. (Access key ID,Secret access key)
Any idea?
There are several ways to set credentials. For more details aws/credentials.
For example, you can specify it by setting environment variables:
AWS_ACCESS_KEY = <your_access_key>
AWS_SECRET_KEY = <your_secret_key>
Then just use credentials.NewEnvCredentials()
in your config instance:
sess, _ := session.NewSession(&aws.Config{
Region: aws.String("us-east-1"),
Credentials: credentials.NewEnvCredentials(),
})