dqtl46964 2019-03-28 05:47
浏览 172
已采纳

如何使用Golang检查s3对象大小

I have implemented a function to download an object from AWS S3 bucket. This works fine. But I need to display a download progress bar. For this I need to know the size of the object beforehand according to here . Does anyone know how to get the object size?

Here is my code.

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")
}
  • 写回答

1条回答 默认 最新

  • dongqianwei6664 2019-03-28 05:56
    关注

    You can make use of the HeadObject, which contains the 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?