【问题标题】:Boto does not save s3 file to disc on AWS LambdaBoto 不会将 s3 文件保存到 AWS Lambda 上的磁盘
【发布时间】:2023-03-24 19:27:02
【问题描述】:

我正在开发一个模块,该模块从我的 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 ...")

这是我目前拥有的模块的完整脚本:

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 角色

【问题讨论】:

  • 你可以试试把这行改成:string = '/tmp/' + str(count) + '.png'
  • 解决了 - 非常感谢!!

标签: python amazon-web-services amazon-s3 aws-lambda boto


【解决方案1】:

您可以尝试将行更改为:

string = '/tmp/' + str(count) + '.png'

没有办法知道您是否在 lambda 机器上的当前目录中具有写入权限。但是/tmp会让你写。

发件人:AWS Lambda FAQ

问:如果我的 AWS Lambda 函数需要磁盘上的暂存空间怎么办?

每个 Lambda 函数在 它自己的 /tmp 目录。

【讨论】:

  • 是否有 Lambda 文档链接,我可以在其中找到此类信息?
  • "问:如果我的 AWS Lambda 函数需要磁盘上的暂存空间怎么办?每个 Lambda 函数在其自己的 /tmp 目录中接收 500MB 的非持久磁盘空间。" source
  • 感谢@buildmaestro。我更新了我的答案。提出问题时,FAQ 不存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-15
  • 2016-02-10
  • 2018-09-14
  • 2016-07-22
  • 2013-12-19
  • 2010-09-23
  • 2010-11-08
相关资源
最近更新 更多