Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions storagebatchoperations/cancelJob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/

// Your Google Cloud project ID.
// const projectId = 'my-project-id';

// A unique identifier for this job.
// const jobId = '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 (err) {
// This might be expected if the job completed quickly or failed creation
console.log(`INFO: cancelJob threw: ${err.message}`);
}
}

cancelJob().catch(console.error);
// [END storage_batch_cancel_job]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
87 changes: 87 additions & 0 deletions storagebatchoperations/createJob.js
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, bucketName, objectPrefix) {
// [START storage_batch_create_job]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/

// Your Google Cloud project ID.
// const projectId = 'my-project-id';

// The name of your GCS bucket
// const bucketName = 'bucketName';

// A unique identifier for this job.
// const jobId = '94d60cc1-2d95-41c5-b6e3-ff66cd3532d5';

// The prefix of objects to include in the operation.
// const objectPrefix = '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,
},
},
};

// Run request
const [operation] = await client.createJob(request);
const [response] = await operation.promise();
console.log(`Created job: ${response.name}`);
}

createJob();
// [END storage_batch_create_job]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
64 changes: 64 additions & 0 deletions storagebatchoperations/deleteJob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/

// Your Google Cloud project ID.
// const projectId = 'my-project-id';

// A unique identifier for this job.
// const jobId = '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,
};

// Run request
await client.deleteJob(request);
console.log(`Deleted job: ${name}`);
}

deleteJob();
// [END storage_batch_delete_job]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
64 changes: 64 additions & 0 deletions storagebatchoperations/getJob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/

// Your Google Cloud project ID.
// const projectId = 'my-project-id';

// A unique identifier for this job.
// const jobId = '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,
};

// Run request
const [response] = await client.getJob(request);
console.log(`Got job: ${response.name}`);
}

getJob();
// [END storage_batch_get_job]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
63 changes: 63 additions & 0 deletions storagebatchoperations/listJobs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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) {
// [START storage_batch_list_jobs]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/

// Your Google Cloud project ID.
// const projectId = 'my-project-id';

// Imports the Control library
const {StorageBatchOperationsClient} =
require('@google-cloud/storagebatchoperations').v1;

// Instantiates a client
const client = new StorageBatchOperationsClient();

async function listJobs() {
const parent = await client.locationPath(projectId, 'global');

// Create the request
const request = {
parent,
};

// Run request
const [response] = await client.listJobs(request);
for (const job of response) {
console.log(job.name);
}
}

listJobs();
// [END storage_batch_list_jobs]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
22 changes: 22 additions & 0 deletions storagebatchoperations/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "storage-batch-operations-samples",
"version": "0.0.1",
"author": "Google Inc.",
"license": "Apache-2.0",
"description": "Examples of how to utilize the @google-cloud/storagebatchoperations library.",
"scripts": {
"test": "c8 mocha -p -j 2 system-test --timeout 600000"
},
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"devDependencies": {
"@google-cloud/storage": "^7.17.1",
"@google-cloud/storagebatchoperations": "^0.1.0",
"c8": "^10.0.0",
"chai": "^4.5.0",
"mocha": "^10.7.0",
"uuid": "^10.0.0"
}
}
Loading