我已经实现了一个从 AWS S3 存储桶下载对象的功能。这很好用。但我需要显示下载进度条。为此,我需要根据以下公式事先知道对象的大小here https://github.com/cheggaaa/pb。有谁知道如何获取对象的大小?
这是我的代码。
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")
}
您可以使用头部对象 https://docs.aws.amazon.com/sdk-for-go/api/service/s3/#S3.HeadObject,其中包含header Content-Length https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectHEAD.html#RESTObjectHEAD-responses-examples.
Amazon Simple Storage Service 的 HeadObject API 操作。
HEAD 操作从对象中检索元数据而不返回
物体本身。如果您只感兴趣,此操作很有用
在对象的元数据中。要使用 HEAD,您必须具有 READ 访问权限
目的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)