【问题标题】:Download S3 file with a different filename than the bucket key下载文件名与存储桶密钥不同的 S3 文件
【发布时间】:2016-11-05 03:43:25
【问题描述】:

我正在尝试更改从 S3 下载的文件的名称,但它一直将存储桶密钥作为文件名。

我正在使用此函数获取签名 URL,以便从我的 S3 存储桶中下载内容。

func GetFileLink(url, filename string) (string, error) {
    svc := s3.New(some params)

    params := &s3.GetObjectInput{
        Bucket: aws.String(a bucket name),
        Key:    aws.String(key),
    }

    req, _ := svc.GetObjectRequest(params)
    req.SignedHeaderVals = make(map[string][]string)
    req.SignedHeaderVals.Add("Content-Disposition", "filename=the filename I want")
    str, err := req.Presign(15 * time.Minute)
    if err != nil {
        global.Log("[AWS GET LINK]:", params, err)
    }

    return str, err
}

我在我的 HTML 文件中使用它来下载另一个名称的文件:

<a href="Link given by the function" download="the filename I want">Download the file.</a>

但我不断得到名为存储桶密钥的文件。如何更改正在下载的文件的名称?

【问题讨论】:

    标签: html amazon-web-services go amazon-s3


    【解决方案1】:

    根据Amazon GET Object Docs,你需要的参数其实是response-content-disposition

    根据GetObjectInput 文档,GetObjectInput 有一个参数来设置ResponseContentDisposition 的值。

    试试:

    params := &s3.GetObjectInput{
        Bucket: aws.String(a bucket name),
        Key:    aws.String(key),
        ResponseContentDisposition: "attachment; filename=the filename I want",
    }
    
    req, _ := svc.GetObjectRequest(params)
    str, err := req.Presign(15 * time.Minute)
    

    (注意:SignedHeaderVals 的使用不是必须的)。

    感谢 michael 更正我原来的答案。

    【讨论】:

    • 当我意识到req.SignedHeaderVals.Add 不是正确的解决方案时,我删除了评论。您正在添加与标头相关的内容——不是查询字符串参数——这就是response-content-disposition 的含义。 ResponseContentDisposition 需要作为参数传递给GetObjectInput。请随意获取此信息并相应地修改您的答案。
    • ...假设您同意。 GetObjectInput 的文档说这“设置了响应的标题”,这可能具有误导性。 S3 覆盖此请求的响应中的标头的请求会更精确,如果它看起来像它那样工作的话。
    猜你喜欢
    • 1970-01-01
    • 2015-05-20
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 2017-02-16
    • 2013-10-15
    • 2019-09-06
    相关资源
    最近更新 更多