Created
January 5, 2022 02:21
-
-
Save nelsondev19/5efaa894e26f0e7c8566ae3891dbb9a8 to your computer and use it in GitHub Desktop.
Azure Blob Storage with JavaScript (Express JS)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { BlobServiceClient } from "@azure/storage-blob"; | |
import { config } from "dotenv"; | |
config(); | |
const blobService = BlobServiceClient.fromConnectionString( | |
process.env.AZURE_STORAGE_CONNECTION_STRING | |
); | |
// Methods for blobs (Files) | |
export const uploadBlob = async (req, res) => { | |
try { | |
const { container } = req.body; | |
const { originalname, buffer } = req.file; | |
const containerClient = blobService.getContainerClient(container); | |
await containerClient.getBlockBlobClient(originalname).uploadData(buffer); | |
res.json({ message: "success" }); | |
} catch (error) { | |
res.status(500).json({ message: error.message }); | |
} | |
}; | |
export const getBlob = async (req, res) => { | |
try { | |
const { container, filename } = req.params; | |
const containerClient = blobService.getContainerClient(container); | |
res.header("Content-Type", "image/jpg"); | |
const response = await containerClient | |
.getBlockBlobClient(filename) | |
.downloadToBuffer(); | |
res.send(response); | |
} catch (error) { | |
res.status(500).json({ message: error.message }); | |
} | |
}; | |
export const downloadBlob = async (req, res) => { | |
try { | |
const { container, filename } = req.params; | |
const containerClient = blobService.getContainerClient(container); | |
const response = await containerClient | |
.getBlockBlobClient(filename) | |
.downloadToBuffer(); | |
res.send(response); | |
} catch (error) { | |
res.status(500).json({ message: error.message }); | |
} | |
}; | |
export const deleteBlob = async (req, res) => { | |
try { | |
const { container, filename } = req.body; | |
const containerClient = blobService.getContainerClient(container); | |
const response = await containerClient | |
.getBlockBlobClient(filename) | |
.deleteIfExists(); | |
res.send(response); | |
} catch (error) { | |
res.status(500).json({ message: error.message }); | |
} | |
}; | |
// Methods for Containers (Folders) | |
export const createContainer = (req, res) => { | |
try { | |
const { container } = req.body; | |
blobService.createContainer(container); | |
res.json({ message: "success" }); | |
} catch (error) { | |
res.status(500).json({ message: error.message }); | |
} | |
}; | |
export const deleteContainer = (req, res) => { | |
try { | |
const { container } = req.body; | |
blobService.deleteContainer(container); | |
res.json({ message: "success" }); | |
} catch (error) { | |
res.status(500).json({ message: error.message }); | |
} | |
}; | |
export const listContainer = async (req, res) => { | |
try { | |
let containers = []; | |
for await (const container of blobService.listContainers()) { | |
containers.push(container.name); | |
} | |
res.json({ containers }); | |
} catch (error) { | |
res.status(500).json({ message: error.message }); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dependencies
Connection String