Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从s3 Bucket获取数据_Python_Amazon Web Services_Amazon S3_Aws Lambda_Bucket - Fatal编程技术网

Python 从s3 Bucket获取数据

Python 从s3 Bucket获取数据,python,amazon-web-services,amazon-s3,aws-lambda,bucket,Python,Amazon Web Services,Amazon S3,Aws Lambda,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') fi

这是我的密码

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)
从存储桶中获取数据时出现此错误。

错误消息写入“GetObject操作:拒绝访问”,这很可能意味着您没有S3读取权限。您可以通过向lambda角色添加以下内容来纠正此问题:

    {"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" ]}

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

您的lambda执行角色是什么?只需从bucketHi获取csv文件即可。我注意到你有几个问题有答案,但没有一个被接受。如果他们都没有帮助,那也没关系。但如果他们有帮助,接受他们是一个很好的做法。它消除了重复问题的数量,并在将来帮助其他人。
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::clg-data/*"
        }
    ]
}