-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblob.js
74 lines (61 loc) · 2.89 KB
/
blob.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const express = require("express");
const router = express.Router();
// Middleware.
const auth = require("./middleware/auth");
// Image Upload Initialization.
const multer = require('multer');
//TODO: Look into placing more restrictions on size limits, file size etc.
const inMemoryStorage = multer.memoryStorage();
const uploadStrategy = multer({ storage: inMemoryStorage }).array('images');
const { Readable } = require('stream');
// Azure Blob Storage Initialization.
const { BlobServiceClient, StorageSharedKeyCredential, newPipeline } = require('@azure/storage-blob');
const containerName = process.env.AZURE_CONTAINER_NAME;
const ONE_MEGABYTE = 1024 * 1024;
const uploadOptions = { bufferSize: 4 * ONE_MEGABYTE, maxBuffers: 20 };
const sharedKeyCredential = new StorageSharedKeyCredential(
process.env.AZURE_STORAGE_ACCOUNT_NAME,
process.env.AZURE_STORAGE_ACCOUNT_ACCESS_KEY);
const pipeline = newPipeline(sharedKeyCredential);
const blobServiceClient = new BlobServiceClient(
`https://${process.env.AZURE_STORAGE_ACCOUNT_NAME}.blob.core.windows.net`,
pipeline
);
// Helper function to generate a filename.
const generateBlobName = (originalName) => {
// Use a random number to generate a unique file name,
// removing "0." from the start of the string.
const identifier = Math.random().toString().replace(/0\./, '');
return `${identifier}-${originalName}`;
};
// Upload images into Azure Blob Storage.
// Returns an array of imageURIs as a response.
router.post("/uploadImages", [auth, uploadStrategy], async (req, res) => {
// try {
// // Create array of images' Azure URIs to return.
// let imageAzureURIs = [];
// const containerClient = blobServiceClient.getContainerClient(containerName);
// await req.files.forEach(async (file, i) => {
// // Generate a random filename to store in Azure Blob.
// let blobName = generateBlobName(file.originalname);
// // Generate the Azure URI for the image and add it to the list.
// let imageAzureURI = `https:${process.env.AZURE_STORAGE_ACCOUNT_NAME}.blob.core.windows.net/${process.env.AZURE_CONTAINER_NAME}/${blobName}`;
// imageAzureURIs = [...imageAzureURIs, imageAzureURI];
// // Upload each image to Azure Blob Storage
// let stream = Readable.from(file.buffer);
// let blockBlobClient = containerClient.getBlockBlobClient(blobName);
// await blockBlobClient.uploadStream(
// stream,
// uploadOptions.bufferSize,
// uploadOptions.maxBuffers,
// { blobHTTPHeaders: { blobContentType: file.mimetype } }
// );
// });
// res.status(200).json(imageAzureURIs);
// } catch (err) {
// console.log(err);
// res.status(500).json(err);
// }
res.status(200).json("testing");
})
module.exports = router;