2
votes

I am using Azure Storage JavaScript Client Library for uploading the file to azure storage by referring this link https://dmrelease.blob.core.windows.net/azurestoragejssample/samples/sample-blob.html

The below code is my upload snippet to azure storage (Which takes file and store it in storage)

var speedSummary = blobService.createBlockBlobFromBrowserFile('mycontainer', file.name, file, {blockSize : customBlockSize}, function(error, result, response) {
    finishedOrError = true;
    if (error) {
        // Upload blob failed
    } else {
        // Upload successfully
    }
});

The main problem is when I upload the same file again. It is overwriting the file. Is there any property or header that can be added to prevent overwriting. I want duplicate files also to be stored without overwriting

Please help me to resolve this issue. Thanks in advance.

1

1 Answers

1
votes

Is there any property or header that can be added to prevent overwriting.

Absolutely. You can specify EtagNonMatch access condition with * as value. From the documentation link:

If the ETag for the blob does not match the specified ETag. Specify the wildcard character (*) to perform the operation only if the resource does not exist, and fail the operation if it does exist.

This will cause the blob upload to fail in case blob by the same name exists. Access conditions can be specified in accessConditions options parameter.

You can learn more about conditional headers support in Blob Storage here: https://docs.microsoft.com/en-us/rest/api/storageservices/specifying-conditional-headers-for-blob-service-operations.

I want duplicate files also to be stored without overwriting

This is something you would need to handle on your own. If the blob upload fails because the blob already exists, you will get a Pre Condition Failed (HTTP Status Code 412) error back. Based on this error, you would need to come up with a new name for the blob and upload it again.

UPDATE

Here's the code (not tested), that you can use:

var options = {
  blockSize : customBlockSize,
  accessConditions: {
    EtagNonMatch: '*'
  }
};
var speedSummary = blobService.createBlockBlobFromBrowserFile('mycontainer', file.name, file, options, function(error, result, response) {
    finishedOrError = true;
    if (error) {
        // Upload blob failed
    } else {
        // Upload successfully
    }
});