Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

upload file in azure blob storage

I am trying to upload the file that I have stored in MemoryStream using the following code.

        private static void SaveStream(MemoryStream stream, string fileName)
        {
            var blobStorageService = new BlobStorageService();
            UploadBlob(stream, fileName);
        }

        public void UploadBlob(MemoryStream fileStream,string fileName)
        {
            var blobContainer = _blobServiceClient.GetBlobContainerClient(Environment
                               .GetEnvironmentVariable("ContainerName"));
            var blobClient = blobContainer.GetBlobClient(fileName);
            blobClient.Upload(fileStream);  <--- Error Message

        }

Error Message: System.ArgumentException: 'content.Position must be less than content.Length.Please set content.Position to the start of the data to upload.'

like image 693
Bippan Kumar Avatar asked Sep 11 '20 04:09

Bippan Kumar


People also ask

Can you store files in Azure blob storage?

Blob storage is optimized for storing massive amounts of unstructured data, such as text or binary data. Blob storage is ideal for: Serving images or documents directly to a browser. Storing files for distributed access.

How do I upload a file in Azure blob storage using REST API?

Upload contents of a folder to Data Box Blob storage To get your account key, in the Azure portal, go to your storage account. Go to Settings > Access keys, select a key, and paste it into the AzCopy command. If the specified destination container does not exist, AzCopy creates it and uploads the file into it.


1 Answers

This happened because the current position is at the end of the stream. You can set the position to the start of the stream before uploading

var blobClient = blobContainer.GetBlobClient(fileName);
fileStream.Position =0;
blobClient.Upload(fileStream)
like image 123
Toan Nguyen Avatar answered Sep 22 '22 09:09

Toan Nguyen