21

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

Jonas V
  • 668
  • 2
  • 5
  • 20
Waleed93
  • 1,130
  • 2
  • 15
  • 24

2 Answers2

49

Just add my example in TypeScript:

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


const s3Configuration: S3ClientConfig = {
    credentials: {
        accessKeyId: '<ACCESS_KEY_ID>',
        secretAccessKey: '<SECRET_ACCESS_KEY>'
    },
    region: '<REGION>',
};
const s3 = new S3Client(s3Configuration);
const command = new GetObjectCommand({Bucket: '<BUCKET>', Key: '<KEY>' });
const url = await getSignedUrl(s3, command, { expiresIn: 15 * 60 }); // expires in seconds
console.log('Presigned URL: ', url);
Jonas V
  • 668
  • 2
  • 5
  • 20
Tri Bui
  • 1,266
  • 1
  • 11
  • 10
  • 5
    Thanks very much, Tri. Where the heck is this documented? – JJJSchmidt Dec 24 '20 at 02:57
  • It's a new modular aws-sdk officially released few days back. During my time of writing this question, it was in testing phase. You can view info on github page – Waleed93 Dec 25 '20 at 20:57
  • 2
    Much obliged, @Waleed93. For others [here is a link](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_s3_request_presigner.html) to the documentation. – JJJSchmidt Dec 30 '20 at 18:13
  • 1
    Thanks, I could not find in their docs what were the params for GetObjectCommand – gdaniel Aug 25 '21 at 14:27
-3

RESOLVED

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

Waleed93
  • 1,130
  • 2
  • 15
  • 24