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.
npm install @edirect/storage-gatewayStorage Gateway client library for eDirect applications. Provides a simple interface to interact with the Storage Gateway API for managing storage configurations and file operations.
``bash`
npm install @edirect/storage-gateway
`typescript
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',
});
`
`typescript`
new StorageGatewayClient(baseUrl: string, headers?: StorageGatewayHeaders)
Parameters:
- baseUrl - The base URL of the Storage Gateway APIheaders
- (optional) - Headers to include in all requests
Types:
`typescript`
interface StorageGatewayHeaders {
authorization: string;
[key: string]: string;
}
---
List all storage configurations with optional pagination.
`typescript
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
---
Create a new storage configuration.
`typescript`
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
---
Get a specific storage configuration by key.
`typescript`
const config = await client.getStorageConfiguration('my-storage');
Parameters:
- key - The storage configuration key
---
Update an existing storage configuration.
`typescript`
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
---
Delete a storage configuration.
`typescript`
await client.deleteStorageConfiguration('my-storage');
Parameters:
- key - The storage configuration key to delete
---
List files in storage.
`typescript
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
---
Upload a file to storage.
`typescript
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
---
Download a file from storage.
`typescript
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
---
Get file metadata information.
`typescript
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
---
Delete a file from storage.
`typescript`
await client.deleteFile({
storageKey: 'my-storage',
path: 'documents/hello.txt',
});
Parameters:
- params.storageKey - The storage configuration keyparams.path
- - Path to the file to delete
---
List available storage providers.
`typescript
const providers = await client.listStorageProviders();
console.log(providers);
// ['s3', 'azure-blob', 'gcs', 'local', ...]
`
---
`typescript
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);
``