Uploading File To Azure Storage causes the error: Timeouts are not supported on this stream

柔情痞子 提交于 2019-12-13 16:21:58

问题


I have a form including a file upload to Azure Storage. This is where ToStream() method called:

 Image img= Image.FromStream(file.InputStream, true, true);
 if (img.Height != height || img.Width != width)
 img= img.GetThumbnailImage(width, height, null, new IntPtr());
 img.ToStream().SaveAsBlob(blobname, filename);

And this is ToStream() Method:

public static Stream ToStream(this Image image)
{
    Stream ms = new MemoryStream();
    image.Save(ms, ImageFormat.Jpeg);
    ms.Position = 0;
    return ms;
}

Where I get the error is:

  image.Save(ms, ImageFormat.Jpeg);

The error is

'ms.ReadTimeout' threw an exception of type 'System.InvalidOperationException'
'ms.WriteTimeout' threw an exception of type 'System.InvalidOperationException'

Its base is:

Timeouts are not supported on this stream 

Here is SaveAsBlob() Method:

public static void SaveAsBlob(this Stream stream, string containername, string blobname)
{
     BlobHelper helper = new BlobHelper();
     CloudBlobContainer container = helper.ContainerGet(containername);
     helper.BlobDelete(container, blobname);
     helper.BlobAdd(container, blobname, stream);
}

BlobHelper Class:

public class BlobHelper
{
    private const int MaxBlockSize = 4000000;

    private CloudStorageAccount _CloudStorageAccount { get; set; }

    public BlobHelper()
    {
        this._CloudStorageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings[StaticConfiguration.ConnectionString].ConnectionString);
    }

    public CloudBlobContainer ContainerGet(string adi)
    {
        CloudBlobClient blobClient = _CloudStorageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference(adi);
        container.CreateIfNotExists();
        BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
        containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob;
        container.SetPermissions(containerPermissions);
        return container;
    }

    public Uri BlobAdd(string filePath, CloudBlobContainer container)
    {
        byte[] fileContent = File.ReadAllBytes(filePath);
        string blobName = Path.GetFileName(filePath);
        return BlobAdd(fileContent, container, blobName);
    }

    public Uri BlobAdd(CloudBlobContainer container, string adi, Stream stream)
    {
        adi = adi.Replace("//", "/");
        byte[] content = new byte[stream.Length];
        stream.Read(content, 0, (int)stream.Length);
        return BlobAdd(content, container, adi);
    }

    public Uri BlobAdd(byte[] fileContent, CloudBlobContainer container, string blobName)
    {
        CloudBlockBlob blob = container.GetBlockBlobReference(blobName);

        HashSet<string> blocklist = new HashSet<string>();
        foreach (FileBlock block in GetFileBlocks(fileContent))
        {
            blob.PutBlock(
                block.Id,
                new MemoryStream(block.Content, true),
                null
                );

            blocklist.Add(block.Id);
        }
        blob.PutBlockList(blocklist);
        blob.FetchAttributes();
        blob.Properties.CacheControl = "public, max-age=31536000";
        blob.SetProperties();
        return blob.Uri;
    }

    private IEnumerable<FileBlock> GetFileBlocks(byte[] fileContent)
    {
        HashSet<FileBlock> hashSet = new HashSet<FileBlock>();
        if (fileContent.Length == 0)
            return new HashSet<FileBlock>();

        int blockId = 0;
        int ix = 0;

        int currentBlockSize = MaxBlockSize;

        while (currentBlockSize == MaxBlockSize)
        {
            if ((ix + currentBlockSize) > fileContent.Length)
                currentBlockSize = fileContent.Length - ix;

            byte[] chunk = new byte[currentBlockSize];
            Array.Copy(fileContent, ix, chunk, 0, currentBlockSize);

            hashSet.Add(
                new FileBlock()
                {
                    Content = chunk,
                    Id = Convert.ToBase64String(System.BitConverter.GetBytes(blockId))
                });

            ix += currentBlockSize;
            blockId++;
        }

        return hashSet;
    }

    public void BlobGet(CloudBlobContainer container, string adi, Stream stream)
    {
        CloudBlockBlob blob = container.GetBlockBlobReference(adi);
        blob.DownloadToStream(stream);
    }

    public IEnumerable<IListBlobItem> BlobListGet(CloudBlobContainer container)
    {
        return container.ListBlobs();
    }

    public void BlobDelete(CloudBlobContainer container, string adi)
    {
        CloudBlockBlob blob = container.GetBlockBlobReference(adi);
        blob.DeleteIfExists();
    }
}

回答1:


If you are getting a 403 error at the call to container.CreateIfNotExists or container.SetPermissions, ensure that the connection string you are using contains an account key that is valid. You can check this in the Azure portal and regenerate the key and replace the old one in your web/app.config if not.



来源:https://stackoverflow.com/questions/20929384/uploading-file-to-azure-storage-causes-the-error-timeouts-are-not-supported-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!