Getting s3 pre-signed URLS using the Node.js AWS SDK v3

An easy-to-find copy/pastable reference for creating pre-signed URLs for getting of files using the Node.js AWS SDK v3.

Maybe there’s a reference in the docs that provides an easy-to-use documentation of the AWS SDK v3 method for creating pre-signed URLs using Node (or TypeScript). Alas, after 45 minutes of googling, stack overflowing, and other searching I couldn’t find it. Yes, the docs had examples, but they weren’t copy/pastable for lazy/productive coders like myself. Perhaps I need to improve my search skills? I managed to solve it, but that took more effort than I expected.

For what it is worth, I hope the maintainers of the Node.js AWS SDK v3 add a copy/pastable version somewhere more obvious. The official docs will show up in searches before this article.

In any case, I finally dug in and figured it out. So I can find it again in the future, I’m posting it here.

yarn add @aws-sdk/s3-request-presigner
yarn add @aws-sdk/client-s3

I encapsulated my solution in a function in s3FileFetch.js so I could use it accros a project:

import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";

const config = {
  credentials: {
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  region: "us-west-2",

const client = new S3Client(config);

async function getSignedFileUrl(fileName, bucket, expiresIn) {
  const command = new GetObjectCommand({
    Bucket: bucket,
    Key: fileName,

  return await getSignedUrl(client, command, { expiresIn });

export default getSignedFileUrl;
import getFileUrl from "/lib/s3FileFetch";

async function returnThumbnail(thumbnail_key) {
  return await getFileUrl(thumbnail_key, "my_bucket", 3600);

This works but I’m certain it can be improved. If you have any suggestions on how to do this action better, let me know in social media or email!

  • 2022-06-19: Moved configuration and instantiation of s3 object out of the function thanks to a suggestion by Jake Patrick.

Daniel Roy Greenfeld

Source link