python-2.7 - AWS Python Lambda函数-将文件上传到S3

标签 python-2.7 amazon-s3 aws-lambda

我有一个用Python 2.7编写的AWS Lambda函数,我想在其中执行以下操作:

1)从一个HTTP地址中获取一个.xls文件。

2)将其存放在临时位置。

3)将文件存储在S3存储桶中。

我的代码如下:

from __future__ import print_function
import urllib
import datetime 
import boto3
from botocore.client import Config

def lambda_handler(event, context):

    """Make a variable containing the date format based on YYYYYMMDD"""
    cur_dt = datetime.datetime.today().strftime('%Y%m%d')

    """Make a variable containing the url and current date based on the variable
    cur_dt"""
    dls = "http://11.11.111.111/XL/" + cur_dt + ".xlsx"
    urllib.urlretrieve(dls, cur_dt + "test.xls")

    ACCESS_KEY_ID = 'Abcdefg'
    ACCESS_SECRET_KEY = 'hijklmnop+6dKeiAByFluK1R7rngF'
    BUCKET_NAME = 'my-bicket'
    FILE_NAME = cur_dt + "test.xls";

    data = open('/tmp/' + FILE_NAME, 'wb')

    # S3 Connect
    s3 = boto3.resource(
        's3',
        aws_access_key_id=ACCESS_KEY_ID,
        aws_secret_access_key=ACCESS_SECRET_KEY,
        config=Config(signature_version='s3v4')
    )

    # Uploaded File
    s3.Bucket(BUCKET_NAME).put(Key=FILE_NAME, Body=data, ACL='public-read')

但是,当我运行此函数时,出现以下错误:

“IOError:[Errno 30]只读文件系统”

我已经花了数小时试图解决这个问题,但是我倒在脸上了。任何帮助,将不胜感激。

最佳答案

'IOError: [Errno 30] Read-only file system'



您似乎缺少一些写访问权限。如果您的lambda还有其他政策,请尝试将此政策附加到您的角色上:

arn:aws:iam::aws:policy/AWSLambdaFullAccess



如果您无法在存储桶中写入数据,它也对S3具有完全访问权限。如果它解决了您的问题,则此后您将删除一些权利。

关于python-2.7 - AWS Python Lambda函数-将文件上传到S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44183216/

相关文章:

python - 如何获取多个列表并嵌套它们?

python : Find tuples from a list of tuples having duplicate data in the 0th element(of the tuple)

regex - 使用正则表达式从 aws s3 url 中提取存储桶名称

node.js - 尝试访问数据时 DynamoDB 查询功能不起作用

Python + opencv 与 PyCharm- 'opencv' 没有属性 'imread'

Python 模块 turtle 未正确导入

ruby-on-rails - Heroku 与 VPS 定价

amazon-web-services - Amazon SES 中无法接收电子邮件

node.js - 从 S3 存储桶获取对象时,aws lambda 函数的访问被拒绝

amazon-web-services - 如何在 python 中从 CDK v2 定义 AWS Step Function 的参数?