Serve blobstore as static file

85 views
Skip to first unread message

Timofey Koolin

unread,
Nov 22, 2011, 4:04:26 PM11/22/11
to Google Appengine
Can I make url for users can serve files from blobstore directly, without reading it by script?
It can be any binary data - images (i know about images.get_serving_url) and not images.
--
Blog: www.rekby.ru

N. Rosencrantz

unread,
Nov 22, 2011, 5:21:41 PM11/22/11
to google-a...@googlegroups.com, alex.h...@gmail.com, kim...@gmail.com, rober...@gmail.com, shan...@gmail.com, kyriakos.ko...@gmail.com, staffan....@gmail.com
Have you considered BlobstoreDownloadHandler? Using it is straighforward:

class FileDownloadHandler(blobstore_handlers.BlobstoreDownloadHandler):
 
def get(self, file_id):
    file_info
= FileInfo.get_by_id(long(file_id))
   
if not file_info or not file_info.blob:
     
self.error(404)
     
return
   
self.send_blob(file_info.blob, save_as=True)

I use a FileInfo class and I can recommend it.
A good way to serve blobs I think is a FileInfo class for instance

class FileInfo(db.Model):
  blob
= blobstore.BlobReferenceProperty(required=True)
  uploaded_by
= db.UserProperty(required=True)
  uploaded_at
= db.DateTimeProperty(required=True, auto_now_add=True)

Then you can conveniently serve it as if it were a static file ie. /file/1001
along the structure file/<file.id> :

class FileInfoHandler(BaseHandler):
 
def get(self, file_id):
    file_info
= FileInfo.get_by_id(long(file_id))
   
if not file_info:
     
self.error(404)
     
return
   
self.render_template("info.html", {
       
'file_info': file_info,
       
'logout_url': users.create_logout_url('/'),
   
})
You may want to look at Nick Johnson's example for implementation with the blobstore
http://blog.notdot.net/2010/03/Implementing-a-dropbox-service-with-the-Blobstore-API-Part-1
I think the example file serving app from Nick Johnson is good and if you use python and blobstore maybe you want to follow the link.
My opinion is that serving and uploading files should be built into the framework but it is not. Instead we have to solve the same problem repeatedly: File uploads and file serving.

Better to solve uploads and serving once and for all and the FileInfo class is so little boilerplate code I can add it to most of my apps since most of my apps need the functionality "add a file to the blobstore" and "serve a file from the blobstore" so if you ask me I say that these functions should be possible without writing your own app just as part of the app server.

Regards,
Niklas

Jeff Schnitzer

unread,
Nov 22, 2011, 5:57:14 PM11/22/11
to google-a...@googlegroups.com
Does this approach get the same edge caching benefit that image_serving_url links get?  The image links point directly at a CDN domain.

Jeff

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine/-/PilliI2r9eoJ.

To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.



--
I am the 20%

Timofey Koolin

unread,
Nov 22, 2011, 11:41:27 PM11/22/11
to google-a...@googlegroups.com
Yes, I know way with download_handler. I want do it without my script: have static difficult url and download direct by url - as images.

Now I select between blobstore with self download_handler and Google Storage for developers.

2011/11/23 Jeff Schnitzer <je...@infohazard.org>



--
Blog: www.rekby.ru
Reply all
Reply to author
Forward
0 new messages