c# - 上传到 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();
} ```
解决方案
在不知道例外情况的情况下,我会说您可能会遇到以下情况:https ://docs.microsoft.com/en-us/rest/api/storageservices/using-blob-service-operations-with-azure-premium -贮存
高级 GPv2 帐户不支持块 blob 或文件、表和队列服务。
您可能想尝试premium BlockBlobStorage
:
但是,高级 BlockBlobStorage 帐户确实支持阻止和追加 Blob。
如果没有,请尝试捕获异常。自定义异常包含属性中的 ,您将在其中找到错误的请求案例HttpStatusMessage
。RequestInformation
推荐阅读
- java - spring boot调度在方法级别启用/禁用任务
- matlab - 如何在 matlab 中绘制 PD 补偿器的输出?
- sql - WHERE 子句中的动态日期取决于当前日期
- python - 将 ProcessPoolExecutor 分配给定义的 cpus 并使用队列进行通信
- c - 使用递归反转数组
- bash - bash:用于识别导致错误的特定别名的脚本
- spring - Spring获取接收到的正文的MediaType
- ios - Swift:暂停沿路径移动的动画?
- reporting-services - 如果我们有 2 个类别组名称,如何将类别组名称放在顶部和底部?
- c++ - 直接读取和写入字节到操作系统的可见帧缓冲区?