npm install @aws-sdk/client-s3
npm install @aws-sdk/s3-request-presigner
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
"type": "module"
node geturl.js