How to set Container build path in Cloud Function?

46 Views Asked by At

I'm creating a Cloud Function that extracts data from an API and throws it into a bucket. When I run the build of my function, it says that it is not possible to access the container present in the location "US" (which my organization does not allow access to).

I already configured my function to run only in "southamerica" ​​but when the build is going to run it creates the container in some region "us" and when my function is going to access that container it doesn't have access.

As you can see here, my organization does not allow access to "us"

[Policy error] (https://i.stack.imgur.com/tCSJ8.png)

[Container Error] (https://i.stack.imgur.com/NkvgD.png)

My Cloud Function Code:

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!'

My config.json file (Used to deploy my function in cloud build)

{
"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"
}

I need to change my function's container creation path to "southamerica".

0

There are 0 best solutions below