python - How to write Big files into Blobstore using experimental API? -


i have dilemma.. i'm uploading files both in scribd store , blobstore using tipfy framework. have webform action not created blobstore.create_upload_url (i'm using url_for('myhandler')). did because if i'm using blobstore handler post response parsed , cannot use normal python-scribd api upload file scribd store. have working scribd saver:

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:             # ... exception message ,             msg = e.message             # ...         # reset stream 0 (beginning) file can read again         uploaded_file.seek(0)         #removed try-except see debug info in browser window         # create file          file_name = files.blobstore.create(_blobinfo_uploaded_filename=fname)         # open file , write         files.open(file_name, 'a') f:             f.write(uploaded_file.read())         # finalize file. before attempting read it.               files.finalize(file_name)         # 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 not allowed 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 uploaded @ ' + 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') 

as can see saves file blobstore.. if i'm uploading big file (i.e. 5mb) i'm receiving

requesttoolargeerror: request api call file.append() 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')) 

how can fix it? thanks!

you need make multiple, smaller calls file api, instance this:

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

note payload size limit on regular requests app engine apps 10mb; if want upload larger files, you'll need use regular blobstore upload mechanism.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -