You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Cloud Storage解压参数化文件名遇match_glob参数错误求助

问题原因

你使用的google-cloud-storage==2.5.0版本中,list_blobs()方法并没有match_glob参数——这个参数是在2.10.0版本才新增的,所以会抛出TypeError。


两种解决办法

方案1:升级库版本

修改requirements.txt中的版本声明:

google-cloud-storage>=2.10.0

然后重新升级依赖:

pip install -r requirements.txt --upgrade

升级后可直接使用match_glob,但注意修正你代码里的两处错误:

import io
from google.cloud import storage

def fileUnzip(data,content):
    """
    Decompression logic to unzip a file based on the compression format
    """
    storage_client = storage.Client('projectname')
    source_bucket = storage_client.get_bucket("bucket-1")
    output_bucket = storage_client.get_bucket("bucket-2")
    sourceFileObject = "tm*.zip"
    
    # 使用match_glob筛选文件
    blobs = source_bucket.list_blobs(match_glob=sourceFileObject)
    # 遍历每个匹配到的blob(blobs是迭代器,不能直接调用download_as_string)
    for blob in blobs:
        compressedData = io.BytesIO(blob.download_as_string())
        # 这里添加你的解压逻辑

方案2:不升级版本,手动实现通配符匹配

如果不想升级库,可结合prefix缩小遍历范围,再用fnmatch模块实现通配符匹配:

import io
import fnmatch
from google.cloud import storage

def fileUnzip(data,content):
    """
    Decompression logic to unzip a file based on the compression format
    """
    storage_client = storage.Client('projectname')
    source_bucket = storage_client.get_bucket("bucket-1")
    output_bucket = storage_client.get_bucket("bucket-2")
    sourceFileObject = "tm*.zip"
    
    # 提取前缀减少遍历量
    prefix = sourceFileObject.split("*")[0]
    # 获取前缀匹配的所有blobs
    blobs = source_bucket.list_blobs(prefix=prefix)
    # 用fnmatch筛选符合tm*.zip规则的文件
    matching_blobs = [blob for blob in blobs if fnmatch.fnmatch(blob.name, sourceFileObject)]
    
    for blob in matching_blobs:
        compressedData = io.BytesIO(blob.download_as_string())
        # 这里添加你的解压逻辑

内容的提问来源于stack exchange,提问作者jay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 23:35:00