1.0.4 • Published 6 months ago

@b2y/document-module v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

/**

document-module

A flexible multi-provider storage adapter for file operations across AWS S3, Azure Blob Storage, Google Drive, and local storage.

Features

  • Common interface for multiple storage providers
  • Supports AWS S3, Azure Blob Storage, Local file system, and Google Drive (placeholder)
  • Easily switch between providers without changing your code
  • Support for uploading files from paths, buffers, or streams
  • Generate public/presigned URLs for stored files
  • Configurable via environment variables or direct parameters

Installation

npm install @b2y/document-module

Usage

Basic Usage

const { storageService, STORAGE_TYPES,FILE_PATHS } = require('@b2y/document-module');

// Upload a file using the default provider (set via environment)
const result = await storageService.uploadFile(
  { 
    path: './myfile.jpg',
    originalname: 'myfile.jpg',
    mimetype: 'image/jpeg'
  }, 
  'uploads'
);

console.log('Upload result:', result);

// Get a public URL for the file
const url = await storageService.getPublicUrl(result.path);
console.log('File URL:', url);

// Switch to a different provider
storageService.setStorageProvider(STORAGE_TYPES.LOCAL_DRIVE, {
  basePath: './local-storage',
  baseUrl: 'http://localhost:3000/files'
});


const { StorageFactory, STORAGE_TYPES,FILE_PATHS } = require('@b2y/document-module');

// Create an S3 provider with custom configuration
const s3Provider = StorageFactory.createStorageProvider(STORAGE_TYPES.AMAZON_S3, {
  region: 'us-west-2',
  bucketName: 'my-bucket',
  accessKeyId: 'MY_ACCESS_KEY',
  secretAccessKey: 'MY_SECRET_KEY',
  urlExpiresIn: 3600 // URL expiration in seconds
});

// Create an Azure provider
const azureProvider = StorageFactory.createStorageProvider(STORAGE_TYPES.AZURE_BLOB, {
  connectionString: 'MY_CONNECTION_STRING',
  containerName: 'my-container',
  sasExpiryTime: 3600
});

// Upload using the specific provider
const result = await s3Provider.uploadFile(
  Buffer.from('Hello World'), 
  'text-files/hello.txt'
);


Environment Variables
You can configure the adapter using environment variables:
General

DOCUMENT_STORAGE_TYPE: Default storage type ('AMAZON_S3', 'AZURE_BLOB', 'LOCAL_DRIVE', 'GOOGLE_DRIVE')

AWS S3

AWS_REGION: AWS region
AWS_ACCESS_KEY_ID: AWS access key ID
AWS_SECRET_ACCESS_KEY: AWS secret access key
AWS_S3_BUCKET_NAME: S3 bucket name
AWS_URL_EXPIRES_IN: URL expiration time in seconds (default: 86400)

Azure Blob Storage

AZURE_STORAGE_CONNECTION_STRING: Azure storage connection string
AZURE_STORAGE_ACCOUNT_NAME: Azure storage account name
AZURE_STORAGE_ACCOUNT_KEY: Azure storage account key
AZURE_STORAGE_CONTAINER_NAME: Azure container name (default: 'documents')
AZURE_SAS_EXPIRY_SECONDS: SAS token expiry time in seconds (default: 86400)
AZURE_PUBLIC_ACCESS: Set to 'true' to use public container access

Local Storage

LOCAL_STORAGE_PATH: Base path for local storage (default: './storage')
LOCAL_STORAGE_URL: Base URL for accessing files (e.g., 'http://localhost:3000/files')