Home > Blockchain >  AWS SDK: Pre-signed URL for multiple file uploads
AWS SDK: Pre-signed URL for multiple file uploads

Time:12-11

Is it possible to generate Pre-signed URL for multiple file uploads with AWS SDK?

I am using JavaScript SDK and the code below is working for a single file upload.

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

const client = new S3Client({ region: 'ap-northeast-1' })
const command = new PutObjectCommand({
  Bucket: 'BUCKET_NAME',
  Key: `public/1/photos/124.jpg`,
})
const signedUrl = await getSignedUrl(client, command, { expiresIn: 3600 })

CodePudding user response:

Iterate over multiple file inputs:

  const urls = await Promise.all(
    ["image1.jpg", "image2.jpg", "image3.jpg"].map((key) => {
      const command = new PutObjectCommand({
        Bucket: "BUCKET_NAME",
        Key: key,
      });

      return getSignedUrl(client, command, { expiresIn: 3600 });
    })
  );

A pre-signed URL is tied to a single file/bucket/method combination. The JS SDK v3 getSignedUrl method returns a single URL (Promise<string>). So iteration it is!

  • Related