Python Forum
RESTful API in Google Cloud Functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RESTful API in Google Cloud Functions
#1
hi there,

new to python and this forum! I'm working on a proof of concept and trying to make a serverless function follow the RESTful pattern. The code below works for the list_upload function and successfully posts a file to GCS. However, the list_download function fails and cannot find the positional parameter <blob_name> when accessing the endpoint to perform a GET operation and pull the list from GCS.

What's interesting is this same code runs locally and works when using Swagger. Any ideas or paths I can follow?


@APP.route('/<bucket_name>/lists/<blob_name>', methods=['GET'])
def list_download(bucket_name, blob_name):
    """Endpoint gets list data from list stored in Google Cloud Storage. Returns JSON.
    ---
    summary: "Retrieves list from Google Cloud Storage bucket."
    description: "This can only be done by an authenticated user."
    operationId: "list_download"
    produces:
      - "application/json"
    parameters:
      - name: bucket_name
        in: path
        type: string
        required: true
      - name: blob_name
        in: path
        type: string
        required: true
    responses:
      200:
        description: Returns list data in JSON format
    """
    # Access Google Cloud Storage
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    logging.info('bucket.id=%s', bucket.id)
    blob = bucket.blob(blob_name)
    logging.info('blob.path=%s', blob.path)

    # Get Blob Data
    blob_data = blob.download_as_string()

    # Setup response to client
    response_data = {
        'bucket': bucket_name,
        'name': blob_name,
        'message': 'success',
        'data': base64.b64encode(blob_data).decode("utf-8")
        }
    # Return json response to client
    return json.dumps(response_data, indent=4)
@APP.route('/<bucket_name>/lists/', methods=['POST'])
def list_upload(bucket_name):
    """Endpoint accepts JSON payload and posts a file to Google Cloud Storage. Returns JSON.
    ---
      summary: "Create list in Google Cloud Storage bucket from JSON payload"
      description: "This can only be done by an authenticated user."
      operationId: "list_upload"
      produces:
      - "application/json"
      parameters:
      - in: "body"
        name: "body"
        description: "Create list in object storage."
        required: true
      responses:
        default:
          description: "List successfully created in object storage."
    """
    # Upload list to a bucket
    # Capture request parameters for bucket and blob
    request_json = request.get_json()
    bucket_name = request_json['bucket']
    blob_name = request_json['name']
    blob_data = base64.b64decode(request_json['data'])

    # Logging
    logging.info('bucket_name=%s', bucket_name)
    logging.info('blob_name=%s', blob_name)
    logging.info('blob_data=%s', blob_data)

    # Access Google Cloud Storage
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    logging.info('bucket.id=%s', bucket.id)
    blob = bucket.blob(blob_name)
    logging.info('blob.path=%s', blob.path)

    # Upload blob data
    blob.upload_from_string(blob_data)

    # Setup response to client
    response_data = {
        'bucket': bucket_name,
        'name': blob_name,
        'message': 'success'
    }
    # Return json response to client
    return json.dumps(response_data, indent=4)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information Comparing cloud hosts for serverless functions (FaaS) for Python hmartin 0 272 Mar-16-2024, 04:30 PM
Last Post: hmartin
  Creating a RESTful web service and web service client dangermaus33 0 1,281 Dec-01-2020, 09:56 PM
Last Post: dangermaus33
Exclamation Quick Help - Deploy Flask App on Google Cloud Madoo 0 1,481 Nov-07-2020, 01:43 PM
Last Post: Madoo
  Restful API Python Flask Postman starter_student 1 1,788 Sep-22-2019, 08:16 AM
Last Post: ndc85430
  python on a cloud server IMuriel 4 3,177 Jan-28-2019, 03:42 PM
Last Post: IMuriel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020