Boto在AWS Lambda上无法将S3文件保存到磁盘

3
我正在开发一个模块,从我的 MySQL 数据库中获取有关图像序列的信息,然后使用 boto 库在 Lambda 上本地下载它们。之后,应该进行一些图像处理,但我还没有完成。
我一直遇到的错误很奇怪。在(我相信)下载第一张图片后,它说文件/目录不存在:
    Traceback (most recent call last): File "/var/task/module.py", line 41, in lambda_handler key.get_contents_to_filename(string) 

File "/var/task/boto/s3/key.py", line 1714, in get_contents_to_filename os.remove(filename) OSError: [Errno 2] No such file or directory: '1.png'

我的代码中第41行位于这里:

# Download and Save all of the cropped images for this company  
count = 1
files_list = []
for image in cropped_images:
    key = bucket.get_key(image)
    string = str(count) + '.png'
    key.get_contents_to_filename(string)
    files_list.append(string) 
    count += 1
print("should be done downloading all of the pictures from S3 ...")

这是我目前为止编写的该模块的完整脚本:
```HTML

在这里展示了我迄今为止编写的该模块的完整脚本:

```
from __future__ import print_function
import json, os
import pymysql

import os, glob, subprocess, boto
from boto.s3.connection import S3Connection

conn = pymysql.connect(host='*******', port=3306, user='*****', passwd='******', db='*******')
cur = conn.cursor()

conn = S3Connection('***************','************************')
bucket = conn.get_bucket('**********')

def upload_file(company_friendly, filepath):
    key = bucket.new_key(company_friendly + ".gif")
    key.set_contents_from_filename(filepath)
    key.set_acl('public-read')
    return True

def lambda_handler(event, context):
    ### Grab Company Friendly from DB ###
    cur.execute("SELECT ************ FROM *********** WHERE company_id = %i LIMIT 1" % (event['company_id']))

    company_data = cur.fetchone()
    company_friendly = company_data[0]

    ### Grab all snapshots from DB ###
    cur.execute("SELECT *********** FROM ************ WHERE company_id = %i ORDER BY date ASC" % (event['company_id']))
    snapshot_data = cur.fetchall()  

    cropped_images = []
    for snapshot in snapshot_data:
        cropped_images.append(snapshot[0])

    ### Download and Save all of the cropped images for this company  ###
    count = 1
    files_list = []
    for image in cropped_images:
        key = bucket.get_key(image)
        string = str(count) + '.png'
        key.get_contents_to_filename(string)
        files_list.append(string) #this makes sure that the animated gif compiles the images in order
        count += 1
    print("should be done downloading all of the pictures from S3 ...")

    return(json.dumps({'status_code':200, 'msg':"company friendly name is " + company_friendly}, sort_keys=True))

任何想法,为什么它不能保存图像文件?我正在使用Lambda控制台建议的默认S3角色。

1
你能试着将这行代码改为:string = '/tmp/' + str(count) + '.png' 吗? - helloV
搞定了 - 非常感谢!! - 24x7
1个回答

3
您能否尝试将代码行更改为:string = '/tmp/' + str(count) + '.png'。在Lambda机器上,无法知道当前目录是否具有写入访问权限,但/tmp目录可以让您进行写入操作。来自:AWS Lambda FAQ。每个Lambda函数都有500MB的非持久性磁盘空间,位于其自己的/tmp目录中,因此如果您需要在AWS Lambda函数上使用磁盘临时空间,可以使用这个目录。

有Lambda文档的链接可以找到这样的信息吗? - Murilo Garcia
问:如果我的AWS Lambda函数需要磁盘上的临时空间怎么办?每个Lambda函数在其自己的/tmp目录中都会收到500MB的非持久性磁盘空间。 来源 - buildmaestro
感谢@buildmaestro。我更新了我的答案。当问题被提出时,FAQ并不存在。 - helloV

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接