By nury5alamo
await s3
.upload({
Bucket: paths({ id: req.user._id, ct: fields?.ct }),
ACL: 'public-read',
Key: key,
Body: body,
ContentType: contentType,
})
.send((err, data) => {
if (err) {
return res.status(500).json('Internal Error Please try again');
}
if (data) {
console.log('data', data);
return res.status(200).json({ files: data.Location });
}
});
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
full code here
(is this approach good ) :
handle.use(auth).post((req, res) => {
const s3 = new AWS.S3({
endpoint: new AWS.Endpoint('fra1.digitaloceanspaces.com'),
accessKeyId: process.env.SPACES_KEY,
secretAccessKey: process.env.SPACES_SECRET,
region: 'fra1',
// sslEnabled: true,
});
function paths({ id, ct }) {
console.log('id', id, ct);
switch (ct) {
case 'tck':
return `nor-platforms/${id}/tickets`;
case 'prd':
return `nor-platforms/${id}/products`;
case 'pcs':
return `nor-platforms/${id}`;
default:
return false;
}
}
try {
const form = new formidable.IncomingForm({ multiples: true, maxFileSize: 50 * 1024 * 1024 });
form.parse(req, async function (err, fields, files) {
console.log('files', files, 'fields', fields);
const { file } = files;
const body = file.length > 0 ? file.map((itm) => fs.readFileSync(itm.filepath)) : fs.readFileSync(file.filepath);
const contentType = file.length > 0 ? file.map((itm) => itm.mimetype) : file.mimetype;
const key = file.length > 0 ? file.map((itm) => itm.originalFilename) : file.originalFilename;
// const data = await saveFile(files.file, req.user._id);
console.log('body', body, 'key', key, 'contenttype', contentType);
// res.status(200).json({ files: data });
console.log('se', s3);
await s3
.upload({
Bucket: paths({ id: req.user._id, ct: fields?.ct }),
ACL: 'public-read',
Key: key,
Body: body,
ContentType: contentType,
})
.send((err, data) => {
if (err) {
return res.status(500).json('Internal Error Please try again');
}
if (data) {
console.log('data', data);
return res.status(200).json({ files: data.Location });
}
});
});
} catch (err) {
res.status(500).json('Internal Error Please try again');
}
});
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.