Cloud Storage Client Library for Node.js

repository·main·Indexed 21 days ago

https://github.com/googleapis/nodejs-storage

A Node.js idiomatic client library for interacting with Google Cloud Storage, enabling data storage and retrieval, website content serving, and archival data management. The library supports bucket and file operations, including lifecycle management, IAM and ACL permissions, signed URLs, and HMAC service account key management. Version 7.19.0 is compatible with active and maintenance versions of Node.js.

Tokens
19.7K
Snippets
103
Records
116
Agent score
76%

What's inside @google-cloud/storage

  1. Manage Bucket configurations and policies

    main

    You can programmatically manage various bucket-level settings and lifecycle policies using the client library. Key capabilities include:

    • Lifecycle & Versioning: Enable Bucket Lifecycle Management and Bucket Versioning.
    • Access Control: Enable Uniform Bucket Level Access and Requester Pays.
    • Security & Encryption: Enable Default KMS Keys, Default Event Based Hold, and Public Access Prevention.
    • Retention & Deletion: Manage Retention Policies and Soft Delete Policies.
    <!-- See individual sample files for specific implementations -->
    // Enable Bucket Lifecycle Management: samples/enableBucketLifecycleManagement.js
    // Enable Bucket Versioning: samples/enableBucketVersioning.js
    // Enable Uniform Bucket Level Access: samples/enableUniformBucketLevelAccess.js
    // Enable Requester Pays: samples/enableRequesterPays.js
    // Enable Default KMS Key: samples/enableDefaultKMSKey.js
  2. Manage Notifications and Retries

    main

    Use these samples to manage event notifications and client-side retry logic:

    • Create Notification: Set up notifications for bucket events.
    • Delete Notification: Remove an existing notification.
    • Configure Retries: Customize the retry configuration for client requests.

    Usage Examples:

    node samples/createNotification.js
    node samples/configureRetries.js
    node samples/configureRetries.js
  3. Prerequisites for using Google Cloud Storage

    main

    Before using the Node.js client library, ensure you have completed the following setup steps:

    1. Project Setup: Select or create a Google Cloud Platform project.
    2. Billing: Enable billing for your project.
    3. API Activation: Enable the Google Cloud Storage API in the Google Cloud Console.
    4. Authentication: Set up authentication (such as Application Default Credentials or a Service Account key) to allow your local workstation or environment to access the API.
  4. Create and Manage Buckets

    main

    Samples for creating buckets with various advanced configurations:

    • Create New Bucket: Standard bucket creation.
    • Create Bucket With Storage Class and Location: Create a bucket with a specific storage class and geographic location.
    • Create a Dual-Region Bucket: Create a bucket spanning two specific regions.
    • Create Bucket With Turbo Replication: Enable turbo replication for faster cross-region replication.
    • Create a hierarchical namespace enabled bucket: Enable hierarchical namespace features.
    • Create a Bucket with object retention enabled: Enable object retention policies.

    Usage Examples:

    # Create with storage class and location
    node createBucketWithStorageClassAndLocation.js <BUCKET_NAME> <CLASS_NAME> <LOCATION>
    
    # Create dual-region bucket
    node createBucketWithDualRegion.js <BUCKET_NAME> <LOCATION> <REGION1> <REGION2>
    node createBucketWithStorageClassAndLocation.js <BUCKET_NAME> <CLASS_NAME> <LOCATION>
  5. Retrieve Metadata and Status

    main

    The library allows you to inspect the state and metadata of buckets and files. You can retrieve:

    • File Metadata: Get metadata for specific objects, including custom metadata and notifications.
    • Bucket Status: Check Requester Pays status, Uniform Bucket Level Access, and Public Access Prevention.
    • Policies: Retrieve Retention Policies, Soft Delete Policies, and Default Event Based Hold settings.
    • Other: Get Autoclass settings, RPO, and Service Account information.
    <!-- See individual sample files for specific implementations -->
    // Get Metadata: samples/getMetadata.js
    // Get Requester Pays Status: samples/getRequesterPaysStatus.js
    // Get Soft Delete Policy: samples/getSoftDeletePolicy.js
    // Get Retention Policy: samples/getRetentionPolicy.js
  6. Setup and run Google Cloud Storage Node.js samples

    main

    To run the provided code samples for the Google Cloud Storage Node.js client, you must first ensure you have followed the setup instructions in the main repository's Using the client library guide. Once the environment is configured, navigate to the samples directory and install the necessary dependencies.

    Follow these steps in your terminal:

    1. Navigate to the samples directory: cd samples
    2. Install dependencies: npm install
    3. Return to the root directory: cd ..
    cd samples
    npm install
    cd ..
  7. Access Google Cloud Storage Node.js samples

    main
    Code samples for the Google Cloud Storage Node.js client are located in the samples/ directory of the repository. To run a specific sample, navigate to its directory and follow the instructions provided in its local README.md file.
  8. Explore Google Cloud Storage Node.js samples

    main

    The @google-cloud/storage-samples repository provides a wide range of practical examples for interacting with Google Cloud Storage using the Node.js client library. These samples cover common tasks such as file uploads, downloads, security configurations, and bucket management.

    Key categories of samples include:

    • File Operations: Uploading files (from memory, in chunks, or via Transfer Manager), renaming files, streaming downloads/uploads, and managing encrypted files.
    • Bucket & Policy Management: Setting retention policies, soft delete policies, Autoclass, and Public Access Prevention.
    • Security & Access Control: Managing IAM members, setting event-based holds, temporary holds, and rotating encryption keys.
    • Advanced Features: Using the Transfer Manager for bulk operations, managing KMS keys, and restoring soft-deleted buckets or objects.
  9. Check Node.js version compatibility

    main

    The @google-cloud/storage client library is compatible with all current active and maintenance versions of Node.js.

    If you are using an end-of-life version of Node.js, it is recommended to update to an actively supported LTS version. While legacy versions are supported on a best-efforts basis, they are not tested in continuous integration, may lack security patches/features, and cannot have up-to-date dependencies.

  10. Manage Bucket Access Control (ACL) and IAM

    main

    The following samples demonstrate how to manage permissions for Google Cloud Storage buckets and files using Access Control Lists (ACL) and Identity and Access Management (IAM):

    • Add Bucket Conditional Binding: Apply IAM bindings with conditions.
    • Add Bucket Default Owner Acl: Set default owner ACLs for a bucket.
    • Add Bucket Iam Member: Add an IAM member to a bucket.
    • Add Bucket Owner Acl: Add an owner ACL to a bucket.
    • Add File Owner Acl: Add an owner ACL to a specific file.

    Run these samples using node with the corresponding script path.

    node samples/addBucketIamMember.js
    node samples/addBucketOwnerAcl.js
    node samples/addFileOwnerAcl.js
  11. Configure Bucket Metadata and Settings

    main

    Use these samples to manage bucket-level configurations:

    • Add Bucket Label: Add metadata labels to a bucket.
    • Bucket Website Configuration: Configure a bucket to host a static website, specifying the main page suffix and the 404 error page.
    • Get Bucket Metadata: Retrieve metadata for a specific bucket.
    • Change Bucket's Default Storage Class: Update the default storage class for a bucket.
    • Configure Bucket CORS: Set Cross-Origin Resource Sharing (CORS) settings, including maxAgeSeconds, method, origin, and responseHeader.

    Usage Examples:

    # Add a label
    node addBucketLabel.js <BUCKET_NAME> <LABEL_KEY> <LABEL_VALUE>
    
    # Configure website
    node addBucketWebsiteConfiguration.js <BUCKET_NAME> <MAIN_PAGE_SUFFIX> <NOT_FOUND_PAGE>
    
    # Get metadata
    node bucketMetadata.js <BUCKET_NAME>
    
    # Change storage class
    node changeDefaultStorageClass.js <BUCKET_NAME> <CLASS_NAME>
    
    # Configure CORS
    node configureBucketCors.js <BUCKET_NAME> <MAX_AGE_SECONDS> <METHOD> <ORIGIN> <RESPONSE_HEADER>
    node addBucketLabel.js <BUCKET_NAME> <LABEL_KEY> <LABEL_VALUE>