python - How to write array of raw bytes to google cloud storage as binary file -
python - How to write array of raw bytes to google cloud storage as binary file -
i'm making ae app algorithmically produces raw binary file (mostly 16-bit bytes 32-bit header bytes) downloaded user. can produce array , write file in normal python using next simple code:
import numpy np import array 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, won't work in ae because writes not allowed, tried similar in gcs:
import cloudstorage 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 typeerror
, saying write()
expects string , nil else. note array
module's .tofile()
function not work ae/gcs.
is there way me write file this? understanding can't somehow encode string raw binary written correctly (not ascii or somesuch), want may impossible? :( blobstore different? there's similar(-sounding) question (here), it's not @ i'm trying do.
it's worth mentioning don't need write file -- need able serve user, if helps.
use array.tostring function:
gcs_file.write(testdata.tostring())
python google-app-engine google-cloud-storage binaryfiles blobstore
Comments
Post a Comment