In case this helps anyone else, here is how I got it to work: response.headers['Content-Type'] = 'application/octet-stream' response.headers['Content-Disposition'] = 'attachment;filename=%s' % file_name file_reader = cloudstore.open(file_path) return file_reader.read()
any other attempts to use response.write or response.body kept returning 'None' On Monday, October 16, 2017 at 6:09:07 AM UTC-5, Julian Sanchez wrote: > > I'm in the process of 'upgrading' an old Web2py application hosted on GAE > that performs a fair amount of file manipulation. The original version > used the blobstore to create files, and the 'default/download' function was > modified to serve those blobs like this (abbreviated): > > blob_key = request.args[0] > blob_info = blobstore.get(blob_key) > > response.headers['X-AppEngine-BlobKey'] = blob_key; > response.headers['Content-Type'] = blob_info.content_type; > response.headers['Content-Disposition'] = "attachment; > filename=%s" % blob_info.filename > > blob_reader = blob_info.open() > response.stream(blob_reader) > > Blobstore is now gone so I changed the code to use a GCS bucket. > Everything works well everywhere in the application as far as uploading and > creating files internally, but when I want to offer those files for > download I can't get it to work. As you know, files on GCS buckets are > accessible via their path (i.e. cloudstore.open(file_path)) however if I > try something like: > > file_reader = cloudstore.open(file_path) > response.stream(file_reader) > > all I get is a blank webpage with 'None' written on it, and no file. > I can't (and don't want to) offer the actual file_path for download > straight to the user because the bucket permissions are private and web2py > is the only one authorized to access it. > > I've tried a number of combinations (setting headers with content-type and > content-disposition versus no headers, response.write versus > response.stream, reading file to StringIO object and try to stream/write > that object instead) but none of them work. I'm at a point where I'm > blindly trying different code combinations. Does anybody have a working > code example for this? > > Thanks, > Julian > > -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.