Access中使用boto3通过AWS LAMBDA
问题描述:
S3 + SNS +构成LAMBDA
数据处理流水线被拒绝becasue S3不能发送notificaiton出它的存储区域,所以我利用SNS的发送S3通知到其他地区的Lambda。
lambda函数编码与
from __future__ import print_function
import boto3
def lambda_handler (event, context):
input_file_bucket = event["Records"][0]["s3"]["bucket"]["name"]
input_file_key = event["Records"][0]["s3"]["object"]["key"]
input_file_name = input_file_bucket+"/"+input_file_key
s3=boto3.resource("s3")
obj = s3.Object(bucket_name=input_file_bucket, key=input_file_key)
response = obj.get()
return event #echo first key valuesdf
,当我跑保存和测试,我得到了以下错误
{
"stackTrace": [
[
"/var/task/lambda_function.py",
20,
"lambda_handler",
"response = obj.get()"
],
[
"/var/runtime/boto3/resources/factory.py",
394,
"do_action",
"response = action(self, *args, **kwargs)"
],
[
"/var/runtime/boto3/resources/action.py",
77,
"__call__",
"response = getattr(parent.meta.client, operation_name)(**params)"
],
[
"/var/runtime/botocore/client.py",
310,
"_api_call",
"return self._make_api_call(operation_name, kwargs)"
],
[
"/var/runtime/botocore/client.py",
395,
"_make_api_call",
"raise ClientError(parsed_response, operation_name)"
]
],
"errorType": "ClientError",
"errorMessage": "An error occurred (AccessDenied) when calling the GetObject operation: Access Denied"
}
我配置了
full S3 access
,并设置拉姆达角色我的目标存储桶上的存储桶政策
everyone can do anything(list, delete, etc.)
看来我还没有制定好政策。
答
您正在寻找的是具有有限的权限
答
我也有类似问题的特定对象S3的可能性,我通过安装适当的政策,以我的用户解决了这个问题。
IAM - >用户 - >用户名 - >权限 - >附加策略。
此外,请确保您添加正确的访问密钥和秘密访问密钥,您可以使用AmazonCLI。
答
添加到阿姆里的答案,如果你的水桶是私有的,你有凭据来访问它,你可以使用boto3.client:
import boto3
s3 = boto3.client('s3',aws_access_key_id='ACCESS_KEY',aws_secret_access_key='SECRET_KEY')
response = s3.get_object(Bucket='BUCKET', Key='KEY')
*此文件:S3://桶/ A/b/c/some.text,桶是'桶',关键是'a/b/c/some.text'
这很含糊。你能否指出一个人可以解决这个问题? –
两种可能性 1.针对读取的S3对象级别权限被拒绝 2.附加到lambda的角色没有获取/读取S3对象的权限 – omuthu
对于我帮助将's3:GetObject'添加到策略中。 –