谷歌云函数 - Python - 将存储桶中的文件解压缩到同一个存储桶

时间:2021-05-20 19:43:50

标签: python-3.x google-cloud-platform unzip bucket

我正在尝试通过 Python (3.7) 中的谷歌云函数将已经在谷歌存储桶中的文件解压缩到同一个存储桶。解压缩必须在存储桶中完成:不能下载/上传。

首先有一个模块来列出存储桶中的文件:

rd_client = storage.Client()
bu_rd_bucket = rd_client.get_bucket(current_bucket)
blobs = bu_rd_bucket.list_blobs()
for blob in blobs:

我尝试了两种方法:

1- 使用 zipfile 库:

if len(re.findall('xxxxx(.*).zip', str(blob.name)))>0:
      with zipfile.ZipFile('gcs://{}/{}'.format(current_bucket, blob.name), 'r') as zip_ref:
          zip_ref.extractall('gcs://' + current_bucket)***

使用此脚本,我收到一条错误消息:FileNotFoundError: [Errno 2] No such file or directory: 'gcs://xxxxxxxxxxxxxxx.zip'(我使用绝对或相对文件路径得到相同的结果)

2- 使用shutil 库:

if len(re.findall('xxxxx(.*).zip', str(blob.name)))>0:
     shutil.unpack_archive(blob.name, current_bucket)

使用此脚本,我收到一条错误消息:shutil.ReadError: xxxxxxxxxxxxxxxxxxxxxxx.zip is not a zip file

我已经在我的本地机器上尝试了这两个脚本,并且运行良好。

我在谷歌上没有找到关于这个具体问题的太多相关信息。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

<块引用>

解压必须在存储桶中完成:不能下载/上传。

您的要求是不可能的。 Cloud Storage 不提供解压缩对象所需的计算处理功能。