Skip to content

Commit 42cf767

Browse files
committed
Adding a working example of Lambda function in Python that uses Google Cloud Vision API
1 parent ba1532b commit 42cf767

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

python-lambda-example/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
credentials.json
2+
package
3+
package.zip

python-lambda-example/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
This is an example of a Python lambda function that uses Google Cloud Vision API.
2+
3+
* Compile the code: `$ ./bundle.sh`
4+
* Upload `package.zip` to AWS Lambda
5+
* If you need to install a dependency: `pip install --target ./package <package name>`
6+
7+
Reference: https://docs.aws.amazon.com/lambda/latest/dg/python-package.html#python-package-create-package-with-dependency

python-lambda-example/bundle.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
rm -f package.zip
3+
rm -rf package
4+
pip install --target ./package requests
5+
pip install --target ./package google-cloud-vision
6+
cd package
7+
zip -r ../package.zip .
8+
cd ..
9+
zip -g package.zip lambda_function.py
10+
zip -g package.zip credentials.json
11+
echo 'Now upload package.zip to AWS Lambda (please make sure that the Lambda function timeout configuration is enough)'
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"type": "service_account",
3+
"project_id": "",
4+
"private_key_id": "",
5+
"private_key": ""
6+
"client_email": "",
7+
"client_id": "",
8+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
9+
"token_uri": "https://oauth2.googleapis.com/token",
10+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
11+
"client_x509_cert_url": ""
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from google.cloud import vision
2+
import os
3+
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "credentials.json"
4+
def lambda_handler(event, context):
5+
client = vision.ImageAnnotatorClient()
6+
print(client)

0 commit comments

Comments
 (0)