繁体   English   中英

上传到 Azure Blob 存储时出现 400 错误

[英]400 error when uploading to Azure Blob Storage

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

从文档中,似乎没有任何建议表明代码方面的方法应该有所不同。 然而,我们在尝试上传时收到 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();
                    } ```

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

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

您可能想尝试使用premium BlockBlobStorage

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

如果没有,请尝试捕获异常。 自定义异常包含HttpStatusMessageRequestInformation属性,在那里你会发现你的错误的请求的情况下。

错误代码对应

BlockListTooLong Bad Request (400) 阻止列表包含的块不能超过 50,000。

此处列出了其他错误代码:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM