【问题标题】:"BadValueError: name must be under 500 bytes." when i download a big file“BadValueError:名称必须小于 500 字节。”当我下载一个大文件时
【发布时间】:2013-08-14 11:22:22
【问题描述】:

当我尝试从 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(resource)

问候。

【问题讨论】:

  • 您需要包含堆栈跟踪,哪一行代码是引发的错误?
  • 您没有显示 self.filename 的设置方式/内容。在文档中,传递给 blobstore.BlobInfo.get() 的值应该是一个 blob 键。
  • 好的,我已经添加了我所有的方法

标签: python google-app-engine download blobstore


【解决方案1】:

所以看看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) 获取最终 blobstore 文件的 blob 密钥。但不能 100% 确定。

【讨论】:

    【解决方案2】:

    解决方案是使用:

    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())
    

    感谢您的宝贵时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      • 1970-01-01
      • 2021-07-18
      • 2015-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多