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

Function main

storage-control/getAnywhereCache.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_get_anywhere_cache]
26 /**
27 * Retrieves details of a specific Anywhere Cache instance.
28 *
29 * This function is useful for checking the current state, configuration (like TTL),
30 * and other metadata of an existing cache.
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 callGetAnywhereCache() {
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.getAnywhereCache(request);
59 console.log(`Anywhere Cache details for '${cacheName}':`);
60 console.log(` Name: ${response.name}`);
61 console.log(` Zone: ${response.zone}`);
62 console.log(` State: ${response.state}`);
63 console.log(` TTL: ${response.ttl.seconds}s`);
64 console.log(` Admission Policy: ${response.admissionPolicy}`);
65 console.log(
66 ` Create Time: ${new Date(response.createTime.seconds * 1000).toISOString()}`
67 );
68 } catch (error) {
69 // Handle errors (e.g., cache not found, permission denied).
70 console.error(
71 `Error retrieving Anywhere Cache '${cacheName}': ${error.message}`
72 );
73
74 if (error.code === 5) {
75 console.error(
76 `Ensure the cache '${cacheName}' exists in bucket '${bucketName}'.`
77 );
78 }
79 throw error;
80 }
81 }

Callers 1

Calls 1

callGetAnywhereCacheFunction · 0.85

Tested by

no test coverage detected