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 |
wibling
2015/03/25 09:59:01
NIT: This -> The
and
packages -> package
| |
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][googleapisbeta] packages. | |
13 | |
14 This means that the authentication model for using the APIs in this package | |
15 uses the [googleapis_auth][googleapisauth] 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 | |
wibling
2015/03/25 09:59:01
'demonstrating to use of this' -> demonstrating th
| |
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(); | |
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) | |
wibling
2015/03/25 09:59:01
.addApp -> .addAll
| |
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. | |
wibling
2015/03/25 09:59:01
NIT: 'which uses' -> 'using'
| |
73 }); | |
74 ``` | |
75 | |
76 When other parts of the application are running inside a service scope with | |
77 these services registered they are directly available through getters like this: | |
wibling
2015/03/25 09:59:01
I would change the above to something like.
The s
| |
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 accessing the `gcloud` services is well | |
91 integrated into both the local development server and the authentication model | |
92 for gaining access to Google Cloud Platform services for App Engine | |
93 applications. | |
94 | |
wibling
2015/03/25 09:59:01
I would rephrase the above to something like:
The
| |
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. | |
99 | |
100 The Cloud Datastore API provides a mapping of Dart objects to entities stored | |
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 an object into the | |
117 Datastore create 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(); | |
wibling
2015/03/25 09:59:01
Don't you need to have brackets like
(await db.q
| |
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 | |
wibling
2015/03/25 09:59:01
Could -> Cloud
| |
140 Google Cloud Storage provide a highly available object storage (aka BLOB | |
wibling
2015/03/25 09:59:01
NIT: object storage -> object store
| |
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 = await storage.createBucket('my-bucket'); | |
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').openWrite()); | |
158 ``` | |
159 | |
160 ## Cloud Pub/Sub | |
161 Google Cloud Pub/Sub provides many-to-many, asynchronous messaging. See the | |
162 product page [https://cloud.google.com/pubsub/][PubSub] for more information. | |
163 | |
164 Cloud Pub/Sub uses two concepts for messaging. _Topics_ are used if you want | |
165 to send messages and _subscriptions_ are used to subscribe to topics and | |
166 receive the messages. This de-couples the producer of a message from the | |
wibling
2015/03/25 09:59:01
NIT: de-couples -> decouples
| |
167 consumer of a message. | |
168 | |
169 The following code creates a _topic_ and sends a simple test message: | |
170 | |
171 ```dart | |
172 var topic = await pubsub.createTopic('my'topic'); | |
173 await topic.publishString('Hello, world!') | |
174 ``` | |
175 | |
176 With the following code a _subscription_ is created on the _topic_ and | |
177 a message is pulled using the subscription. A received message must be | |
178 acknowledged when the consumer has processed it. | |
179 | |
180 ```dart | |
181 var subscription = | |
182 await pubsub.createSubscription('my-subscription', 'my-topic); | |
183 var pullEvent = await subscription.pull(); | |
184 print(pullEvent.message.asString); | |
185 await pullEvent.acknowledge() | |
186 ``` | |
187 | |
188 It is also possible to receive messages using push events instead of pulling | |
189 from the subscription. To do this the subscription should be configured as a | |
190 push subscription with an HTTP endpoint. | |
191 | |
192 ```dart | |
193 await pubsub.createSubscription( | |
194 'my-subscription', | |
195 'my-topic', | |
196 endpoint: Uri.parse('https://server.example.com/push')); | |
197 ``` | |
198 | |
199 With this subscription all messages will be send to the URL provided in the | |
200 `endpoint` argument. The server needs to acknowledge the reception of the | |
201 message with a `200 OK` reply. | |
4 | 202 |
5 ### Running tests | 203 ### Running tests |
6 | 204 |
7 If you want to run the end-to-end tests, a Google Cloud project is required. | 205 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: | 206 When running these tests the following environment variables needs to be set: |
wibling
2015/03/25 09:59:01
NIT: needs -> need
| |
9 | 207 |
10 GCLOUD_E2E_TEST_PROJECT | 208 GCLOUD_E2E_TEST_PROJECT |
11 GCLOUD_E2E_TEST_KEY | 209 GCLOUD_E2E_TEST_KEY |
12 | 210 |
13 The vaule of the environment variable `GCLOUD_E2E_TEST_PROJECT` is the name | 211 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 | 212 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://`) | 213 `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. | 214 to a JSON key file for a service account providing access to the Cloud Project. |
215 | |
216 [Datastore]: https://cloud.google.com/datastore/ | |
217 [GCS]: https://cloud.google.com/storage/ | |
218 [PubSub]: https://cloud.google.com/pubsub/ | |
219 [googleapis]: https://pub.dartlang.org/packages/googleapis | |
220 [googleapisbeta]: https://pub.dartlang.org/packages/googleapis_beta | |
221 [googleapisauth]: https://pub.dartlang.org/packages/googleapis_beta | |
222 [appengine]: https://pub.dartlang.org/packages/appengine | |
OLD | NEW |