74 lines
2.6 KiB
JavaScript
74 lines
2.6 KiB
JavaScript
var aws = require("aws-sdk");
|
|
require("dotenv").config();
|
|
// Configure aws with your accessKeyId and your secretAccessKey
|
|
aws.config.update({
|
|
region: "ca-central-1", // Put your aws region here
|
|
accessKeyId: process.env.AWSAccessKeyId,
|
|
secretAccessKey: process.env.AWSSecretKey
|
|
});
|
|
|
|
const S3_BUCKET = process.env.bucket;
|
|
// Now lets export this function so we can call it from somewhere else
|
|
|
|
exports.sign_s3 = (req, res) => {
|
|
console.log("Get S3 Signed Put for bucket: " + S3_BUCKET);
|
|
const s3 = new aws.S3(); // Create a new instance of S3
|
|
// if (!req.body.fileName || req.body.fileType) {
|
|
// console.log('req.body.fileName', req.body.fileName)
|
|
// console.log('req.body.fileType', req.body.fileType)
|
|
// res.json({ success: false, error: "fileName or fileType missing." });
|
|
// console.log("Error: fileName or fileType missing. ");
|
|
// return;
|
|
// }
|
|
const fileName = req.body.fileName;
|
|
const fileType = req.body.fileType;
|
|
// Set up the payload of what we are sending to the S3 api
|
|
const s3Params = {
|
|
Bucket: S3_BUCKET,
|
|
Key: fileName,
|
|
Expires: 500,
|
|
ContentType: fileType,
|
|
ACL: "public-read"
|
|
};
|
|
// Make a request to the S3 API to get a signed URL which we can use to upload our file
|
|
s3.getSignedUrl("putObject", s3Params, (err, data) => {
|
|
if (err) {
|
|
console.log(err);
|
|
res.json({ success: false, error: err });
|
|
}
|
|
// Data payload of what we are sending back, the url of the signedRequest and a URL where we can access the content after its saved.
|
|
const returnData = {
|
|
signedRequest: data,
|
|
url: `https://${S3_BUCKET}.s3.amazonaws.com/${fileName}`
|
|
};
|
|
// Send it all back
|
|
res.json({ success: true, data: { returnData } });
|
|
});
|
|
};
|
|
|
|
exports.get_s3 = (req, res) => {
|
|
const s3 = new aws.S3(); // Create a new instance of S3
|
|
const fileName = req.body.fileName;
|
|
//const fileType = req.body.fileType;
|
|
// Set up the payload of what we are sending to the S3 api
|
|
const s3Params = {
|
|
Bucket: S3_BUCKET,
|
|
Key: fileName,
|
|
Expires: 500
|
|
};
|
|
// Make a request to the S3 API to get a signed URL which we can use to upload our file
|
|
s3.getSignedUrl("getObject", s3Params, (err, data) => {
|
|
if (err) {
|
|
console.log(err);
|
|
res.json({ success: false, error: err });
|
|
}
|
|
// Data payload of what we are sending back, the url of the signedRequest and a URL where we can access the content after its saved.
|
|
const returnData = {
|
|
signedRequest: data,
|
|
url: `https://${S3_BUCKET}.s3.amazonaws.com/${fileName}`
|
|
};
|
|
// Send it all back
|
|
res.json({ success: true, data: { returnData } });
|
|
});
|
|
};
|