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 The `gcloud` package 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][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 the 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(); |
| 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 ..addAll(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 using these services. |
| 73 }); |
| 74 ``` |
| 75 |
| 76 The services registered with the service scope can now be reached from within |
| 77 all the code running in the same service scope using the below getters. |
| 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 in the Dart [appengine] package. This |
| 90 means the `gcloud` services are available both via the appengine context and |
| 91 service scopes. The authentication required to access the Google Cloud Platform |
| 92 services is handled automatically. |
| 93 |
| 94 This means that getting to the App Engine Datastore can be through either |
| 95 the App Engine context |
| 96 |
| 97 ```dart |
| 98 var db = context.services.db; |
| 99 ``` |
| 100 |
| 101 or just using the service scope registration. |
| 102 |
| 103 ```dart |
| 104 var db = dbService; |
| 105 ``` |
| 106 |
| 107 ## Cloud Datastore |
| 108 Google Cloud Datastore provide a NoSQL, schemaless database for storing |
| 109 non-relational data. See the product page |
| 110 [https://cloud.google.com/datastore/][Datastore] for more information. |
| 111 |
| 112 The Cloud Datastore API provides a mapping of Dart objects to entities stored |
| 113 in the Datastore. The following example shows how to annotate a class to |
| 114 make it possible to store instances of it in the Datastore. |
| 115 |
| 116 ```dart |
| 117 @db.Kind() |
| 118 class Person extends db.Model { |
| 119 @db.StringProperty() |
| 120 String name; |
| 121 |
| 122 @db.IntProperty() |
| 123 int age; |
| 124 } |
| 125 ``` |
| 126 |
| 127 The `Kind` annotation tell that instances of this class can be stored. The |
| 128 class must also inherit from `Model`. Now to store an object into the |
| 129 Datastore create an instance and use the `commit` function. |
| 130 |
| 131 ```dart |
| 132 var person = new Person() |
| 133 ..name = '' |
| 134 ..age = 42; |
| 135 await db.commit(inserts: [person]); |
| 136 ``` |
| 137 |
| 138 The function `query` is used to build a `Query` object which can be run to |
| 139 perform the query. |
| 140 |
| 141 ```dart |
| 142 var persons = (await db.query(Person).run()).toList(); |
| 143 ``` |
| 144 |
| 145 NOTE: This package include a lower level API provided through the class |
| 146 `Datastore` on top of which the `DatastoreDB` API is build. The main reason |
| 147 for this additional API level is to bridge the gap between the different APIs |
| 148 exposed inside App Engine and through the public REST API. We reserve the |
| 149 rights to modify and maybe even remove this additional layer at any time. |
| 150 |
| 151 ## Cloud Storage |
| 152 Google Cloud Storage provide a highly available object store (aka BLOB |
| 153 store). See the product page [https://cloud.google.com/storage/][GCS] |
| 154 for more information. |
| 155 |
| 156 In Cloud Storage the objects (BLOBs) are organized in _buckets_. Each bucket |
| 157 has a name in a global namespace. The following code creates a new bucket |
| 158 named `my-bucket` and writes the content of the file `my-file.txt` to the |
| 159 object named `my-object`. |
| 160 |
| 161 ```dart |
| 162 var bucket = await storage.createBucket('my-bucket'); |
| 163 new File('my-file.txt').openRead().pipe(bucket.write('my-object')); |
| 164 ``` |
| 165 |
| 166 The following code will read back the object. |
| 167 |
| 168 ```dart |
| 169 bucket.read('my-object').pipe(new File('my-file-copy.txt').openWrite()); |
| 170 ``` |
| 171 |
| 172 ## Cloud Pub/Sub |
| 173 Google Cloud Pub/Sub provides many-to-many, asynchronous messaging. See the |
| 174 product page [https://cloud.google.com/pubsub/][PubSub] for more information. |
| 175 |
| 176 Cloud Pub/Sub uses two concepts for messaging. _Topics_ are used if you want |
| 177 to send messages and _subscriptions_ are used to subscribe to topics and |
| 178 receive the messages. This decouples the producer of a message from the |
| 179 consumer of a message. |
| 180 |
| 181 The following code creates a _topic_ and sends a simple test message: |
| 182 |
| 183 ```dart |
| 184 var topic = await pubsub.createTopic('my'topic'); |
| 185 await topic.publishString('Hello, world!') |
| 186 ``` |
| 187 |
| 188 With the following code a _subscription_ is created on the _topic_ and |
| 189 a message is pulled using the subscription. A received message must be |
| 190 acknowledged when the consumer has processed it. |
| 191 |
| 192 ```dart |
| 193 var subscription = |
| 194 await pubsub.createSubscription('my-subscription', 'my-topic); |
| 195 var pullEvent = await subscription.pull(); |
| 196 print(pullEvent.message.asString); |
| 197 await pullEvent.acknowledge() |
| 198 ``` |
| 199 |
| 200 It is also possible to receive messages using push events instead of pulling |
| 201 from the subscription. To do this the subscription should be configured as a |
| 202 push subscription with an HTTP endpoint. |
| 203 |
| 204 ```dart |
| 205 await pubsub.createSubscription( |
| 206 'my-subscription', |
| 207 'my-topic', |
| 208 endpoint: Uri.parse('https://server.example.com/push')); |
| 209 ``` |
| 210 |
| 211 With this subscription all messages will be send to the URL provided in the |
| 212 `endpoint` argument. The server needs to acknowledge the reception of the |
| 213 message with a `200 OK` reply. |
4 | 214 |
5 ### Running tests | 215 ### Running tests |
6 | 216 |
7 If you want to run the end-to-end tests, a Google Cloud project is required. | 217 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: | 218 When running these tests the following environment variables need to be set: |
9 | 219 |
10 GCLOUD_E2E_TEST_PROJECT | 220 GCLOUD_E2E_TEST_PROJECT |
11 GCLOUD_E2E_TEST_KEY | 221 GCLOUD_E2E_TEST_KEY |
12 | 222 |
13 The vaule of the environment variable `GCLOUD_E2E_TEST_PROJECT` is the name | 223 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 | 224 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://`) | 225 `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. | 226 to a JSON key file for a service account providing access to the Cloud Project. |
| 227 |
| 228 [Datastore]: https://cloud.google.com/datastore/ |
| 229 [GCS]: https://cloud.google.com/storage/ |
| 230 [PubSub]: https://cloud.google.com/pubsub/ |
| 231 [googleapis]: https://pub.dartlang.org/packages/googleapis |
| 232 [googleapisbeta]: https://pub.dartlang.org/packages/googleapis_beta |
| 233 [googleapisauth]: https://pub.dartlang.org/packages/googleapis_beta |
| 234 [appengine]: https://pub.dartlang.org/packages/appengine |
OLD | NEW |