【发布时间】:2020-07-25 13:54:03
【问题描述】:
有人知道为什么我在尝试将对象放入 lambda 函数内的 S3 时会收到“拒绝访问”吗?我有具有 AdministorAccess 的无服务器 AWS 用户,并允许访问 serverless.yml 内的 s3 资源:
iamRoleStatements:
- Effect: Allow
Action:
- s3:PutObject
Resource: "arn:aws:s3:::*"
编辑 - 这是文件
serverless.yml
service: testtest
app: testtest
org: workx
provider:
name: aws
runtime: nodejs12.x
iamRoleStatements:
- Effect: Allow
Action:
- s3:PutObject
Resource: "arn:aws:s3:::*/*"
functions:
hello:
handler: handler.hello
events:
- http:
path: users/create
method: get
handler.js
'use strict';
const AWS = require('aws-sdk');
// get reference to S3 client
const S3 = new AWS.S3();
// Uload the content to s3 and allow download
async function uploadToS3(content) {
console.log('going to upload to s3!');
const Bucket = 'mtest-exports';
const key = 'testtest.csv';
try {
const destparams = {
Bucket,
Key: key,
Body: content,
ContentType: "text/csv",
};
console.log('going to put object', destparams);
const putResult = await S3.putObject(destparams).promise();
return putResult;
} catch (error) {
console.log(error);
throw error;
}
}
module.exports.hello = async event => {
const result = await uploadToS3('hello world');
return {
statusCode: 200,
body: JSON.stringify(result),
};
};
【问题讨论】:
-
试试
"Resource": "*"?如果这没有帮助,请编辑您的问题以包含导致错误的代码。 -
试试这个
Resource: "arn:aws:s3:::*/*" -
@JohnRotenstein 我试过了,没用
-
@ThanhNguyenVan 也试过你的方法,但没用
-
请编辑您的问题以包含导致错误的代码。
标签: amazon-s3 aws-lambda serverless-framework serverless