使用与存储区密钥不同的文件名下载S3文件

Top*_*opo 0 html amazon-s3 go amazon-web-services

我正在尝试更改我从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
}
Run Code Online (Sandbox Code Playgroud)

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

<a href="Link given by the function" download="the filename I want">Download the file.</a>
Run Code Online (Sandbox Code Playgroud)

但我一直得到名为bucket key的文件.如何更改正在下载的文件的名称?

Chr*_*mon 7

根据Amazon GET对象文档,您需要的参数实际上是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)
Run Code Online (Sandbox Code Playgroud)

(注意:SignedHeaderVals不需要使用).

感谢michael对我原来的答案进行了修正.