You are viewing the preview version of this book
Click here for the full version.

Setting the filename

When a file is displayed as an image on a page or as video playing in a HTML5 <video> element the filename is not important. It can be a long string of random characters as the user does not see it.

But when you sign a URL that is meant to be downloaded, such as an Ebook or other downloadable content, the Save as dialog will present the object key as the default. Since this is directly observable by the users, it's important to provide a user-friendly value. Instead of f1945ed5-2d79-4e6e-b53e-bd9a2a1c25b6.pdf, it's better to use my-great-ebook.pdf.

There are three ways to determine the filename when a signed URL is downloaded.

How the filename is evaluated

The first is the Key of the object. Whatever you defined when you created the object will be the filename if nothing overwrites it.

The second one is the Content Disposition metadata that is attached to the object, most likely when the file was uploaded. For example, with Terraform, the object will be downloaded as ebook.pdf, no matter what the actual key is:

resource "aws_s3_bucket_object" "object" {
  # ...
  content_disposition =
    "attachment; filename=\"ebook.pdf\""

And finally, a new filename can be forced when the URL is signed. This can be done with the ResponseContentDisposition parameter:

const signedUrl = await getSignedUrl(
  new GetObjectCommand({
    Bucket: process.env.FILES_BUCKET,
    Key: productToDownload.file,
      "attachment; filename=\"ebook.pdf\"",
How to set the filename

Use the ResponseContentDisposition parameter to set the filename. Do not rely on the Key of the object.