将文件上传到 Azure 存储会导致错误:此流不支持超时

2024-01-04

我有一个表单,其中包含上传到 Azure 存储的文件。 这是调用 ToStream() 方法的地方:

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

这是 ToStream() 方法:

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

我收到错误的地方是:

  image.Save(ms, ImageFormat.Jpeg);

错误是

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

其基础是:

Timeouts are not supported on this stream 

这是 SaveAsBlob() 方法:

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 类:

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();
    }
}

如果您在调用时收到 403 错误container.CreateIfNotExists or container.SetPermissions,确保您使用的连接字符串包含有效的帐户密钥。您可以在 Azure 门户中检查这一点,然后重新生成密钥,如果没有,则替换 web/app.config 中的旧密钥。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将文件上传到 Azure 存储会导致错误:此流不支持超时 的相关文章

随机推荐

  • 如何以编程方式运行 Java 应用程序中的所有 JUnit 测试?

    通过 Eclipse 我可以轻松运行应用程序中的所有 JUnit 测试 我希望能够从应用程序 jar 在目标系统上运行测试 而无需 Eclipse 或 Ant 或 Maven 或任何其他开发工具 我可以看到如何从命令行运行特定的测试或套件
  • 如何使用pyInstaller完整打包所有必需的库?

    我已经使用 pyinstaller 创建了我的 python 应用程序的独立应用程序 pyinstaller windowed app py 它实际上在我的计算机上运行并且按预期工作 但是当我在我朋友的计算机上尝试它时 它不起作用 它运行但
  • 选择表列名称作为值[重复]

    这个问题在这里已经有答案了 给定一个具有任意数量的记录 X 和任意数量的列 Y 的 SQL 表 RecordID Column1 Column 2 Column 3 Column Y 1 Value11 Value12 Value13 Va
  • DPI 无法正确缩放

    我创建了一个自定义 UserControl 其功能与 numbericUpDown 非常相似 但具有各种增强功能 例如 它可以显示分数 但是 此控件的缩放比例不如窗体上的其他一些控件 这迫使我的 UI 看起来很尴尬 我尝试了控件及其父控件的
  • 片段 onHiddenChanged 未调用

    我最近将 Fragments 添加到我的应用程序中 对于新的应用程序 我需要获得 显示我的片段后立即通知 所以我可以尽快做一些计算 片段再次显示 我的 Fragment 与 TabIndicator 一起使用 并且仅使用一个 Fragmen
  • REXML::Document.new 我们可以在这一行给出编码参数吗?

    doc REXML Document 新文件 每当我的 xml 文件包含 UTF 8 以外的一些特殊字符时 我的代码就会失败 REXML ParseException
  • 在配置私有 GKE 集群时了解 --master-ipv4-cidr

    我试图进一步了解当我在 Google 的 Kubernetes Engine 中配置私有集群时到底发生了什么 Google 在此提供了一个配置私有集群的示例 其中控制平面服务 例如 Kubernetes API 位于172 16 0 16
  • 如何获取访问Google Play开发者API的授权

    我正在尝试访问 Google Play 开发者 APIhttps developers google com android publisher https developers google com android publisher 为
  • 以编程方式在 WinForms VB.NET/C# 中聚焦/突出显示 ListView 列标题

    在 WinForms ListView 上 将鼠标悬停在列标题上会导致其聚焦或强调 例如 我需要动态地执行此操作 因为在我的程序中使用左右键切换焦点列 尽管广泛搜索了以下属性 ListView Columns i ListView Colu
  • Haskell 在现实世界中的用途是什么? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 关于 Haskell 有很多炒作 但是 很难获得有关如何在现实世界应用程序中使用它的信息 Haskell 最流行的项目 用法是什么 为什么它擅长
  • typeahead.js 预取不起作用

    我无法让 typeahead js 中的预取函数工作 但它对于本地数据工作得很好 我首先尝试链接到返回 json 对象或列表的 servlet 但过了一会儿我放弃了 并开始检查提供的示例 因此 他们的示例链接到的页面如下所示 http tw
  • 每 10 秒调用一个函数 Angular2

    我正在尝试创建一个Timer这称为API call每 10 秒 我使用setTimeOut但问题是 它变成了无限循环 即使我推送到另一个页面 它也会继续加入 if 条件 例子 我在启动 10 秒 API 调用的方法上调用此方法 setTim
  • 将字典从 Swift 发送到 PHP

    如何将 Swift 生成的字典作为 PHP URL 中的参数发布 具体来说 任务是更新托管数据库上的许多字段 我不是将每个字段的新值定义为单独的参数 而是希望传递一个字典 其中键是字段名称 值是新值 该数据已经作为Dictionary
  • c++:程序设置 - boost.PropertyTree 还是 boost.program_options?

    我正在寻找一种在 C 中存储程序设置或选项或配置的解决方案 这些可能是在 GUI 中公开的设置 需要在代码运行之间保存 在我的搜索中我遇到了boost PropertyTree http www boost org doc libs 1 4
  • Matlab 中的矩阵到向量转换

    我有一个 MxN 矩阵 想转换为向量 MNx1 其中矩阵中行的所有元素作为向量的元素 我尝试使用reshape但我没有成功 这是小代码片段和预期结果 S 0 1 1 0 1 1 1 1 预期结果 S prime 0 1 1 0 1 1 1
  • 是否有使用 sun.jdbc.odbc.JdbcOdbcDriver 的替代方法? [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我最近将我们工作中的一个旧应用程序从 Java 1 5 迁移到 1 6 我注意到在构建过程中 我现在收
  • 如何使命名路由出口与 loadChildren 一起工作?

    我创建了两个关于路由的 loadChildren 和出口导航问题的插件 由于某种原因 加载的子模块中具有空的基本路径不适用于出口导航 In this https plnkr co edit ps0ZiD3mHTte227Ws69T p pr
  • Java HashMap 检测冲突

    有没有办法检测 Java Hash map 中的冲突 任何人都可以指出某些可能发生大量碰撞的情况吗 当然 如果你重写一个对象的哈希码并简单地返回一个常量值 那么肯定会发生冲突 我不是在谈论这个 我想知道除了前面提到的之外 在什么情况下会发生
  • WebBrowser 控件 - 安装 IE 11 后页面呈现错误

    我对 Winforms NET 类 WebBrowser 有问题安装后Internet Explorer 11 预览版 当我调用我的网页时 它看起来像是禁用了 javascript If your WebBrowser基于应用程序和您的网页
  • 将文件上传到 Azure 存储会导致错误:此流不支持超时

    我有一个表单 其中包含上传到 Azure 存储的文件 这是调用 ToStream 方法的地方 Image img Image FromStream file InputStream true true if img Height heigh