J. *_*Doe 5 python amazon-s3 amazon-web-services python-3.x
我刚刚使用无服务器部署了 lambda,但不允许我访问我想要的 s3 存储桶。这段代码中有什么明显损坏的地方吗?
service: handler
frameworkVersion: '2'
provider:
name: aws
runtime: python3.8
lambdaHashingVersion: 20201221
iam:
role:
statements:
- Effect: 'Allow'
Action:
- 's3:GetObject'
- 's3:PutObject'
Resource: "arn:aws:s3:::my_bucket"
plugins:
- serverless-python-requirements
package:
exclude:
- node_modules/**
functions:
login:
handler: handler.login
events:
- httpApi:
path: /login
method: post
Run Code Online (Sandbox Code Playgroud)
这是尝试访问 s3 的函数
def check_s3(user):
s3 = boto3.client('s3')
obj = s3.get_object(Bucket="my_bucket", Key=user)
data = json.loads(obj['Body'].read())
return data
Run Code Online (Sandbox Code Playgroud)
我收到的错误:
[ERROR] ClientError: An error occurred (AccessDenied) when calling the GetObject operation: Access Denied
Traceback (most recent call last):
File "/var/task/handler.py", line 11, in login
d = check_s3(username)
File "/var/task/handler.py", line 34, in check_s3
obj = s3.get_object(Bucket="my_bucket", Key=user)
File "/var/runtime/botocore/client.py", line 386, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/var/runtime/botocore/client.py", line 705, in _make_api_call
raise error_class(parsed_response, operation_name)
Run Code Online (Sandbox Code Playgroud)