我正在使用AWS Lambda将图像上传到S3。
运行本地测试:serverless invoke local function --function khanh
有效。
但是,当我部署应用程序并在Amazon Lamda上测试它时,它会返回此错误。
{
"errorMessage": "Access Denied",
"errorType": "AccessDenied",
"stackTrace": [
"Request.extractError (/var/task/node_modules/aws-sdk/lib/services/s3.js:577:35)",
"Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:105:20)",
"Request.emit (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:77:10)",
"Request.emit (/var/task/node_modules/aws-sdk/lib/request.js:683:14)",
"Request.transition (/var/task/node_modules/aws-sdk/lib/request.js:22:10)",
"AcceptorStateMachine.runTo (/var/task/node_modules/aws-sdk/lib/state_machine.js:14:12)",
"/var/task/node_modules/aws-sdk/lib/state_machine.js:26:10",
"Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:38:9)",
"Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:685:12)",
"Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:115:18)"
]
}
这是我的handler.js
'use strict';
const phantomjsLambdaPack = require('phantomjs-lambda-pack');
const exec = phantomjsLambdaPack.exec;
const path = require('path');
const fs = require('fs');
var AWS = require('aws-sdk');
AWS.config.accessKeyId='******';
AWS.config.secretAccessKey='****';
module.exports.khanh = (event, context, callback) => {
const scriptPath = path.join(__dirname, 'phantomjs-script.js');
exec(scriptPath, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`Result: ${stdout}`);
console.log(`Should have no error: ${stderr}`);
//スクリーンショットをS3にアップロードする
const tmp_file_path = path.join(__dirname, '2step.png');
const params = {Bucket: 'twitterfollower-dev-serverlessdeploymentbucket-bnjh23hrxw31', Key: 'screen/2step.png', Body: fs.createReadStream(tmp_file_path),};
new AWS.S3().upload(params, (error, data) => {
callback(error, 'fin!!');
});
})
}