Search code examples
google-app-enginepython-2.7google-cloud-datastoreblobstoreurlfetch

Getting a file with urlfetch and saving it to the Google datastore


I have read Open a file from urlfetch in GAE and Google App Engine, How to make native file object? but still can't figure out how to save a .pdf file from an URL to the Google datastore or blobstore. Any hint or a pointer to a working example?

I'm using Python and my code is essentially the one from https://developers.google.com/appengine/docs/python/urlfetch/?hl=it-IT&csw=1

from google.appengine.api import urlfetch

url = "http://www.gutenberg.org/files/1342/1342-pdf.pdf"
result = urlfetch.fetch(url)
if result.status_code == 200:
  doSomethingWithResult(result.content)

Solution

  • something like

    First your model to store the PDF in

    class PDFStore(ndb.Model):
      content = ndb.BlobProperty()
    

    The create an instance of that model, put the data into it the save it.

    def doSomethingWithResult(result):
        PDF = result.content
    
        blobstore_data = PDFStore()
    
        blobstore_data.content = PDF
    
        blobstore_data.put()
    

    It's exactly as in the first link you put up: Open a file from urlfetch in GAE