N. Rosencrantz
unread,Nov 22, 2011, 5:21:41 PM11/22/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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