Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 670
Waleed93 Avatar asked Jun 27 '20 17:06

Waleed93


People also ask

How do I get pre-signed URL S3?

Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that contains the object that you want a presigned URL for. In the Objects list, select the object that you want to create a presigned URL for.

How do I access an S3 bucket from AWS SDK?

To access Amazon Simple Storage Service, create an AWS. S3 service object. Call the listBuckets method of the Amazon S3 service object to retrieve a list of your buckets. The data parameter of the callback function has a Buckets property containing an array of maps to represent the buckets.

What is signed S3 URL?

S3 pre-signed URLs are a form of an S3 URL that temporarily grants restricted access to a single S3 object to perform a single operation — either PUT or GET — for a predefined time limit. To break it down: It is secure — the URL is signed using an AWS access key.

How do I create a signed URL?

A signed URL will be produced for each provided URL, authorized for the specified HTTP method and valid for the given duration. The signurl command uses the private key for a service account (the '<private-key-file>' argument) to generate the cryptographic signature for the generated URL.


1 Answers

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);
like image 200
Tri Bui Avatar answered Sep 29 '22 15:09

Tri Bui