upload file in azure blob storage

Viewed 7744

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.'

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)
Related