11

ジレンマがあります.フレームワークとしてtipfyを使用して、scribdストアとblobstoreの両方にファイルをアップロードしています。blobstore.create_upload_url によってアクションが作成されない Web フォームがあります (url_for('myhandler') を使用しているだけです)。blobstore ハンドラーを使用している場合、POST 応答が解析され、通常の python-scribd API を使用してファイルを scribd ストアにアップロードできないためです。今、私はscribdセーバーを動かしています:

class UploadScribdHandler(RequestHandler, BlobstoreUploadMixin):
    def post(self):
        uploaded_file = self.request.files.get('upload_file')
        fname = uploaded_file.filename.strip()
        try:
            self.post_to_scribd(uploaded_file, fname)
        except Exception, e:
            # ... get the exception message and do something with it
            msg = e.message
            # ...
        # reset the stream to zero (beginning) so the file can be read again
        uploaded_file.seek(0)
        #removed try-except to see debug info in browser window
        # Create the file

        file_name = files.blobstore.create(_blobinfo_uploaded_filename=fname)
        # Open the file and write to it
        with files.open(file_name, 'a') as f:
            f.write(uploaded_file.read())
        # Finalize the file. Do this before attempting to read it.      
        files.finalize(file_name)
        # Get the file's blob key
        blob_key = files.blobstore.get_blob_key(file_name)

        return Response('done')

    def post_to_scribd(self, uploaded_file, fname):
        errmsg =''
        uploaded_file = self.request.files.get('upload_file')
        fname = uploaded_file.filename.strip()
        fext = fname[fname.rfind('.')+1:].lower()
        if (fext not in ALLOWED_EXTENSION):
            raise Exception('This file type does not allowed to be uploaded\n')
        if SCRIBD_ENABLED:
            doc_title = self.request.form.get('title')
            doc_description = self.request.form.get('description')
            doc_tags = self.request.form.get('tags')
            try:
                document = scribd.api_user.upload(uploaded_file, fname, access='private')
                #while document.get_conversion_status() != 'DONE':
                #   time.sleep(2)
                if not doc_title:
                    document.title = fname[:fname.rfind('.')]
                else:
                    document.title = doc_title
                if not doc_description:
                    document.description = 'This document was uploaded at ' + str(datetime.datetime.now()) +'\n'
                else:
                    document.description = doc_description
                document.tags = doc_tags
                document.save()
            except scribd.ResponseError, err:
                raise Exception('Scribd failed: error code:%d, error message: %s\n' % (err.errno, err.strerror))
            except scribd.NotReadyError, err:
                raise Exception('Scribd failed: error code:%d, error message: %s\n' % (err.errno, err.strerror))
            except:
                raise Exception('something wrong exception')

ご覧のとおり、ファイルもブロブストアに保存されます..しかし、大きなファイル(つまり5Mb)をアップロードしている場合、私は受け取っています

RequestTooLargeError: The request to API call file.Append() was too large.
Request: docs.upload(access='private', doc_type='pdf', file=('PK\x03\x04\n\x00\x00\x00\x00\x00"\x01\x10=\x00\x00(...)', 'test.pdf'))

どうすれば修正できますか?ありがとう!

4

2 に答える 2

7

たとえば、次のように、ファイル API に対して複数の小さな呼び出しを行う必要があります。

with files.open(file_name, 'a') as f:
    data = uploaded_file.read(65536)
    while data:
      f.write(data)
      data = uploaded_file.read(65536)

App Engine アプリへの通常のリクエストのペイロード サイズの制限は 10 MB です。より大きなファイルをアップロードする場合は、通常のブロブストア アップロード メカニズムを使用する必要があります。

于 2011-04-13T01:45:27.470 に答える
6

ついに私は解決策を見つけました。

upload_fileが文字列として扱われるため、NickJohnesonの回答で属性エラーが発生しました。文字列にはread()メソッドがありませんでした。

原因文字列にはメソッドread()がありません。ファイル文字列をつなぎ合わせて、彼が書いたように書き込みます。

class UploadRankingHandler(webapp.RequestHandler):
  def post(self):
    fish_image_file = self.request.get('file')

    file_name = files.blobstore.create(mime_type='image/png', _blobinfo_uploaded_filename="testfilename.png")

    file_str_list = splitCount(fish_image_file,65520)

    with files.open(file_name, 'a') as f:
      for line in file_str_list:
        f.write(line)

splitCount()について確認できます。ここ

http://www.bdhwan.com/entry/gaewritebigfile

于 2011-08-18T11:31:45.307 に答える