【发布时间】:2021-06-03 23:19:22
【问题描述】:
这不是重复的帖子
我在 python 中从 GCS 存储桶读取 .gz(zip) 文件时遇到以下问题
文件名:ABC.dat.gz
内容 = 下载的_blob.read () 。 AttributeError: 'bytes' 对象没有属性 'read'
代码:
blob = bucket.blob('sftp/poc/ABC.dat.gz')
downloaded_blob = blob.download_as_string()
print(downloaded_blob)
content = downloaded_blob.read ()
buff = BytesIO (content) # put content into file object
f = gzip.GzipFile(fileobj=buff)
print('Lots of content here 8')
res = f.read().decode('utf-8')
print(res)
【问题讨论】:
-
您将 blob 作为字节对象下载。 byte 类型没有任何称为 read 的方法,这就是您收到错误的原因。你想读什么?您已经将 blob 读入字节对象
-
感谢克里斯。实际上我正在尝试从 GCS 读取 .gz 文件并打印其中的内容
-
但是你这里没有文件对象,你有一个字节对象。也许您想先下载 zip 文件然后解压缩。 googleapis.dev/python/storage/latest/…
-
谢谢Chirs,我正在尝试下载文件但它没有发生,你能帮我修改上面的代码吗
标签: python python-3.x google-cloud-platform google-cloud-storage