gpt4 book ai didi

c# - 如何使用 C# 将文件上传到 Azure Blob 存储?

转载 作者:行者123 更新时间:2023-12-02 06:29:43 28 4
gpt4 key购买 nike

我有一个控制台应用程序,它是在 Core.NET 2.2 框架之上使用 C# 编写的。

我想将存储从本地更改为 Azure Blob 存储。我下载了WindowsAzure.Storage连接到我的 Azure 帐户。

我有以下界面

public interface IStorage
{
Task Create(Stream stram, string path);
}

我创建了以下接口(interface)作为 blob 容器工厂

public interface IBlobContainerFactory
{
CloudBlobContainer Get();
}

这是我的 Azure 实现

public class AzureBlobStorage : IStorage
{
private IBlobContainerFactory ContainerFactory

public AzureBlobStorage(IBlobContainerFactory containerFactory)
{
ContainerFactory = containerFactory;
}

public async Task Create(Stream stream, string path)
{
CloudBlockBlob blockBlob = ContainerFactory.Get().GetBlockBlobReference(path);

await blockBlob.UploadFromStreamAsync(stream);
}
}

然后,在我的 program.cs 文件中,我尝试了以下操作

if (Configuration["Default:StorageType"].Equals("Azure", StringComparison.CurrentCultureIgnoreCase))
{
services.AddSingleton(opts => new AzureBlobOptions
{
ConnectionString = Configuration["Storages:Azure:ConnectionString"],
DocumentContainer = Configuration["Storages:Azure:DocumentContainer"]
});

services.AddSingleton<IBlobContainerFactory, DefaultBlobContainerFactory>();
services.AddScoped<IStorage, AzureBlobStorage>();
}
else
{
services.AddScoped<IStorage, LocalStorage>();
}

Container = services.BuildServiceProvider();

// Resolve the storage from the IoC container
IStorage storage = Container.GetService<IStorage>();

// Read a local file
using (FileStream file = File.Open(@"C:\Screenshot_4.png", FileMode.Open))
{
try
{
// write it to the storeage
storage.Create(file, "test/1.png");
}
catch (Exception e)
{

}
}

但是,当我使用AzureBlobStorage时,什么也没有发生。文件不会写入存储,也不会引发异常!

如何排除故障?如何正确地将文件写入存储?

请注意,当我将 Default:StorageType 中的配置更改为 Local 时,文件将按预期在本地写入。但无法将其写入 Azure 博客。

最佳答案

我关注了这篇文章:https://learn.microsoft.com/en-us/dotnet/api/overview/azure/storage?view=azure-dotnet

public interface IStorage
{
Task Create(Stream stream, string path);
}

public class AzureBlobStorage : IStorage
{
public async Task Create(Stream stream, string path)
{
// Initialise client in a different place if you like
string storageConnectionString = "DefaultEndpointsProtocol=https;"
+ "AccountName=[ACCOUNT]"
+ ";AccountKey=[KEY]"
+ ";EndpointSuffix=core.windows.net";

CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString);
var blobClient = account.CreateCloudBlobClient();

// Make sure container is there
var blobContainer = blobClient.GetContainerReference("test");
await blobContainer.CreateIfNotExistsAsync();

CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(path);
await blockBlob.UploadFromStreamAsync(stream);
}
}

class Program
{
static void Main(string[] args)
{
// Put your DI here
var storage = new AzureBlobStorage();

// Read a local file
using (FileStream file = File.Open(@"C:\cartoon.PNG", FileMode.Open))
{
try
{
// Pattern to run an async code from a sync method
storage.Create(file, "1.png").ContinueWith(t =>
{
if (t.IsCompletedSuccessfully)
{
Console.Out.WriteLine("Blob uploaded");
}
}).Wait();
}
catch (Exception e)
{
// Omitted
}
}
}
}

关于c# - 如何使用 C# 将文件上传到 Azure Blob 存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54508975/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com