dongmi1995
2019-02-06 13:55 浏览 63
已采纳

如何使用Golang从公共S3存储桶下载

I'm implementing a function to download a file from an s3 bucket. This worked fine when the bucket was private and I set the credentials

os.Setenv("AWS_ACCESS_KEY_ID", "test")
os.Setenv("AWS_SECRET_ACCESS_KEY", "test")

However, I made the s3 bucket public as described in here and now I want to download it without credentials.

func DownloadFromS3Bucket(bucket, item, path string) {
    file, err := os.Create(filepath.Join(path, item))
    if err != nil {
        fmt.Printf("Error in downloading from file: %v 
", err)
        os.Exit(1)
    }

    defer file.Close()

    sess, _ := session.NewSession(&aws.Config{
        Region: aws.String(constants.AWS_REGION)},
    )

    // Create a downloader with the session and custom options
    downloader := s3manager.NewDownloader(sess, func(d *s3manager.Downloader) {
        d.PartSize = 64 * 1024 * 1024 // 64MB per part
        d.Concurrency = 6
    })

    numBytes, err := downloader.Download(file,
        &s3.GetObjectInput{
            Bucket: aws.String(bucket),
            Key:    aws.String(item),
        })
    if err != nil {
        fmt.Printf("Error in downloading from file: %v 
", err)
        os.Exit(1)
    }

    fmt.Println("Download completed", file.Name(), numBytes, "bytes")
}

But now I'm getting an error.

Error in downloading from file: NoCredentialProviders: no valid providers in chain. Deprecated.
    For verbose messaging see aws.Config.CredentialsChainVerboseErrors

Any idea how to download it without credentials?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答 默认 最新

  • 已采纳
    dqwh1205 dqwh1205 2019-02-06 14:41

    We can set Credentials: credentials.AnonymousCredentials when creating session. Following is the working code.

    func DownloadFromS3Bucket(bucket, item, path string) {
        file, err := os.Create(filepath.Join(path, item))
        if err != nil {
            fmt.Printf("Error in downloading from file: %v 
    ", err)
            os.Exit(1)
        }
    
        defer file.Close()
    
        sess, _ := session.NewSession(&aws.Config{
            Region: aws.String(constants.AWS_REGION), Credentials: credentials.AnonymousCredentials},
        )
    
        // Create a downloader with the session and custom options
        downloader := s3manager.NewDownloader(sess, func(d *s3manager.Downloader) {
            d.PartSize = 64 * 1024 * 1024 // 64MB per part
            d.Concurrency = 6
        })
    
        numBytes, err := downloader.Download(file,
            &s3.GetObjectInput{
                Bucket: aws.String(bucket),
                Key:    aws.String(item),
            })
        if err != nil {
            fmt.Printf("Error in downloading from file: %v 
    ", err)
            os.Exit(1)
        }
    
        fmt.Println("Download completed", file.Name(), numBytes, "bytes")
    }
    
    点赞 评论 复制链接分享

相关推荐