Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将原始字节数组作为二进制文件写入google云存储_Python_Google App Engine_Google Cloud Storage_Binaryfiles_Blobstore - Fatal编程技术网

Python 如何将原始字节数组作为二进制文件写入google云存储

Python 如何将原始字节数组作为二进制文件写入google云存储,python,google-app-engine,google-cloud-storage,binaryfiles,blobstore,Python,Google App Engine,Google Cloud Storage,Binaryfiles,Blobstore,我正在制作一个AE应用程序,通过算法生成一个原始二进制文件(主要是16位字节和一些32位头字节),然后由用户下载。我可以使用以下简单代码生成此数组并将其写入普通python中的文件: 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_

我正在制作一个AE应用程序,通过算法生成一个原始二进制文件(主要是16位字节和一些32位头字节),然后由用户下载。我可以使用以下简单代码生成此数组并将其写入普通python中的文件:

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)
显然,这在AE中不起作用,因为不允许写入,所以我尝试在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()
这段代码给了我一个
TypeError
,基本上是说
write()
需要一个字符串,而不需要其他任何东西。请注意,
阵列
模块的
.tofile()
功能不适用于AE/GCS

我有没有办法写这样的文件?我的理解是,我无法以某种方式将字符串编码为正确写入的原始二进制文件(不是ASCII或类似文件),因此我想要的可能是不可能的:(Blobstore有什么不同吗?还有一个类似的问题(),但这根本不是我想要做的

值得一提的是,我并不真的需要编写该文件——我只需要能够将其返回给用户,如果这有帮助的话。

使用该函数:

gcs_file.write(testdata.tostring())