Last active
September 7, 2023 07:49
-
-
Save alsoamit/1e9e34dad3b0b4959955e302a4d234a3 to your computer and use it in GitHub Desktop.
Generate presigned url s3 nodejs 16.x (aws lambda & express js controller)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## This code example is optimized for Node 16.x runtime. | |
## Don't forget to add AWS_REGION and BUCKET_NAME as env variables in Lambda Configuration. | |
## Give the Lambda required permission to edit the Bucket. | |
const AWS = require('aws-sdk'); | |
AWS.config.update({ region: process.env.AWS_REGION }); | |
const s3 = new AWS.S3({ | |
signatureVersion: "v4", | |
}); | |
// config | |
const EXPIRATION_DURATION=3000; | |
const BUCKET=process.env.BUCKET_NAME; | |
// handler | |
exports.handler = async (event) => { | |
const {filename} = event.body; | |
let url = `Data/images/${filename}`; | |
const s3Params = { | |
Bucket: BUCKET, | |
Key: url, | |
Expires: EXPIRATION_DURATION, | |
ContentType: 'multipart/form-data', | |
}; | |
const uploadURL = await s3.getSignedUrlPromise('putObject', s3Params); | |
const response = { | |
url: uploadURL | |
}; | |
return response; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Generate presigned url s3 for Node 16.x (express js controller) | |
## Your aws access keys should have s3 putItem permission allowed for it to work | |
const AWS = require("aws-sdk"); | |
// store these configs in a separate file | |
const awsConfig = { | |
region: process.env.AWS_REGION, | |
s3: { | |
endpoint: process.env.S3_BUCKET_ENDPOINT, | |
region: process.env.AWS_REGION, | |
accessKeyId: process.env.ACCESS_KEY, | |
secretAccessKey: process.env.SECRET_ACCESS_KEY, | |
bucket: process.env.S3_BUCKET_NAME, | |
signatureVersion: "v4", // important (if not set, you'll receive signature unmatched error) | |
}, | |
}; | |
// s3 config | |
const s3 = new AWS.S3({ | |
endpoint: awsConfig.s3.endpoint, | |
accessKeyId: awsConfig.s3.accessKeyId, | |
secretAccessKey: awsConfig.s3.secretAccessKey, | |
Bucket: awsConfig.s3.bucket, | |
signatureVersion: awsConfig.s3.signatureVersion, | |
region: awsConfig.s3.region, | |
}); | |
class UploadController { | |
async getPresignedUrl(req, res) { | |
const { filename, type, productId } = req.body; | |
console.log(type); | |
let url = `temp/${filename}`; | |
if (productId) { | |
url = `products/${productId}/${filename}`; | |
} | |
const s3Params = { | |
Bucket: awsConfig.s3.bucket, | |
Key: url, | |
Expires: 3000, | |
ContentType: "multipart/form-data", | |
}; | |
const uploadURL = await s3.getSignedUrlPromise("putObject", s3Params); | |
return res.status(200).json({status: 1, msg: "success", data: {url: uploadURL}); | |
} | |
} | |
export default new UploadController(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment