如何设置Cloud Function的容器构建路径以规避美国区域访问限制?
解决Cloud Function构建容器区域错误问题
我正在创建一个从API提取数据并存入存储桶的Cloud Function。运行函数构建时,系统提示无法访问位于美国区域的容器——我的组织禁止访问该区域。
我已将函数配置为仅在南美区域(southamerica-east1)运行,但构建时容器仍会创建在美国区域,导致函数无法访问该容器。
组织的区域访问限制及容器创建错误情况:
- 政策错误截图:

- 容器错误截图:

我的Cloud Function代码
import requests import json import csv import hashlib import os import time from google.cloud import storage bucket_name = os.environ['BUCKET_ID'] def main(request): request_json = request.get_json() url = request_json['url'] type_file = request_json['type'] blob_from_config = request_json['blob'] storage_client = storage.Client() source_bucket = storage_client.bucket(bucket_name) blob = source_bucket.get_blob(f"config/config.json") read_output = blob.download_as_text() clean_data = json.loads(read_output) api_path = clean_data['api_path'] if type_file == 'json': try: response = requests.get(url) if response.status_code == 200: source_bucket_output = storage_client.bucket(clean_data['bucket']) hash = hashlib.md5(str(time.time()).encode()) filename = f"file_{hash.hexdigest()}.json" blob_output = source_bucket_output.blob(f"{api_path}/{blob_from_config}/{filename}") blob_output.upload_from_string( data=json.dumps(response.json()), content_type='application/json' ) print('****** File Created:', blob_output) except Exception as e: print('****** Exception JSON:', e) elif type_file == 'csv': try: response = requests.get(url) if response.status_code == 200: source_bucket_output = storage_client.bucket(clean_data['bucket']) hash = hashlib.md5(str(time.time()).encode()) filename = f"file_{hash.hexdigest()}.csv" blob_output = source_bucket_output.blob(f"{api_path}/{blob_from_config}/{filename}") blob_output.upload_from_string( data=csv.writer(response.text), content_type='application/CSV' ) print('****** File Created:', blob_output) except Exception as e: print('****** Exception CSV:', e) return 'End of process!'
部署用config.json文件
{ "project_id": "PROJECT_ID", "bucket": "BUCKET", "api_path": "pipeline_api", "location": "southamerica-east1", "region": "southamerica-east1", "api_uri": "https://southamerica-east1-PROJECT_ID.cloudfunctions.net/api_teste", "endpoints": [ { "url":"https://jsonplaceholder.typicode.com/posts", "type": "json", "blob": "api_teste/load" } ], "project_id_bq": "BQ_PROJECT", "dataset_name": "DATA_SET_NAME", "retention_data": "172800" }
我需要将函数的容器创建路径改为南美区域。
解决方案
Cloud Function的运行区域和构建区域是分开配置的,你已经设置了运行区域,但构建阶段默认使用美国区域的容器仓库,需单独指定构建区域:
1. gcloud命令行部署指定构建区域
部署时添加--build-region参数,明确指定南美区域:
gcloud functions deploy api_teste \ --runtime python39 \ --region southamerica-east1 \ --build-region southamerica-east1 \ --trigger-http \ --set-env-vars BUCKET_ID=你的存储桶ID
2. Cloud Build配置文件指定区域
若使用Cloud Build自动化部署,创建cloudbuild.yaml并设置构建区域:
steps: - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk' args: - functions - deploy - api_teste - --runtime=python39 - --region=southamerica-east1 - --trigger-http - --set-env-vars=BUCKET_ID=你的存储桶ID options: region: southamerica-east1
3. 设置项目级Cloud Build默认区域
进入Cloud Build控制台 → 「设置」→ 「常规」→ 「默认区域」,选择southamerica-east1,后续所有构建都会默认使用该区域。
注意:config.json中的location和region仅控制函数运行位置,不影响构建阶段的容器创建区域,必须通过上述方式单独配置构建区域。
内容的提问来源于stack exchange,提问作者Gabriel Castro
相关产品推荐
相关产品推荐

