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

标签 amazon-web-services go amazon-s3 aws-sdk

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

这是我的代码。

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")
}

最佳答案

您可以使用 HeadObject ,其中包含 header Content-Length .

HeadObject API operation for Amazon Simple Storage Service.

The HEAD operation retrieves metadata from an object without returning the object itself. This operation is useful if you're only interested in an object's metadata. To use HEAD, you must have READ access to the object.

关于amazon-web-services - 如何使用 golang 检查 s3 对象大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55390909/

相关文章:

heroku - 什么是单个 Heroku Web Dyno?

go - 无法从其他项目导入本地项目 : cannot find module providing package <package_name>

loops - 一次最多批处理 10 个输入

haskell - 如何使用 Amazonka 获取签名的 put 对象 url

python - 如何将 reducer 的结果打印到单个文件中

c# - 将 WCF 与负载平衡 (AWS) 结合使用时,安全上下文 token 无效

amazon-web-services - SNS批量发布

algorithm - 在 Go 中将 map 转换为树

c++ - 无法通过查询字符串身份验证访问 Amazon s3 数据

amazon-web-services - 为什么我的终端返回这个 s3 错误?