/** * Download file from S3 * @param {String} attachmentId the attachment id * @return {Promise} promise resolved to downloaded data */ async function downloadFromS3 (attachmentId) { const file = await s3.getObject({ Bucket: config.AMAZON.ATTACHMENT_S3_BUCKET, Key: attachmentId }).promise() return { data: file.Body, mimetype: file.ContentType } }
/** * Upload file to S3 * @param {String} attachmentId the attachment id * @param {Buffer} data the file data * @param {String} mimetype the MIME type * @param {String} fileName the original file name * @return {Promise} promise to upload file to S3 */ async function uploadToS3 (attachmentId, data, mimetype, fileName) { const params = { Bucket: config.AMAZON.ATTACHMENT_S3_BUCKET, Key: attachmentId, Body: data, ContentType: mimetype, Metadata: { fileName } } // Upload to S3 return s3.upload(params).promise() }
prepare(function (done) { AWS.config.update({ accessKeyId: config.AMAZON.AWS_ACCESS_KEY_ID, secretAccessKey: config.AMAZON.AWS_SECRET_ACCESS_KEY, region: config.AMAZON.AWS_REGION, endpoint: config.S3_ENDPOINT, sslEnabled: false, s3ForcePathStyle: true }) initBucket() .then(result => { done() }) }, function (done) { done() })
/** * Delete file from S3 * @param {String} attachmentId the attachment id * @return {Promise} promise resolved to deleted data */ async function deleteFromS3 (attachmentId) { return s3.deleteObject({ Bucket: config.AMAZON.ATTACHMENT_S3_BUCKET, Key: attachmentId }).promise() }