before changes
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
const AWS = require('aws-sdk')
|
||||
const fs = require('fs')
|
||||
const { stringifyError } = require('../utils/logService')
|
||||
var s3 = new AWS.S3()
|
||||
|
||||
const fetchS3Object = async ({ fileName, bucket, filePath }) => {
|
||||
filePath = filePath || '/tmp/' + fileName
|
||||
console.log('Fetching', stringifyObj({ fileName, filePath }))
|
||||
try {
|
||||
var params = { Bucket: bucket, Key: fileName }
|
||||
const downloadResult = await s3.getObject(params).promise()
|
||||
fs.writeFileSync(filePath, downloadResult.Body, function (err) {
|
||||
if (err) console.log(err.code, '-', err.message)
|
||||
})
|
||||
return filePath
|
||||
} catch (error) {
|
||||
console.log('Download from S3 Error:', stringifyError(error))
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
const upload = ({
|
||||
filePath,
|
||||
fileName,
|
||||
bucket,
|
||||
contentType,
|
||||
fileType,
|
||||
// accessControl,
|
||||
}) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
// accessControl = accessControl || 'public-read'
|
||||
fs.readFile(filePath, function (err, data) {
|
||||
if (err) reject(err)
|
||||
const params = {
|
||||
Bucket: bucket, // pass your bucket name
|
||||
Key: fileName,
|
||||
Body: data,
|
||||
// ContentType: contentType,
|
||||
// ContentDisposition: `inline; fileName=${fileName}.${fileType}`,
|
||||
// ACL: accessControl,
|
||||
}
|
||||
s3.upload(params, function (err, data) {
|
||||
if (err) {
|
||||
reject(err)
|
||||
console.log(`${fileName} Upload to s3`, stringifyError(err))
|
||||
} else {
|
||||
console.log(
|
||||
`Successfully uploaded data ${fileName}`,
|
||||
stringifyError(data)
|
||||
)
|
||||
resolve(data.Location)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
upload,
|
||||
fetchS3Object,
|
||||
}
|
||||
Reference in New Issue
Block a user