Skip to content

Commit eba4eff

Browse files
committed
added helloWorldStorage function
1 parent db0f9dd commit eba4eff

File tree

6 files changed

+74
-7
lines changed

6 files changed

+74
-7
lines changed

.vscode/settings.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
"downlevel",
99
"typechecking",
1010
"linebreak",
11-
"Pubsub"
11+
"Pubsub",
12+
"metageneration",
13+
"keyfile",
14+
"bigquery",
15+
"bigtable"
1216
],
1317
"terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
1418
"cSpell.enabled": true,

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@
22

33
<!-- TOC -->
44

5+
- [Introduction](#introduction)
56
- [Clone project](#clone-project)
67
- [Build project (command-line)](#build-project-command-line)
78
- [Deploy project to Cloud Functions Local Emulator (command-line)](#deploy-project-to-cloud-functions-local-emulator-command-line)
89
- [Build bundle to deploy to Google Cloud Platform (command-line)](#build-bundle-to-deploy-to-google-cloud-platform-command-line)
910

1011
<!-- /TOC -->
1112

13+
## Introduction
14+
15+
Cloud Functions come in two distinct variants: foreground (HTTP) and background.
16+
You invoke HTTP functions from standard HTTP requests.You can use background functions to handle events from your Cloud infrastructure, such as messages on a Google Cloud Pub/Sub topic, or changes in a Google Cloud Storage bucket. This project includes three example functions:
17+
18+
- `helloWorldHTTP` - foreground (HTTP) function
19+
- `helloWorldPubSub` - background functions to handle Google Cloud Pub/Sub topic events
20+
- `helloWorldStorage` - background functions to handle events from Google Cloud Storage bucket
21+
1222
## Clone project
1323

1424
Clone project from GitHub:

google-cloud.d.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ declare namespace GoogleCloudPlatform {
33
export namespace CloudFunctions {
44
export interface PubsubEvent {
55
eventId: string
6-
timestamp: string // ISO 8601
6+
timestamp: Date // ISO 8601
77
eventType: string
88
resource: string
99
data: PubSub.PubsubMessage
1010
}
1111

12+
export interface StorageEvent {
13+
eventId?: string
14+
timestamp: Date // ISO 8601
15+
eventType: string
16+
resource: string
17+
data: Storage.StorageMessage
18+
}
19+
1220
type Callback = () => void;
1321
}
1422

@@ -50,4 +58,24 @@ declare namespace GoogleCloudPlatform {
5058
}
5159
}
5260

61+
export namespace Storage {
62+
export interface StorageMessage {
63+
kind?: string
64+
resourceState?: string
65+
id?: string
66+
selfLink?: string
67+
name?: string
68+
bucket?: string
69+
generation?: number
70+
metageneration?: number
71+
contentType?: string
72+
timeCreated?: Date
73+
updated?: Date
74+
storageClass?: string
75+
size?: number
76+
md5Hash?: string
77+
mediaLink?: string
78+
crc32c?: string
79+
}
80+
}
5381
}

index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3+
/*
4+
const pubsub = require("@google-cloud/pubsub")({
5+
keyFilename: "/path/to/keyfile.json",
6+
projectId: "grape-spaceship-123"})
7+
console.log(pubsub)
8+
*/
39
const settings = require("./settings.json");
4-
console.log("Function settings: " + settings);
10+
console.log(settings);
511
function helloWorldHTTP(req, res) {
612
console.log(req);
713
res
@@ -12,7 +18,12 @@ function helloWorldHTTP(req, res) {
1218
}
1319
exports.helloWorldHTTP = helloWorldHTTP;
1420
function helloWorldPubSub(event, callback) {
15-
console.log("Hello World! I have got event: ", event);
21+
console.log("Hello World! I have got event from Google Cloud Pub/Sub topic: ", event.data);
1622
callback();
1723
}
1824
exports.helloWorldPubSub = helloWorldPubSub;
25+
function helloWorldStorage(event, callback) {
26+
console.log("Hello World! I have got event from Google Cloud Storage bucket: ", event.data);
27+
callback();
28+
}
29+
exports.helloWorldStorage = helloWorldStorage;

index.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import {Request, Response} from "express"
22

33
import PubsubEvent = GoogleCloudPlatform.CloudFunctions.PubsubEvent
4+
import StorageEvent = GoogleCloudPlatform.CloudFunctions.StorageEvent
45
import Callback = GoogleCloudPlatform.CloudFunctions.Callback
56

7+
/*
8+
const pubsub = require("@google-cloud/pubsub")({
9+
keyFilename: "/path/to/keyfile.json",
10+
projectId: "grape-spaceship-123"})
11+
console.log(pubsub)
12+
*/
13+
614
import * as settings from "./settings.json"
715

8-
console.log("Function settings: " + settings)
16+
console.log(settings)
917

1018
export function helloWorldHTTP(req: Request, res: Response) {
1119
console.log(req)
@@ -18,6 +26,11 @@ export function helloWorldHTTP(req: Request, res: Response) {
1826
}
1927

2028
export function helloWorldPubSub(event: PubsubEvent, callback: Callback) {
21-
console.log("Hello World! I have got event: ", event)
29+
console.log("Hello World! I have got event from Google Cloud Pub/Sub topic: ", event.data)
30+
callback()
31+
}
32+
33+
export function helloWorldStorage(event: StorageEvent, callback: Callback) {
34+
console.log("Hello World! I have got event from Google Cloud Storage bucket: ", event.data)
2235
callback()
2336
}

tslint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"linebreak-style": [
2121
true,
2222
"LF"
23-
]
23+
],
24+
"no-var-requires": false
2425
}
2526
}

0 commit comments

Comments
 (0)