forked from googleapis/google-cloud-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
372 lines (348 loc) · 9.36 KB
/
Copy pathindex.js
File metadata and controls
372 lines (348 loc) · 9.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*!
* Copyright 2014 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*!
* @module storage
*/
'use strict';
var extend = require('extend');
/**
* @type {module:storage/bucket}
* @private
*/
var Bucket = require('./bucket.js');
/**
* @type {module:common/streamrouter}
* @private
*/
var streamRouter = require('../common/stream-router.js');
/**
* @type {module:common/util}
* @private
*/
var util = require('../common/util.js');
/**
* Required scopes for Google Cloud Storage API.
* @const {array}
* @private
*/
var SCOPES = ['https://www.googleapis.com/auth/devstorage.full_control'];
/**
* @const {string}
* @private
*/
var STORAGE_BASE_URL = 'https://www.googleapis.com/storage/v1/b';
/*! Developer Documentation
*
* Invoke this method to create a new Storage object bound with pre-determined
* configuration options. For each object that can be created (e.g., a bucket),
* there is an equivalent static and instance method. While they are classes,
* they can be instantiated without use of the `new` keyword.
*
* @param {object} options - Configuration object.
*/
/**
* To access your Cloud Storage buckets, you will use the `bucket` function
* returned from this `storage` object.
*
* The examples below will demonstrate the different usage patterns your app may
* need to connect to `gcloud` and access your bucket.
*
* @alias module:storage
* @constructor
*
* @param {object} options - [Configuration object](#/docs/?method=gcloud).
*
* @example
* var gcloud = require('gcloud')({
* keyFilename: '/path/to/keyfile.json',
* projectId: 'my-project'
* });
*
* var gcs = gcloud.storage();
*/
function Storage(options) {
if (!(this instanceof Storage)) {
return new Storage(options);
}
options = options || {};
if (!options.projectId) {
throw util.missingProjectIdError;
}
this.makeAuthorizedRequest_ = util.makeAuthorizedRequestFactory({
credentials: options.credentials,
keyFile: options.keyFilename,
scopes: SCOPES,
email: options.email
});
this.projectId = options.projectId;
}
/**
* Google Cloud Storage uses access control lists (ACLs) to manage object and
* bucket access. ACLs are the mechanism you use to share objects with other
* users and allow other users to access your buckets and objects.
*
* This object provides constants to refer to the three permission levels that
* can be granted to a scope:
*
* - `gcs.acl.OWNER_ROLE` - ("OWNER")
* - `gcs.acl.READER_ROLE` - ("READER")
* - `gcs.acl.WRITER_ROLE` - ("WRITER")
*
* For more detailed information, see
* [About Access Control Lists](http://goo.gl/6qBBPO).
*
* @type {object}
*
* @example
* var gcs = gcloud.storage({
* projectId: 'grape-spaceship-123'
* });
*
* var albums = gcs.bucket('albums');
*
* //-
* // Make all of the files currently in a bucket publicly readable.
* //-
* albums.acl.add({
* scope: 'allUsers',
* role: gcs.acl.READER_ROLE
* }, function(err, aclObject) {});
*
* //-
* // Make any new objects added to a bucket publicly readable.
* //-
* albums.acl.default.add({
* scope: 'allUsers',
* role: gcs.acl.READER_ROLE
* }, function(err, aclObject) {});
*
* //-
* // Grant a user ownership permissions to a bucket.
* //-
*
* albums.acl.add({
* scope: 'user-useremail@example.com',
* role: gcs.acl.OWNER_ROLE
* }, function(err, aclObject) {});
*/
Storage.acl = {
OWNER_ROLE: 'OWNER',
READER_ROLE: 'READER',
WRITER_ROLE: 'WRITER'
};
Storage.prototype.acl = Storage.acl;
/**
* Get a reference to a Google Cloud Storage bucket.
*
* @param {object|string} name - Name of the existing bucket.
* @return {module:storage/bucket}
*
* @example
* var gcloud = require('gcloud')({
* projectId: 'grape-spaceship-123',
* keyFilename: '/path/to/keyfile.json'
* });
*
* var gcs = gcloud.storage();
*
* var albums = gcs.bucket('albums');
* var photos = gcs.bucket('photos');
*/
Storage.prototype.bucket = function(name) {
return new Bucket(this, name);
};
/**
* Create a bucket.
*
* @throws {Error} If a name is not provided.
*
* @param {string} name - Name of the bucket to create.
* @param {object=} metadata - Metadata to set for the bucket.
* @param {function} callback - The callback function.
*
* @example
* var callback = function(err, bucket, apiResponse) {
* // `bucket` is a Bucket object.
* };
*
* gcs.createBucket('new-bucket', callback);
*
* //-
* // Create a bucket in a specific location and region. <em>See the <a
* // href="https://cloud.google.com/storage/docs/json_api/v1/buckets/insert">
* // Official JSON API docs</a> for complete details on the `location` option.
* // </em>
* //-
* var metadata = {
* location: 'US-CENTRAL1',
* storageClass: 'DURABLE_REDUCED_AVAILABILITY'
* };
*
* gcs.createBucket('new-bucket', metadata, callback);
*
* //-
* // Enable versioning on a new bucket.
* //-
* var metadata = {
* versioning: {
* enabled: true
* }
* };
*
* gcs.createBucket('new-bucket', metadata, callback);
*/
Storage.prototype.createBucket = function(name, metadata, callback) {
var self = this;
if (!name) {
throw new Error('A name is required to create a bucket.');
}
if (!callback) {
callback = metadata;
metadata = {};
}
var query = {
project: this.projectId
};
var body = extend(metadata, {
name: name
});
this.makeReq_('POST', '', query, body, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}
var bucket = self.bucket(name);
bucket.metadata = resp;
callback(null, bucket, resp);
});
};
/**
* Get Bucket objects for all of the buckets in your project.
*
* @param {object=} query - Query object.
* @param {boolean} options.autoPaginate - Have pagination handled
* automatically. Default: true.
* @param {number} query.maxResults - Maximum number of items plus prefixes to
* return.
* @param {string} query.pageToken - A previously-returned page token
* representing part of the larger set of results to view.
* @param {function} callback - The callback function.
*
* @example
* gcs.getBuckets(function(err, buckets) {
* if (!err) {
* // buckets is an array of Bucket objects.
* }
* });
*
* //-
* // To control how many API requests are made and page through the results
* // manually, set `autoPaginate` to `false`.
* //-
* var callback = function(err, buckets, nextQuery) {
* if (nextQuery) {
* // More results exist.
* gcs.getBuckets(nextQuery, callback);
* }
*
* // The `metadata` property is populated for you with the metadata at the
* // time of fetching.
* buckets[0].metadata;
*
* // However, in cases where you are concerned the metadata could have
* // changed, use the `getMetadata` method.
* buckets[0].getMetadata(function(err, metadata, apiResponse) {});
* };
*
* gcs.getBuckets({
* autoPaginate: false
* }, callback);
*
* //-
* // Get the buckets from your project as a readable object stream.
* //-
* gcs.getBuckets()
* .on('error', console.error)
* .on('data', function(bucket) {
* // bucket is a Bucket object.
* })
* .on('end', function() {
* // All buckets retrieved.
* });
*
* //-
* // If you anticipate many results, you can end a stream early to prevent
* // unnecessary processing and API requests.
* //-
* gcs.getBuckets()
* .on('data', function(bucket) {
* this.end();
* });
*/
Storage.prototype.getBuckets = function(query, callback) {
var that = this;
if (!callback) {
callback = query;
query = {};
}
query.project = query.project || this.projectId;
this.makeReq_('GET', '', query, null, function(err, resp) {
if (err) {
callback(err, null, null, resp);
return;
}
var buckets = (resp.items || []).map(function(item) {
var bucket = that.bucket(item.id);
bucket.metadata = item;
return bucket;
});
var nextQuery = null;
if (resp.nextPageToken) {
nextQuery = extend({}, query, { pageToken: resp.nextPageToken });
}
callback(null, buckets, nextQuery, resp);
});
};
/**
* Make a new request object from the provided arguments and wrap the callback
* to intercept non-successful responses.
*
* @private
*
* @param {string} method - Action.
* @param {string} path - Request path.
* @param {*} query - Request query object.
* @param {*} body - Request body contents.
* @param {function} callback - The callback function.
*/
Storage.prototype.makeReq_ = function(method, path, query, body, callback) {
var reqOpts = {
method: method,
qs: query,
uri: STORAGE_BASE_URL + path
};
if (body) {
reqOpts.json = body;
}
this.makeAuthorizedRequest_(reqOpts, callback);
};
/*! Developer Documentation
*
* This method can be used with either a callback or as a readable object
* stream. `streamRouter` is used to add this dual behavior.
*/
streamRouter.extend(Storage, 'getBuckets');
module.exports = Storage;