Skip to content

Commit 71dc2d9

Browse files
committed
First commit
1 parent 53983ff commit 71dc2d9

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

gcloudfunctiongcsevent/README.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Google Cloud Function Cloud Storage Event Python example
2+
3+
This folder contains a Google Cloud Function example in Python on Google Cloud Platform (GCP).
4+
5+
It handles a Google Cloud Function that sends information to the function log about an object when it appears in a Cloud Storage bucket.
6+
7+
## Requirements
8+
9+
* You must have a [Google Cloud Platform (GCP)](http://cloud.google.com/) account.
10+
11+
* The code was written for Python 3 and Google Cloud Python Client Library.
12+
13+
## Using the code
14+
15+
* Access the Google Cloud console.
16+
17+
* Create a Cloud Storage bucket.
18+
19+
* Create a Google Cloud Function:
20+
* Name: `<CLOUD_FUNCTION_NAME>`
21+
* Memory allocated: `256 MB`
22+
* Trigger: `Cloud Storage`
23+
* Event Type: `Finalise/Create`
24+
* Bucket: `<BUCKET_NAME>`
25+
* Source code. You can use 2 options:
26+
* Inline editor:
27+
Edit the code of the `main.py` in the browser.
28+
* ZIP upload:
29+
Upload a ZIP file containing the `main.py` and `requirements.txt` files.
30+
* ZIP file: `<ZIP_LOCAL_NAME>`
31+
* Stage bucket: `<BUCKET_NAME_FOR_STAGGING>`
32+
* Runtime: `Python 3.7 (Beta)`
33+
* Function to execute: `gcs_event`
34+
* Region: `<GOOGLE_CLOUD_REGION>`
35+
* Timeout: `60 seconds`
36+
37+
* Save the Google Cloud Function.
38+
39+
The function is deployed and run.
40+
41+
* Test the function.
42+
43+
Copy a file in the source Cloud Storage bucket.
44+
45+
You should see the next message in the Google Cloud Function log:
46+
47+
```bash
48+
Bucket: <BUCKET_NAME>
49+
Object: <OBJECT_NAME>
50+
Object size: <OBJECT_SIZE>
51+
```
666 Bytes
Binary file not shown.

gcloudfunctiongcsevent/main.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# main.py
4+
# It handles a Google Cloud Funtion that sends information to the log about an object when it appears in a Cloud Storage bucket.
5+
6+
def gcs_event(event, context):
7+
"""Triggered by a change to a Cloud Storage bucket.
8+
Args:
9+
event (dict): Event payload.
10+
context (google.cloud.functions.Context): Metadata for the event.
11+
"""
12+
13+
file = event
14+
print(f"Bucket: {file['bucket']}.")
15+
print(f"Object: {file['name']}.")
16+
print(f"Object size: {file['size']}.")
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Function dependencies, for example:
2+
# package>=version

0 commit comments

Comments
 (0)