UNPKG

npm-storage-package

Version:

Universal cloud storage package for AWS S3, Google Cloud Storage, and Azure Blob Storage with Express.js integration

176 lines 6.37 kB
import AWS from "aws-sdk"; import fs from "fs"; import { Storage } from "@google-cloud/storage"; import { BlobServiceClient } from "@azure/storage-blob"; export async function storageAction(provider, operation, config, options) { if (!config?.bucket) throw new Error("Bucket name is required"); switch (provider) { case "s3": return s3Action(operation, config, options); case "gcs": return gcsAction(operation, config, options); case "azure": return azureAction(operation, config, options); default: throw new Error(`Unsupported provider: ${provider}`); } } // ✅ AWS S3 async function s3Action(operation, config, options) { const s3 = new AWS.S3({ region: config.region, credentials: config.credentials, }); if (operation === "upload") { if (!options.fileName) { throw new Error("fileName is required"); } if (!options.filePath && !options.buffer) { throw new Error("Either filePath or buffer is required"); } let data; if (options.buffer) { data = options.buffer; } else { data = fs.readFileSync(options.filePath); } await s3 .putObject({ Bucket: config.bucket, Key: options.fileName, Body: data, ContentType: options.mimeType, }) .promise(); return `https://${config.bucket}.s3.amazonaws.com/${options.fileName}`; } if (operation === "delete") { if (!options.key) throw new Error("key is required for delete operation"); await s3.deleteObject({ Bucket: config.bucket, Key: options.key }).promise(); return true; } if (operation === "getUrl") { if (!options.key) throw new Error("key is required for getUrl operation"); // Check if file exists before returning URL try { await s3.headObject({ Bucket: config.bucket, Key: options.key }).promise(); } catch (error) { if (error.code === "NotFound" || error.statusCode === 404) { throw new Error(`File not found: ${options.key}`); } throw error; } return `https://${config.bucket}.s3.amazonaws.com/${options.key}`; } throw new Error(`Unsupported operation: ${operation}`); } // ✅ Google Cloud Storage async function gcsAction(operation, config, options) { // Build GCS storage options const storageOptions = {}; if (config.projectId) { storageOptions.projectId = config.projectId; } if (config.keyFilename) { storageOptions.keyFilename = config.keyFilename; } if (config.credentials) { storageOptions.credentials = config.credentials; } const storage = new Storage(storageOptions); const bucket = storage.bucket(config.bucket); if (operation === "upload") { if (!options.fileName) { throw new Error("fileName is required"); } if (!options.filePath && !options.buffer) { throw new Error("Either filePath or buffer is required"); } const file = bucket.file(options.fileName); if (options.buffer) { // Upload from buffer await file.save(options.buffer, { metadata: { contentType: options.mimeType, }, }); } else { // Upload from file path await bucket.upload(options.filePath, { destination: options.fileName }); } return `https://storage.googleapis.com/${config.bucket}/${options.fileName}`; } if (operation === "delete") { if (!options.key) throw new Error("key is required for delete operation"); await bucket.file(options.key).delete(); return true; } if (operation === "getUrl") { if (!options.key) throw new Error("key is required for getUrl operation"); // Check if file exists before returning URL const file = bucket.file(options.key); const [exists] = await file.exists(); if (!exists) { throw new Error(`File not found: ${options.key}`); } return `https://storage.googleapis.com/${config.bucket}/${options.key}`; } throw new Error(`Unsupported operation: ${operation}`); } // ✅ Azure Blob Storage async function azureAction(operation, config, options) { if (!config.credentials?.connectionString) { throw new Error("Azure connectionString is required in credentials"); } const client = BlobServiceClient.fromConnectionString(config.credentials.connectionString); const container = client.getContainerClient(config.bucket); if (operation === "upload") { if (!options.fileName) { throw new Error("fileName is required"); } if (!options.filePath && !options.buffer) { throw new Error("Either filePath or buffer is required"); } const blob = container.getBlockBlobClient(options.fileName); if (options.buffer) { // Upload from buffer await blob.upload(options.buffer, options.buffer.length, { blobHTTPHeaders: { blobContentType: options.mimeType, }, }); } else { // Upload from file path await blob.uploadFile(options.filePath); } return blob.url; } if (operation === "delete") { if (!options.key) throw new Error("key is required for delete operation"); await container.deleteBlob(options.key); return true; } if (operation === "getUrl") { if (!options.key) throw new Error("key is required for getUrl operation"); // Check if file exists before returning URL const blob = container.getBlockBlobClient(options.key); const exists = await blob.exists(); if (!exists) { throw new Error(`File not found: ${options.key}`); } return `${container.url}/${options.key}`; } throw new Error(`Unsupported operation: ${operation}`); } //# sourceMappingURL=storageAction.js.map