-2

I am unable to find a way to upload a file not as a stream (buffer, base64) but as a file(png,jgeg,jpg) to Azure Storage Blob.

MY Stream Code is

const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  sharedKeyCredential, defaultAzureCredentials
);

createBlob = (blobName,blob)=>{
    try{
      async function main() {
          const containerClient = blobServiceClient.getContainerClient('blue');
          const content = base64_encode(blob.buffer);
          const blockBlobClient = containerClient.getBlockBlobClient(blobName);
          const uploadBlobResponse = await blockBlobClient.upload(content, content.length);
          console.log(`Upload block blob ${blobName} successfully`, uploadBlobResponse.requestId);
          return uploadBlobResponse.requestId;
        }

        main();
  }
  catch(err){
      res.send(err)
  }
}

function base64_encode(file) {
    // read binary data
    //var bitmap = fs.readFileSync(file);
    // convert binary data to base64 encoded string
    return file.toString('base64');
}
Amaan Imtiyaz
  • 254
  • 6
  • 16
  • You basically upload the buffer (which is binary data) and name the blob appropriately. I'm not understanding what's the issue in that. Can you please explain? – Gaurav Mantri Jan 30 '20 at 12:14
  • The question lacks clarity - as I read it you are asking how to upload a file as a file. – RamblinRose Jan 30 '20 at 12:17
  • @GauravMantri This is the basic upload code from the azure documentation. I don't know why you didn't understand – Amaan Imtiyaz Jan 31 '20 at 09:55

2 Answers2

4

It seems that you were using @azure/storage-blob and your code inspired from Create a blob by uploading data to.

There is a function uploadFile of BlockBlobClient that can help to directly upload a local file to Azure Blob Storage, as the figure below.

enter image description here

Here is my sample code.

const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");

// Enter your storage account name and shared key
const account = "<your account name>";
const accountKey = "<your account key>";

// Use StorageSharedKeyCredential with storage account and account key
// StorageSharedKeyCredential is only avaiable in Node.js runtime, not in browsers
const sharedKeyCredential = new StorageSharedKeyCredential(account, accountKey);
const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  sharedKeyCredential
);

var containerName = '<your container name>';
var blobName = '<your blob name>';

const containerClient = blobServiceClient.getContainerClient(containerName);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);

var filePath = '<your local file path>';
blockBlobClient.uploadFile(filePath);
Peter Pan
  • 23,476
  • 4
  • 25
  • 43
1

You can specify the content type in the options

  await blockBlobClient.uploadStream(stream, bufferSize, maxConcurrency, {
    blobHTTPHeaders: {
      blobContentType: "image/jpeg"
    }
  })

Jeremy Meng
  • 420
  • 1
  • 4
  • 12