@edirect/storage-gateway
v11.0.36
Published
Storage Gateway client library for eDirect applications. Provides a simple interface to interact with the Storage Gateway API for managing storage configurations and file operations.
Maintainers
Keywords
Readme
@edirect/storage-gateway
Storage Gateway client library for eDirect applications. Provides a simple interface to interact with the Storage Gateway API for managing storage configurations and file operations.
Installation
npm install @edirect/storage-gatewayQuick Start
import { StorageGatewayClient } from '@edirect/storage-gateway';
// Create client without headers
const client = new StorageGatewayClient('https://api.example.com');
// Or with authentication headers
const client = new StorageGatewayClient('https://api.example.com', {
authorization: 'Bearer your-token-here',
});
// List storage configurations
const configs = await client.listStorageConfigurations();
// Upload a file
const file = new File(['content'], 'example.txt');
const result = await client.uploadFile({
storageKey: 'my-storage',
file: file,
path: 'documents/example.txt',
});API Reference
Constructor
new StorageGatewayClient(baseUrl: string, headers?: StorageGatewayHeaders)Parameters:
baseUrl- The base URL of the Storage Gateway APIheaders(optional) - Headers to include in all requests
Types:
interface StorageGatewayHeaders {
authorization: string;
[key: string]: string;
}Storage Configuration Methods
listStorageConfigurations
List all storage configurations with optional pagination.
const configs = await client.listStorageConfigurations();
// With pagination
const configs = await client.listStorageConfigurations({
skip: 0,
take: 10,
});Parameters:
params.skip(optional) - Number of records to skipparams.take(optional) - Number of records to return
createStorageConfiguration
Create a new storage configuration.
const config = await client.createStorageConfiguration({
key: 'my-storage',
provider: 's3',
credentials: {
accessKeyId: 'your-access-key',
secretAccessKey: 'your-secret-key',
region: 'us-east-1',
bucket: 'my-bucket',
},
});Parameters:
data- Configuration data object
getStorageConfiguration
Get a specific storage configuration by key.
const config = await client.getStorageConfiguration('my-storage');Parameters:
key- The storage configuration key
updateStorageConfiguration
Update an existing storage configuration.
const updated = await client.updateStorageConfiguration({
key: 'my-storage',
data: {
credentials: {
accessKeyId: 'new-access-key',
secretAccessKey: 'new-secret-key',
},
},
});Parameters:
params.key- The storage configuration keyparams.data- Updated configuration data
deleteStorageConfiguration
Delete a storage configuration.
await client.deleteStorageConfiguration('my-storage');Parameters:
key- The storage configuration key to delete
Storage Operations Methods
listFiles
List files in storage.
const files = await client.listFiles({
storageKey: 'my-storage',
});
// With path filter
const files = await client.listFiles({
storageKey: 'my-storage',
path: 'documents/',
});Parameters:
params.storageKey- The storage configuration keyparams.path(optional) - Path to list files from
uploadFile
Upload a file to storage.
const file = new File(['Hello, World!'], 'hello.txt', { type: 'text/plain' });
const result = await client.uploadFile({
storageKey: 'my-storage',
file: file,
path: 'documents/hello.txt',
});Parameters:
params.storageKey- The storage configuration keyparams.file- The file to upload (File or Blob)params.path(optional) - Destination path for the file
downloadFile
Download a file from storage.
const blob = await client.downloadFile({
storageKey: 'my-storage',
path: 'documents/hello.txt',
});
// Save to file (Node.js)
const buffer = await blob.arrayBuffer();
fs.writeFileSync('downloaded.txt', Buffer.from(buffer));
// Or create download link (Browser)
const url = URL.createObjectURL(blob);Parameters:
params.storageKey- The storage configuration keyparams.path- Path to the file to download
Returns: Promise<Blob>
getFileInfo
Get file metadata information.
const info = await client.getFileInfo({
storageKey: 'my-storage',
path: 'documents/hello.txt',
});
console.log(info);
// { name: 'hello.txt', size: 1024, lastModified: '2024-01-01T00:00:00Z', ... }Parameters:
params.storageKey- The storage configuration keyparams.path- Path to the file
deleteFile
Delete a file from storage.
await client.deleteFile({
storageKey: 'my-storage',
path: 'documents/hello.txt',
});Parameters:
params.storageKey- The storage configuration keyparams.path- Path to the file to delete
Storage Provider Methods
listStorageProviders
List available storage providers.
const providers = await client.listStorageProviders();
console.log(providers);
// ['s3', 'azure-blob', 'gcs', 'local', ...]Complete Example
import { StorageGatewayClient } from '@edirect/storage-gateway';
async function main() {
// Initialize client
const client = new StorageGatewayClient('https://storage-gateway.example.com', {
authorization: 'Bearer your-jwt-token',
});
// List available providers
const providers = await client.listStorageProviders();
console.log('Available providers:', providers);
// Create a new storage configuration
const config = await client.createStorageConfiguration({
key: 'documents-storage',
provider: 's3',
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: 'us-east-1',
bucket: 'my-documents-bucket',
},
});
console.log('Created config:', config);
// Upload a file
const file = new File(['Hello, World!'], 'hello.txt', { type: 'text/plain' });
const uploadResult = await client.uploadFile({
storageKey: 'documents-storage',
file: file,
path: 'greetings/hello.txt',
});
console.log('Upload result:', uploadResult);
// List files
const files = await client.listFiles({
storageKey: 'documents-storage',
path: 'greetings/',
});
console.log('Files:', files);
// Get file info
const fileInfo = await client.getFileInfo({
storageKey: 'documents-storage',
path: 'greetings/hello.txt',
});
console.log('File info:', fileInfo);
// Download file
const blob = await client.downloadFile({
storageKey: 'documents-storage',
path: 'greetings/hello.txt',
});
const text = await blob.text();
console.log('Downloaded content:', text);
// Delete file
await client.deleteFile({
storageKey: 'documents-storage',
path: 'greetings/hello.txt',
});
console.log('File deleted');
// Delete storage configuration
await client.deleteStorageConfiguration('documents-storage');
console.log('Configuration deleted');
}
main().catch(console.error);License
MIT
