Skip to content

Instantly share code, notes, and snippets.

@derofim
Created October 11, 2018 14:03
Show Gist options
  • Save derofim/e318400d6fa659cd33f84cf9172f10ce to your computer and use it in GitHub Desktop.
Save derofim/e318400d6fa659cd33f84cf9172f10ce to your computer and use it in GitHub Desktop.
AWS node
const AWS = require('aws-sdk');
const { AWS: awsConfig } = require('../../config');
AWS.config.update({
accessKeyId: awsConfig.accessKeyId,
secretAccessKey: awsConfig.secretAccessKey,
region: awsConfig.region
});
function uploadFileToS3(file, bucketName) {
try {
const Bucket = bucketName;
const s3bucket = new AWS.S3({
accesKeyId: awsConfig.accessKeyId,
secretAccesKey: awsConfig.secretAccessKey,
Bucket
});
s3bucket.createBucket(() => {
const params = {
ACL: 'public-read',
Bucket,
Key: file.name,
Body: file.data
};
s3bucket.upload(params, (err, data) => {
if(err) throw new Error(err);
});
});
} catch (e) {
console.error('can\'t upload file to S3 AWS Bucket', e);
};
};
module.exports = uploadFileToS3;
const uploadFileToS3 = require('../../misc/AWS');
// ...
account.post('/change/picture',
async (req, res, next) => {
try {
const busboy = new Busboy({ headers: req.headers });
busboy.on('finish', () => {
const file = req.files.picture;
if (file.mimetype !== 'image/jpeg') {
apiCodes[20](res);
};
if (file.size > avatarSettings.fileSizeLimitByte) {
apiCodes[21](res);
};
const fileSize = 'default';
// SPA VUE allow only image/jpeg
file.name = `${req.user.wallet.address}${fileSize}.jpeg`;
const BucketName = `${config.bucketName}/${AWN_S3_Settings.nameFolderOfAvatars}`;
uploadFileToS3(file, BucketName);
});
req.pipe(busboy);
} catch(err) {
apiCodes[22](res);
console.error(err);
// next(err);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment