I want to be able to download a file from an aws lambda function.
For that I’ve come up with the following code:
export const downloadFile = async (fileType: FileType): Promise<FileEntry> => {
const writeStream = fs.createWriteStream(path.join(__dirname, fileType.name))
const readStream = s3Client.getObject({
Bucket: process.env.FILE_UPLOAD_BUCKET,
Key: fileType.key
}).createReadStream()
readStream.pipe(writeStream)
return new Promise((resolve, reject) => {
writeStream.on('finish', () => {
writeStream.close()
resolve({
filename: fileType.uri,
contentType: 'application/pdf',
content: fs.readFileSync(fileType.uri),
encoding: 'utf8'
})
})
writeStream.on('error', (err) => {
fs.unlink(fileType.uri, () => { reject(err) })
})
})
}
However it is not working, since I am getting the following error:
"EROFS: read-only file system, open '/var/task/api/controllers/Admin/filename.pdf'"
By the way this folder structure is the same I have in the controller I am using for handling this API call