blogs/endpointslambda/aeflex-endpoints/
@app.route('/processmessage', methods=['POST']) def process(): """Process messages with information about S3 objects""" message = request.get_json().get('inputMessage', '') # add other processing as needed # for example, add event to PubSub or # download object using presigned URL, save in Cloud Storage, invoke ML APIs return jsonify({'In app code for endpoint, received message': message})
host: "echo-api.endpoints.aeflex-endpoints.cloud.goog"
inputMessage
# This section configures the processmessage endpoint. "/processmessage": post: description: "Process the given message." operationId: "processmessage" produces: - "application/json" responses: 200: description: "Return a success response" schema: $ref: "#/definitions/successMessage" parameters: - description: "Message to process" in: body name: inputMessage required: true schema: $ref: "#/definitions/inputMessage" security: - api_key: [] definitions: successMessage: properties: message: type: string inputMessage: # This section contains information about the S3 bucket and object to be processed. properties: Bucket: type: string ObjectKey: type: string ContentType: type: string ContentLength: type: integer ETag: type: string PresignedUrl: type: string
gcloud service-management deploy openapi.yaml
Service Configuration [2017-03-05r2] uploaded for service "echo-api.endpoints.aeflex-endpoints.cloud.goog"
endpoints_api_service: # The following values are to be replaced by information from the output of # 'gcloud service-management deploy openapi.yaml' command. name: echo-api.endpoints.aeflex-endpoints.cloud.goog config_id: 2017-03-05r2
gcloud app deploy
blogs/endpointslambda/lambdafunctioninline.py.
from __future__ import print_function import boto3 import json import os import urllib import urllib2 print('Loading function') s3 = boto3.client('s3') endpoint_api_key = os.environ['ENDPOINT_API_KEY'] endpoint_url = "https://aeflex-endpoints.appspot.com/processmessage" def lambda_handler(event, context): # Get the object information from the event bucket = event['Records'][0]['s3']['bucket']['name'] object_key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode('utf8')) try: # Retrieve object metadata response = s3.head_object(Bucket=bucket, Key=object_key) # Generate pre-signed URL for object presigned_url = s3.generate_presigned_url('get_object', Params = {'Bucket': bucket, 'Key': object_key}, ExpiresIn = 3600) data = {"inputMessage": { "Bucket": bucket, "ObjectKey": object_key, "ContentType": response['ContentType'], "ContentLength": response['ContentLength'], "ETag": response['ETag'], "PresignedUrl": presigned_url } } headers = {"Content-Type": "application/json", "x-api-key": endpoint_api_key } # Invoke Cloud Endpoints API request = urllib2.Request(endpoint_url, data = json.dumps(data), headers = headers) response = urllib2.urlopen(request) print('Response text: {} \nResponse status: {}'.format(response.read(), response.getcode())) return response.getcode() except Exception as e: print(e) print('Error integrating lambda function with endpoint for the object {} in bucket {}'.format(object_key, bucket)) raise e