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, RESTCommand
1 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);