Hey there, fellow developer! Ready to dive into the world of Azure Blob Storage? Let's get your JavaScript app talking to the cloud with the @azure/storage-blob
package. Buckle up, because we're about to make storage a breeze!
Before we jump in, make sure you've got:
First things first, let's get that package installed:
npm install @azure/storage-blob
Easy peasy, right?
Now, let's import what we need and get that client set up:
const { BlobServiceClient } = require("@azure/storage-blob"); const connStr = "your_connection_string_here"; const blobServiceClient = BlobServiceClient.fromConnectionString(connStr);
Let's create a cozy home for our blobs:
async function createContainer(containerName) { const containerClient = blobServiceClient.getContainerClient(containerName); await containerClient.create(); console.log(`Container "${containerName}" created successfully!`); }
Time to send some data to the cloud:
async function uploadBlob(containerName, blobName, data) { const containerClient = blobServiceClient.getContainerClient(containerName); const blockBlobClient = containerClient.getBlockBlobClient(blobName); await blockBlobClient.upload(data, data.length); console.log(`Blob "${blobName}" uploaded successfully!`); }
Let's see what we've got up there:
async function listBlobs(containerName) { const containerClient = blobServiceClient.getContainerClient(containerName); for await (const blob of containerClient.listBlobsFlat()) { console.log(`Blob name: ${blob.name}`); } }
Bringing data back down to earth:
async function downloadBlob(containerName, blobName) { const containerClient = blobServiceClient.getContainerClient(containerName); const blobClient = containerClient.getBlobClient(blobName); const downloadBlockBlobResponse = await blobClient.download(); const downloaded = await streamToBuffer(downloadBlockBlobResponse.readableStreamBody); console.log(`Blob "${blobName}" downloaded successfully!`); return downloaded; } // Helper function to convert stream to buffer async function streamToBuffer(readableStream) { return new Promise((resolve, reject) => { const chunks = []; readableStream.on("data", (data) => { chunks.push(data instanceof Buffer ? data : Buffer.from(data)); }); readableStream.on("end", () => { resolve(Buffer.concat(chunks)); }); readableStream.on("error", reject); }); }
Sometimes we need to clean house:
async function deleteBlob(containerName, blobName) { const containerClient = blobServiceClient.getContainerClient(containerName); const blobClient = containerClient.getBlobClient(blobName); await blobClient.delete(); console.log(`Blob "${blobName}" deleted successfully!`); }
Give your blobs some extra info:
async function setMetadata(containerName, blobName, metadata) { const containerClient = blobServiceClient.getContainerClient(containerName); const blobClient = containerClient.getBlobClient(blobName); await blobClient.setMetadata(metadata); console.log(`Metadata set for blob "${blobName}"`); }
Optimize your storage costs:
async function setAccessTier(containerName, blobName, tier) { const containerClient = blobServiceClient.getContainerClient(containerName); const blobClient = containerClient.getBlobClient(blobName); await blobClient.setAccessTier(tier); console.log(`Access tier set to "${tier}" for blob "${blobName}"`); }
Always wrap your Azure operations in try-catch blocks and use async/await for clean, readable code:
async function safeOperation() { try { // Your Azure operations here } catch (error) { console.error("An error occurred:", error.message); } }
Never hardcode your connection strings! Use environment variables or Azure Key Vault to keep your secrets safe.
For large files, consider using parallel uploads:
async function parallelUpload(containerName, blobName, filePath) { const containerClient = blobServiceClient.getContainerClient(containerName); const blockBlobClient = containerClient.getBlockBlobClient(blobName); await blockBlobClient.uploadFile(filePath, { concurrency: 20, onProgress: (ev) => console.log(`Uploaded ${ev.loadedBytes} bytes`) }); }
And there you have it! You're now equipped to integrate Azure Blob Storage into your JavaScript applications like a pro. Remember, practice makes perfect, so don't be afraid to experiment and push the boundaries of what you can do with cloud storage.
Keep coding, keep learning, and most importantly, have fun with it! If you want to dive deeper, check out the official Azure Blob Storage documentation. Happy coding!