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

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

报告相同问题?

悬赏问题

  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动
  • ¥15 大一C语言期末考试,求帮助🙏🙏
  • ¥15 ch340驱动未分配COM
  • ¥15 Converting circular structure to JSON
  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader