-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: add storage batch operations samples #4180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thiyaguk09
wants to merge
9
commits into
GoogleCloudPlatform:main
from
thiyaguk09:storagebatchoperations-samples
+642
−0
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dfad2cf
feat: add storage batch operations samples
thiyaguk09 8b9d2a2
bug fix
thiyaguk09 3ea4225
added test case
thiyaguk09 7b85f34
lint fix
thiyaguk09 7dceecc
feat(storage-batch): Improve Batch Operation API samples (Documentati...
thiyaguk09 a2f6598
Incorporated feedback from review comments
thiyaguk09 07bc988
Merge branch 'main' into storagebatchoperations-samples
ddelgrosso1 fcf02df
Merge branch 'main' into storagebatchoperations-samples
thiyaguk09 bf44b8e
Merge branch 'main' into storagebatchoperations-samples
thiyaguk09 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // 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 | ||
| // | ||
| // https://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'; | ||
|
|
||
| /** | ||
| * This application demonstrates how to perform basic operations on an Batch Operations | ||
| * instance with the Google Cloud Storage API. | ||
| * | ||
| * For more information, see the documentation at https://cloud.google.com/storage/docs/batch-operations/overview. | ||
| */ | ||
|
|
||
| function main(projectId, jobId) { | ||
| // [START storage_batch_cancel_job] | ||
|
|
||
| /** | ||
| * Cancel a batch job instance. | ||
| * | ||
| * The operation to cancel a batch job instance in Google Cloud Storage (GCS) is used to stop | ||
| * a running or queued asynchronous task that is currently processing a large number of GCS objects. | ||
| * | ||
| * @param {string} projectId The Google Cloud project ID. | ||
| * Example: 'my-project-id' | ||
| * @param {string} jobId A unique identifier for this job. | ||
| * Example: '94d60cc1-2d95-41c5-b6e3-ff66cd3532d5' | ||
| */ | ||
|
|
||
| // Imports the Control library | ||
| const {StorageBatchOperationsClient} = | ||
| require('@google-cloud/storagebatchoperations').v1; | ||
|
|
||
| // Instantiates a client | ||
| const client = new StorageBatchOperationsClient(); | ||
|
|
||
| async function cancelJob() { | ||
| const name = client.jobPath(projectId, 'global', jobId); | ||
|
|
||
| // Create the request | ||
| const request = { | ||
| name, | ||
| }; | ||
|
|
||
| // Run request | ||
| try { | ||
| await client.cancelJob(request); | ||
| console.log(`Cancelled job: ${name}`); | ||
| } catch (error) { | ||
| // This might be expected if the job completed quickly or failed creation | ||
| console.error( | ||
| `Error canceling batch jobs for jobId ${jobId}:`, | ||
| error.message | ||
| ); | ||
|
|
||
| if (error.code === 5) { | ||
| // NOT_FOUND (gRPC code 5) error can occur if the batch job does not exist. | ||
| console.error( | ||
| `Ensure the job '${jobId}' exists in project '${projectId}'.` | ||
| ); | ||
| } else if (error.code === 9) { | ||
| // FAILED_PRECONDITION (gRPC code 9) can occur if the job is already being cancelled | ||
| // or is not in a RUNNING state that allows the cancel operation. | ||
| console.error( | ||
| `Batch job '${jobId}' may not be in a state that allows canceling (e.g., must be RUNNING).` | ||
| ); | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| cancelJob(); | ||
| // [END storage_batch_cancel_job] | ||
| } | ||
|
|
||
| process.on('unhandledRejection', err => { | ||
| console.error(err.message); | ||
| process.exitCode = 1; | ||
| }); | ||
| main(...process.argv.slice(2)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // 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 | ||
| // | ||
| // https://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'; | ||
|
|
||
| /** | ||
| * This application demonstrates how to perform basic operations on an Batch Operations | ||
| * instance with the Google Cloud Storage API. | ||
| * | ||
| * For more information, see the documentation at https://cloud.google.com/storage/docs/batch-operations/overview. | ||
| */ | ||
|
|
||
| function main(projectId, jobId, bucketName, objectPrefix) { | ||
| // [START storage_batch_create_job] | ||
|
|
||
| /** | ||
| * Create a new batch job instance. | ||
| * | ||
| * @param {string} projectId Your Google Cloud project ID. | ||
| * Example: 'my-project-id' | ||
| * @param {string} bucketName The name of your GCS bucket. | ||
| * Example: 'your-gcp-bucket-name' | ||
| * @param {string} jobId A unique identifier for this job. | ||
| * Example: '94d60cc1-2d95-41c5-b6e3-ff66cd3532d5' | ||
| * @param {string} objectPrefix The prefix of objects to include in the operation. | ||
| * Example: 'prefix1' | ||
| */ | ||
|
|
||
| // Imports the Control library | ||
| const {StorageBatchOperationsClient} = | ||
| require('@google-cloud/storagebatchoperations').v1; | ||
|
|
||
| // Instantiates a client | ||
| const client = new StorageBatchOperationsClient(); | ||
|
|
||
| async function createJob() { | ||
| const parent = await client.locationPath(projectId, 'global'); | ||
|
|
||
| // Create the request | ||
| const request = { | ||
| parent, | ||
| jobId, | ||
| job: { | ||
| bucketList: { | ||
| buckets: [ | ||
| { | ||
| bucket: bucketName, | ||
| prefixList: { | ||
| includedObjectPrefixes: [objectPrefix], | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| deleteObject: { | ||
| permanentObjectDeletionEnabled: false, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| try { | ||
| // Run the request, which returns an Operation object | ||
| const [operation] = await client.createJob(request); | ||
| console.log(`Waiting for operation ${operation.name} to complete...`); | ||
|
|
||
| // Wait for the operation to complete and get the final resource | ||
| const [response] = await operation.promise(); | ||
| console.log(`Created job: ${response.name}`); | ||
| } catch (error) { | ||
| console.error('Failed to create batch job:', error.message); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| createJob(); | ||
| // [END storage_batch_create_job] | ||
| } | ||
|
|
||
| process.on('unhandledRejection', err => { | ||
| console.error(err.message); | ||
| process.exitCode = 1; | ||
| }); | ||
| main(...process.argv.slice(2)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // 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 | ||
| // | ||
| // https://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'; | ||
|
|
||
| /** | ||
| * This application demonstrates how to perform basic operations on an Batch Operations | ||
| * instance with the Google Cloud Storage API. | ||
| * | ||
| * For more information, see the documentation at https://cloud.google.com/storage/docs/batch-operations/overview. | ||
| */ | ||
|
|
||
| function main(projectId, jobId) { | ||
| // [START storage_batch_delete_job] | ||
| /** | ||
| * Delete a batch job instance. | ||
| * | ||
| * This operation is used to remove a completed, failed, or cancelled Batch Operation | ||
| * job from the system's list. It is essentially a cleanup action. | ||
| * | ||
| * @param {string} projectId Your Google Cloud project ID. | ||
| * Example: 'my-project-id' | ||
| * @param {string} jobId A unique identifier for this job. | ||
| * Example: '94d60cc1-2d95-41c5-b6e3-ff66cd3532d5' | ||
| */ | ||
|
|
||
| // Imports the Control library | ||
| const {StorageBatchOperationsClient} = | ||
| require('@google-cloud/storagebatchoperations').v1; | ||
|
|
||
| // Instantiates a client | ||
| const client = new StorageBatchOperationsClient(); | ||
|
|
||
| async function deleteJob() { | ||
| const name = client.jobPath(projectId, 'global', jobId); | ||
|
|
||
| // Create the request | ||
| const request = { | ||
| name, | ||
| }; | ||
|
|
||
| try { | ||
| // Run request | ||
| await client.deleteJob(request); | ||
| console.log(`Deleted job: ${name}`); | ||
| } catch (error) { | ||
| console.error( | ||
| `Error deleting batch jobs for jobId ${jobId}:`, | ||
| error.message | ||
| ); | ||
|
|
||
| if (error.code === 5) { | ||
| // NOT_FOUND (gRPC code 5) error can occur if the batch job does not exist. | ||
| console.error( | ||
| `Ensure the job '${jobId}' exists in project '${projectId}'.` | ||
| ); | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| deleteJob(); | ||
| // [END storage_batch_delete_job] | ||
| } | ||
|
|
||
| process.on('unhandledRejection', err => { | ||
| console.error(err.message); | ||
| process.exitCode = 1; | ||
| }); | ||
| main(...process.argv.slice(2)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // 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 | ||
| // | ||
| // https://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'; | ||
|
|
||
| /** | ||
| * This application demonstrates how to perform basic operations on an Batch Operations | ||
| * instance with the Google Cloud Storage API. | ||
| * | ||
| * For more information, see the documentation at https://cloud.google.com/storage/docs/batch-operations/overview. | ||
| */ | ||
|
|
||
| function main(projectId, jobId) { | ||
| // [START storage_batch_get_job] | ||
| /** | ||
| * Retrieves details of a specific batch job instance. | ||
| * | ||
| * This operation is used to retrieve the detailed current state, execution status, | ||
| * and original configuration of a specific Batch Operation job that was previously | ||
| * created for a Google Cloud Storage bucket. | ||
| * | ||
| * @param {string} projectId Your Google Cloud project ID. | ||
| * Example: 'my-project-id' | ||
| * @param {string} jobId A unique identifier for this job. | ||
| * Example: '94d60cc1-2d95-41c5-b6e3-ff66cd3532d5' | ||
| */ | ||
|
|
||
| // Imports the Control library | ||
| const {StorageBatchOperationsClient} = | ||
| require('@google-cloud/storagebatchoperations').v1; | ||
|
|
||
| // Instantiates a client | ||
| const client = new StorageBatchOperationsClient(); | ||
|
|
||
| async function getJob() { | ||
| const name = client.jobPath(projectId, 'global', jobId); | ||
|
|
||
| // Create the request | ||
| const request = { | ||
| name, | ||
| }; | ||
|
|
||
| try { | ||
| // Run request | ||
| const [response] = await client.getJob(request); | ||
| console.log(`Batch job details for '${jobId}':`); | ||
| console.log(`Name: ${response.name}`); | ||
| console.log(`State: ${response.state}`); | ||
| console.log( | ||
| `Create Time: ${new Date(response.createTime.seconds * 1000).toISOString()}` | ||
| ); | ||
| } catch (error) { | ||
| console.error( | ||
| `Error retrieving batch jobs for jobId ${jobId}:`, | ||
| error.message | ||
| ); | ||
|
|
||
| if (error.code === 5) { | ||
| // NOT_FOUND (gRPC code 5) error can occur if the batch job does not exist. | ||
| console.error( | ||
| `Ensure the job '${jobId}' exists in project '${projectId}'.` | ||
| ); | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| getJob(); | ||
| // [END storage_batch_get_job] | ||
| } | ||
|
|
||
| process.on('unhandledRejection', err => { | ||
| console.error(err.message); | ||
| process.exitCode = 1; | ||
| }); | ||
| main(...process.argv.slice(2)); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.