MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

storage-control/resumeAnywhereCache.js:24–85  ·  view source on GitHub ↗

* This application demonstrates how to perform basic operations on an Anywhere Cache * instance with the Google Cloud Storage API. * * For more information, see the documentation at https://cloud.google.com/storage/docs/anywhere-cache.

(bucketName, cacheName)

Source from the content-addressed store, hash-verified

22 */
23
24function main(bucketName, cacheName) {
25 // [START storage_control_resume_anywhere_cache]
26 /**
27 * Resumes a disabled Anywhere Cache instance.
28 *
29 * This action reverts a cache from a PAUSED state or a DISABLED state back to RUNNING,
30 * provided it is done within the 1-hour grace period before the cache is permanently deleted.
31 *
32 * @param {string} bucketName The name of the bucket where the cache resides.
33 * Example: 'your-gcp-bucket-name'
34 * @param {string} cacheName The unique identifier of the cache instance.
35 * Example: 'my-anywhere-cache-id'
36 */
37
38 // Imports the Control library
39 const {StorageControlClient} = require('@google-cloud/storage-control').v2;
40
41 // Instantiates a client
42 const controlClient = new StorageControlClient();
43
44 async function callResumeAnywhereCache() {
45 const anywhereCachePath = controlClient.anywhereCachePath(
46 '_',
47 bucketName,
48 cacheName
49 );
50
51 // Create the request
52 const request = {
53 name: anywhereCachePath,
54 };
55
56 try {
57 // Run request
58 const [response] = await controlClient.resumeAnywhereCache(request);
59
60 console.log(`Successfully resumed anywhere cache: ${response.name}.`);
61 console.log(` Current State: ${response.state}`);
62 } catch (error) {
63 // Catch and handle potential API errors.
64 console.error(
65 `Error resuming Anywhere Cache '${cacheName}': ${error.message}`
66 );
67
68 if (error.code === 5) {
69 // NOT_FOUND (gRPC code 5)
70 console.error(
71 `Please ensure the cache '${cacheName}' exists in bucket '${bucketName}'.`
72 );
73 } else if (error.code === 9) {
74 // FAILED_PRECONDITION (gRPC code 9)
75 console.error(
76 `Cache '${cacheName}' may not be in a state that allows resuming (e.g., already RUNNING or past the 1-hour deletion grace period).`
77 );
78 }
79 throw error;
80 }
81 }

Callers 1

Calls 1

callResumeAnywhereCacheFunction · 0.85

Tested by

no test coverage detected