|
| 1 | +/** |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +async function main( |
| 20 | + projectId, |
| 21 | + description, |
| 22 | + gcsSourceBucket, |
| 23 | + gcsSinkBucket, |
| 24 | + startDate = new Date() |
| 25 | +) { |
| 26 | + // [START storagetransfer_transfer_to_nearline] |
| 27 | + |
| 28 | + // Imports the Google Cloud client library |
| 29 | + const { |
| 30 | + StorageTransferServiceClient, |
| 31 | + } = require('@google-cloud/storage-transfer'); |
| 32 | + |
| 33 | + /** |
| 34 | + * TODO(developer): Uncomment the following lines before running the sample. |
| 35 | + */ |
| 36 | + // The ID of the Google Cloud Platform Project that owns the job |
| 37 | + // projectId = 'my-project-id' |
| 38 | + |
| 39 | + // A useful description for your transfer job |
| 40 | + // description = 'My transfer job' |
| 41 | + |
| 42 | + // Google Cloud Storage source bucket name |
| 43 | + // gcsSourceBucket = 'my-gcs-source-bucket' |
| 44 | + |
| 45 | + // Google Cloud Storage destination bucket name |
| 46 | + // gcsSinkBucket = 'my-gcs-destination-bucket' |
| 47 | + |
| 48 | + // Date to start daily migration |
| 49 | + // startDate = new Date() |
| 50 | + |
| 51 | + // Creates a client |
| 52 | + const client = new StorageTransferServiceClient(); |
| 53 | + |
| 54 | + /** |
| 55 | + * Create a daily migration from a GCS bucket to another GCS bucket for |
| 56 | + * objects untouched for 30+ days. |
| 57 | + */ |
| 58 | + async function createDailyNearline30DayMigration() { |
| 59 | + // Runs the request and creates the job |
| 60 | + const [transferJob] = await client.createTransferJob({ |
| 61 | + transferJob: { |
| 62 | + projectId, |
| 63 | + description, |
| 64 | + status: 'ENABLED', |
| 65 | + schedule: { |
| 66 | + scheduleStartDate: { |
| 67 | + day: startDate.getDate(), |
| 68 | + month: startDate.getMonth() + 1, |
| 69 | + year: startDate.getFullYear(), |
| 70 | + }, |
| 71 | + }, |
| 72 | + transferSpec: { |
| 73 | + gcsDataSource: { |
| 74 | + bucketName: gcsSourceBucket, |
| 75 | + }, |
| 76 | + gcsDataSink: { |
| 77 | + bucketName: gcsSinkBucket, |
| 78 | + }, |
| 79 | + objectConditions: { |
| 80 | + minTimeElapsedSinceLastModification: { |
| 81 | + seconds: 2592000, // 30 days |
| 82 | + }, |
| 83 | + }, |
| 84 | + transferOptions: { |
| 85 | + deleteObjectsFromSourceAfterTransfer: true, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + console.log(`Created transferJob: ${transferJob.name}`); |
| 92 | + } |
| 93 | + |
| 94 | + createDailyNearline30DayMigration(); |
| 95 | + // [END storagetransfer_transfer_to_nearline] |
| 96 | +} |
| 97 | + |
| 98 | +const [projectId, description, gcsSourceBucket, gcsSinkBucket, startDate] = [ |
| 99 | + ...process.argv.slice(2), |
| 100 | +]; |
| 101 | + |
| 102 | +main( |
| 103 | + projectId, |
| 104 | + description, |
| 105 | + gcsSourceBucket, |
| 106 | + gcsSinkBucket, |
| 107 | + new Date(startDate) |
| 108 | +); |
| 109 | + |
| 110 | +process.on('unhandledRejection', err => { |
| 111 | + console.error(err); |
| 112 | + process.exitCode = 1; |
| 113 | +}); |
0 commit comments