Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将变量传递给google cloud函数_Python_Api_Google Cloud Platform_Google Cloud Functions_Automl - Fatal编程技术网

Python 如何将变量传递给google cloud函数

Python 如何将变量传递给google cloud函数,python,api,google-cloud-platform,google-cloud-functions,automl,Python,Api,Google Cloud Platform,Google Cloud Functions,Automl,我目前正在创建一个云任务,它将定期将新数据导入automl数据集。目标是GCP云函数http目标。因为我不想在云函数中硬编码数据集ID。我希望它从web UI接受数据集id。因此,我以这种方式键入flask的代码 @app.route('/train_model', methods=["POST", "GET"]) def train_model(): if request.method == 'POST': form = request.form mod

我目前正在创建一个云任务,它将定期将新数据导入automl数据集。目标是GCP云函数http目标。因为我不想在云函数中硬编码数据集ID。我希望它从web UI接受数据集id。因此,我以这种方式键入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'))
云函数代码

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)    
但它给了我这个错误

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
回溯(最近一次呼叫最后一次):
文件“/env/local/lib/python3.7/site packages/google/cloud/functions/worker.py”,第346行,在run\u http\u函数中
结果=\函数\处理程序。调用\用户\函数(flask.request)
文件“/env/local/lib/python3.7/site packages/google/cloud/functions/worker.py”,第217行,在invoke\u user\u函数中
返回调用用户函数(请求或事件)
文件“/env/local/lib/python3.7/site packages/google/cloud/functions/worker.py”,第210行,在call\u user\u函数中
返回self.\u user\u函数(请求或事件)
文件“/user\u code/main.py”,第52行,在create\u csv中
导入数据(数据集\u完整\u id,输入\u配置)
导入数据中的文件“/env/local/lib/python3.7/site packages/google/cloud/automl_v1/gapic/auto_ml_client.py”,第793行
请求,重试=重试,超时=超时,元数据=元数据
文件“/env/local/lib/python3.7/site packages/google/api_core/gapic_v1/method.py”,第143行,在调用中__
返回包装函数(*args,**kwargs)
文件“/env/local/lib/python3.7/site packages/google/api\u core/retry.py”,第286行,在retry\u wrapped\u func中
on_错误=on_错误,
文件“/env/local/lib/python3.7/site packages/google/api_core/retry.py”,第184行,在retry_目标中
返回目标()
文件“/env/local/lib/python3.7/site packages/google/api_core/timeout.py”,第214行,func_格式,带_timeout
返回函数(*args,**kwargs)
文件“/env/local/lib/python3.7/site packages/google/api\u core/grpc\u helpers.py”,第59行,错误\u重新映射\u可调用
六、从(例外情况。从grpc错误(exc),exc)
文件“”,第3行,从
google.api_core.exceptions.InvalidArgument:400发现的错误列表:1.字段:名称;消息:必填字段无效

要能够使用App Engine应用程序中的参数调用云函数:

  • 创建一个云函数

  • 使能

  • 部署
  • 4.将所有参数传递到
    数据
    在我们的例子中,我们传递
    数据集\u id
    =
    m\u您的数据集\u id

    5.通过访问
    https://your-project.appspot.com

    6.检查日志:


    试试这个:您好,paul,谢谢您的回答,我之前尝试过这个,因为我无法在python中运行curl命令,所以我使用requests模块在flask代码部分创建了一个post请求。它告诉我这个错误有类型响应,但应该是:bytes,unicode>这能回答你的问题吗@DustinIngram您好,谢谢您的回答,我尝试了您的解决方案,但如何在python中使用curl?非常感谢。另外,我的目标HTTP是这样设置的,它仍然给我错误的字段未设置。谢谢。您好,谢谢您的回复,您的回复接近我想要的,但是我如何才能将其添加到云任务中以定期执行,云任务似乎只接受url,无法使用请求。
    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 )
    
    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))