2015-12-22 289 views
3

我正在研究一個模塊,該模塊從我的MySQL數據庫獲取有關圖像序列的信息,然後使用boto庫在Lambda上本地下載它們。之後,它應該做一些圖像處理,但我還沒有完成。Boto不保存s3文件到AWS上的光盤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)) 

任何想法爲什麼它不保存圖像文件?我使用由氧控制檯

+1

你可以嘗試改變行:string ='/ tmp /'+ str(count)+'.png' – helloV

+0

它正確地排序 - 非常感謝! – 24x7

回答

4

你可以嘗試轉產到建議的默認S3作用:

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

有沒有辦法知道,如果你有一個拉姆達在當前目錄的寫訪問機。但/tmp會讓你寫。

來源:AWS Lambda FAQ

問:如果我需要爲我的AWS lambda函數在磁盤上的暫存空間?

每個Lambda函數在 自己的/ tmp目錄中接收到500MB的非持久磁盤空間。

+0

有沒有Lambda文檔的鏈接,我可以找到像這樣的信息? –

+0

「問:如果我的AWS Lambda功能需要磁盤暫存空間,該怎麼辦? 每個Lambda函數都會在其自己的/ tmp目錄中接收500MB的非永久性磁盤空間。」 [來源](https://aws.amazon.com/lambda/faqs/) – buildmaestro

+0

謝謝@buildmaestro。我更新了我的答案。當問題被問到時,常見問題解答不存在。 – helloV

相關問題