amazon-web-services - 如何使用 golang 检查 s3 对象大小
问题描述
我已经实现了一个从 AWS S3 存储桶下载对象的功能。这工作正常。但我需要显示一个下载进度条。为此,我需要根据这里事先知道对象的大小 。有谁知道如何获取对象大小?
这是我的代码。
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。Content-Length
Amazon Simple Storage Service 的 HeadObject API 操作。
HEAD 操作从对象中检索元数据,而不返回对象本身。如果您只对对象的元数据感兴趣,此操作很有用。要使用 HEAD,您必须对对象具有 READ 访问权限。
推荐阅读
- r - ARIMAX 预测问题 (auto.arima)
- java - 使用不同的值多次调用方法
- javascript - Google Analytics - 访问每个站点时显示的位置访问提示
- r - 需要通过排除字符来取最大值
- php - 如何在一个范围内找到值 - laravel
- javascript - React Native 0.60 社交登录问题
- javascript - Groovy:在静态范围内找到表观变量“classVar”,但不引用局部变量、静态字段或类
- sql-server - 当使用特定值更新多个列时,在 VIEW 上使用触发器执行存储过程
- java - 动态杰克逊字段过滤
- terminal - 使用 yq 编辑数组中的 yaml 对象。加速Terminalizer的终端演员(记录)