我已经使用Files API for GAE编写了以下方法,用于将图像从我的SQL服务器迁移到GAE Blobstore。
import urllib2,csv
from abc.model import *
from google.appengine.api import files
from google.appengine.ext import ndb, blobstore
from urllib2 import HTTPError
def foo():
i = []
j = []
csv_reader = csv.reader(open('tbl_property_images.csv','r'))
csv_prop = csv.reader(open('property.csv','r'))
ids = []
for ele in csv_prop:
ids.append(ele[0])
for row in csv_reader:
i.append(row[2])
j.append(row[3])
i = iter(i)
j = iter(j)
k = list(zip(i, j))
d = {}
for x, y in k:
if x in d:
d[x] = d[x] + [y]
else:
d[x] = [y]
d.pop('fld_property_id')
to_put = []
for ab in d.iterkeys():
if ab in ids:
for b in d[ab]:
url = 'abc###.com/%s' % b
try:
file_name = files.blobstore.create(mime_type='image/jpeg')
image = urllib2.urlopen(url)
with files.open(file_name, 'a') as f:
f.write(image.read())
files.finalize(file_name)
blob_key = files.blobstore.get_blob_key(file_name)
blob_info = blobstore.BlobInfo.get(blob_key)
kwargs = {}
kwargs['id'] = File.allocate_ids(1)[0]
kwargs['identifier'] = '%s-%s' % (kwargs['id'], blob_info.filename)
file = File(filename=blob_info.filename, content_type=blob_info.content_type, size=blob_info.size, blob=blob_info.key(), **kwargs)
prop = Property.query(Property.id == ab).get()
image1 = Image.build(file=file, property=prop.key)
prop.image_url = image1.image_url
to_put.append(prop)
to_put.append(file)
# if prop.key not in to_put properties key
to_put.append(image1)
except HTTPError:
print url
continue
ndb.put_multi(to_put)
如果我从localhost的Interactive Console运行它,它可以正常工作。但放入生产网站时会中断。
我的日志中出现以下错误。
追踪(最近一次通话):
中的文件“”,第1行文件“temp_part2.py”,第39行,foo
f.write(image.read())
文件“/home/rohit/workspace/google_appengine/google/appengine/api/files/file.py”,第300行,退出
self.close()
文件“/home/rohit/workspace/google_appengine/google/appengine/api/files/file.py”,第294行,关闭
self._make_rpc_call_with_retry('Close', request, response)
文件“/home/rohit/workspace/google_appengine/google/appengine/api/files/file.py”,第430行,_make_rpc_call_with_retry
_make_call(method, request, response)
在_make_call中输入文件“/home/rohit/workspace/google_appengine/google/appengine/api/files/file.py”,第255行 _raise_app_error(e)中
文件“/home/rohit/workspace/google_appengine/google/appengine/api/files/file.py”,第198行,在_raise_app_error中
raise FileNotOpenedError(e)
FileNotOpenedError:ApplicationError:10
我暂时坚持这个错误,任何帮助都将受到高度赞赏。
答案 0 :(得分:0)
请参阅另一个类似的问题,https://stackoverflow.com/a/18308034/1686094
保持文件打开超过30秒也会导致此错误。尝试将写入分成多个写入。