### Install Google Cloud Storage Library Source: https://context7.com/googleapis/nodejs-storage/llms.txt Install the official Google Cloud Storage client library for Node.js via npm. ```bash npm install @google-cloud/storage ``` -------------------------------- ### Create a Storage Bucket with Node.js Source: https://github.com/googleapis/nodejs-storage/blob/main/README.md Demonstrates how to initialize the Storage client and create a new bucket in Google Cloud Storage. This requires proper authentication setup via Application Default Credentials or a service account key file. ```javascript const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); const bucketName = 'your-unique-bucket-name'; async function createBucket() { await storage.createBucket(bucketName); console.log(`Bucket ${bucketName} created.`); } createBucket().catch(console.error); ``` -------------------------------- ### Run Node.js Storage Benchmark Source: https://github.com/googleapis/nodejs-storage/blob/main/internal-tooling/README.md This command sequence demonstrates how to set up and run the Node.js storage benchmarking tool. It involves navigating to the project directory, installing dependencies, changing to the build directory, and executing the performance test script with custom iterations. ```bash cd nodejs-storage npm install cd build/internal-tooling node performanceTest.js --iterations 10000 ``` -------------------------------- ### Copy Files in Google Cloud Storage (Node.js) Source: https://context7.com/googleapis/nodejs-storage/llms.txt Provides examples for copying files within the same Google Cloud Storage bucket or between different buckets. Demonstrates copying with conditional preconditions and copying to a new location within the same bucket. Requires the '@google-cloud/storage' package. ```javascript const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); async function copyFile() { const srcBucketName = 'source-bucket'; const srcFileName = 'original.txt'; const destBucketName = 'dest-bucket'; const destFileName = 'copied.txt'; const copyDestination = storage.bucket(destBucketName).file(destFileName); // Copy to another bucket await storage .bucket(srcBucketName) .file(srcFileName) .copy(copyDestination, { preconditionOpts: { ifGenerationMatch: 0 } }); console.log(`gs://${srcBucketName}/${srcFileName} copied to gs://${destBucketName}/${destFileName}`); // Copy within same bucket await storage .bucket(srcBucketName) .file(srcFileName) .copy('backup/original.txt'); } copyFile().catch(console.error); ``` -------------------------------- ### GET /b Source: https://github.com/googleapis/nodejs-storage/blob/main/README.md Retrieves a list of all buckets associated with the authenticated project. ```APIDOC ## GET /b ### Description Lists all buckets in the project. ### Method GET ### Endpoint /b ### Parameters #### Query Parameters - **project** (string) - Required - The project ID to list buckets for. ### Request Example GET /b?project=my-project-id ### Response #### Success Response (200) - **items** (array) - A list of bucket resource objects. #### Response Example { "items": [{ "name": "bucket-name", "kind": "storage#bucket" }] } ``` -------------------------------- ### Move/Rename Files in Google Cloud Storage (Node.js) Source: https://context7.com/googleapis/nodejs-storage/llms.txt Illustrates how to move or rename files within a Google Cloud Storage bucket. Includes examples for renaming a file and moving a file to a different directory, optionally using preconditions. Requires the '@google-cloud/storage' package. ```javascript const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); async function moveFile() { const bucketName = 'my-bucket'; const srcFileName = 'old-name.txt'; const destFileName = 'new-name.txt'; // Move/rename file await storage .bucket(bucketName) .file(srcFileName) .move(destFileName, { preconditionOpts: { ifGenerationMatch: 0 } }); console.log(`gs://${bucketName}/${srcFileName} moved to gs://${bucketName}/${destFileName}`); // Move to different directory await storage .bucket(bucketName) .file('data/file.txt') .move('archive/file.txt'); } moveFile().catch(console.error); ``` -------------------------------- ### Stream Upload to Cloud Storage (Node.js) Source: https://context7.com/googleapis/nodejs-storage/llms.txt Provides an example of uploading data to Cloud Storage using Node.js streams. This approach is memory-efficient for large files as it processes data in chunks. It demonstrates creating a write stream and piping data from various sources, including other streams or files. ```javascript const {Storage} = require('@google-cloud/storage'); const stream = require('stream'); const fs = require('fs'); const storage = new Storage(); async function streamUpload() { const bucketName = 'my-bucket'; const destFileName = 'streamed-file.txt'; const file = storage.bucket(bucketName).file(destFileName); // Create write stream and pipe data const writeStream = file.createWriteStream({ resumable: true, contentType: 'text/plain' }); // From string using PassThrough const passthroughStream = new stream.PassThrough(); passthroughStream.write('Stream content here'); passthroughStream.end(); passthroughStream.pipe(writeStream) .on('finish', () => { console.log('Upload complete'); }) .on('error', (err) => { console.error('Upload failed:', err); }); // From file stream fs.createReadStream('./large-file.txt') .pipe(file.createWriteStream()) .on('finish', () => console.log('File uploaded')); } streamUpload().catch(console.error); ``` -------------------------------- ### Use Transfer Manager for Parallel Operations Source: https://context7.com/googleapis/nodejs-storage/llms.txt This example demonstrates using the Transfer Manager from the Node.js client library for efficient parallel uploads and downloads of multiple files. It includes uploading and downloading files in parallel, as well as handling large files by uploading/downloading them in chunks. Configure `concurrencyLimit` based on your network and system capabilities. ```javascript const {Storage, TransferManager} = require('@google-cloud/storage'); const storage = new Storage(); async function useTransferManager() { const bucketName = 'my-bucket'; const transferManager = new TransferManager(storage.bucket(bucketName)); // Upload multiple files in parallel const filePaths = ['./file1.txt', './file2.txt', './file3.txt']; await transferManager.uploadManyFiles(filePaths, { concurrencyLimit: 10, skipIfExists: true }); console.log('Files uploaded'); // Download multiple files in parallel const [files] = await storage.bucket(bucketName).getFiles({prefix: 'data/'}); await transferManager.downloadManyFiles(files, { concurrencyLimit: 10, prefix: './downloads/' }); console.log('Files downloaded'); // Upload large file in chunks await transferManager.uploadFileInChunks('./large-file.zip', { chunkSizeBytes: 32 * 1024 * 1024, // 32MB chunks concurrencyLimit: 5 }); // Download large file in chunks await transferManager.downloadFileInChunks( storage.bucket(bucketName).file('large-file.zip'), { destination: './downloaded-large-file.zip', chunkSizeBytes: 32 * 1024 * 1024, concurrencyLimit: 5 } ); } useTransferManager().catch(console.error); ``` -------------------------------- ### Get File Metadata from Google Cloud Storage (Node.js) Source: https://context7.com/googleapis/nodejs-storage/llms.txt Demonstrates how to retrieve detailed metadata for a file stored in Google Cloud Storage. Includes information such as bucket name, file name, size, content type, checksums, creation/update times, and custom metadata. Requires the '@google-cloud/storage' package. ```javascript const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); async function getMetadata() { const bucketName = 'my-bucket'; const fileName = 'my-file.txt'; const [metadata] = await storage .bucket(bucketName) .file(fileName) .getMetadata(); console.log(`Bucket: ${metadata.bucket}`); console.log(`Name: ${metadata.name}`); console.log(`Size: ${metadata.size}`); console.log(`Content-Type: ${metadata.contentType}`); console.log(`CRC32C: ${metadata.crc32c}`); console.log(`MD5 Hash: ${metadata.md5Hash}`); console.log(`Generation: ${metadata.generation}`); console.log(`Metageneration: ${metadata.metageneration}`); console.log(`Created: ${new Date(metadata.timeCreated)}`); console.log(`Updated: ${new Date(metadata.updated)}`); console.log(`Storage Class: ${metadata.storageClass}`); // Custom metadata if (metadata.metadata) { console.log('Custom metadata:'); for (const [key, value] of Object.entries(metadata.metadata)) { console.log(` ${key}: ${value}`); } } } getMetadata().catch(console.error); ``` -------------------------------- ### Get Autoclass Configuration Source: https://github.com/googleapis/nodejs-storage/blob/main/samples/README.md Retrieves the current Autoclass configuration for a bucket, which automatically manages object storage classes based on access patterns. ```javascript node samples/getAutoclass.js ``` -------------------------------- ### Get Default Event-Based Hold Source: https://github.com/googleapis/nodejs-storage/blob/main/samples/README.md Checks the default event-based hold status for a bucket, which determines if new objects uploaded to the bucket are automatically placed on hold. ```javascript node samples/getDefaultEventBasedHold.js ``` -------------------------------- ### Manage IAM Policies for Buckets Source: https://context7.com/googleapis/nodejs-storage/llms.txt This snippet shows how to manage Identity and Access Management (IAM) policies for Google Cloud Storage buckets using the Node.js client library. It covers getting the current policy, adding new role bindings, updating the policy, and testing permissions. Ensure you have the necessary permissions to modify bucket IAM policies. ```javascript const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); async function manageIam() { const bucketName = 'my-bucket'; const bucket = storage.bucket(bucketName); // Get current IAM policy const [policy] = await bucket.iam.getPolicy({requestedPolicyVersion: 3}); console.log('Current policy:', JSON.stringify(policy, null, 2)); // Add new member with role policy.bindings.push({ role: 'roles/storage.objectViewer', members: [ 'user:viewer@example.com', 'group:readers@example.com' ] }); // Update policy await bucket.iam.setPolicy(policy); console.log('Policy updated'); // Test permissions const [permissions] = await bucket.iam.testPermissions([ 'storage.buckets.get', 'storage.objects.list', 'storage.objects.create' ]); console.log('Permissions:', permissions); } manageIam().catch(console.error); ``` -------------------------------- ### Initialize Google Cloud Storage Client Source: https://context7.com/googleapis/nodejs-storage/llms.txt Demonstrates how to instantiate the Storage client using Application Default Credentials, explicit service account files, or custom retry configurations. ```javascript const {Storage} = require('@google-cloud/storage'); // Using Application Default Credentials const storage = new Storage(); // Using explicit credentials with project ID const storage = new Storage({ projectId: 'your-project-id', keyFilename: '/path/to/keyfile.json' }); // With custom retry options const storage = new Storage({ retryOptions: { autoRetry: true, maxRetries: 5, retryDelayMultiplier: 2, totalTimeout: 600, maxRetryDelay: 64 } }); ``` -------------------------------- ### Create Bucket with Storage Class and Location using Node.js Source: https://github.com/googleapis/nodejs-storage/blob/main/README.md This Node.js sample illustrates how to create a Google Cloud Storage bucket specifying its storage class (e.g., STANDARD, NEARLINE) and location. This allows for cost optimization and performance tuning. ```javascript const {Storage} = require('@google-cloud/storage'); async function createBucketWithStorageClassAndLocation(bucketName) { // Creates a client const storage = new Storage(); const options = { location: 'US-CENTRAL1', storageClass: 'STANDARD', }; // Creates the new bucket await storage.createBucket(bucketName, options); console.log(`Bucket ${bucketName} created in ${options.location} with storage class ${options.storageClass}.`); } ``` -------------------------------- ### Create New Bucket using Node.js Source: https://github.com/googleapis/nodejs-storage/blob/main/README.md This Node.js sample shows the basic process of creating a new Google Cloud Storage bucket. It requires the desired bucket name as input. ```javascript const {Storage} = require('@google-cloud/storage'); async function createNewBucket(bucketName) { // Creates a client const storage = new Storage(); // Creates the new bucket await storage.createBucket(bucketName); console.log(`Bucket ${bucketName} created.`); } ``` -------------------------------- ### Create Bucket with Turbo Replication using Node.js Source: https://github.com/googleapis/nodejs-storage/blob/main/README.md This sample demonstrates how to create a Google Cloud Storage bucket with turbo replication enabled using Node.js. Turbo replication accelerates data replication across regions. ```javascript const {Storage} = require('@google-cloud/storage'); async function createBucketWithTurboReplication(bucketName) { // Creates a client const storage = new Storage(); const options = { replication: { userProject: 'your-gcp-project-id', // Replace with your project ID // The destination region for turbo replication // See https://cloud.google.com/storage/docs/locations#available-locations destination: 'US-EAST1', }, }; // Creates the new bucket await storage.createBucket(bucketName, options); console.log(`Bucket ${bucketName} created with turbo replication to US-EAST1.`); } ``` -------------------------------- ### Create Bucket With Storage Class and Location (Node.js) Source: https://github.com/googleapis/nodejs-storage/blob/main/samples/README.md Creates a new bucket specifying its storage class and location. This allows for cost optimization and data residency control. ```javascript const {Storage} = require('@google-cloud/storage'); /** * Creates a new bucket with a specified storage class and location. * * @param {string} bucketName The name of the bucket to create. * @param {string} className The storage class (e.g., 'STANDARD', 'NEARLINE', 'COLDLINE', 'ARCHIVE'). * @param {string} location The location of the bucket (e.g., 'US-CENTRAL1'). */ async function createBucketWithStorageClassAndLocation(bucketName, className, location) { const storage = new Storage(); const options = { location: location, storageClass: className, }; await storage.createBucket(bucketName, options); console.log(`Bucket ${bucketName} created with storage class ${className} in location ${location}.`); } createBucketWithStorageClassAndLocation(...process.argv.slice(2)).catch(err => { console.error(err); process.exitCode = 1; }); ``` -------------------------------- ### Upload Files to Cloud Storage (Node.js) Source: https://context7.com/googleapis/nodejs-storage/llms.txt Demonstrates uploading files from the local filesystem to a Cloud Storage bucket. Supports simple uploads, uploads with metadata and preconditions, and resumable uploads for large files. Requires the '@google-cloud/storage' package. ```javascript const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); async function uploadFile() { const bucketName = 'my-bucket'; const filePath = './local/path/to/file.txt'; const destFileName = 'uploaded-file.txt'; // Simple upload await storage.bucket(bucketName).upload(filePath, { destination: destFileName, }); console.log(`${filePath} uploaded to ${bucketName}`); // Upload with preconditions and metadata await storage.bucket(bucketName).upload(filePath, { destination: destFileName, preconditionOpts: { ifGenerationMatch: 0 // Only if file doesn't exist }, metadata: { contentType: 'text/plain', metadata: { customKey: 'customValue' } } }); // Resumable upload for large files await storage.bucket(bucketName).upload(filePath, { destination: destFileName, resumable: true, chunkSize: 5 * 1024 * 1024 // 5MB chunks }); } uploadFile().catch(console.error); ``` -------------------------------- ### Manage Object Lifecycle Rules Source: https://context7.com/googleapis/nodejs-storage/llms.txt Demonstrates how to define lifecycle rules for automatic object deletion, storage class transitions, and prefix-based management. ```javascript const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); async function manageLifecycle() { const bucketName = 'my-bucket'; const bucket = storage.bucket(bucketName); await bucket.addLifecycleRule({ action: { type: 'Delete' }, condition: { age: 365 } }); await bucket.addLifecycleRule({ action: { type: 'SetStorageClass', storageClass: 'COLDLINE' }, condition: { age: 90, matchesStorageClass: ['STANDARD', 'NEARLINE'] } }); await bucket.addLifecycleRule({ action: { type: 'Delete' }, condition: { age: 30, matchesPrefix: ['temp/', 'cache/'] } }); const [metadata] = await bucket.getMetadata(); console.log('Lifecycle rules:', metadata.lifecycle?.rule); } manageLifecycle().catch(console.error); ``` -------------------------------- ### List Files in a Google Cloud Storage Bucket (Node.js) Source: https://context7.com/googleapis/nodejs-storage/llms.txt Demonstrates how to list all files within a specified Google Cloud Storage bucket. Supports filtering by prefix, using delimiters for directory-like structures, and paginated results. Requires the '@google-cloud/storage' package. ```javascript const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); async function listFiles() { const bucketName = 'my-bucket'; // List all files const [files] = await storage.bucket(bucketName).getFiles(); console.log('Files:'); files.forEach(file => { console.log(file.name); }); // List with prefix (like a directory) const [prefixedFiles] = await storage.bucket(bucketName).getFiles({ prefix: 'images/' }); // List with delimiter for directory-like listing const [dirFiles, , apiResponse] = await storage.bucket(bucketName).getFiles({ prefix: 'data/', delimiter: '/', autoPaginate: false }); console.log('Files:', dirFiles.map(f => f.name)); console.log('Prefixes:', apiResponse.prefixes); // Subdirectories // Paginated listing const [pagedFiles, nextQuery] = await storage.bucket(bucketName).getFiles({ maxResults: 100, autoPaginate: false }); } listFiles().catch(console.error); ``` -------------------------------- ### Create Dual-Region Bucket using Node.js Source: https://github.com/googleapis/nodejs-storage/blob/main/README.md This Node.js sample demonstrates how to create a Google Cloud Storage bucket that is configured for dual-region replication. This enhances data availability and durability. ```javascript const {Storage} = require('@google-cloud/storage'); async function createBucketWithDualRegion(bucketName) { // Creates a client const storage = new Storage(); const options = { location: 'NAM4', }; // Creates the new bucket await storage.createBucket(bucketName, options); console.log(`Bucket ${bucketName} created with dual-region.`); } ``` -------------------------------- ### Copy File using Node.js Source: https://github.com/googleapis/nodejs-storage/blob/main/README.md Demonstrates how to copy a file within Google Cloud Storage using the Node.js client library. It requires the source and destination bucket and file names as input. ```javascript const {Storage} = require('@google-cloud/storage'); async function copyFile(bucketName, srcFilename, destFilename) { // Creates a client const storage = new Storage(); await storage .bucket(bucketName) .file(srcFilename) .copy(destFilename); console.log( `gs://${bucketName}/${srcFilename} copied to gs://${bucketName}/${destFilename}.` ); } ``` -------------------------------- ### Create Bucket with Object Retention using Node.js Source: https://github.com/googleapis/nodejs-storage/blob/main/README.md Demonstrates how to create a Google Cloud Storage bucket with object retention enabled using Node.js. This enforces retention policies on objects within the bucket. ```javascript const {Storage} = require('@google-cloud/storage'); async function createBucketWithObjectRetention(bucketName) { // Creates a client const storage = new Storage(); const options = { retentionPolicy: { retentionPeriod: 3600, // 1 hour in seconds }, }; // Creates the new bucket await storage.createBucket(bucketName, options); console.log(`Bucket ${bucketName} created with object retention enabled for 1 hour.`); } ``` -------------------------------- ### Manage Object Versioning in Google Cloud Storage Source: https://context7.com/googleapis/nodejs-storage/llms.txt Demonstrates how to enable versioning on a bucket, list all versions of files, retrieve a specific file generation, and delete a specific version using the Node.js client library. ```javascript const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); async function manageVersioning() { const bucketName = 'my-bucket'; const bucket = storage.bucket(bucketName); // Enable versioning await bucket.setMetadata({ versioning: { enabled: true } }); console.log('Versioning enabled'); // List all versions of files const [files] = await bucket.getFiles({ versions: true }); files.forEach(file => { console.log(`${file.name} - Generation: ${file.generation}`); }); // Access specific version const file = bucket.file('my-file.txt', { generation: 1234567890 }); const [contents] = await file.download(); // Delete specific version await file.delete(); } manageVersioning().catch(console.error); ``` -------------------------------- ### Create Google Cloud Storage Buckets Source: https://context7.com/googleapis/nodejs-storage/llms.txt Create new buckets with specific configurations such as location, storage class, versioning, and retention policies. ```javascript const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); async function createBucket() { const [bucket] = await storage.createBucket('my-new-bucket'); const [regionalBucket] = await storage.createBucket('my-regional-bucket', { location: 'US-CENTRAL1', storageClass: 'STANDARD' }); const [versionedBucket] = await storage.createBucket('my-versioned-bucket', { versioning: { enabled: true } }); } ``` -------------------------------- ### Download Files from Cloud Storage (Node.js) Source: https://context7.com/googleapis/nodejs-storage/llms.txt Illustrates downloading files from a Cloud Storage bucket. This includes downloading to a specified local file path, downloading the entire file content into memory as a Buffer, and downloading a specific byte range of a file. ```javascript const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); async function downloadFile() { const bucketName = 'my-bucket'; const fileName = 'my-file.txt'; // Download to local file const destPath = './downloaded-file.txt'; await storage.bucket(bucketName).file(fileName).download({ destination: destPath }); console.log(`Downloaded to ${destPath}`); // Download into memory const [contents] = await storage.bucket(bucketName).file(fileName).download(); console.log('File contents:', contents.toString()); // Download byte range const [partial] = await storage.bucket(bucketName).file(fileName).download({ start: 0, end: 1023 // First 1KB }); } downloadFile().catch(console.error); ``` -------------------------------- ### Generate Signed URLs for Temporary Access Source: https://context7.com/googleapis/nodejs-storage/llms.txt Shows how to create time-limited V4 signed URLs for reading, writing, or deleting objects, allowing temporary access without requiring authentication. ```javascript const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); async function generateSignedUrl() { const bucketName = 'my-bucket'; const fileName = 'private-file.txt'; const [readUrl] = await storage .bucket(bucketName) .file(fileName) .getSignedUrl({ version: 'v4', action: 'read', expires: Date.now() + 15 * 60 * 1000 }); const [uploadUrl] = await storage .bucket(bucketName) .file('new-file.txt') .getSignedUrl({ version: 'v4', action: 'write', expires: Date.now() + 15 * 60 * 1000, contentType: 'text/plain' }); const [deleteUrl] = await storage .bucket(bucketName) .file(fileName) .getSignedUrl({ version: 'v4', action: 'delete', expires: Date.now() + 15 * 60 * 1000 }); } generateSignedUrl().catch(console.error); ``` -------------------------------- ### Create Notification using Node.js Source: https://github.com/googleapis/nodejs-storage/blob/main/README.md Demonstrates how to create a Cloud Storage notification using Node.js. This allows you to receive notifications when specific events occur in a bucket. Requires bucket name and topic name. ```javascript const {Storage} = require('@google-cloud/storage'); async function createNotification(bucketName, topicName) { // Creates a client const storage = new Storage(); const notification = { topic: `projects/_/topics/${topicName}`, eventTypes: [ 'OBJECT_FINALIZE', 'OBJECT_DELETE', ], objectNamePrefix: '', payloadFormat: 'JSON_API_V1', }; // Creates the new notification await storage.bucket(bucketName).createNotification(notification); console.log(`Notification created for bucket ${bucketName} on topic ${topicName}.`); } ``` -------------------------------- ### Create Bucket With Dual Region (Node.js) Source: https://github.com/googleapis/nodejs-storage/blob/main/samples/README.md Creates a new bucket configured for dual-region replication. This requires the bucket name, the primary location, and two specific regions within that location. ```javascript const {Storage} = require('@google-cloud/storage'); /** * Creates a new bucket with dual-region replication enabled. * * @param {string} bucketName The name of the bucket to create. * @param {string} location The primary location for the bucket (e.g., 'US'). * @param {string} region1 The first region for dual-region replication (e.g., 'US-EAST1'). * @param {string} region2 The second region for dual-region replication (e.g., 'US-WEST1'). */ async function createBucketWithDualRegion(bucketName, location, region1, region2) { const storage = new Storage(); const options = { location: { type: 'dual-region', location: location, regionalBuckets: [ { region: region1, }, { region: region2, }, ], }, }; await storage.createBucket(bucketName, options); console.log(`Bucket ${bucketName} created with dual-region replication in ${location} (${region1}, ${region2}).`); } createBucketWithDualRegion(...process.argv.slice(2)).catch(err => { console.error(err); process.exitCode = 1; }); ``` -------------------------------- ### Create Hierarchical Namespace Bucket using Node.js Source: https://github.com/googleapis/nodejs-storage/blob/main/README.md This sample shows how to create a Google Cloud Storage bucket with hierarchical namespace enabled using Node.js. This feature is useful for file system-like operations. ```javascript const {Storage} = require('@google-cloud/storage'); async function createBucketWithHierarchicalNamespace(bucketName) { // Creates a client const storage = new Storage(); const options = { hierarchicalNamespace: { enabled: true, }, }; // Creates the new bucket await storage.createBucket(bucketName, options); console.log(`Bucket ${bucketName} created with hierarchical namespace enabled.`); } ``` -------------------------------- ### Create Bucket With Turbo Replication (Node.js) Source: https://github.com/googleapis/nodejs-storage/blob/main/samples/README.md Creates a new bucket configured with Turbo Replication. This feature accelerates replication of objects to buckets in different regions. ```javascript const {Storage} = require('@google-cloud/storage'); /** * Creates a new bucket with Turbo Replication enabled. * * @param {string} bucketName The name of the bucket to create. */ async function createBucketWithTurboReplication(bucketName) { const storage = new Storage(); const options = { replication: { turboReplication: true, }, }; await storage.createBucket(bucketName, options); console.log(`Bucket ${bucketName} created with Turbo Replication enabled.`); } createBucketWithTurboReplication(process.argv[2]).catch(err => { console.error(err); process.exitCode = 1; }); ``` -------------------------------- ### List Google Cloud Storage Buckets Source: https://context7.com/googleapis/nodejs-storage/llms.txt Retrieve a list of buckets in a project, supporting prefix filtering and manual pagination for large datasets. ```javascript const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); async function listBuckets() { const [buckets] = await storage.getBuckets(); const [filteredBuckets] = await storage.getBuckets({ prefix: 'my-app-' }); const [bucketsPage, nextQuery] = await storage.getBuckets({ autoPaginate: false, maxResults: 10 }); } ``` -------------------------------- ### Create New Bucket (Node.js) Source: https://github.com/googleapis/nodejs-storage/blob/main/samples/README.md Creates a new, empty bucket in Google Cloud Storage. This is a fundamental operation for organizing data. ```javascript const {Storage} = require('@google-cloud/storage'); /** * Creates a new bucket. * * @param {string} bucketName The name of the bucket to create. */ async function createNewBucket(bucketName) { const storage = new Storage(); // Creates the new bucket await storage.createBucket(bucketName); console.log(`Bucket ${bucketName} created.`); } createNewBucket(process.argv[2]).catch(err => { console.error(err); process.exitCode = 1; }); ``` -------------------------------- ### Generate V4 Signed Policy Source: https://github.com/googleapis/nodejs-storage/blob/main/samples/README.md Creates a signed policy document that allows a user to upload files to a bucket based on specific constraints defined in the policy. ```javascript node samples/generateV4SignedPolicy.js ``` -------------------------------- ### Create Bucket With Hierarchical Namespace (Node.js) Source: https://github.com/googleapis/nodejs-storage/blob/main/samples/README.md Creates a new bucket with hierarchical namespace enabled. This feature is often used for performance optimizations with big data workloads. ```javascript const {Storage} = require('@google-cloud/storage'); /** * Creates a new bucket with hierarchical namespace enabled. * * @param {string} bucketName The name of the bucket to create. */ async function createBucketWithHierarchicalNamespace(bucketName) { const storage = new Storage(); const options = { hierarchicalNamespace: { enabled: true, }, }; await storage.createBucket(bucketName, options); console.log(`Bucket ${bucketName} created with hierarchical namespace enabled.`); } createBucketWithHierarchicalNamespace(process.argv[2]).catch(err => { console.error(err); process.exitCode = 1; }); ``` -------------------------------- ### Create Notification (Node.js) Source: https://github.com/googleapis/nodejs-storage/blob/main/samples/README.md Creates a notification configuration on a bucket. This allows you to receive notifications when specific events occur on objects within the bucket. ```javascript const {Storage} = require('@google-cloud/storage'); /** * Creates a notification on a bucket. * * @param {string} bucketName The name of the bucket to create the notification on. */ async function createNotification(bucketName) { const storage = new Storage(); const options = { destination: 'http://example.com/my-notification-handler', topic: 'my-topic', eventTypes: [ 'OBJECT_FINALIZE', 'OBJECT_DELETE', ], }; await storage.bucket(bucketName).createNotification(options); console.log(`Notification created for bucket ${bucketName}.`); } createNotification(process.argv[2]).catch(err => { console.error(err); process.exitCode = 1; }); ``` -------------------------------- ### Inject Third-Party Tracking and Ad Scripts Source: https://github.com/googleapis/nodejs-storage/blob/main/system-test/data/long-html-file.html This script dynamically loads Adzerk, Google Analytics, Quantcast, and Comscore libraries. It detects the current protocol to ensure secure HTTPS connections where applicable and initializes tracking with specific site identifiers. ```javascript var p = "http", d = "static"; if (document.location.protocol == "https:") { p += "s"; d = "engine"; } var z = document.createElement("script"); z.type = "text/javascript"; z.async = true; z.src = p + "://" + d + ".adzerk.net/ados.js"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(z, s); var ados = ados || {}; ados.run = ados.run || []; ados.run.push(function () { ados_setKeywords('compression,gzip,algorithm'); ados_load(); }); (function (i, s, o, g, r, a, m) { i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments) }, i[r].l = 1 * new Date(); a = s.createElement(o), m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga'); ga('create', 'UA-5620270-3'); ga('set', 'dimension2', '|compression|gzip|algorithm|'); ga('send', 'pageview'); var _qevents = _qevents || [], _comscore = _comscore || []; (function () { var ssl='https:'==document.location.protocol, s=document.getElementsByTagName('script')[0], qc=document.createElement('script'); qc.async=true; qc.src=(ssl?'https://secure':'http://edge')+'.quantserve.com/quant.js'; s.parentNode.insertBefore(qc, s); var sc=document.createElement('script'); sc.async=true; sc.src=(ssl?'https://sb':'http://b') + '.scorecardresearch.com/beacon.js'; s.parentNode.insertBefore(sc, s); })(); _comscore.push({ c1: "2", c2: "17440561" }); _qevents.push({ qacct: "p-c1rF4kxgLUzNc" }); ``` -------------------------------- ### Configure Bucket CORS Policies Source: https://context7.com/googleapis/nodejs-storage/llms.txt Explains how to set and remove Cross-Origin Resource Sharing (CORS) configurations on a bucket to enable browser-based access to resources. ```javascript const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); async function configureCors() { const bucketName = 'my-bucket'; await storage.bucket(bucketName).setCorsConfiguration([ { maxAgeSeconds: 3600, method: ['GET', 'POST', 'PUT', 'DELETE', 'HEAD'], origin: ['https://example.com', 'https://app.example.com'], responseHeader: ['Content-Type', 'x-custom-header'] } ]); await storage.bucket(bucketName).setCorsConfiguration([]); } configureCors().catch(console.error); ``` -------------------------------- ### POST /projects/{projectId}/hmacKeys Source: https://github.com/googleapis/nodejs-storage/blob/main/README.md Creates a new HMAC key for a service account, allowing for interoperable authentication with Cloud Storage. ```APIDOC ## POST /projects/{projectId}/hmacKeys ### Description Creates a new HMAC key for the specified service account. ### Method POST ### Endpoint /projects/{projectId}/hmacKeys ### Parameters #### Path Parameters - **projectId** (string) - Required - The ID of the project owning the service account. #### Request Body - **serviceAccountEmail** (string) - Required - The email address of the service account. ### Request Example { "serviceAccountEmail": "service-account@project-id.iam.gserviceaccount.com" } ### Response #### Success Response (200) - **metadata** (object) - The metadata of the created HMAC key. - **secret** (string) - The secret associated with the HMAC key. #### Response Example { "metadata": { "accessId": "GOOG..." }, "secret": "base64encodedsecret" } ``` -------------------------------- ### Delete Files in a Google Cloud Storage Bucket (Node.js) Source: https://context7.com/googleapis/nodejs-storage/llms.txt Shows how to delete files from a Google Cloud Storage bucket. Includes simple deletion, deletion with generation match preconditions to avoid race conditions, and batch deletion of files matching a prefix. Requires the '@google-cloud/storage' package. ```javascript const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); async function deleteFile() { const bucketName = 'my-bucket'; const fileName = 'file-to-delete.txt'; // Simple delete await storage.bucket(bucketName).file(fileName).delete(); console.log(`gs://${bucketName}/${fileName} deleted`); // Delete with generation match precondition await storage.bucket(bucketName).file(fileName).delete({ ifGenerationMatch: 1234567890 }); // Delete multiple files await storage.bucket(bucketName).deleteFiles({ prefix: 'temp/' }); } deleteFile().catch(console.error); ``` -------------------------------- ### Upload Content from Memory to Cloud Storage (Node.js) Source: https://context7.com/googleapis/nodejs-storage/llms.txt Explains how to save content directly to Cloud Storage from memory without creating a local file. This method is suitable for smaller pieces of data or dynamically generated content. It accepts strings or Buffers and allows setting content type and metadata. ```javascript const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); async function uploadFromMemory() { const bucketName = 'my-bucket'; const destFileName = 'file-from-memory.txt'; const contents = 'Hello, World! This content is uploaded from memory.'; // Simple save await storage.bucket(bucketName).file(destFileName).save(contents); console.log(`${destFileName} uploaded to ${bucketName}`); // Save with options await storage.bucket(bucketName).file(destFileName).save(contents, { contentType: 'text/plain', metadata: { custom: 'metadata' } }); // Save Buffer const buffer = Buffer.from('Binary content here'); await storage.bucket(bucketName).file('binary-file.bin').save(buffer); } uploadFromMemory().catch(console.error); ``` -------------------------------- ### Delete Bucket using Node.js Source: https://github.com/googleapis/nodejs-storage/blob/main/README.md This Node.js sample shows how to delete a Google Cloud Storage bucket. Note that a bucket must be empty before it can be deleted. ```javascript const {Storage} = require('@google-cloud/storage'); async function deleteBucket(bucketName) { // Creates a client const storage = new Storage(); // Deletes the bucket await storage.bucket(bucketName).delete(); console.log(`Bucket ${bucketName} deleted.`); } ``` -------------------------------- ### Delete File using Node.js Source: https://github.com/googleapis/nodejs-storage/blob/main/README.md Demonstrates how to delete a file from a Google Cloud Storage bucket using Node.js. Requires the bucket name and the file name to be deleted. ```javascript const {Storage} = require('@google-cloud/storage'); async function deleteFile(bucketName, filename) { // Creates a client const storage = new Storage(); // Deletes the file await storage.bucket(bucketName).file(filename).delete(); console.log(`gs://${bucketName}/${filename} deleted.`); } ``` -------------------------------- ### Update File Metadata in Google Cloud Storage Source: https://context7.com/googleapis/nodejs-storage/llms.txt Demonstrates how to update metadata for an existing file, including content type, cache control, and custom user-defined metadata fields. ```javascript const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); async function setMetadata() { const bucketName = 'my-bucket'; const fileName = 'my-file.txt'; const [metadata] = await storage .bucket(bucketName) .file(fileName) .setMetadata({ contentType: 'text/plain', cacheControl: 'public, max-age=3600', contentDisposition: 'attachment; filename="download.txt"', metadata: { author: 'John Doe', version: '1.0' } }); console.log('Metadata updated:', metadata); } setMetadata().catch(console.error); ``` -------------------------------- ### Manage HMAC Keys for S3 Interoperability Source: https://context7.com/googleapis/nodejs-storage/llms.txt This code snippet demonstrates how to create and manage HMAC keys for authenticating requests to the Cloud Storage XML API using the Node.js client library. It covers creating a new key, listing existing keys, retrieving metadata for a specific key, deactivating a key, and deleting a key. Remember to store the generated secret securely. ```javascript const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); async function manageHmacKeys() { const serviceAccountEmail = 'my-service-account@project.iam.gserviceaccount.com'; // Create HMAC key const [hmacKey, secret] = await storage.createHmacKey(serviceAccountEmail); console.log('Access ID:', hmacKey.metadata.accessId); console.log('Secret:', secret); // Store securely! // List HMAC keys const [hmacKeys] = await storage.getHmacKeys(); hmacKeys.forEach(key => { console.log(`Key: ${key.metadata.accessId}, State: ${key.metadata.state}`); }); // Get specific HMAC key const key = storage.hmacKey(hmacKey.metadata.accessId); const [metadata] = await key.getMetadata(); console.log('Key metadata:', metadata); // Deactivate HMAC key await key.setMetadata({state: 'INACTIVE'}); // Delete HMAC key (must be inactive first) await key.delete(); } manageHmacKeys().catch(console.error); ``` -------------------------------- ### Copy Old Version Of File (Node.js) Source: https://github.com/googleapis/nodejs-storage/blob/main/samples/README.md Copies an old version of a file from one bucket to another. Requires source bucket name, source file name, destination bucket name, destination file name, and the generation number of the file version to copy. ```javascript const {Storage} = require('@google-cloud/storage'); /** * Copies an old version of a file from one bucket to another. * * @param {string} srcBucketName The name of the source bucket. * @param {string} srcFileName The name of the source file. * @param {string} destBucketName The name of the destination bucket. * @param {string} destFileName The name of the destination file. * @param {string} generation The generation number of the file version to copy. */ async function copyOldVersionOfFile(srcBucketName, srcFileName, destBucketName, destFileName, generation) { const storage = new Storage(); await storage .bucket(srcBucketName) .file(srcFileName, { generation: generation }) .copy(storage.bucket(destBucketName).file(destFileName)); console.log( `gs://${srcBucketName}/${srcFileName} (generation ${generation}) copied to gs://${destBucketName}/${destFileName}.` ); } copyOldVersionOfFile(...process.argv.slice(2)).catch(err => { console.error(err); process.exitCode = 1; }); ``` -------------------------------- ### Create Bucket With Object Retention (Node.js) Source: https://github.com/googleapis/nodejs-storage/blob/main/samples/README.md Creates a new bucket with object retention enabled. This ensures that objects stored in the bucket cannot be deleted or overwritten for a specified retention period. ```javascript const {Storage} = require('@google-cloud/storage'); /** * Creates a new bucket with object retention enabled. * * @param {string} bucketName The name of the bucket to create. */ async function createBucketWithObjectRetention(bucketName) { const storage = new Storage(); const options = { retentionPolicy: { retentionPeriod: 3600, // Example: 1 hour in seconds isLocked: false, // Set to true to make the retention policy unchangeable }, }; await storage.createBucket(bucketName, options); console.log(`Bucket ${bucketName} created with object retention enabled.`); } createBucketWithObjectRetention(process.argv[2]).catch(err => { console.error(err); process.exitCode = 1; }); ``` -------------------------------- ### PATCH /storage/v1/b/{bucket} Source: https://context7.com/googleapis/nodejs-storage/llms.txt Updates the bucket metadata to enable versioning and demonstrates how to retrieve and delete specific object versions. ```APIDOC ## PATCH /storage/v1/b/{bucket} ### Description Enables object versioning on a specific bucket to preserve, retrieve, and restore previous versions of files. ### Method PATCH ### Endpoint /storage/v1/b/{bucket} ### Parameters #### Path Parameters - **bucket** (string) - Required - The name of the bucket. #### Request Body - **versioning** (object) - Required - Configuration object for versioning. - **enabled** (boolean) - Required - Set to true to enable versioning. ### Request Example { "versioning": { "enabled": true } } ### Response #### Success Response (200) - **kind** (string) - The resource type. - **name** (string) - The bucket name. - **versioning** (object) - The updated versioning configuration. #### Response Example { "kind": "storage#bucket", "name": "my-bucket", "versioning": { "enabled": true } } ``` -------------------------------- ### Copy Old Version of File using Node.js Source: https://github.com/googleapis/nodejs-storage/blob/main/README.md This sample shows how to copy a specific old version of a file in Google Cloud Storage using Node.js. It requires the bucket name, source file name, destination file name, and the generation number of the old version. ```javascript const {Storage} = require('@google-cloud/storage'); async function copyOldVersionOfFile(bucketName, srcFilename, destFilename, generation) { // Creates a client const storage = new Storage(); await storage .bucket(bucketName) .file(srcFilename, { generation: generation, }) .copy(destFilename); console.log( `gs://${bucketName}/${srcFilename} (generation ${generation}) copied to gs://${bucketName}/${destFilename}.` ); } ``` -------------------------------- ### Enable Uniform Bucket-Level Access Source: https://github.com/googleapis/nodejs-storage/blob/main/samples/README.md Configures a bucket to use uniform bucket-level access, which disables fine-grained access control lists (ACLs) in favor of IAM policies applied at the bucket level. ```javascript node samples/enableUniformBucketLevelAccess.js ``` -------------------------------- ### Generate V4 Upload Signed URL Source: https://github.com/googleapis/nodejs-storage/blob/main/samples/README.md Generates a V4 signed URL specifically for uploading objects to a bucket, allowing third-party clients to perform uploads securely. ```javascript node samples/generateV4UploadSignedUrl.js ``` -------------------------------- ### Generate V4 Read Signed URL Source: https://github.com/googleapis/nodejs-storage/blob/main/samples/README.md Generates a V4 signed URL for reading an object from a bucket, offering improved security and compatibility over older signing methods. ```javascript node samples/generateV4ReadSignedUrl.js ``` -------------------------------- ### Generate Encryption Key Source: https://github.com/googleapis/nodejs-storage/blob/main/samples/README.md Generates a base64-encoded encryption key for use with customer-supplied encryption keys (CSEK) in Google Cloud Storage. ```javascript node samples/generateEncryptionKey.js ``` -------------------------------- ### Generate Signed URL Source: https://github.com/googleapis/nodejs-storage/blob/main/samples/README.md Creates a signed URL that provides time-limited access to a GCS object without requiring a Google account. ```javascript node samples/generateSignedUrl.js ``` -------------------------------- ### Manage Public Access for Files and Buckets Source: https://context7.com/googleapis/nodejs-storage/llms.txt Provides methods to grant or revoke public read access for individual files or entire buckets within Google Cloud Storage. ```javascript const {Storage} = require('@google-cloud/storage'); const storage = new Storage(); async function makePublic() { const bucketName = 'my-bucket'; const fileName = 'public-file.txt'; await storage.bucket(bucketName).file(fileName).makePublic(); await storage.bucket(bucketName).file(fileName).makePrivate(); await storage.bucket(bucketName).makePublic({ includeFiles: true }); } makePublic().catch(console.error); ``` -------------------------------- ### Enable Requester Pays for GCS Bucket Source: https://github.com/googleapis/nodejs-storage/blob/main/samples/README.md Enables the 'Requester Pays' feature on a specific Google Cloud Storage bucket. This ensures that the requester, rather than the bucket owner, pays for access requests and data egress. ```javascript node samples/enableRequesterPays.js ``` -------------------------------- ### Set File Metadata Source: https://github.com/googleapis/nodejs-storage/blob/main/samples/README.md Updates the custom metadata associated with a specific file in a Google Cloud Storage bucket. Requires the bucket name and file name as input parameters. ```javascript node fileSetMetadata.js ```