能否使用Python Requests库替代httplib2访问Google Cloud Storage?
用Python Requests库访问Google Cloud Storage存储桶的方案
当然可以用Python Requests库直接发起HTTP请求访问Google Cloud Storage!完全能替代你原来用httplib2的实现,下面我给你拆解具体步骤和代码:
核心思路
GCS的REST API需要OAuth2身份验证,所以第一步得先通过服务账号获取有效的访问令牌,之后把这个令牌放到请求头里,用Requests发送GET请求到对应的GCS API端点就行。
具体操作步骤&代码示例
1. 先准备好依赖
确保你已经安装了需要的库:
pip install requests oauth2client python-jose[cryptography]
(注:python-jose是用来处理JWT签名的,oauth2client依赖它完成身份验证)
2. 完整实现代码
import json import requests from urllib.parse import quote from oauth2client.client import SignedJwtAssertionCredentials # 配置信息(和你原来的代码保持一致) client_email = 'my.iam.gserviceaccount.com' json_file = 'services.json' cloud_storage_bucket = 'my_bucket' file_path = 'reviews/reviews_myapp_201603.csv' # 加载服务账号私钥 with open(json_file, 'r') as f: private_key = json.loads(f.read())['private_key'] # 生成OAuth2凭证并获取访问令牌 credentials = SignedJwtAssertionCredentials( client_email, private_key, scope='https://www.googleapis.com/auth/devstorage.read_only' ) # 获取有效访问令牌 access_token = credentials.get_access_token().access_token # 构造GCS API的请求URL(注意对文件路径做URL编码) encoded_file_path = quote(file_path, safe='') gcs_api_url = f'https://storage.googleapis.com/storage/v1/b/{cloud_storage_bucket}/o/{encoded_file_path}' # 用Requests发送GET请求 headers = {'Authorization': f'Bearer {access_token}'} response = requests.get(gcs_api_url, headers=headers) # 处理响应结果 if response.status_code == 200: print("成功获取文件信息:") print(json.dumps(response.json(), indent=2)) else: print(f"请求失败,状态码:{response.status_code}") print(response.text)
额外提示:推荐使用新版身份验证库
oauth2client其实已经被Google弃用了,现在官方推荐用google-auth和google-auth-oauthlib库,如果你想升级的话,代码可以改成这样:
import json import requests from urllib.parse import quote from google.oauth2 import service_account # 配置信息不变 client_email = 'my.iam.gserviceaccount.com' json_file = 'services.json' cloud_storage_bucket = 'my_bucket' file_path = 'reviews/reviews_myapp_201603.csv' # 用新版库生成凭证 credentials = service_account.Credentials.from_service_account_file( json_file, scopes=['https://www.googleapis.com/auth/devstorage.read_only'] ) # 获取访问令牌 access_token = credentials.token # 后面的请求部分和上面完全一致 encoded_file_path = quote(file_path, safe='') gcs_api_url = f'https://storage.googleapis.com/storage/v1/b/{cloud_storage_bucket}/o/{encoded_file_path}' headers = {'Authorization': f'Bearer {access_token}'} response = requests.get(gcs_api_url, headers=headers) # 处理响应...
这样就完全用Requests替代了原来的httplib2+googleapiclient的组合,逻辑更直观,也符合你想要的HTTP请求方式。
内容的提问来源于stack exchange,提问作者Samyak Jain
相关产品推荐
相关产品推荐

