forked from GoogleCloudPlatform/nodejs-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfer.js
420 lines (380 loc) · 13.2 KB
/
transfer.js
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Copyright 2015-2016, Google, Inc.
// 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.
'use strict';
// [START all]
// [START setup]
var moment = require('moment');
var google = require('googleapis');
// Instantiate a storage client
var storagetransfer = google.storagetransfer('v1');
// [END setup]
// [START auth]
function auth (callback) {
google.auth.getApplicationDefault(function (err, authClient) {
if (err) {
return callback(err);
}
// The createScopedRequired method returns true when running on GAE or a
// local developer machine. In that case, the desired scopes must be passed
// in manually. When the code is running in GCE or a Managed VM, the scopes
// are pulled from the GCE metadata server.
// See https://cloud.google.com/compute/docs/authentication for more
// information.
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
// Scopes can be specified either as an array or as a single,
// space-delimited string.
authClient = authClient.createScoped([
'https://www.googleapis.com/auth/cloud-platform'
]);
}
callback(null, authClient);
});
}
// [END auth]
// [START create_transfer_job]
/**
* Review the transfer operations associated with a transfer job.
*
* @param {object} options Configuration options.
* @param {string} options.srcBucket The name of the source bucket.
* @param {string} options.destBucket The name of the destination bucket.
* @param {string} options.date The date of the first transfer in the format YYYY/MM/DD.
* @param {string} options.time The time of the first transfer in the format HH:MM.
* @param {string} [options.description] Optional. Description for the new transfer job.
* @param {function} callback The callback function.
*/
function createTransferJob (options, callback) {
var startDate = moment(options.date, 'YYYY/MM/DD');
var transferTime = moment(options.time, 'HH:mm');
auth(function (err, authClient) {
if (err) {
return callback(err);
}
var transferJob = {
projectId: process.env.GCLOUD_PROJECT,
status: 'ENABLED',
transferSpec: {
gcsDataSource: {
bucketName: options.srcBucket
},
gcsDataSink: {
bucketName: options.destBucket
},
transferOptions: {
deleteObjectsFromSourceAfterTransfer: false
}
},
schedule: {
scheduleStartDate: {
year: startDate.year(),
month: startDate.month() + 1,
day: startDate.date()
},
startTimeOfDay: {
hours: transferTime.hours(),
minutes: transferTime.minutes()
}
}
};
if (options.description) {
transferJob.description = options.description;
}
storagetransfer.transferJobs.create({
auth: authClient,
resource: transferJob
}, function (err, transferJob) {
if (err) {
return callback(err);
}
console.log('Created transfer job: %s', transferJob.name);
return callback(null, transferJob);
});
});
}
// [END create_transfer_job]
// [START get_transfer_job]
/**
* Get a transfer job.
*
* @param {string} jobName The name of the transfer job to get.
* @param {function} callback The callback function.
*/
function getTransferJob (jobName, callback) {
auth(function (err, authClient) {
if (err) {
return callback(err);
}
storagetransfer.transferJobs.get({
auth: authClient,
projectId: process.env.GCLOUD_PROJECT,
jobName: jobName
}, function (err, transferJob) {
if (err) {
return callback(err);
}
console.log('Found transfer job: %s', transferJob.name);
return callback(null, transferJob);
});
});
}
// [END get_transfer_job]
// [START update_transfer_job]
/**
* Get a transfer job.
*
* @param {object} options Configuration options.
* @param {string} options.job The name of the transfer job to get.
* @param {string} options.field The field to update. Can be description, status, or transferSpec.
* @param {string} options.value The new value for the field.
* @param {function} callback The callback function.
*/
function updateTransferJob (options, callback) {
auth(function (err, authClient) {
if (err) {
return callback(err);
}
var patchRequest = {
projectId: process.env.GCLOUD_PROJECT,
transferJob: {
name: options.job
},
updateTransferJobFieldMask: options.field
};
if (options.field === 'description') {
patchRequest.transferJob.description = options.value;
} else if (options.field === 'status') {
patchRequest.transferJob.status = options.value;
} else if (options.field === 'transferSpec') {
patchRequest.transferJob.transferSpec = JSON.parse(options.value);
}
storagetransfer.transferJobs.patch({
auth: authClient,
jobName: options.job,
resource: patchRequest
}, function (err, transferJob) {
if (err) {
return callback(err);
}
console.log('Updated transfer job: %s', transferJob.name);
return callback(null, transferJob);
});
});
}
// [END update_transfer_job]
// [START list_transfer_jobs]
/**
* List transfer jobs for the authenticated project.
*
* @param {function} callback The callback function.
*/
function listTransferJobs (callback) {
auth(function (err, authClient) {
if (err) {
return callback(err);
}
storagetransfer.transferJobs.list({
auth: authClient,
filter: JSON.stringify({ project_id: process.env.GCLOUD_PROJECT })
}, function (err, result) {
if (err) {
return callback(err);
} else if (!result.transferJobs) {
return callback(null, []);
}
console.log('Found %d jobs!', result.transferJobs.length);
return callback(null, result.transferJobs);
});
});
}
// [END list_transfer_jobs]
// [START list_transfer_operations]
/**
* List transfer operations in the authenticated project.
*
* @param {string} [jobName] An optional job name by which to filter results.
* @param {function} callback The callback function.
*/
function listTransferOperations (jobName, callback) {
auth(function (err, authClient) {
if (err) {
return callback(err);
}
var filter = {
project_id: process.env.GCLOUD_PROJECT
};
if (jobName) {
filter.job_names = [jobName];
}
storagetransfer.transferOperations.list({
name: 'transferOperations',
filter: JSON.stringify(filter),
auth: authClient
}, function (err, result, apiResponse) {
if (err) {
return callback(err);
} else if (!result.operations) {
return callback(null, []);
}
console.log('Found %d operations!', result.operations.length);
return callback(null, result.operations);
});
});
}
// [END list_transfer_operations]
// [START get_transfer_operation]
/**
* Get the specified transfer operation.
*
* @param {string} transferOperationName The name of the transfer operation.
* @param {function} callback The callback function.
*/
function getTransferOperation (transferOperationName, callback) {
auth(function (err, authClient) {
if (err) {
return callback(err);
}
storagetransfer.transferOperations.get({
name: transferOperationName,
auth: authClient
}, function (err, transferOperation) {
if (err) {
return callback(err);
}
console.log('Found transfer operation: %s', transferOperation);
return callback(null, transferOperation);
});
});
}
// [END get_transfer_operation]
// [START pause_transfer_operation]
/**
* Pause the specified transfer operation.
*
* @param {string} transferOperationName The name of the transfer operation.
* @param {function} callback The callback function.
*/
function pauseTransferOperation (transferOperationName, callback) {
auth(function (err, authClient) {
if (err) {
return callback(err);
}
storagetransfer.transferOperations.pause({
name: transferOperationName,
auth: authClient
}, function (err) {
if (err) {
return callback(err);
}
console.log('Paused transfer operation: %s', transferOperationName);
return callback(null);
});
});
}
// [END pause_transfer_operation]
// [START resume_transfer_operation]
/**
* Pause the specified transfer operation.
*
* @param {string} transferOperationName The name of the transfer operation.
* @param {function} callback The callback function.
*/
function resumeTransferOperation (transferOperationName, callback) {
auth(function (err, authClient) {
if (err) {
return callback(err);
}
storagetransfer.transferOperations.resume({
name: transferOperationName,
auth: authClient
}, function (err) {
if (err) {
return callback(err);
}
console.log('Resumed transfer operation: %s', transferOperationName);
return callback(null);
});
});
}
// [END resume_transfer_operation]
// [END all]
// The command-line program
var cli = require('yargs');
var utils = require('../utils');
var program = module.exports = {
createTransferJob: createTransferJob,
getTransferJob: getTransferJob,
listTransferJobs: listTransferJobs,
updateTransferJob: updateTransferJob,
listTransferOperations: listTransferOperations,
getTransferOperation: getTransferOperation,
pauseTransferOperation: pauseTransferOperation,
resumeTransferOperation: resumeTransferOperation,
main: function (args) {
// Run the command-line program
cli.help().strict().parse(args).argv;
}
};
cli
.demand(1)
.command('jobs <cmd> [args]', 'Run a job command.', function (yargs) {
yargs
.demand(2)
.command('create <srcBucket> <destBucket> <time> <date> [description]', 'Create a transfer job.', {}, function (options) {
program.createTransferJob(utils.pick(options, ['srcBucket', 'destBucket', 'time', 'date', 'description']), utils.makeHandler(false));
})
.command('get <job>', 'Get a transfer job.', {}, function (options) {
program.getTransferJob(options.job, utils.makeHandler());
})
.command('list', 'List transfer jobs.', {}, function (options) {
program.listTransferJobs(utils.makeHandler());
})
.command('set <job> <field> <value>', 'Change the status, description or transferSpec of a transfer job.', {}, function (options) {
program.updateTransferJob(utils.pick(options, ['job', 'field', 'value']), utils.makeHandler(false));
})
.example('node $0 jobs create my-bucket my-other-bucket 2016/08/12 16:30 "Move my files"', 'Create a transfer job.')
.example('node $0 jobs get transferJobs/123456789012345678', 'Get a transfer job.')
.example('node $0 jobs list', 'List transfer jobs.')
.example('node $0 jobs set transferJobs/123456789012345678 description "My new description"', 'Update the description for a transfer job.')
.example('node $0 jobs set transferJobs/123456789012345678 status DISABLED', 'Disable a transfer job.')
.wrap(100);
}, function () {})
.command('operations <cmd> [args]', 'Run an operation command.', function (yargs) {
yargs
.demand(2)
.command('list [job]', 'List transfer operations, optionally filtering by a job name.', {}, function (options) {
program.listTransferOperations(options.job, utils.makeHandler());
})
.command('get <operation>', 'Get a transfer operation.', {}, function (options) {
program.getTransferOperation(options.operation, utils.makeHandler());
})
.command('pause <operation>', 'Pause a transfer operation.', {}, function (options) {
program.pauseTransferOperation(options.operation, utils.makeHandler(false));
})
.command('resume <operation>', 'Resume a transfer operation.', {}, function (options) {
program.resumeTransferOperation(options.operation, utils.makeHandler(false));
})
.example('node $0 operations list', 'List all transfer operations.')
.example('node $0 operations list transferJobs/123456789012345678', 'List all transfer operations for a specific job.')
.example('node $0 operations get transferOperations/123456789012345678', 'Get a transfer operation.')
.example('node $0 operations pause transferOperations/123456789012345678', 'Pause a transfer operation.')
.example('node $0 operations resume transferOperations/123456789012345678', 'Resume a transfer operation.')
.wrap(100);
}, function () {})
.example('node $0 jobs --help', 'Show job commands.')
.example('node $0 operations --help', 'Show operations commands.')
.wrap(100)
.recommendCommands()
.epilogue('For more information, see https://cloud.google.com/storage/transfer');
if (module === require.main) {
program.main(process.argv.slice(2));
}