|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +/** |
| 18 | + * This application demonstrates how to perform basic operations on an Batch Operations |
| 19 | + * instance with the Google Cloud Storage API. |
| 20 | + * |
| 21 | + * For more information, see the documentation at https://cloud.google.com/storage/docs/batch-operations/overview. |
| 22 | + */ |
| 23 | + |
| 24 | +function main(projectId, jobId, bucketName, objectPrefix) { |
| 25 | + // [START storage_batch_create_job] |
| 26 | + /** |
| 27 | + * TODO(developer): Uncomment these variables before running the sample. |
| 28 | + */ |
| 29 | + |
| 30 | + // Your Google Cloud project ID. |
| 31 | + // const projectId = 'my-project-id'; |
| 32 | + |
| 33 | + // The name of your GCS bucket |
| 34 | + // const bucketName = 'bucketName'; |
| 35 | + |
| 36 | + // A unique identifier for this job. |
| 37 | + // const jobId = '94d60cc1-2d95-41c5-b6e3-ff66cd3532d5'; |
| 38 | + |
| 39 | + // The prefix of objects to include in the operation. |
| 40 | + // const objectPrefix = 'prefix1'; |
| 41 | + |
| 42 | + // Imports the Control library |
| 43 | + const {StorageBatchOperationsClient} = |
| 44 | + require('@google-cloud/storagebatchoperations').v1; |
| 45 | + |
| 46 | + // Instantiates a client |
| 47 | + const client = new StorageBatchOperationsClient(); |
| 48 | + |
| 49 | + async function createJob() { |
| 50 | + const parent = await client.locationPath(projectId, 'global'); |
| 51 | + |
| 52 | + // Create the request |
| 53 | + const request = { |
| 54 | + parent, |
| 55 | + jobId, |
| 56 | + job: { |
| 57 | + bucketList: { |
| 58 | + buckets: [ |
| 59 | + { |
| 60 | + bucket: bucketName, |
| 61 | + prefixList: { |
| 62 | + includedObjectPrefixes: [objectPrefix], |
| 63 | + }, |
| 64 | + }, |
| 65 | + ], |
| 66 | + }, |
| 67 | + deleteObject: { |
| 68 | + permanentObjectDeletionEnabled: false, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }; |
| 72 | + |
| 73 | + // Run request |
| 74 | + const [operation] = await client.createJob(request); |
| 75 | + const [response] = await operation.promise(); |
| 76 | + console.log(`Created job: ${response.name}`); |
| 77 | + } |
| 78 | + |
| 79 | + createJob(); |
| 80 | + // [END storage_batch_create_job] |
| 81 | +} |
| 82 | + |
| 83 | +process.on('unhandledRejection', err => { |
| 84 | + console.error(err.message); |
| 85 | + process.exitCode = 1; |
| 86 | +}); |
| 87 | +main(...process.argv.slice(2)); |
0 commit comments