【问题标题】:Image migration method not working in production, but works in localhost图像迁移方法在生产中不起作用,但在 localhost 中起作用
【发布时间】:2015-03-24 08:06:16
【问题描述】:

我使用 GAE 的 Files API 编写了以下方法,用于将图像从我的 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)

如果我从本地主机的交互式控制台运行它,它可以正常工作。但是在投入生产网站时会中断。

我的日志中出现以下错误。

Traceback(最近一次调用最后一次): 文件“”,第 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)

文件“/home/rohit/workspace/google_appengine/google/appengine/api/files/file.py”,第 255 行,在 _make_call 中 _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

我被这个错误困扰了一段时间,任何帮助将不胜感激。

【问题讨论】:

    标签: python sql-server google-app-engine google-cloud-datastore blobstore


    【解决方案1】:

    查看另一个类似问题,https://stackoverflow.com/a/18308034/1686094

    保持文件打开超过 30 秒也会导致此错误。尝试将写入分成多个写入。

    【讨论】:

    • 感谢您的回答。你能解释一下我怎样才能进行多次写入吗?
    • 我不确定,可能每个条目都是put() 而不是ndb.put_multi()
    • 感谢您的快速回复,但它不起作用我的朋友,put() 也用于提交我的数据。你知道任何类似于 Java 的 BufferedOutputStream 类的 python 库吗?
    猜你喜欢
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 2019-02-17
    相关资源
    最近更新 更多