So I want to use S3 to store user uploaded files (images, videos, documents and audio) on my website. I do not want to use wix media as it does not have the functionality I require.
I have been trying to use code to get files uploaded to S3 directly from my site but I have been unable to do so. I have been unable to find a guide or tutoral that shows how this can be done from wix specifically.
I have been following this tutorial: https://www.youtube.com/watch?v=joXy_OTCO_E that shows you how to do it from a locally hosted website with this code:
const AWS = require('aws-sdk');
const Busboy = require('busboy');
const BUCKET_NAME = '';
const IAM_USER_KEY = '';
const IAM_USER_SECRET = '';
function uploadToS3(file) {
let s3bucket = new AWS.S3({
accessKeyId: IAM_USER_KEY,
secretAccessKey: IAM_USER_SECRET,
Bucket: BUCKET_NAME
});
s3bucket.createBucket(function () {
var params = {
Bucket: BUCKET_NAME,
Key: file.name,
Body: file.data
};
s3bucket.upload(params, function (err, data) {
if (err) {
console.log('error in callback');
console.log(err);
}
console.log('success');
console.log(data);
});
});
}
module.exports = (app) => {
// The following is an example of making file upload with additional body
// parameters.
// To make a call with PostMan
// Don't put any headers (content-type)
// Under body:
// check form-data
// Put the body with "element1": "test", "element2": image file
app.post('/api/upload', function (req, res, next) {
// This grabs the additional parameters so in this case passing in
// "element1" with a value.
const element1 = req.body.element1;
var busboy = new Busboy({ headers: req.headers });
// The file upload has completed
busboy.on('finish', function() {
console.log('Upload finished');
// Your files are stored in req.files. In this case,
// you only have one and it's req.files.element2:
// This returns:
// {
// element2: {
// data: ...contents of the file...,
// name: 'Example.jpg',
// encoding: '7bit',
// mimetype: 'image/png',
// truncated: false,
// size: 959480
// }
// }
// Grabs your file object from the request.
const file = req.files.element2;
console.log(file);
// Begins the upload to the AWS S3
uploadToS3(file);
});
req.pipe(busboy);
});
}
However I have been unable to get it to work. I have already installed the “busboy” and “aws-sdk” nodes and I have tried putting different bits in backend to try and get it to work but it still does not. (To be honest I do not understand a lot of it)
Can anyone help me? Thank you.