Topo Topo - 1 month ago 11
HTML Question

Download S3 file with a different filename than the bucket key

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?

Answer

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.