简体   繁体   English

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

[英]How to write array of raw bytes to google cloud storage as binary file

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. 我正在制作一个AE应用程序,该应用程序通过算法生成原始二进制文件(大多数为16位字节,有些为32位头字节),然后由用户下载。 I can produce this array and write it to a file in normal python using the following simple code: 我可以使用以下简单代码生成此数组并将其写入普通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)

Obviously, this won't work in AE because writes are not allowed, so I tried to do something similar in the GCS: 显然,这在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()

This code gives me a TypeError , basically saying that write() expects a string and nothing else. 这段代码给了我TypeError ,基本上是说write()需要一个字符串,而没有别的。 Note that the array module's .tofile() function does not work with AE/GCS. 请注意, array模块的.tofile()函数不适用于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? 我的理解是,我无法以某种方式将字符串编码为可以正确写入的原始二进制文件(而不是ASCII或类似字符),所以我想要的是不可能的吗? :( 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. :( Blobstore有什么不同吗?还有一个类似的(听起来)问题( 在这里 ),但这根本不是我想要做的。

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. 值得一提的是,我真的不需要编写文件,只要有帮助,我只需要能够将其提供给用户即可。

使用array.tostring函数:

gcs_file.write(testdata.tostring())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用GAE的Python API将原始字节写入Google云存储 - How to write raw bytes to Google cloud storage with GAE's Python API 使用NumPy数组数据编写原始二进制文件 - Write a raw binary file with NumPy array data 如何使用 python 将原始 json 数据 [作为 pyhon 字典] 写入 Google 云存储? - How to write raw json data [as pyhon Dictionary] to Google cloud storage using python? 如何将ASCII字节文件写入二进制文件作为实际字节? - How to write a file of ASCII bytes to a binary file as actual bytes? 如何使用带有 Python 的 Google Cloud Functions 将列表写入 Google Cloud Storage 中的文件 - How to write a list to a file in Google Cloud Storage using Google Cloud Functions with Python 如何从Python脚本上传Google Cloud Storage上的字节图像 - How to upload a bytes image on Google Cloud Storage from a Python script 如何在云端 function 的谷歌存储桶中以二进制模式打开文件? - How to open a file in binary mode in google storage bucket from cloud function? 如何通过Google App Engine中的应用程序将文件写入Google Cloud Storage? - How do I write a file through my app in the Google App Engine to the Google Cloud Storage? 将 csv 写入谷歌云存储 - Write csv to google cloud storage 将.raw文件上传到Google Cloud Storage - Upload .raw files to Google Cloud Storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM