Dec-30-2018, 06:58 AM
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?
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)