---
# System prepended metadata

title: Azure Blob Storage+Multer and Node.js Setting

---

# Azure Blob Storage+Multer and Node.js Setting

## Azure Blob申請
(1) 選擇服務:儲存體帳戶
![](https://i.imgur.com/SrpJ4D0.png)

(2) 依專案需求設定申請
![](https://i.imgur.com/Mc7TX5t.png)

(3)新增完成後, 點選名稱進入
![](https://i.imgur.com/Iyzqiqp.png)

(4)新增容器, 可使用code新增, 或是直接在介面設置
![](https://i.imgur.com/wFVNcJf.png)

(5)存取金鑰>連結字串
![](https://i.imgur.com/4WPtM0Y.png)

(6) Node JS設定-1-新增環境變數
![](https://i.imgur.com/2x0Qfz2.png)

(7) 新建檔案tempUpload.ts(npm需安裝multer,multer-azure-storage)
```TS=1

import * as multer from 'multer';
import * as azureStorage from 'multer-azure-storage';

const tempUploadMiddleware = multer({
  storage: new azureStorage({
    azureStorageConnectionString: process.env.BLOB_KEY,
    containerName: process.env.BLOB_CONTAINER_NAME,
  }),
});

export default tempUploadMiddleware;
```
(8)在Routes.ts 加入設定
```TS=1
  {
    path: '/api/testBlob',
    method: 'get',
    middlewares: [
      tempUploadMiddleware.any(),
      TestController.testBlob,
    ],
  },
```

(9)TestController讀取設定
```TS=1
export const testBlob = async (req: Request, res: Response, next: NextFunction) => {
  try {
    console.log('# req.query:', req.query);
    console.log('# req.body:', req.body);

    const fileUrls = new Array();
    let file;
    const fileSize = req.files.length;
    for (let i = 0; i < fileSize; i++) {
      console.log(req.files[i]);
      if (req.files[i].fieldname === 'excelFile') {
        file = req.files[i];
        fileUrls.push(file.url);
      }
    }

    // @ts-ignore
    res.json(JSON.stringify(fileUrls));

  } catch (err) {
    next(err);
  }
};
```
file可使用的參數:
```JSON=1
{
  fieldname: 'excelFile', 
  originalname: 'ExportExcel-Summary_20201030.xlsx',      
  encoding: '7bit',                             
  mimetype: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',                  
  blobName: 'excelFile-e296f27c.xlsx', 
  container: 'tmsblob',                         
  blob: 'excelFile-e296f27.xlsx',
  blobType: 'BlockBlob',                         
  size: '18914',                                 
  etag: '"0x8D88C5EAB0F5EA0"',                   
  metadata: {}, 
  url: 'https://tmsstorage1.blob.core.windows.net/tmsblob/excelFile-e296f27c.xlsx'                                     
}
```

(10) Blob storage上傳後的結果
![](https://i.imgur.com/fzhDcma.png)
