我正在尝试修改用户表单提交的一些二进制数据,并将其写入谷歌云存储。我试着关注 Google document's example , 但在写作时我遇到了错误,例如:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 34: ordinal not in range.
我的代码简单如下
gcs_file = gcs.open(filename,'w',content_type='audio/mp3')
gcs_file.write(buf)
gcs_file.close()
我尝试使用“wb”模式打开文件,但出现“无效模式 wb”。错误。
我在 GCS's maillist 发现了一个类似的问题这是在Java上。 GCS 开发团队的建议是使用 writeChannel.write() 而不是 PrintWriter。有人可以建议如何让它在 Python 中工作吗?
最佳答案
我想问题是 gcs_file.write() 方法需要“str”类型的数据。由于您的 buf 类型是“unicode”并且显然包含一些 Unicode 字符(可能在 ID3 标记中),您会得到 UnicodeDecodeError。所以你只需要将 buf 编码为 UTF-8:
gcs_file = gcs.open(filename,'w',content_type='audio/mp3')
gcs_file.write(buf.encode('utf-8'))
gcs_file.close()
关于python - 如何使用 GAE 的 Python API 将原始字节写入谷歌云存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18290658/