2011-04-12 7 views
11

मुझे दुविधा है .. मैं फ्रेमवर्क के रूप में tipfy का उपयोग कर scribd स्टोर और blobstore दोनों में फाइल अपलोड कर रहा हूँ। मेरे पास क्रिया के साथ वेबफॉर्म है blobstore.create_upload_url द्वारा नहीं बनाया गया है (मैं सिर्फ url_for ('myhandler') का उपयोग कर रहा हूं)। मैंने ऐसा इसलिए किया क्योंकि अगर मैं ब्लॉबस्टोर हैंडलर का उपयोग कर रहा हूं तो POST प्रतिक्रिया पार्स की गई है और मैं स्क्रिड स्टोर में फ़ाइल अपलोड करने के लिए सामान्य पायथन-स्क्रिब एपीआई का उपयोग नहीं कर सकता। अब मैं काम कर रहा है स्क्रिप्ड सेवर:प्रयोगात्मक एपीआई का उपयोग करके ब्लॉबस्टोर में बड़ी फ़ाइलों को कैसे लिखें?

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

आप इसे देख सकते हैं भी Blobstore में फ़ाइल की बचत होती है .. लेकिन अगर मैं बड़ा फ़ाइल को अपलोड कर रहा हूँ (यानी 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')) 

प्राप्त कर रहा हूँ मेरे द्वारा यह कैसे किया जा सकता है? धन्यवाद!

+0

आपके प्रश्न और इसके उत्तरों ने मुझे बहुत मदद की, चीयर्स! – selurvedu

उत्तर

7

आप इस तरह उदाहरण के लिए, फ़ाइल एपीआई के लिए कई छोटे कॉल करने के लिए की जरूरत है:

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 एप्लिकेशन के लिए नियमित रूप अनुरोधों पर पेलोड आकार सीमा 10MB है; अगर आप बड़ी फाइलें अपलोड करना चाहते हैं, तो आपको नियमित ब्लॉबस्टोर अपलोड तंत्र का उपयोग करना होगा।

+0

अपने उदाहरण कोड का उपयोग करके, क्या आप सोच सकते हैं कि यह एक विशेषता त्रुटि क्यों देगा - 'InMemoryUploadedFile' ऑब्जेक्ट में कोई विशेषता नहीं है 'eof'? (आपके उदाहरण की दूसरी पंक्ति पर) – oldpal

+0

@bfox संभवतः क्योंकि इसमें वह विशेषता नहीं है। मैं एक विकल्प के साथ अपना जवाब अपडेट करूंगा। –

+0

@minus क्या आप इस के साथ काम करने के लिए आए थे? ब्लॉबस्टोर में 3-4 एमबी फ़ाइल अपलोड करने का प्रयास करते समय आपके पास एक ही समस्या है। –

6

अंततः मुझे समाधान मिला।

निक जॉन्सन के उत्तर में विशेषता त्रुटि हुई क्योंकि upload_file को स्ट्रिंग के रूप में माना जाता है। स्ट्रिंग में पढ़ा नहीं गया था() विधि।

कारण स्ट्रिंग में विधि पढ़ने की विधि नहीं है(), मैंने फ़ाइल स्ट्रिंग को विभाजित किया और इसे लिखने जैसा लिखा।

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

संबंधित मुद्दे