OLD | NEW |
---|---|
1 ## Google Cloud Platform | 1 ## Google Cloud Platform support package (gcloud) |
2 | 2 |
3 High level interface for Google Cloud Platform APIs | 3 This `gcloud` packages provides a high level "idomatic Dart" interface to |
4 some of the most widely used Google Cloud Platform services. Currently the | |
5 following services are supported: | |
6 | |
7 * Cloud Datastore | |
8 * Cloud Storage | |
9 * Cloud Pub/Sub | |
10 | |
11 The APIs in this package are all based on the generic generated APIs in the | |
12 [googleapis] and [googleapis_beta] packages. | |
13 | |
14 This means that the authentication model for using the APIs in this package | |
15 uses the [googleapis_auth] package. | |
16 | |
17 Note that this package is only intended for being used with the standalone VM | |
18 in a server or command line application. Don't expect this package to work on | |
19 the browser. | |
20 | |
21 The code snippets below demonstrating to use of this package all assume that | |
22 the following imports are present: | |
23 | |
24 ```dart | |
25 import 'package:googleapis_auth/auth_io.dart' as auth; | |
26 import 'package:http/http.dart' as http; | |
27 import 'package:gcloud/db.dart'; | |
28 import 'package:gcloud/storage.dart'; | |
29 import 'package:gcloud/pubsub.dart'; | |
30 import 'package:gcloud/service_scope.dart' as ss; | |
31 import 'package:gcloud/src/datastore_impl.dart'; | |
32 ``` | |
33 | |
34 ### Getting access to the APIs | |
35 | |
36 The first step in using the APIs is to get an authenticated HTTP client and | |
37 with that create API class instances for accessing the different APIs. The | |
38 code below assumes that you have a Google Cloud Project called `my-project` | |
39 with credentials for a service account from that project stored in the file | |
40 `my-project.json`. | |
41 | |
42 ```dart | |
43 // Read the service account credentials from the file. | |
44 var jsonCredentials = new File('my-project.json').readAsStringSync()); | |
kustermann
2015/03/24 17:53:28
Remove final ')'
Søren Gjesse
2015/03/25 08:28:26
Done.
| |
45 var credentials = new auth.ServiceAccountCredentials.fromJson(jsonCredentials); | |
46 | |
47 // Get an HTTP authenticated client using the service account credentials. | |
48 var scopes = [] | |
49 ..addAll(dastore_impl.DatastoreImpl.SCOPES); | |
50 ..addApp(Storage.SCOPES) | |
51 ..addAll(PubSub.SCOPES) | |
52 var client = await auth.clientViaServiceAccount(creds, scopes); | |
53 | |
54 // Instantiate objects to access Cloud Datastore, Cloud Storage | |
55 // and Cloud Pub/Sub APIs. | |
56 var db = new DatastoreDB( | |
57 new dastore_impl.DatastoreImpl(client, 's~my-project')); | |
58 var storage = new Storage(client, 'my-project'); | |
59 var pubsub = new PubSub(client, 'my-project'); | |
60 ``` | |
61 | |
62 All the APIs in this package supports the use of 'service scopes'. Service | |
63 scopes are described in details below. | |
64 | |
65 ```dart | |
66 ss.fork(() { | |
67 // register the services in the new service scope. | |
68 registerDbService(db); | |
69 registerStorageService(storage); | |
70 registerPubSubService(pubsub); | |
71 | |
72 // Run application which uses these services. | |
73 }); | |
74 ``` | |
75 | |
76 When other parts of the application is running inside a service scope with | |
kustermann
2015/03/24 17:53:28
is -> are
Søren Gjesse
2015/03/25 08:28:26
Done.
| |
77 these services registered they are directly available through getters like this: | |
78 | |
79 ```dart | |
80 dbService. | |
81 storageService. | |
82 pubsubService. | |
83 ``` | |
84 | |
85 This way it is not necessary to pass the service objects around in your code. | |
86 | |
87 ### Use with App Engine | |
88 | |
89 The `gcloud` package is also integrated into the Dart [appengine] package so | |
90 that when running Dart on App Engine assessing the `gcloud` services is well | |
kustermann
2015/03/24 17:53:28
assessing -> accessing
Søren Gjesse
2015/03/25 08:28:26
Done.
| |
91 integrated into both the local developemnt server and the authentication model | |
kustermann
2015/03/24 17:53:28
developemnt -> development
Søren Gjesse
2015/03/25 08:28:26
Done.
| |
92 for gaining access to Google Cloud Platform services for App Engine | |
93 applications. | |
94 | |
95 ## Cloud Datastore | |
96 Google Cloud Datastore provide a NoSQL, schemaless database for storing | |
97 non-relational data. See the product page | |
98 [https://cloud.google.com/datastore/][Datastore] for more information. | |
kustermann
2015/03/24 17:53:28
Is this link valid?
I thought it's either (...)[.
Søren Gjesse
2015/03/25 08:28:26
It is I think that (...)[...] is not working. At l
| |
99 | |
100 The Cloud Datastore API provide mapping of Dart objects to entities stored | |
kustermann
2015/03/24 17:53:28
provide -> provides a
Søren Gjesse
2015/03/25 08:28:26
Done.
| |
101 in the Datastore. The following example shows how to annotate a class to | |
102 make it possible to store instances of it in the Datastore. | |
103 | |
104 ```dart | |
105 @db.Kind() | |
106 class Person extends db.Model { | |
107 @db.StringProperty() | |
108 String name; | |
109 | |
110 @db.IntProperty() | |
111 int age; | |
112 } | |
113 ``` | |
114 | |
115 The `Kind` annotation tell that instances of this class can be stored. The | |
116 class must also inherit from `Model`. Now to store into the Datastore create | |
kustermann
2015/03/24 17:53:28
to store -> to store an object in the
Søren Gjesse
2015/03/25 08:28:26
Done.
| |
117 an instance and use the `commit` function. | |
118 | |
119 ```dart | |
120 var person = new Person() | |
121 ..name = '' | |
122 ..age = 42; | |
123 await db.commit(inserts: [person]); | |
124 ``` | |
125 | |
126 The function `query` is used to build a `Query` object which can be run to | |
127 perform the query. | |
128 | |
129 ```dart | |
130 var persons = await db.query(Person).run().toList(); | |
131 ``` | |
132 | |
133 NOTE: This package include a lower level API provided through the class | |
134 `Datastore` on top of which the `DatastoreDB` API is build. The main reason | |
135 for this additional API level is to bridge the gap between the different APIs | |
136 exposed inside App Engine and through the public REST API. We reserve the | |
137 rights to modify and maybe even remove this additional layer at any time. | |
138 | |
139 ## Could Storage | |
140 Google Cloud Storage provide a highly available object storage (aka BLOB | |
141 store). See the product page [https://cloud.google.com/storage/][GCS] | |
142 for more information. | |
143 | |
144 In Cloud Storage the objects (BLOBs) are organized in _buckets_. Each bucket | |
145 has a name in a global namespace. The following code creates a new bucket | |
146 named `my-bucket` and writes the content of the file `my-file.txt` to the | |
147 object named `my-object`. | |
148 | |
149 ```dart | |
150 var bucket = storage.createBucket('my-bucket'); | |
kustermann
2015/03/24 17:53:28
is an 'await' missing here?
Søren Gjesse
2015/03/25 08:28:26
Yes, added.
| |
151 new File('my-file.txt').openRead().pipe(bucket.write('my-object')); | |
152 ``` | |
153 | |
154 The following code will read back the object. | |
155 | |
156 ```dart | |
157 bucket.read('my-object').pipe(new File('my-file-copy.txt').openRead()); | |
kustermann
2015/03/24 17:53:28
openRead -> openWrite
Søren Gjesse
2015/03/25 08:28:26
Done.
| |
158 ``` | |
159 | |
160 ## Cloud Pub/Sub | |
161 Google Cloud Pub/Sub provide many-to-many, asynchronous messaging. See the | |
kustermann
2015/03/24 17:53:28
provide -> provides
Søren Gjesse
2015/03/25 08:28:26
Done.
| |
162 product page [https://cloud.google.com/pubsub/][PubSub] for more information. | |
163 | |
164 Cloud Pub/Sub ues two concepts for messaging. _Topics_ are used if you want | |
kustermann
2015/03/24 17:53:28
ues -> uses
Søren Gjesse
2015/03/25 08:28:26
Done.
| |
165 to send messages and _subscriptions_ are used to subscribe to topics and | |
166 receive the messages. | |
kustermann
2015/03/24 17:53:28
Maybe add: "This de-couples the producer of a mess
Søren Gjesse
2015/03/25 08:28:26
Done.
| |
167 | |
168 The following code creates a _topic_ and sends a simple test message: | |
169 | |
170 ```dart | |
171 var topic = await pubsub.createTopic('my'topic'); | |
172 await topic.publishString('Hello, world!') | |
173 ``` | |
174 | |
175 ```dart | |
176 var subscription = | |
177 await pubsub.createSubscription('my-subscription', 'my-topic); | |
178 var pullEvent = await subscription.pull(); | |
179 await pullEvent.acknowledge() | |
kustermann
2015/03/24 17:53:28
maybe add a comment that `pullEvent` has the real
Søren Gjesse
2015/03/25 08:28:26
Added
print(pullEvent.message.asString);
| |
180 ``` | |
181 | |
182 It is also possible to receive messages using push events instead of pulling | |
183 from the subscription. To do this the subscription should be configured as a | |
184 push subscription with an HTTP endpoint. | |
185 | |
186 ```dart | |
187 await pubsub.createSubscription( | |
188 'my-subscription', | |
189 'my-topic', | |
190 endpoint: Uri.parse('https://server.example.com/push')); | |
191 ``` | |
192 | |
193 With this subscription all messages will be send to the URL provided in the | |
194 `endpoint` argument. The server will acknowledge the reception of the message | |
kustermann
2015/03/24 17:53:28
will -> needs to
Søren Gjesse
2015/03/25 08:28:26
Done.
| |
195 with a `200 OK` reply. | |
4 | 196 |
5 ### Running tests | 197 ### Running tests |
6 | 198 |
7 If you want to run the end-to-end tests, a Google Cloud project is required. | 199 If you want to run the end-to-end tests, a Google Cloud project is required. |
8 When running these tests the following envrionment variables needs to be set: | 200 When running these tests the following environment variables needs to be set: |
9 | 201 |
10 GCLOUD_E2E_TEST_PROJECT | 202 GCLOUD_E2E_TEST_PROJECT |
11 GCLOUD_E2E_TEST_KEY | 203 GCLOUD_E2E_TEST_KEY |
12 | 204 |
13 The vaule of the environment variable `GCLOUD_E2E_TEST_PROJECT` is the name | 205 The vaule of the environment variable `GCLOUD_E2E_TEST_PROJECT` is the name |
14 of the Google Cloud project to use. The value of the environment variable | 206 of the Google Cloud project to use. The value of the environment variable |
15 `GCLOUD_E2E_TEST_KEY` is a Google Cloud Storage path (starting wiht `gs://`) | 207 `GCLOUD_E2E_TEST_KEY` is a Google Cloud Storage path (starting with `gs://`) |
16 to a JSON key file for a service account providing access to the Cloud Project. | 208 to a JSON key file for a service account providing access to the Cloud Project. |
209 | |
210 [Datastore]: https://cloud.google.com/datastore/ | |
211 [GCS]: https://cloud.google.com/storage/ | |
212 [PubSub]: https://cloud.google.com/pubsub/ | |
213 [googleapis]: https://pub.dartlang.org/packages/googleapis | |
214 [googleapis_beta]: https://pub.dartlang.org/packages/googleapis_beta | |
215 [googleapis_auth]: https://pub.dartlang.org/packages/googleapis_beta | |
216 [appengine]: https://pub.dartlang.org/packages/appengine | |
OLD | NEW |