Creating Blob in Azure Blob Storage with the same name simultaneously - c#

I have task to load some images into the blob storage simultaneously. Name of blob is defined as md5 of the blob. It can happen that different threads try to load same files from different locations.
Now I need to know how to block other threads from loading same file, if first already trying to upload such blob.

You can do it without leasing it by using optimistic concurrency. Basicly set an access condition that says this blob will be different from all etags of blobs with this name. If there is indeed a blob with some etag the second upload will fail.
var access = AccessCondition.GenerateIfNoneMatchCondition("*");
await blobRef.UploadFromStreamAsync(stream, access, null, null);

Related

How to keep empty directories in Azure blob storage?

I use BlobContainerClient from Azure.Storage.Blobs library. I try to delete some files in Blob Container. When I do so, and if some directories get empty after deletions, these directories also disappear and can no longer be seen in Azure Portal.
I need to keep all empty directories in Container. How is it possible?
Also, this question could be formulated this way:
how is it possible to create an empty directory in Azure Storage?
It is not possible to keep empty directories in Azure Blob Storage if the Hierarchical namespace is disabled
To keep the empty directories in Azure Blob Storage, you need to use Data Lake Gen2 Storage Account
To check if the Hierarchical namespace is enabled or disabled, you can use below lines of C# code:
var serviceClient = new BlobServiceClient(connectionString);
AccountInfo accountInfo = serviceClient.GetAccountInfo();
Console.WriteLine(accountInfo.IsHierarchicalNamespaceEnabled);
You need to use latest version of Azure.Storage.Blobs namespace. The package version is
Azure.Storage.Blobs v12.10.0

Download 2 GB file in Service Fabric application

I am designing an application which will support upload and download of XML file with limit of 2 GBs. I am using Azure Blob Storage as persistent storage to upload these files and then download them. Below piece of code throws OutOfMemory exception :
var blob = this.GetBlockBlobReference(blobId, blobContainerName);
return await blob.DownloadTextAsync().ConfigureAwait(false);
Also, I am not sure which approach will be faster and cleaner.
For faster downloading / uploading blobs, you can consider using
Microsoft Azure Storage Data Movement Library. This library is designed for high-performance uploading, downloading and copying Azure Storage Blob and File.
You can set parallel threads via code below:
// Setup the number of the concurrent operations
TransferManager.Configurations.ParallelOperations = 64;
Then use TransferManager.UploadAsync or TransferManager.DownloadAsync method for uploading / downloading blobs respectively.
There is an example in github of uploading, you can modify it for downloading easily.

how to get Temporary path of the uploaded file in the IIS server

i want to get the full path of the uploaded file in the IIS Server without saving the file in the disk by calling File.SaveAs() method. why i want to do this?. Once i have the full path of the file without saving it in the disk i want to upload this file in azure blob container.
I have successfully uploaded the file in azure blob container after saving the file in the disk but i don't want to first save and then delete the file from disk after uploading the file to azure blob container.
Files posted to the server are stored in streams, not in temporary directories. Assuming your object is of type HttpPostedFileBase, you would need to access the InputStream property and read the bytes into your Azure Blob.

C# Upload files into a folder in azure blob storage

I know that we can upload files to azure blob storage using below:
CloudBlockBlob cloudBlockBlob = fileContainer.GetBlockBlobReference(fileName);
await cloudBlockBlob.UploadFromFileAsync(fileFullPath);
I already created some folders in the container. I tried a few time but the files always uploaded outside the folder.
How do we upload the files into a specific folder in the blob storage?
Actually there aren't real folders in Azure Blob Storage, it's just a virtual concept. In other words, Azure Blob Storage only has simple 2-level "container - blob" structure, the so-called "folders" are just prefixes of existing blob names.
For example, if there is a blob named a/b/c.jpg. The a and b are virtual folder names, you can't directly create or delete them, they exist there just because of blob a/b/c.jpg.
I guess you have to get the right reference first ( you need to include the fullpath of the file in the GetBlockBlobReference) as in :
CloudBlockBlob cloudBlockBlob = fileContainer.GetBlockBlobReference($"yourfoldername/{fileName}");
One important thing is you DON'T need to create folder, it will automatically create it for you based on your path in the GetBlockBlobReference.

Read Oracle BLOB data as chunks

I have the following queries on fetching a BLOB data from Oracle (
I am trying to use OracleDataReader - .Net to read the BLOB value.):
Is it possible to read a BLOB data on Oracle database as chunks without loading the entire BLOB on to server memory? I believe OracleDataReader.GetBytes() will load the entire blob on server memory.
Passing a null buffer on to GetBytes() fetches the size of the BLOB but would that require the BLOB to be loaded on server's memory?
What would be the optimal way to fetch the BLOB size and BLOB data as chunks without loading the entire BLOB in memory?
Look at DBMS_LOB.READ

Categories