0

当我尝试从 blobstore 下载文件时遇到问题。我的应用程序将一些文件与 blobstore 中的数据合并以创建一个大文件 (50 MB)。这是我的代码:

    fileName=""
    def post(self):
    cadenaSalida = []
    for empiece in range(1):
        cadenaSubsidio="ficheroCSV" + str(empiece)
        logging.info(cadenaSubsidio)
        gqlQuery = blobstore.BlobInfo.gql("WHERE filename=:1",cadenaSubsidio)
        blobs  = gqlQuery.fetch(1)
        if blobs :
            for doc in blobs :
                logging.info(doc.key())
                blob_reader = blobstore.BlobReader(doc.key())
                cadenaSalida.append(blob_reader.readlines(None))
                logging.info("DESPUES DE LEER TODO EL FICHERO")
        else:
            logging.info("NO HAY DOCUMENTOS")


    logging.info("Creamos el fichero")
    cadenaTotal="csvTotal.csv"
    logging.info(cadenaTotal)
    self.creartxt(cadenaTotal)
    logging.info(self.fileName)
    f=self.abrirtxt()
    logging.info(f)
    self.escribirtxt(f, cadenaSalida)
    self.cerrartxt(f)

    resource = str(urllib.unquote(self.fileName))
    blob_info = blobstore.BlobInfo.get(resource)
    self.send_blob(blob_info)

它分配的 self.fileName 的值:

    def creartxt (self,nombreCSV):
    # Create the file
    self.fileName = files.blobstore.create(mime_type='application/octet-stream',_blobinfo_uploaded_filename=nombreCSV)

   def abrirtxt (self):
    f= files.open(self.fileName, 'a')
    return f

我使用的其他方法是:

    def escribirtxt(self,f, lineas):
    logging.info("VAMOS A ESCRIBIR EL TXT")
    for linea in lineas:
        logging.info(linea)
        try:
            f.write(str(linea))
        except:
            logging.info("Da el punetero errror e intentamos abrir el fichero")
            f= files.open(self.fileName, 'a')
            logging.info("Antes de escribir")
            f.write(linea)
            logging.info("Despues de escribir")



   def cerrartxt(self,f):
       f.close()  
       files.finalize(self.fileName)  

但是,出现错误:“BadValueError: name must be under 500 bytes.”。我怎样才能避免这个错误?

堆栈跟踪是:

抱歉,堆栈跟踪是:

name must be under 500 bytes.
Traceback (most recent call last):
  File "/base/data/home/runtimes/python/python_lib/versions/1/google/appengine/ext/webapp/_webapp25.py", line 716, in __call__
    handler.post(*groups)
  File "/base/data/home/apps/s~ono-hat-vv2/1.369495673401411273/com/__init__.py", line 207, in post
    blob_info = blobstore.BlobInfo.get(resource)
  File "/base/data/home/runtimes/python/python_lib/versions/1/google/appengine/ext/blobstore/blobstore.py", line 300, in get
    blob_keys = cls.__normalize_and_convert_keys(blob_keys)
  File "/base/data/home/runtimes/python/python_lib/versions/1/google/appengine/ext/blobstore/blobstore.py", line 390, in __normalize_and_convert_keys
    keys[index] = datastore.Key.from_path(cls.kind(), str(key), namespace='')
  File "/base/data/home/runtimes/python/python_lib/versions/1/google/appengine/api/datastore_types.py", line 514, in from_path
    ValidateString(id_or_name, 'name')
  File "/base/data/home/runtimes/python/python_lib/versions/1/google/appengine/api/datastore_types.py", line 176, in ValidateString
    raise exception('%s must be under %d bytes.' % (name, max_len))
BadValueError: name must be under 500 bytes.

错误发生在以下行:

blob_info = blobstore.BlobInfo.get(资源)

问候。

4

2 回答 2

0

所以看看self.fileName = files.blobstore.create(mime_ ....它不会创建一个文件名而是一个可写的处理程序。

您继续打开并写入。

但是你要执行以下操作

resource = str(urllib.unquote(self.fileName))
blob_info = blobstore.BlobInfo.get(resource)

但是 BlobInfo.get() 需要一个 blobkey 而不是 blobfile 对象或任何str(urllib.unquote(self.fileName))返回的东西。因此验证错误。

您可能应该使用get_blob_key(create_file_name)Gets the blob key for a finalized blobstore 文件。但不是 100% 确定这一点。

于 2013-08-19T07:35:18.743 回答
0

解决方案是使用:

gqlQuery = blobstore.BlobInfo.gql("WHERE filename=:1",self.cadenaTotal)
blobs  = gqlQuery.fetch(1)
    if blobs :
        for doc in blobs:
            logging.info(doc.key())
            blob_info = blobstore.BlobInfo.get(doc.key())

谢谢你的时间。

于 2013-08-22T07:37:18.900 回答