【问题标题】:How to check s3 object size using golang如何使用 golang 检查 s3 对象大小
【发布时间】:2019-08-18 19:54:04
【问题描述】:

我已经实现了从 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")
}

【问题讨论】:

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


    【解决方案1】:

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

    Amazon Simple Storage Service 的 HeadObject API 操作。

    HEAD 操作从对象中检索元数据而不返回 对象本身。如果您只感兴趣,此操作很有用 在对象的元数据中。要使用 HEAD,您必须对 对象。

    【讨论】:

    • 你能举个例子吗?我找不到它在 go 中实现的任何地方。
    • @MadhukaWickramapala 有例子,看链接,尝试请求并打印响应
    【解决方案2】:

    这应该可行:

    headObj := s3.HeadObjectInput{
        Bucket: aws.String(bucket),
        Key: aws.String(key),
    }
    result, err := S3.HeadObject(&headObj)
    if err != nil {
      // handle error
    }
    return aws.Int64Value(result.ContentLength)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-27
      • 1970-01-01
      • 2017-03-09
      • 2021-11-14
      • 2022-01-23
      • 2010-10-20
      • 1970-01-01
      相关资源
      最近更新 更多