Amazon web services 如何使用golang检查s3对象大小

Amazon web services 如何使用golang检查s3对象大小,amazon-web-services,go,amazon-s3,aws-sdk,Amazon Web Services,Go,Amazon S3,Aws Sdk,我已经实现了一个从AWS S3 bucket下载对象的功能。这个很好用。但我需要显示一个下载进度条。为此,我需要事先知道对象的大小,根据 . 有人知道如何获取对象大小吗 这是我的密码 func DownloadFromS3Bucket(bucket, item, path string) { file, err := os.Create(filepath.Join(path, item)) if err != nil { fmt.Printf("Error in

我已经实现了一个从AWS S3 bucket下载对象的功能。这个很好用。但我需要显示一个下载进度条。为此,我需要事先知道对象的大小,根据 . 有人知道如何获取对象大小吗

这是我的密码

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 \n", 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 \n", err)
        os.Exit(1)
    }

    fmt.Println("Download completed", file.Name(), numBytes, "bytes")
}
您可以使用,其中包含

Amazon简单存储服务的HeadObject API操作

HEAD操作从对象检索元数据而不返回 对象本身。如果您只感兴趣,此操作非常有用 在对象的元数据中。要使用HEAD,您必须具有对 反对

这应该起作用:

headObj := s3.HeadObjectInput{
    Bucket: aws.String(bucket),
    Key: aws.String(key),
}
result, err := S3.HeadObject(&headObj)
if err != nil {
  // handle error
}
return aws.Int64Value(result.ContentLength)

你能举一个使用围棋的例子吗。我找不到它在go中实现的任何地方。@MadhukaWickramapala有一个例子,请查看链接,尝试请求并打印响应