Azure Blob Storage with JavaScript
Tags: azure, nelsoncode, javascript, node
Published: 2022-01-05 02:12:49

In this publication we will see the main methods to use with Azure Blob Storage this example was intended using Express JS

```
npm i @azure/storage-blob dotenv
```

### Blob Service Client

```javascript
import { BlobServiceClient } from "@azure/storage-blob";
import { config } from "dotenv";

config();

const blobService = BlobServiceClient.fromConnectionString(
  process.env.AZURE_STORAGE_CONNECTION_STRING
);
```


### Connection String

![Connection String Azure Blob Storage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fhx57qlvofcwst3z09ly.png)


## Methods for blobs (Files)

### Upload Blob

```javascript
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 });
  }
};
```
### Get Blob

```javascript
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 });
  }
};
```

### Download Blob

```javascript
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 });
  }
};
```

### Delete Blob

```javascript
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)

### Create Container

```javascript
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 });
  }
};
```

### Delete Container

```javascript
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 });
  }
};
```

### List Containers

```javascript
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 });
  }
};
```

Example in GitHub Gist
https://gist.github.com/nelsoncode019/5efaa894e26f0e7c8566ae3891dbb9a8