How to combine Google App Engine with Cloud Natura

2019-08-18 19:28发布

问题:

I thought what I was trying to do would be simple, but that seems to not be the case.

I've found that using the Natural Language API with Google Compute Engine is fairly straightforward, as I can simply import the needed libraries in Python.

This does not seem to be the case with App Engine, as I am plagued by import errors, as soon as I fix one, another arises.

Have any of you ever worked to combine these two services, and if so, how?

Thank you

回答1:

App Engine Standard does not yet support Google Client Libraries (which I assume you are trying to import into your application), it is work in the development, so by now you can try with the following alternatives:

  • App Engine Flexible: it does support Client Libraries, you just have to vendor them into your application as if they were third-party libraries. You can follow this guide in order to add the google-api-python-client library appropriately.
  • REST API: you can use the REST API (which already has a stable version, v1). It might not be as convenient as Client Libraries, but you can make HTTP requests with your Python code and process their responses.
  • Compute Engine: as you pointed out in your question, you will be able to use Client Libraries from your custom Python runtime environment in any machine you want (either locally or an instance in Compute Engine).

UPDATE:

Actually, I have looked deeper into your issue and have been able to solve it using App Engine Standard by using the Google API Client Library (not Google Client Libraries), which is an alternative version that is available for the Standard environment. Below I leave a small working piece of code which you can populate with your own data and try in App Engine environment or even with the Local Development server.

from apiclient.discovery import build

service = build('language', 'v1', developerKey='<YOUR_API_KEY>')                
collection = service.documents()

data = {}
data['document'] = {}
data['document']['language'] = 'en'
data['document']['content'] = 'I am really happy'
data['document']['type'] = 'PLAIN_TEXT'

request = collection.analyzeSentiment(body=data)
res = request.execute()

You will have to obtain an API key for authentication, as explained in the documentation, and you will also need to add the library as explained in the other link I shared.

Last, here you have the documentation on the available methods from the API. The example I provided is using analyzeSentiment(), but you can go with the one you need.

Hope it helps!