amazon-web-servicesamazon-s3aws-sdk-jsaws-sdk-nodejs

How to Get Signed S3 Url in AWS-SDK JS Version 3?


I am following the proposed solution by Trivikr for adding support for s3.getSignedUrl api which is not currently available in newer v3. I am trying to make a signed url for getting an object from bucket.

Just for convenience, the code is being added below:

const { S3, GetObjectCommand } = require("@aws-sdk/client-s3"); // 1.0.0-gamma.2 version
const { S3RequestPresigner } = require("@aws-sdk/s3-request-presigner"); // 0.1.0-preview.2 version
const { createRequest } = require("@aws-sdk/util-create-request"); // 0.1.0-preview.2 version
const { formatUrl } = require("@aws-sdk/util-format-url"); // 0.1.0-preview.1 //version
const fetch = require("node-fetch");

(async () => {
 try {

  const region = "us-east-1";
  const Bucket = `SOME_BUCKET_NAME`;
  const Key = `SOME_KEY_VALUE`;
  const credentials = {
      accessKeyId: ACCESS_KEY_HERE,
      secretAccessKey: SECRET_KEY_HERE,
      sessionToken: SESSION_TOKEN_HERE
  };

  const S3Client = new S3({ region, credentials, signatureVersion: 'v4' });

  console.log('1'); // for quick debugging

  const signer = new S3RequestPresigner({ ...S3Client.config });

  console.log('2') 

  const request = await createRequest(
      S3Client,
      new GetObjectCommand({ Key, Bucket })
  );

  console.log('3');

  let signedUrl = formatUrl(await signer.presign(request));

  console.log(signedUrl);
  
  let response = await fetch(signedUrl);
  console.log("Response", response);

 }catch(e) {
    console.error(e);
 }

I successfully create S3Client and signer but on creating request, I get the following error:

clientStack.concat(...).filter is not a function

Anything wrong I am doing?

Please also note that I am using webpack for bundling


Solution

  • RESOLVED

    I ended up successfully making the signed urls by installing the beta versions rather than preview (default) ones