AWS S3 get signed URL

● パッケージのインストール

npm install @aws-sdk/client-s3
npm install @aws-sdk/s3-request-presigner

● signed URL を取得する

geturl.js

import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
// ************************
const accessKeyId = 'XXXXX'; 
const secretAccessKey = 'XXXXX';
const region = 'XXXXXXXXXX';
const bucket = 'my-bucket';
const key = 'my-file.png';
// ************************
const client = new S3Client({
  region: region,
  credentials: {
    accessKeyId: accessKeyId,
    secretAccessKey: secretAccessKey,
  },
});

const url = await getPresignedUrl(client, bucket, key, 60*60*8);
console.log( `the url is ${url}` );

async function getPresignedUrl(client, bucket, key, expiresIn) {
  const objectParams = {
    Bucket: bucket,
    Key: key,
  };

  const url = await getSignedUrl(client, new GetObjectCommand(objectParams), { expiresIn });
  return url
};

● package.json で esmoduleを読み込めるようにする

package.json

"type": "module"

● 実行する

node geturl.js
No.2269
01/05 13:34

edit