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

Function main

storage-control/updateAnywhereCache.js:24–94  ·  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, admissionPolicy)

Source from the content-addressed store, hash-verified

22 */
23
24function main(bucketName, cacheName, admissionPolicy) {
25 // [START storage_control_update_anywhere_cache]
26 /**
27 * Updates the Admission Policy of an Anywhere Cache instance.
28 *
29 * @param {string} bucketName The name of the bucket where the cache resides.
30 * Example: 'your-gcp-bucket-name'
31 * @param {string} cacheName The unique identifier of the cache instance to update.
32 * Example: 'my-anywhere-cache-id'
33 * @param {string} admissionPolicy Determines when data is ingested into the cache
34 * Example: 'admit-on-second-miss'
35 */
36
37 // Imports the Control library
38 const {StorageControlClient} = require('@google-cloud/storage-control').v2;
39
40 // Instantiates a client
41 const controlClient = new StorageControlClient();
42
43 async function callUpdateAnywhereCache() {
44 const anywhereCachePath = controlClient.anywhereCachePath(
45 '_',
46 bucketName,
47 cacheName
48 );
49
50 // Create the request
51 const request = {
52 anywhereCache: {
53 name: anywhereCachePath,
54 admissionPolicy: admissionPolicy,
55 },
56 updateMask: {
57 paths: ['admission_policy'],
58 },
59 };
60
61 try {
62 // Run request
63 const [operation] = await controlClient.updateAnywhereCache(request);
64 console.log(
65 `Waiting for update operation ${operation.name} to complete...`
66 );
67
68 const [response] = await operation.promise();
69
70 console.log(`Updated anywhere cache: ${response.name}.`);
71 } catch (error) {
72 // Handle errors during the initial request or during the LRO polling.
73 console.error(
74 `Error updating Anywhere Cache '${cacheName}': ${error.message}`
75 );
76
77 if (error.code === 5) {
78 // NOT_FOUND (gRPC code 5)
79 console.error(
80 `Ensure the cache '${cacheName}' exists in bucket '${bucketName}'.`
81 );

Callers 1

Calls 1

callUpdateAnywhereCacheFunction · 0.85

Tested by

no test coverage detected