MinIO JavaScript Client SDK

repository·master·Indexed 22 days ago

https://github.com/minio/minio-js

An S3 Compatible Cloud Storage client for Node.js (v16, v18, v20) that provides high-level APIs to interact with any Amazon S3 compatible object storage server, including MinIO. The SDK supports bucket operations, object manipulation (CRUD), presigned URLs, bucket policies, and notification management. Version 8.0.8 includes built-in TypeScript type definitions.

Tokens
27.5K
Snippets
107
Records
122
Agent score
77%

What's inside minio-js

  1. Overview of MinIO Client Operations

    master

    The MinIO JavaScript client provides several categories of operations:

    • Bucket operations: Managing buckets (e.g., makeBucket, listBuckets, removeBucket).
    • Object operations: Managing files within buckets (e.g., getObject, putObject, removeObject).
    • Presigned operations: Generating temporary URLs for access (e.g., presignedUrl, presignedPutObject).
    • Bucket Policy & Notification operations: Managing access control and event notifications (e.g., setBucketPolicy, listenBucketNotification).
    • Custom Settings: Specialized configurations (e.g., setS3TransferAccelerate).
  2. Use presignedPostPolicy for restricted POST uploads

    master

    For more complex upload requirements, use presignedPostPolicy(policy) to create a POST-based upload URL with specific restrictions. This requires creating a policy using minioClient.newPostPolicy() and applying constraints.

    Workflow

    1. Create a policy: const policy = minioClient.newPostPolicy()
    2. Apply restrictions: Use methods like .setBucket(), .setKey(), .setExpires(), .setContentType(), or .setContentLengthRange() to define what the upload is allowed to do.
    3. Generate URL and Form Data: const { postURL, formData } = await minioClient.presignedPostPolicy(policy)
    4. Execute Upload: Use a client (like superagent) to POST the formData to the postURL.

    Common Policy Methods

    • setBucket(name): Restricts upload to a specific bucket.
    • setKey(name): Restricts upload to a specific object name.
    • setKeyStartsWith(prefix): Restricts object names to a specific prefix.
    • setExpires(date): Sets the policy expiration date.
    • setContentType(type): Restricts the allowed MIME type (e.g., 'text/plain').
    • setContentDisposition(header): Sets the Content-Disposition header.
    • setContentLengthRange(min, max): Restricts the file size in bytes.
    • setUserMetaData(obj): Sets custom key-value metadata.
    // 1. Create policy
    const policy = minioClient.newPostPolicy()
    
    // 2. Apply restrictions
    policy.setBucket('mybucket')
    policy.setKey('hello.txt')
    policy.setContentType('text/plain')
    policy.setContentLengthRange(1024, 1024 * 1024)
    
    // 3. Generate URL and Form Data
    const { postURL, formData } = await minioClient.presignedPostPolicy(policy)
    
    // 4. Example upload using superagent
    const req = superagent.post(postURL)
    // Iterate through formData and attach to request...
  3. Explore MinIO JavaScript Client examples

    master

    The repository contains a comprehensive set of examples categorized by operation type. You can use these to understand how to implement specific features like bucket management, object manipulation, and presigned URLs.

    Bucket Operations

    • Listing buckets and objects (including v2 and metadata extensions)
    • Bucket existence, creation, and removal
    • Versioning, tagging, and lifecycle management
    • Object lock, replication, and encryption configuration

    File Object Operations

    • fputObject: Uploading files from the local filesystem
    • fGetObject: Downloading files to the local filesystem

    Object Operations

    • Standard CRUD: putObject, getObject, removeObject, statObject
    • Advanced: copyObject, composeObject, selectObjectContent
    • Metadata & Retention: Tagging, legal hold, and retention settings

    Presigned Operations

    • Generating presigned URLs for getObject and putObject
    • Using presignedPostPolicy for secure uploads

    Bucket Notification Operations

    • Getting, setting, and removing bucket notifications
    • Listening to bucket notifications (MinIO Extension)

    Bucket Policy Operations

    • Getting and setting bucket policies
  4. Install the MinIO JavaScript Client

    master

    You can install the MinIO JavaScript SDK via npm. This library provides high-level APIs to access any Amazon S3 compatible object storage server.

    Prerequisites:

    • Node.js LTS versions v16, v18, or v20.
    npm install --save minio
  5. Install MinIO from Source

    master

    If you need to build the library from the source code, follow these steps:

    1. Clone the repository.
    2. Install dependencies.
    3. Build the project.
    4. Install globally.
    git clone https://github.com/minio/minio-js
    cd minio-js
    npm install
    npm run build
    npm install -g
  6. Use Tagging and Versioning Configurations

    master

    The MinIO client supports managing metadata via tags and controlling object versioning:

    • Tagging: Use TagList (a Record<string, string>) to manage key-value pairs attached to buckets or objects.
    • Versioning: Use VersioningConfig (a Record<string | number | symbol, unknown>) to configure bucket versioning settings.
    export type TagList = Record<string, string>;
    export type VersioningConfig = Record<string | number | symbol, unknown>;
  7. Configure bucket notification targets

    master

    To set up bucket notifications, you must define target configurations for the specific service you are using (SNS, SQS, or Lambda) and then add them to a NotificationConfig object.

    Each target can be customized with:

    • An Id via setId(id).
    • Specific S3 events via addEvent(event).
    • Key filters using addFilterPrefix(prefix) or addFilterSuffix(suffix).

    Supported target types:

    • TopicConfig: For Simple Notification Service (SNS).
    • QueueConfig: For Simple Queue Service (SQS).
    • CloudFunctionConfig: For Lambda functions.
    import { 
      NotificationConfig, 
      TopicConfig, 
      QueueConfig, 
      CloudFunctionConfig 
    } from 'minio';
    
    const config = new NotificationConfig();
    
    // Configure an SNS Topic target
    const snsTarget = new TopicConfig('arn:aws:sns:us-east-1:123456789012:my-topic');
    snsTarget.setId('my-sns-id');
    snsTarget.addEvent('s3:ObjectCreated:*');
    snsTarget.addFilterPrefix('uploads/');
    
    // Configure an SQS Queue target
    const queueTarget = new QueueConfig('arn:aws:sqs:us-east-1:123456789012:my-queue');
    queueTarget.addFilterSuffix('.png');
    
    // Add targets to the notification configuration
    config.add(snsTarget);
    config.add(queueTarget);
  8. Configure Object Encryption

    master

    When performing operations that require encryption, you can use the Encryption type. The library supports two main types via ENCRYPTION_TYPES:

    • SSE-C (SSEC): Server-Side Encryption with Customer-Provided Keys.
    • KMS: Key Management Service encryption.

    For KMS encryption, you may need to provide SSEAlgorithm and KMSMasterKeyID.

    // Available encryption types
    // ENCRYPTION_TYPES.SSEC = 'SSE-C'
    // ENCRYPTION_TYPES.KMS = 'KMS'
    
    // Example Encryption object shapes:
    // { type: 'SSE-C' }
    // { type: 'KMS', SSEAlgorithm: '...', KMSMasterKeyID: '...' }
  9. Configure Object Retention and Legal Hold

    master

    The library provides types for managing object retention and legal holds:

    Retention Modes

    Use RETENTION_MODES to specify how objects are protected:

    • GOVERNANCE: Users with special permissions can bypass retention.
    • COMPLIANCE: No one can bypass retention.

    Retention Validity

    Use RETENTION_VALIDITY_UNITS to define the time scale:

    • DAYS
    • YEARS

    Use LEGAL_HOLD_STATUS to enable or disable a legal hold:

    • ON (Enabled)
    • OFF (Disabled)

    Applying Retention

    When using PutObjectLegalHoldOptions, you must provide a versionId and the status (using LEGAL_HOLD_STATUS).

    // Constants for configuration
    // RETENTION_MODES: 'GOVERNANCE' | 'COMPLIANCE'
    // RETENTION_VALIDITY_UNITS: 'Days' | 'Years'
    // LEGAL_HOLD_STATUS: 'ON' | 'OFF'
  10. Initialize an AWS S3 Client

    master

    To connect to Amazon S3, instantiate a new Minio.Client using the S3 endpoint and your AWS credentials.

    import * as Minio from 'minio'
    
    const s3Client = new Minio.Client({
      endPoint: 's3.amazonaws.com',
      accessKey: 'YOUR-ACCESSKEYID',
      secretKey: 'YOUR-SECRETACCESSKEY',
    })