使用AWS SDK提供的s3.getObject方法,将S3对象的内容读取到内存中,然后将其写入Lambda函数的/tmp目录下的临时文件。
以下是一个Node.js示例示范了如何处理此问题:
const AWS = require('aws-sdk');
const fs = require('fs');
exports.handler = async (event, context) => {
const s3 = new AWS.S3();
const params = {
Bucket: 'my-bucket',
Key: 'my-file.txt'
};
const fileContent = await s3.getObject(params).promise();
// Write the object content to a temporary file
fs.writeFileSync('/tmp/my-file.txt', fileContent.Body);
// Process the file
// ...
return {
statusCode: 200,
body: 'File processed successfully'
};
};