UNPKG

modern-valhalla

Version:
64 lines (51 loc) 1.44 kB
const AWS = require('aws-sdk'); const fs = require('fs'); const {getFileInfo} = require('./s3-utils'); class S3Client { constructor(config) { AWS.config.update({ accessKeyId: config.s3.key, secretAccessKey: config.s3.secret, region: config.s3.region, httpOptions: { timeout: 10000, }, }); this.client = new AWS.S3(); this.bucket = config.s3.bucket; } putFile(filePath, fileName, isPrivate, callback) { fs.readFile(filePath, (err, fileContent) => { if (err) { return callback(err); } this.putFileContent(fileContent, fileName, isPrivate, callback); }); } putFileContent(fileContent, fileName, isPrivate, callback) { const fileInfo = getFileInfo(fileName); const obj = { Bucket: this.bucket, Key: fileName, Body: fileContent, ACL: isPrivate ? 'authenticated-read' : 'public-read', ServerSideEncryption: 'AES256', Expires: new Date(new Date().setYear(new Date().getFullYear() + 1)), }; if (fileInfo.mimeType) { obj.ContentType = fileInfo.mimeType; } if (fileInfo.gzip) { obj.ContentEncoding = 'gzip'; } this.client.putObject(obj, callback); } deleteFile(fileName, callback) { const params = { Bucket: this.bucket, Key: fileName, }; this.client.deleteObject(params, callback); } } module.exports = S3Client;