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

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line