简体   繁体   English

我如何将变量传递给谷歌云函数

[英]How can i pass variable to a google cloud function

I am currently creating a cloud task which will import new data to an automl dataset periodically.我目前正在创建一个云任务,它将定期将新数据导入到 automl 数据集。 The target is a GCP cloud function http target.目标是 GCP 云函数 http 目标。 As I don't want to hard code the dataset ID in the cloud function.因为我不想在云函数中硬编码数据集 ID。 I want it to accept the dataset id from the web UI.我希望它接受来自 Web UI 的数据集 ID。 Therefore I type the code for flask in this way.因此,我以这种方式键入了flask 的代码。

@app.route('/train_model', methods=["POST", "GET"])
def train_model():
    if request.method == 'POST':
        form = request.form
        model = form.get('model_name')
        date = form.get('date')
        dataset_id=form.get('dataset_id')
        datetime_object = datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
        timezone = pytz.timezone('Asia/Hong_Kong')
        timezone_date_time_obj = timezone.localize(datetime_object)

        # Create a client.
        client = tasks_v2beta3.CloudTasksClient.from_service_account_json(
            "xxx.json")

        # TODO(developer): Uncomment these lines and replace with your values.
        project = 'xxxx'
        dataset_id = dataset_id
        utf = str(dataset_id, encoding='utf-8')
        location = 'us-west2'
        url='https://us-central1-cloudfunction.cloudfunctions.net/create_csv/' + "?dataset_id=dataset_id"

        queue1 = 'testing-queue'

        parent = client.queue_path(project, location, queue1)
        task = {
            "http_request": {
                'http_method': 'POST',
                'url': url
            }}

        # set schedule time
        timestamp = timestamp_pb2.Timestamp()
        timestamp.FromDatetime(timezone_date_time_obj)
        task['schedule_time'] = timestamp
        response = client.create_task(parent, task)

        print(response)
        return redirect(url_for('dataset'))

cloud function code云函数代码

import pandas
from google.cloud import datastore
from google.cloud import storage
from google.cloud import automl

project_id=123456995
compute_region='us-central1'


def create_csv(dataset_id):

    datastore_client = datastore.Client() #
    data = datastore_client.query(kind='{}label'.format(dataset_id))
    storage_client = storage.Client()
    metadata = list(data.fetch())
    path = []
    label = []
    for result in metadata:
        path.append(result['Storage_url'])
        label.append(result['label'])
    record = {
        'Path': path,
        'Label': label
    }
    table = pandas.DataFrame(record)
    csv_pandas = table.to_csv('/tmp/label.csv', header=None, index=None) #create csv through query datatore

    # upload to cloud storage bucket

    bucket_name1='testing'
    destination_blob_name='label.csv'

    bucket = storage_client.bucket(bucket_name1)
    blob = bucket.blob(destination_blob_name)
    blob.upload_from_filename('/tmp/label.csv')

    object = bucket.get_blob(destination_blob_name)
    bucket = object.bucket
    bucket_name = bucket.name
    url = 'gs://' + bucket_name + '/' + object.name

      #import data to the dataset
    client= automl.AutoMlClient()
    dataset_full_id = client.dataset_path(
        project_id, "us-central1", dataset_id
    )
    # Get the multiple Google Cloud Storage URIs
    input_uris = url.split(",")
    gcs_source = automl.types.GcsSource(input_uris=input_uris)
    input_config = automl.types.InputConfig(gcs_source=gcs_source)
    # Import data from the input URI
    client.import_data(dataset_full_id, input_config)    

but it gives me this error.但它给了我这个错误。

