簡體   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