使用不同于存储桶键的文件名下载 S3 文件。

huangapple go评论91阅读模式
英文:

Download S3 file with a different filename than the bucket key

问题

我正在尝试更改我从S3下载的文件的名称,但它始终将存储桶键作为文件名。

我正在使用以下函数获取用于从S3存储桶下载文件的已签名URL。

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="函数返回的链接" download="the filename I want">Download the file.</a>

但是我一直得到以存储桶键命名的文件。如何更改正在下载的文件的名称?

英文:

I'm trying to change the name of a file I'm downloading from S3, but it keeps getting the bucket key as filename instead.

I'm using this function to get a Signed URL to download things from my S3 bucket.

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

	params := &amp;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(&quot;Content-Disposition&quot;, &quot;filename=the filename I want&quot;)
	str, err := req.Presign(15 * time.Minute)
	if err != nil {
    	global.Log(&quot;[AWS GET LINK]:&quot;, params, err)
	}

    return str, err
}

And I'm using this in my HTML file to download the file with another name:

&lt;a href=&quot;Link given by the function&quot; download=&quot;the filename I want&quot;&gt;Download the file.&lt;/a&gt;

But I keep getting the file named as the bucket key. How can I change the name of the file being downloaded?

答案1

得分: 13

根据Amazon GET Object Docs的说明,你实际上需要的参数是response-content-disposition

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

尝试以下代码:

params := &s3.GetObjectInput{
    Bucket:                      aws.String("存储桶名称"),
    Key:                         aws.String("键值"),
    ResponseContentDisposition: aws.String("attachment; filename=我想要的文件名"),
}

req, _ := svc.GetObjectRequest(params)
str, err := req.Presign(15 * time.Minute)

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

感谢michael对我原始答案的更正。

英文:

According to the Amazon GET Object Docs, the parameter you need is actually response-content-disposition.

According to the GetObjectInput docs, GetObjectInput has a parameter to set the ResponseContentDisposition value.

Try:

params := &amp;s3.GetObjectInput{
    Bucket: aws.String(a bucket name),
    Key:    aws.String(key),
    ResponseContentDisposition: &quot;attachment; filename=the filename I want&quot;,
}

req, _ := svc.GetObjectRequest(params)
str, err := req.Presign(15 * time.Minute)

(Note: the usage of SignedHeaderVals is not required).

Thanks to michael for the correction to my original answer.

huangapple
  • 本文由 发表于 2016年11月5日 11:43:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/40434525.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定