Google Cloud Storage Transfer Python API如何获取TransferOperation状态?
谷歌云存储传输服务Python客户端API:检查TransferOperation状态方案
核心解决方法
- 直接获取操作实时状态:Python客户端支持
get_transfer_operation()方法,传入完整的TransferOperation名称(格式为projects/[你的项目ID]/transferOperations/[操作名称]),即可获取操作的进度、结果等核心状态信息。 - 实例化后刷新状态:你提到的
client.TransferOperation(<transfer_operation_name>)方式可行,但单纯实例化不会自动拉取最新数据,需调用refresh()方法同步状态,示例代码如下:
from google.cloud import storage_transfer # 初始化客户端 client = storage_transfer.StorageTransferServiceClient() # 方式一:直接获取最新状态 operation = client.get_transfer_operation("projects/your-project-id/transferOperations/your-operation-name") # 方式二:实例化后刷新 operation = client.TransferOperation("projects/your-project-id/transferOperations/your-operation-name") operation.refresh() # 查看操作状态与元数据 print(f"操作状态: {operation.status}") print(f"操作元数据: {operation.metadata}")
认证问题处理建议
如果之前调用原生REST API遇到认证问题,改用上述Python客户端方法时,只需确保正确配置谷歌云认证:
- 设置
GOOGLE_APPLICATION_CREDENTIALS环境变量,指向你的服务账号密钥文件路径; - 或者初始化客户端时显式指定密钥路径:
client = storage_transfer.StorageTransferServiceClient.from_service_account_json("path/to/key.json")。
内容的提问来源于stack exchange,提问作者Sam Korn
相关产品推荐
相关产品推荐

