从 s3 桶中获取数据

get data from s3 Bucket

def lambda_handler(event, context):
    if event:
        # TODO implement
        params=event.get('city')

        print(params)
        s3 = boto3.client('s3')
        data = s3.get_object(Bucket='clg-data' ,Key='citiescsv.csv')

        file = data['body'].read().decode('utf-8')
        print(file)

这是我的代码

    {"errorMessage": "An error occurred (AccessDenied) when calling the GetObject operation: Access Denied", "errorType": "ClientError",  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 15, in lambda_handler\n    data = s3.get_object(Bucket='clg-data',Key='citiescsv.csv')\n",
    "  File \"/var/runtime/botocore/client.py\", line 357, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n",
    "  File \"/var/runtime/botocore/client.py\", line 676, in _make_api_call\n    raise error_class(parsed_response, operation_name)\n" ]}

我在从存储桶中获取数据时遇到此错误。

错误消息写着“GetObject 操作:访问被拒绝”,这很可能意味着您的 lambda execution role does not have S3 read permissions. You can rectify this by adding the following inline policy 到 lambda 的角色:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::clg-data/*"
        }
    ]
}

也可能是其他原因,例如lambda 需要 KMS 权限,因为存储桶已加密或存储桶不在您的帐户中。