簡體   English   中英

我如何使用 boto3 觸發 Lambda function

[英]how I can trigger Lambda function using boto3

我有一個 s3 存儲桶,這是我將上傳文件dev/uploads/excel的路徑。 現在我想添加一個觸發器來調用我已經制作的 lambda function。是否有任何特定代碼我必須運行一次才能使用 boto3 為這個 function 啟用觸發器?或者需要粘貼到某個地方? 我很困惑它將如何工作?

您需要在 Lambda function 上添加一個 S3 觸發器,並在您的代碼中處理 S3 事件。

要創建 S3 觸發器 select,請在 Lambda 控制台左側添加觸發器選項。

在此處輸入圖像描述

由於您想要觸發新的上傳,您可以創建此事件來觸發 PUT 事件。 對於前綴,您可以添加所需的路徑: dev/uploads/excel

在此處輸入圖像描述

Python 中的 Lambda 示例:

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

此外,還有很多文檔對此進行了解釋,例如以下文檔: 教程:使用 Amazon S3 觸發器調用 Lambda function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM