[英]Reaching an S3 location on AWS Lambda function
我有一個 AWS lambda function 需要訪問 S3 存儲桶中的文件,我的設置如下所示:
#lambda function
def test(event=None, context=None):
item = 'https://s3.console.aws.amazon.com/s3/object/bucket/file.json'
print(item)
當我運行它時,我得到:
“errorMessage”:“[Errno 2] 沒有這樣的文件或目錄:' https://s3.console.aws.amazon.com/s3/object/bucket/file.json '”
我也嘗試過使用這條路徑:
s3://bucket/file.json
編輯
添加實際代碼:
df = None
SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
KEY_FILE_LOCATION = 'arn:aws:s3:::gadsinfo/client_secrets.json'
VIEW_ID = '111'
def test(event=None, context=None):
def initialize_analyticsreporting():
'''Initializes an Analytics Reporting API V4 service object.
Returns:
An authorized Analytics Reporting API V4 service object.
'''
credentials = ServiceAccountCredentials.from_json_keyfile_name(
KEY_FILE_LOCATION, SCOPES)
# Build the service object.
analytics = build('analyticsreporting', 'v4', credentials=credentials)
return analytics
錯誤信息
文件“/var/task/test.py”,第 21 行,initialize_analyticsreporting KEY_FILE_LOCATION,SCOPES)文件“/var/task/oauth2client/service_account.py”,第 219 行,from_json_keyfile_name 中的 open(filename, 'r') 為file_obj: FileNotFoundError: [Errno 2] 沒有這樣的文件或目錄:'arn:aws:s3:::gadsinfo/client_secrets.json'
第 21 行是credentials =...
行。
但得到相同的錯誤信息。 如何在 S3 中找到該文件?
ServiceAccountCredentials.from_json_keyfile_name()
調用應提供本地磁盤上的文件名,而不是對 Amazon S3 object 的引用。
如果 Lambda function 已通過其 IAM 角色獲得足夠的權限,它可以首先將該文件下載到本地磁盤:
import boto3
s3_client = boto3.client('s3')
s3_client.download_file('gadsinfo', 'client_secrets.json', '/tmp/client_secrets.json')
credentials = ServiceAccountCredentials.from_json_keyfile_name(
'/tmp/client_secrets.json', SCOPES)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.