我按照本教程設定了當檔案上傳到我的 S3 存盤桶時觸發的 Lambda:https : //docs.aws.amazon.com/lambda/latest/dg/with-s3-example.html
我還設定了一個前綴(教程沒有),以便只針對一個檔案夾:前綴:海報/
我的 S3 存盤桶中有一個名為posters/. 反正代碼是直接從教程復制過來的:
import json
import urllib.parse
import boto3
print('Loading function')
s3 = boto3.client('s3', 'us-east-2')
def lambda_handler(event, context):
#print("Received event: " json.dumps(event, indent=2))
# Get the object from the event and show its content type
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
try:
response = s3.get_object(Bucket=bucket, Key=key)
print("CONTENT TYPE: " response['ContentType'])
return response['ContentType']
except Exception as e:
print(e)
print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
raise e
并且測驗事件已經配置如下:
{
"Records": [
{
"eventVersion": "2.0",
"eventSource": "aws:s3",
"awsRegion": "us-east-2",
"eventTime": "1970-01-01T00:00:00.000Z",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "EXAMPLE"
},
"requestParameters": {
"sourceIPAddress": "127.0.0.1"
},
"responseElements": {
"x-amz-request-id": "EXAMPLE123456789",
"x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH"
},
"s3": {
"s3SchemaVersion": "1.0",
"configurationId": "testConfigRule",
"bucket": {
"name": "MY_BUCKET_NAME",
"ownerIdentity": {
"principalId": "EXAMPLE"
},
"arn": "arn:aws:s3:::example-bucket"
},
"object": {
"key": "posters/HappyFace.jpg",
"size": 1024,
"eTag": "0123456789abcdef0123456789abcdef",
"sequencer": "0A1B2C3D4E5F678901"
}
}
}
]
}
當我對此進行測驗時,出現以下錯誤:
{
"errorMessage": "An error occurred (NoSuchKey) when calling the GetObject operation: The specified key does not exist.",
"errorType": "NoSuchKey",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 23, in lambda_handler\n raise e\n",
" File \"/var/task/lambda_function.py\", line 17, in lambda_handler\n response = s3.get_object(Bucket=bucket, Key=key)\n",
" File \"/var/runtime/botocore/client.py\", line 386, in _api_call\n return self._make_api_call(operation_name, kwargs)\n",
" File \"/var/runtime/botocore/client.py\", line 705, in _make_api_call\n raise error_class(parsed_response, operation_name)\n"
]
}
我到底做錯了什么?顯然權限很好,因為在為函式角色設定正確的策略后,這個問題就消失了。任何幫助是極大的贊賞。
uj5u.com熱心網友回復:
NoSuchKey 意味著檔案不在存盤桶 鍵顯示的位置。
您是否上傳了在存盤桶中HappyFace.jpg的posters檔案夾內命名的測驗檔案(根據教程Create a bucket and upload a sample object)?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/347661.html
標籤:Python 亚马逊网络服务 亚马逊-s3 aws-lambda
上一篇:在AWSS3上檢查上傳成功