Traceback (most recent call last):
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 346, in run_http_function
    result = _function_handler.invoke_user_function(flask.request)
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 217, in invoke_user_function
    return call_user_function(request_or_event)
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 210, in call_user_function
    return self._user_function(request_or_event)
  File "/user_code/main.py", line 52, in create_csv
    client.import_data(dataset_full_id, input_config)
  File "/env/local/lib/python3.7/site-packages/google/cloud/automl_v1/gapic/auto_ml_client.py", line 793, in import_data
    request, retry=retry, timeout=timeout, metadata=metadata
  File "/env/local/lib/python3.7/site-packages/google/api_core/gapic_v1/method.py", line 143, in __call__
    return wrapped_func(*args, **kwargs)
  File "/env/local/lib/python3.7/site-packages/google/api_core/retry.py", line 286, in retry_wrapped_func
    on_error=on_error,
  File "/env/local/lib/python3.7/site-packages/google/api_core/retry.py", line 184, in retry_target
    return target()
  File "/env/local/lib/python3.7/site-packages/google/api_core/timeout.py", line 214, in func_with_timeout
    return func(*args, **kwargs)
  File "/env/local/lib/python3.7/site-packages/google/api_core/grpc_helpers.py", line 59, in error_remapped_callable
    six.raise_from(exceptions.from_grpc_error(exc), exc)
  File "<string>", line 3, in raise_from
google.api_core.exceptions.InvalidArgument: 400 List of found errors:   1.Field: name; Message: Required field is invalid

To be able to call a cloud function with arguments from App Engine Application:为了能够使用来自 App Engine 应用程序的参数调用云函数:

  1. Create a cloud function that allows unauthenticated function invocation创建允许未经身份验证的函数调用的云函数

  2. Enable CORS requests启用CORS 请求

import requests
import json

from flask import escape

def hello_http(request):

    # Set CORS headers for the preflight request
    if request.method == 'OPTIONS':
        # Allows GET requests from any origin with the Content-Type
        # header and caches preflight response for an 3600s
        headers = {
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'GET',
            'Access-Control-Allow-Headers': 'Content-Type',
            'Access-Control-Max-Age': '3600'
        }

        return ('', 204, headers)

    # Set CORS headers for the main request
    headers = {
        'Access-Control-Allow-Origin': '*'
    }



    request_json = request.get_json(silent=True)
    request_args = request.args

    if request_json and 'dataset_id' in request_json:
        dataset_id = request_json['dataset_id']
    elif request_args and 'dataset_id' in request_args:
        dataset_id = request_args['dataset_id']
    else:
        dataset_id = 'default'

    print('Function got called with dataset id {}'.format(escape(dataset_id)))

    return 'This is you dataset id {}!'.format(escape(dataset_id), 200, headers )
  1. Deploy the quickstart for Python 3 in the App Engine Standard Environment 在 App Engine 标准环境中部署Python 3 快速入门
from flask import Flask
import requests
import json



# If `entrypoint` is not defined in app.yaml, App Engine will look for an app
# called `app` in `main.py`.
app = Flask(__name__)


@app.route('/')
def hello():
    """Return a friendly HTTP greeting."""
    response = requests.post('https://us-central1-your-project.cloudfunctions.net/function-2', data=json.dumps({"dataset_id":"m_your_dataset_id"}), headers={'Accept': 'application/json','Content-Type': 'application/json'})
    return  'Response {}'.format(str(response))


4.Pass all your parameters to data in our case we pass dataset_id = m_your_dataset_id 4.将您的所有参数传递给data在我们的情况下,我们传递dataset_id = m_your_dataset_id

5.Call the function by accessing https://your-project.appspot.com 5.通过访问https://your-project.appspot.com调用该函数

6.Check the logs: 6.检查日志:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Python (Kivy) 中将变量从函数传递到函数 - How can I pass a variable from function to function in Python (Kivy) 如何保留原始字符串(作为变量)以便我可以将其传递给 function? - How to keep a raw string (as a variable) so that I can pass it to a function? python:如何仅使用字符串名称将变量传递给函数 - python: how can I pass a variable to a function with only the string name 如何将当前变量值传递给python函数? - How can I always pass the current variable value into a python function? 如何从谷歌云 Function 的代码更改环境变量的值? - How to change the value of an Environment variable from the code of Google Cloud Function? 如何将作为函数列表的输出传递给我可以在其他函数中使用的变量? - How can I pass the output that is a list of a function into a variable that I can use in other functions? 如何从 Google App Engine 中触发 HTTP Cloud Function? - How can I trigger an HTTP Cloud Function from within Google App Engine? 如何将函数中的变量传递给装饰器 - How do i pass a variable in a function to a decorator 如何调用 arguments 并从 Google 云托管的 function 返回输出? - How can I call arguments and return the outputs from a Google cloud hosted function? 如何检索 id_token 以访问 Google Cloud Function? - How can I retrieve an id_token to access a Google Cloud Function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM