locked
Creates a container but doesnot upload image in Azure Blob Storage RRS feed

  • Question

  • User-862431657 posted

    I am uploading a image to Azure Blob Storage.It creates container but the container doesnot contain any blobs though it doesnot show any errors.My code is like below.Please let me know what could be wrong?

    public CloudBlockBlob UploadBlob(IFormFile file)
    {
    string blobName = Path.GetFileName(ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'));
    _container.CreateIfNotExistsAsync();
    _container.SetPermissionsAsync(new BlobContainerPermissions
    {
    PublicAccess = BlobContainerPublicAccessType.Blob
    });
    //Get reference to a blob

    _blockBlob = _container.GetBlockBlobReference(blobName);

    using (var fileStream = file.OpenReadStream())
    {
    _blockBlob.UploadFromStreamAsync(fileStream);
    }
    var blobsName = _blockBlob.Uri.ToString();
    return _blockBlob;
    }

    Thursday, September 17, 2015 7:38 AM

Answers

  • User-646145796 posted

    Hi,

    This official document: https://azure.microsoft.com/en-gb/documentation/articles/storage-dotnet-how-to-use-blobs/ may be help you to upload file to Azure blob.

    In your code, we can consider change as below:

      public async Task<CloudBlockBlob> UploadBlob(IFormFile file)
            {
                string blobName = Path.GetFileName(ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'));
                await _container.CreateIfNotExistsAsync();
                await _container.SetPermissionsAsync(new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });
                //Get reference to a blob
    
                _blockBlob = _container.GetBlockBlobReference(blobName);
    
                using (var fileStream = file.OpenReadStream())
                {
                    await _blockBlob.UploadFromStreamAsync(fileStream);
                }
                var blobsName = _blockBlob.Uri.ToString();
                return _blockBlob;
            }

    Best Regards,

    Jambor

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 17, 2015 9:58 PM