如何使用Python客户端API下载含特殊字符的GCS云存储桶文件?
问题:Windows系统无法下载GCS中含竖线|的文件
我使用Windows系统,能够正常下载文件名不含特殊字符的GCS文件,但无法下载含竖线|的文件,例如文件名:BRX_23022023|00000.csv。
我编写的代码可根据bucketname和foldername变量创建对应文件夹结构,但遇到含特殊字符的文件时,仅创建文件夹却无法下载文件,本地路径示例:C:\adt-dev-outputs\archive\2023\02。
代码示例
import os from google.cloud import storage from os import makedirs cred_json_file_path = 'C:/Users/config.json' client = storage.Client.from_service_account_json(cred_json_file_path) def download_blob(bucket: storage.Bucket, remotefile: str, localpath: str='.'): """downloads from remotepath to localpath""" localrelativepath = '/'.join(remotefile.split('/')[:-1]) totalpath = f'{localpath}/{localrelativepath}' filename = f'{localpath}/{remotefile}' makedirs(totalpath, exist_ok=True) print(f'Current file details:\n remote file: {remotefile}\n local file: {filename}\n') blob = storage.Blob(remotefile, bucket) blob.download_to_filename(filename, client=client) def download_blob_list(bucketname: str, bloblist: list, localpath: str='.'): """downloads a list of blobs to localpath""" bucket = storage.Bucket(client, name=bucketname) for blob in bloblist: download_blob(bucket, blob, localpath) def list_blobs(bucketname: str, remotepath: str=None, filetypes: list=[]) -> list: """returns a list of blobs filtered by remotepath and filetypes remotepath and filetypes are optional""" result = [] blobs = list(client.list_blobs(bucketname, prefix=remotepath)) for blob in blobs: name = str(blob.name) # skip "folder" names if not name.endswith('/'): # do we need to filter file types? if len(filetypes) > 0: for filetype in filetypes: if name.endswith(filetype): result.append(name) else: result.append(name) return result bucketname = 'adt-dev-outputs' foldername = 'archive/2023/02' filetypes = ['.csv'] # list of extentions to return bloblist = list_blobs(bucketname, remotepath=foldername, filetypes=filetypes) download_blob_list(bucketname, bloblist, localpath=bucketname)
内容的提问来源于stack exchange,提问作者Suj
相关产品推荐
相关产品推荐

