首页 > 解决方案 > 上传到 Azure Blob 存储时出现 400 错误

问题描述

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

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

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

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

标签: c#azure

解决方案


在不知道例外情况的情况下,我会说您可能会遇到以下情况:https ://docs.microsoft.com/en-us/rest/api/storageservices/using-blob-service-operations-with-azure-premium -贮存

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

您可能想尝试premium BlockBlobStorage

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

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


推荐阅读