2015-11-06 255 views
5

我使用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.) 

看來我還沒有制定好政策。

回答

3

您正在尋找的是具有有限的權限

+1

這很含糊。你能否指出一個人可以解決這個問題? –

+1

兩種可能性 1.針對讀取的S3對象級別權限被拒絕 2.附加到lambda的角色沒有獲取/讀取S3對象的權限 – omuthu

+0

對於我幫助將's3:GetObject'添加到策略中。 –

10

我也有類似問題的特定對象S3的可能性,我通過安裝適當的政策,以我的用戶解決了這個問題。

IAM - >用戶 - >用戶名 - >權限 - >附加策略。

此外,請確保您添加正確的訪問密鑰和祕密訪問密鑰,您可以使用AmazonCLI。

1

添加到阿姆裏的答案,如果你的水桶是私有的,你有憑據來訪問它,你可以使用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'