Will not upload to Azure blob storage

Jesper Petersen 26 Reputation points
2020-10-25T18:29:02.65+00:00

This is how I try to upload to Azure. Now I try to upload then there will be no error at all.

Thus when I try to upload it does not get into my blob storage at all.

var storageCredentials = new StorageCredentials("dinekompetencer", "?????");
                var storageAccount = new CloudStorageAccount(storageCredentials, true);

                var blobClient = storageAccount.CreateCloudBlobClient();
                var container = blobClient.GetContainerReference("img");

                var blockBlob = container.GetBlockBlobReference(imgNewName);
                blockBlob.Properties.CacheControl = "max-age=3600";

                blockBlob.UploadFromStreamAsync(fileName);

And the realy code:

public void UploadImageToBlob(string imgNewName, Stream fileName)
        {
            try
            {

                var storageCredentials = new StorageCredentials(AzureHelper.Accountname, AzureHelper.KeyValue);
                var storageAccount = new CloudStorageAccount(storageCredentials, true);

                var blobClient = storageAccount.CreateCloudBlobClient();
                var container = blobClient.GetContainerReference(AzureHelper.ContainerName);

                var blockBlob = container.GetBlockBlobReference(imgNewName);
                blockBlob.Properties.CacheControl = "max-age=3600";

                blockBlob.UploadFromStreamAsync(fileName);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

        }

It is a fixed Blob container that has been created in advance.

UPDATE:

Error are :

Microsoft.WindowsAzure.Storage.StorageException: Cannot access a closed Stream.
---> System.ObjectDisposedException: Cannot access a closed Stream.
at System.IO.MemoryStream.EnsureNotClosed()
at System.IO.MemoryStream.get_Position()
at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.get_Position()
at Microsoft.AspNetCore.Http.ReferenceReadStream.VerifyPosition()
at Microsoft.AspNetCore.Http.ReferenceReadStream.set_Position(Int64 value)
at Microsoft.AspNetCore.Http.ReferenceReadStream.Seek(Int64 offset, SeekOrigin origin)
at Microsoft.WindowsAzure.Storage.Shared.Protocol.CappedLengthReadOnlyStream.Seek(Int64 offset, SeekOrigin origin)
at Microsoft.WindowsAzure.Storage.Shared.Protocol.HttpContentFactory.BuildContentFromStream[T](Stream stream, Int64 offset, Nullable1 length, String md5, RESTCommand1 cmd, OperationContext operationContext)
at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.<>c__DisplayClass83_0.<PutBlobImpl>b__0(RESTCom

Code to upload file:

var img = model.FileToUpload.FileName;
                    var imgNewName = _fileService.GetNewFileNameImg(img);
                    var fileName = model.FileToUpload.OpenReadStream();

                    _azureService.UploadImageToBlob(imgNewName, fileName);
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,570 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. deherman-MSFT 34,841 Reputation points Microsoft Employee
    2020-10-26T19:04:56.403+00:00

    @Jesper Petersen
    We have both v11 and v12 samples available for uploading a blob which might be helpful for you. I was able to find two Stack Overflow threads relating to UploadFromStreamAsync, which might be helpful for resolving your issue. The first mentions need to use await. The second mentions the need to set the position =0. Please check and see if these might resolve your issue.

    -------------------------------

    Please don’t forget to "Accept the answer" and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.