首页 > 解决方案 > 用于 s3 上传的 AWS lambda 函数 - Python 3.8

问题描述

我已经用 Python 为我的 Lambda 函数编写了一些代码。这应该从 url 获取 CSV 数据,并将其上传/放入同一 aws 帐户中的一个 s3 存储桶中。所有策略和 IAM 角色都已设置,但 lambda 仍然没有执行它的任务。代码如下。有人可以检查代码并让我知道错误。

from urllib.request import urlopen
import boto3
import os
import time

BUCKET_NAME = '***'

CSV_URL = 'http://***'

def lambda_handler(event, context):
    response = urlopen(CSV_URL)
    s3 = boto3.client('s3')
    s3.upload_fileobj(response, BUCKET_NAME,time.strftime('%Y/%m/%d'))
    response.close()

除了基本执行之外,我已将以下策略附加到我的 lambda 函数。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::**",
                "arn:aws:s3:::*/*"
            ]
       }
    ]
}

标签: python-3.xamazon-s3file-uploadaws-lambda

解决方案


推荐阅读