Skip to content

Commit 143e0f3

Browse files
committed
feat: add storage batch operations samples
1 parent 3465677 commit 143e0f3

File tree

7 files changed

+433
-0
lines changed

7 files changed

+433
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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) {
25+
// [START storage_batch_cancel_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+
// A unique identifier for this job.
34+
// const jobId = '94d60cc1-2d95-41c5-b6e3-ff66cd3532d5';
35+
36+
// Imports the Control library
37+
const {StorageBatchOperationsClient} =
38+
require('@google-cloud/storagebatchoperations').v1;
39+
40+
// Instantiates a client
41+
const client = new StorageBatchOperationsClient();
42+
43+
async function cancelJob() {
44+
const name = client.jobPath(projectId, 'global', jobId);
45+
46+
// Create the request
47+
const request = {
48+
name,
49+
};
50+
51+
// Run request
52+
try {
53+
await client.cancelJob(request);
54+
console.log(`Cancelled job: ${name}`);
55+
} catch (err) {
56+
// This might be expected if the job completed quickly or failed creation
57+
console.log(`INFO: cancelJob threw: ${err.message}`);
58+
}
59+
}
60+
61+
cancelJob().catch(console.error);
62+
// [END storage_batch_cancel_job]
63+
}
64+
65+
process.on('unhandledRejection', err => {
66+
console.error(err.message);
67+
process.exitCode = 1;
68+
});
69+
main(...process.argv.slice(2));
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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));
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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) {
25+
// [START storage_batch_delete_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+
// A unique identifier for this job.
34+
// const jobId = '94d60cc1-2d95-41c5-b6e3-ff66cd3532d5';
35+
36+
// Imports the Control library
37+
const {StorageBatchOperationsClient} =
38+
require('@google-cloud/storagebatchoperations').v1;
39+
40+
// Instantiates a client
41+
const client = new StorageBatchOperationsClient();
42+
43+
async function deleteJob() {
44+
const name = client.jobPath(projectId, 'global', jobId);
45+
46+
// Create the request
47+
const request = {
48+
name,
49+
};
50+
51+
// Run request
52+
await client.deleteJob(request);
53+
console.log(`Deleted job: ${name}`);
54+
}
55+
56+
deleteJob();
57+
// [END storage_batch_delete_job]
58+
}
59+
60+
process.on('unhandledRejection', err => {
61+
console.error(err.message);
62+
process.exitCode = 1;
63+
});
64+
main(...process.argv.slice(2));

storagebatchoperations/getJob.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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) {
25+
// [START storage_batch_get_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+
// A unique identifier for this job.
34+
// const jobId = '94d60cc1-2d95-41c5-b6e3-ff66cd3532d5';
35+
36+
// Imports the Control library
37+
const {StorageBatchOperationsClient} =
38+
require('@google-cloud/storagebatchoperations').v1;
39+
40+
// Instantiates a client
41+
const client = new StorageBatchOperationsClient();
42+
43+
async function getJob() {
44+
const name = client.jobPath(projectId, 'global', jobId);
45+
46+
// Create the request
47+
const request = {
48+
name,
49+
};
50+
51+
// Run request
52+
const [response] = await client.getJob(request);
53+
console.log(`Got job: ${response.name}`);
54+
}
55+
56+
getJob();
57+
// [END storage_batch_get_job]
58+
}
59+
60+
process.on('unhandledRejection', err => {
61+
console.error(err.message);
62+
process.exitCode = 1;
63+
});
64+
main(...process.argv.slice(2));

storagebatchoperations/listJobs.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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) {
25+
// [START storage_batch_list_jobs]
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+
// Imports the Control library
34+
const {StorageBatchOperationsClient} =
35+
require('@google-cloud/storagebatchoperations').v1;
36+
37+
// Instantiates a client
38+
const client = new StorageBatchOperationsClient();
39+
40+
async function listJobs() {
41+
const parent = await client.locationPath(projectId, 'global');
42+
43+
// Create the request
44+
const request = {
45+
parent,
46+
};
47+
48+
// Run request
49+
const [response] = await client.listJobs(request);
50+
for (const jobs of response) {
51+
console.log(jobs.name);
52+
}
53+
}
54+
55+
listJobs();
56+
// [END storage_batch_list_jobs]
57+
}
58+
59+
process.on('unhandledRejection', err => {
60+
console.error(err.message);
61+
process.exitCode = 1;
62+
});
63+
main(...process.argv.slice(2));
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "storage-batch-operations-samples",
3+
"version": "0.0.1",
4+
"author": "Google Inc.",
5+
"license": "Apache-2.0",
6+
"description": "Examples of how to utilize the @google-cloud/storagebatchoperations library.",
7+
"scripts": {
8+
"test": "c8 mocha -p -j 2 system-test --timeout 600000"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
13+
},
14+
"devDependencies": {
15+
"@google-cloud/storage": "^7.17.1",
16+
"@google-cloud/storagebatchoperations": "^0.1.0",
17+
"c8": "^10.0.0",
18+
"chai": "^4.5.0",
19+
"mocha": "^10.7.0",
20+
"uuid": "^10.0.0"
21+
}
22+
}
23+

0 commit comments

Comments
 (0)