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

Function main

storage-control/createAnywhereCache.js:24–116  ·  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, zoneName)

Source from the content-addressed store, hash-verified

22 */
23
24function main(bucketName, zoneName) {
25 // [START storage_control_create_anywhere_cache]
26
27 /**
28 * Creates an Anywhere Cache instance for a Cloud Storage bucket.
29 * Anywhere Cache is a feature that provides an SSD-backed zonal read cache.
30 * This can significantly improve read performance for frequently accessed data
31 * by caching it in the same zone as your compute resources.
32 *
33 * @param {string} bucketName The name of the bucket to create the cache for.
34 * Example: 'your-gcp-bucket-name'
35 * @param {string} zoneName The zone where the cache will be created.
36 * Example: 'us-central1-a'
37 */
38
39 // Imports the Control library
40 const {StorageControlClient} = require('@google-cloud/storage-control').v2;
41
42 // Instantiates a client
43 const controlClient = new StorageControlClient();
44
45 async function callCreateAnywhereCache() {
46 const bucketPath = controlClient.bucketPath('_', bucketName);
47
48 // Create the request
49 const request = {
50 parent: bucketPath,
51 anywhereCache: {
52 zone: zoneName,
53 ttl: {
54 seconds: '10000s',
55 }, // Optional. Default: '86400s'(1 day)
56 admissionPolicy: 'admit-on-first-miss', // Optional. Default: 'admit-on-first-miss'
57 },
58 };
59
60 try {
61 // Run the request, which returns an Operation object
62 const [operation] = await controlClient.createAnywhereCache(request);
63 console.log(`Waiting for operation ${operation.name} to complete...`);
64
65 // Wait for the operation to complete and get the final resource
66 const anywhereCache = await checkCreateAnywhereCacheProgress(
67 operation.name
68 );
69 console.log(`Created anywhere cache: ${anywhereCache.result.name}.`);
70 } catch (error) {
71 // Handle any error that occurred during the creation or polling process.
72 console.error('Failed to create Anywhere Cache:', error.message);
73 throw error;
74 }
75 }
76
77 // A custom function to check the operation's progress.
78 async function checkCreateAnywhereCacheProgress(operationName) {
79 let operation = {done: false};
80 console.log('Starting manual polling for operation...');
81

Callers 1

Calls 1

callCreateAnywhereCacheFunction · 0.85

Tested by

no test coverage detected