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

如何使用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 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")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料