英文:
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 := &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
}
And I'm using this in my HTML file to download the file with another name:
<a href="Link given by the function" download="the filename I want">Download the file.</a>
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 := &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)
(Note: the usage of SignedHeaderVals
is not required).
Thanks to michael for the correction to my original answer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论