Skip to content

Instantly share code, notes, and snippets.

@erayalakese
Last active August 29, 2022 00:23
Show Gist options
  • Save erayalakese/801a28d6bf8e34a84f60f9424656ec23 to your computer and use it in GitHub Desktop.
Save erayalakese/801a28d6bf8e34a84f60f9424656ec23 to your computer and use it in GitHub Desktop.
Get Signed URL of an AWS S3 Bucket Object
const { getSignedUrl } = require("@aws-sdk/s3-request-presigner");
const { S3Client, GetObjectCommand } = require("@aws-sdk/client-s3");
const client = new S3Client({ region: "eu-central-1" });
(async () => {
// Hardcoding our user for simplicity
const user = {id:1, subscribed: true}
// Preparing our GetObject command
const command = new GetObjectCommand({
Bucket: "member-only-videos",
Key: "mixkit-going-down-a-curved-highway-through-a-mountain-range-41576.mp4"
})
if (user.subscribed) {
// This user is authorized to see this resource
// get signed url
const url = await getSignedUrl(client, command, { expiresIn: 3600 })
console.log({
access: true,
url
})
} else {
console.log({
access: false,
url: ''
})
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment