【问题标题】:Read/Write into Azure Blob from .NET Core Web API using Azur Ad Auth使用 Azure Ad Auth 从 .NET Core Web API 读取/写入 Azure Blob
【发布时间】:2021-10-13 22:48:40
【问题描述】:

我正在尝试从我的 API 合并 blob 上传。于是创建了存储帐户和容器。

并且身份验证模式设置为 Azure AD。在我们的组织政策中,我们被限制使用连接字符串

但是关于从 blob 上传/下载的教程中有 90% 使用的是 Connectionstring .NET Core Blob access

但由于我是基于 Azure AD 的,我真的很困惑如何继续

我尝试过这种方式,但很晚我发现它在 .net 5 中不支持

        var tokenCredential = new TokenCredential(_StorageAuth.AccessToken, TokenRenewerAsync, null, tokenExpiry);
        string blobcontainer = "blob-container-dev-dv1";
        string storageName = "apllmyappdevuksst";

        var storageAccount = new CloudStorageAccount(new StorageCredentials(tokenCredential), storageName, null, true);


        
        //Uri accountUri = new Uri(storageAccount.BlobStorageUri.ToString());
        //BlobServiceClient client = new BlobServiceClient(accountUri, new DefaultAzureCredential());

我真的不知道接下来如何从 .NET core 5 将文件上传到 blob

令牌和存储范围的一切都正确

【问题讨论】:

标签: azure .net-core azure-blob-storage .net-5


【解决方案1】:

我不知道,这太直接了。分享给有类似需求的人 非常感谢@Gaurav Matri 指出这一点

 BlobServiceClient client = new BlobServiceClient(new Uri("https://storageaccount.blob.core.windows.net/container"), new DefaultAzureCredential());
  BlobContainerClient containerClient = client.GetBlobContainerClient("container");

   string f = Path.GetFileName(filename);
   BlobClient blobClient = containerClient.GetBlobClient(f);

   var path = Path.Combine(Directory.GetCurrentDirectory(), "ADOData", "Before2020.txt");
   using FileStream uploadFileStream = File.OpenRead(path);

   await blobClient.UploadAsync(uploadFileStream, true);
   uploadFileStream.Close()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-30
    • 2019-03-14
    • 1970-01-01
    • 2021-06-10
    • 2022-12-06
    • 2020-06-27
    • 2020-06-28
    • 2020-03-09
    相关资源
    最近更新 更多