如何使用Golang检查S3对象大小 [英] How to check s3 object size using golang
本文介绍了如何使用Golang检查S3对象大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我实现了一个从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
", 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")
}
推荐答案
您可以使用HeadObject,它包含header Content-Length
。
Amazon简单存储服务的HeadObject API操作。
HEAD操作从对象检索元数据,而不返回 对象本身。如果您只对此感兴趣,则此操作非常有用 在对象的元数据中。若要使用HEAD,您必须对 对象。
这篇关于如何使用Golang检查S3对象大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文