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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 08:36:19