【问题标题】:400 error when uploading to Azure Blob Storage上传到 Azure Blob 存储时出现 400 错误
【发布时间】:2020-10-22 14:57:45
【问题描述】:

我的应用程序是用 C# 构建的,并且一直使用标准 gen 2 版本的 Azure Blob 存储将文件上传到容器,但在成功上传文件方面我们的行为不一致,因此我们决定尝试使用高级版减少延迟的版本。

从文档来看,似乎没有任何建议表明在代码方面的方法应该有所不同。然而,我们收到 400 个错误请求,尝试上传。我们尝试使用 Sas,但仍然遇到同样的挑战。我也尝试过手动创建容器与从代码动态创建,但仍然遇到同样的问题。

这是两种方法的sn-ps,希望有人能够指出我正确的方向

有sas

                    // level APIs
                    AccountSasBuilder sas = new AccountSasBuilder
                    {
                        // Allow access to blobs
                        Services = AccountSasServices.Blobs,

                        // Allow access to the service level APIs
                        ResourceTypes = AccountSasResourceTypes.Service,

                        // Access expires in 1 hour!
                        ExpiresOn = DateTimeOffset.UtcNow.AddHours(1)
                    };
                    // Allow read access
                    sas.SetPermissions(AccountSasPermissions.All);

                    // Create a SharedKeyCredential that we can use to sign the SAS token
                    StorageSharedKeyCredential credential = new StorageSharedKeyCredential(StorageName,StorageKey);

                    // Build a SAS URI
                    UriBuilder sasUri = new UriBuilder(StorageURL);
                    sasUri.Query = sas.ToSasQueryParameters(credential).ToString();

                    // Create a client that can authenticate with the SAS URI
                    BlobServiceClient service = new BlobServiceClient(sasUri.Uri);

                    //var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureWebJobsStorage"]);
                    //var client = storageAccount.CreateCloudBlobClient();

                    var blobContainer = service.GetBlobContainerClient(container);
                    //blobContainer.CreateIfNotExists(PublicAccessType.BlobContainer);
                    var blockBlob = blobContainer.GetBlobClient(file.Substring(file.LastIndexOf('/') + 1));
                    using (var fileStream = WaitForFile(file, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        var response = blockBlob.UploadAsync(fileStream).Result;
                        fileStream.Close();
                    }

没有sas

        var client = storageAccount.CreateCloudBlobClient();

        var blobContainer = client.GetContainerReference(container);
        blobContainer.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
                    var blockBlob = blobContainer.GetBlockBlobReference(file.Substring(file.LastIndexOf('/') + 1));
                    using (var fileStream = WaitForFile(file, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        blockBlob.UploadFromStream(fileStream);
                        fileStream.Close();
                    } ```

【问题讨论】:

  • 能否请您详细的错误信息,并与filddler捕获请求?

标签: c# azure


【解决方案1】:

在不知道异常的情况下,我会说您可能属于以下情况:https://docs.microsoft.com/en-us/rest/api/storageservices/using-blob-service-operations-with-azure-premium-storage

Premium GPv2 帐户不支持块 blob 或文件、表和队列服务。

你可能想试试premium BlockBlobStorage:

但是,高级 BlockBlobStorage 帐户确实支持阻止和附加 blob。

如果没有,请尝试捕获异常。自定义异常包含 RequestInformation 属性中的 HttpStatusMessage,您可以在其中找到错误的请求案例。

【讨论】:

    【解决方案2】:

    错误码对应

    BlockListTooLong Bad Request (400) 阻止列表可能不包含更多 超过 50,000 个区块。

    这里列出了其他错误代码:

    https://docs.microsoft.com/en-us/rest/api/storageservices/blob-service-error-codes

    您需要通过减小上传的块 blob 大小来修复错误。

    【讨论】:

      猜你喜欢
      • 2018-02-26
      • 2021-06-10
      • 2018-01-30
      • 2019-10-14
      • 2014-08-08
      • 2021-11-30
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      相关资源
      最近更新 更多