我是一名Java开发人员,但我需要做AZ-204证书,并且所有问题都是C#。现在我喜欢看到事情“在行动”,所以我要求 ChatGTP 为我生成一个程序来在 Azure Blob 中创建文件,他想出了这个:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<UseWPF>false</UseWPF>
<UseWindowsForms>false</UseWindowsForms>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.10.0" />
<PackageReference Include="Microsoft.Azure.Identity" Version="1.7.0" />
</ItemGroup>
</Project>
现在是 c# 文件:
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Azure.Identity;
using Azure.Storage.Blobs;
class Program
{
static async Task Main(string[] args)
{
// Replace these values with your actual Azure Blob Storage information
var blobStorageUrl = "https://somestorage.blob.core.windows.net/";
var containerName = "somecontainer1";
var blobName = "vm1example.txt"; // Name of the file in Blob Storage
// Create a token credential using the system-assigned managed identity
var tokenCredential = new DefaultAzureCredential();
// Create a BlobServiceClient using the token credential
var blobServiceClient = new BlobServiceClient(new Uri(blobStorageUrl), tokenCredential);
// Get a reference to the container
var blobContainerClient = blobServiceClient.GetBlobContainerClient(containerName);
// Create or get a reference to the blob
var blobClient = blobContainerClient.GetBlobClient(blobName);
// Specify the content to write to the blob
var content = "Hello, Azure Blob Storage!";
// Convert the content to bytes
var contentBytes = Encoding.UTF8.GetBytes(content);
// Create a stream from the content bytes
using (var stream = new MemoryStream(contentBytes))
{
// Upload the content to the blob
await blobClient.UploadAsync(stream, true);
}
Console.WriteLine("String written to Blob Storage successfully.");
}
}
我尝试用以下方法构建它:
dotnet build -o out
我安装了 SDK 8.0,但我所做的任何事情都无法编译这个东西。上面的错误是:
C:\programas\writeblobfile.csproj:错误 NU1100:无法解析“net6.0”的“Azure.Storage.Blobs (>= 12.10.0)”。 C:\programas\writeblobfile.csproj:错误 NU1100:无法解析“net6.0”的“Microsoft.Azure.Identity (>= 1.7.0)”。 C:\programas\writeblobfile.csproj:错误 NU1100:无法解析“net6.0”的“Microsoft.NETCore.App.Ref (= 6.0.25)” 。 C:\programas\writeblobfile.csproj:错误 NU1100:无法解析 'Microsoft.WindowsDesktop.App.Ref (= 6.0.25)' net6.0'。 C:\programas\writeblobfile.csproj:错误 NU1100:无法解析“net6”的“Microsoft.AspNetCore.App.Ref (= 6.0.25)” .0'。 C:\programas\writeblobfile.csproj:错误 NU1100:无法解析“Microsoft.NETCore.App.Host.win-x64 (= 6.0.25)” 'net6.0'。 0 警告 6 个错误
我知道有这方面的 Microsoft 示例,但它们很大,并且包含来自世界各地的大量内容。难道我们不能有一个小项目来做到这一点吗?
谢谢,
发生错误的原因是目标项目 Net 6.0 和 SDK 8.0 之间存在兼容性问题,并且您使用的是旧版本的 Azure 身份。
根据文档可以使用最新版本的azure身份
<PackageReference Include="Azure.Identity" Version="1.10.3" />
在我的环境中,我尝试使用最新的软件包和版本,它上传了包含内容的文本文档。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.10.3" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.19.0" />
</ItemGroup>
</Project>
相同代码:
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Azure.Identity;
using Azure.Storage.Blobs;
class Program
{
static async Task Main(string[] args)
{
var blobStorageUrl = "https://venkat789.blob.core.windows.net";
var containerName = "sample";
var blobName = "vm1example.txt";
var tokenCredential = new DefaultAzureCredential();
var blobServiceClient = new BlobServiceClient(new Uri(blobStorageUrl), tokenCredential);
var blobContainerClient = blobServiceClient.GetBlobContainerClient(containerName);
var blobClient = blobContainerClient.GetBlobClient(blobName);
var content = "Hello, Azure Blob Storage!";
var contentBytes = Encoding.UTF8.GetBytes(content);
using (var stream = new MemoryStream(contentBytes))
{
await blobClient.UploadAsync(stream, true);
}
Console.WriteLine("String written to Blob Storage successfully.");
}
}
输出:
"String written to Blob Storage successfully."
传送门:
更新:
即使使用 SDK 8.0,我也可以将文本文件上传到 Azure Blob 存储。尝试升级您的 Azure Blob 存储和 Azure 身份 NuGet 包。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.10.4" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.19.1" />
</ItemGroup>
</Project>
输出: