I'm making an AE app that algorithmically produces a raw binary file (mostly 16-bit bytes with some 32-bit header bytes) which is then downloaded by the user. I can produce this array and write it to a file in normal python using the following simple code:
import numpy as np
import array as arr
toy_data_to_write = arr.array('h', np.zeros(10, dtype=np.int16))
output_file = open('testFile.xyz', 'wb')
toy_data_to_write.tofile(output_file)
Obviously, this won't work in AE because writes are not allowed, so I tried to do something similar in the GCS:
import cloudstorage as gcs
self.response.headers['Content-Type'] = 'application/octet-stream'
self.response.headers['Content-Disposition'] = 'attachment; filename=testFile.xyz'
testfilename = '/' + 'xyz-app.appspot.com' + '/testfile'
gcs_file = gcs.open(testfilename,'w',content_type='application/octet-stream')
testdata = arr.array('h', np.zeros(10, dtype=np.int16))
gcs_file.write(testdata)
gcs_file.close()
gcs_file = gcs.open(testfilename)
self.response.write(gcs_file.read())
gcs_file.close()
This code gives me a TypeError
, basically saying that write()
expects a string and nothing else. Note that the array
module's .tofile()
function does not work with AE/GCS.
Is there any way for me to write a file like this? My understanding is that I can't somehow encode a string to be raw binary that will be written correctly (not ASCII or somesuch), so what I want may be impossible? :( Is the Blobstore any different? Also there's a similar(-sounding) question (here), but it's not at all what I'm trying to do.
It's worth mentioning that I don't really need to write the file -- I just need to be able to serve it back to the user, if that helps.