Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download from AWS S3 using golang

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?

like image 267
Madhuka Wickramapala Avatar asked Jan 28 '19 09:01

Madhuka Wickramapala


People also ask

How do I download files from S3?

You can download an object from an S3 bucket in any of the following ways: Select the object and choose Download or choose Download as from the Actions menu if you want to download the object to a specific folder. If you want to download a specific version of the object, select the Show versions button.

How do I download from S3 bucket to local using command line?

You can use cp to copy the files from an s3 bucket to your local system. Use the following command: $ aws s3 cp s3://bucket/folder/file.txt .

Is Golang used in AWS?

The primary way you can import packages for AWS in Golang is by pointing directly to the GitHub repository where the package(s) exist. To start adding Golang code, you need a place to save the code.


Video Answer


2 Answers

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(),
})
like image 138
bayrinat Avatar answered Oct 23 '22 17:10

bayrinat


Just set AWS_ACCESS_KEY and AWS_SECRET_KEY environment variables (you do not need to call credentials.NewEnvCredentials() as part of session.NewSession - what you did is perfect):

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")
}

This is the complete example from AWS Go SDK: https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/go/example_code/s3/s3_download_object.go

I wrote a blog post on how to test the S3 downloader, and created a testable S3 bucket scanner, which list all bucket's files and temporary download each file, see: https://medium.com/@tufin/a-testable-go-aws-s3-scanner-e54de0c26197

Here you can find the code: https://github.com/Tufin/blog/tree/master/s3-scanner

like image 23
Effi Bar-She'an Avatar answered Oct 23 '22 17:10

Effi Bar-She'an