【问题标题】:Azure Storage Blob without Autofac没有 Autofac 的 Azure 存储 Blob
【发布时间】:2020-02-26 12:45:05
【问题描述】:

我们正在开发 .NET Core 3.0 Web-API 以将图像上传到 Azure blob 存储。我遇到了一个样本来达到同样的效果。

下面是 Startup.cs 中使用 Autofac 的部分,特别是 ContainerBuilder 和 IComponentContext。

        private static void ConfigureStorageAccount(ContainerBuilder builder)
        {
            AzureTableStorageDebugConnectionString = Configuration["Azure:Storage:ConnectionString"];

            builder.Register(c => CreateStorageAccount(AzureTableStorageDebugConnectionString));
        }

        private static CloudStorageAccount CreateStorageAccount(string connection)
        {
            if (String.IsNullOrEmpty(connection))
            {
                throw new Exception("Azure Storage connection string is null!");
            }
            return CloudStorageAccount.Parse(connection);
        }

        private static void ConfigureServicesWithRepositories(ContainerBuilder builder)
        {
            builder.RegisterType<ImageUploadService>().AsImplementedInterfaces().InstancePerLifetimeScope();           
        }

        private static void ConfigureAzureCloudBlobContainers(ContainerBuilder builder)
        {
            builder.Register(c => c.Resolve<CloudStorageAccount>().CreateCloudBlobClient());

            builder.Register(c => GetBlobContainer(c, UploadedImagesCloudBlobContainerName))
                .Named<CloudBlobContainer>(UploadedImagesCloudBlobContainerName);
        }

        private static CloudBlobContainer GetBlobContainer(IComponentContext context, string blobContainerName)
        {
            var blob = context.Resolve<CloudBlobClient>().GetContainerReference(blobContainerName);

            var createdSuccessfully = blob.CreateIfNotExistsAsync().Result;

            if (createdSuccessfully)
            {
                blob.SetPermissionsAsync(new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });
            }

            return blob;
        }

        private static void ConfigureCloudBlobContainersForServices(ContainerBuilder builder)
        {
            builder.RegisterType<ImageUploadService>()
                   .WithParameter(
                       (pi, c) => pi.ParameterType == (typeof(CloudBlobContainer)),
                       (pi, c) => c.ResolveNamed<CloudBlobContainer>(UploadedImagesCloudBlobContainerName))
                       .AsImplementedInterfaces();
        }


是否可以完全摆脱 Autofac 并使用 Core 3.0 在 Startup.cs 中实现相同的功能?

【问题讨论】:

  • 你能说得更具体点吗?你的目标是什么?我不认为你提供的实现是好的......
  • Startup 不应将文件上传到 blob。您的服务应该根据请求执行此操作
  • @MartinBrandl 目标是在不使用 Autofac 的情况下将图像上传到 Azure Blob 存储。
  • @HariHaran 在上面的Startup.cs代码中,没有上传。上传实现已投入使用。
  • @HariHaran 是的,它是从博客中复制的,但并非毫无意义。我还想在 .NET Core 3.0 Web-API 中实现存储库模式。 Autofac 充当控制反转容器。

标签: c# azure azure-blob-storage autofac asp.net-core-webapi


【解决方案1】:

据我所知,Autofac 用于 DI。如果您不想使用它,您可以直接将内容上传到您的存储帐户,如下所示:

string connString = "the connection string from portal for your storage account, DefaultEndpointsProtocol=https;AccountName=storagetest789;AccountKey=G36m***==;EndpointSuffix=core.windows.net";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connString);
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer cloudBlobContainer =cloudBlobClient.GetContainerReference("container_name");
cloudBlobContainer.CreateIfNotExists();
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference("blob_name");
cloudBlockBlob.UploadFromFile("file_path");

建议:

为避免重新创建CloudBlobClient,可以创建一个工厂类,可以直接产生CloudBlobContainerCloudBlockBlob

而且,您可以使用Microsoft official DI implementation。并在Starup注册您的工厂。

//For example, the interface is IStorageFactory, and your implementation is MyStroageFactory
services.AddSingleton<IStorageFactory, MyStroageFactory>();

然后,您可以注入工厂。例如,在控制器中:

public class HomeController : Controller
{

    private IStorageFactory _myStorageFactory;

    public HomeController(IStorageFactory myStorageFactory)
    {
        _myStorageFactory = myStorageFactory;
    }

    public IActionResult Index()
    {
        //For example, I defined a getCloudBlockBlob method in factory
        CloudBlockBlob cloudBlockBlob = _myStorageFactory.getCloudBlockBlob("container_name","blob_name");
        cloudBlockBlob.UploadFromFile(....);

        return Ok("Uploaded!");
    }

}

【讨论】:

  • 谢谢@Jack Jia。我已经在工厂类的构造函数中启动了cloudBlobContainer。
猜你喜欢
  • 2017-11-10
  • 1970-01-01
  • 1970-01-01
  • 2012-03-02
  • 2016-03-04
  • 2021-07-09
  • 2019-07-28
  • 1970-01-01
  • 2019-10-25
相关资源
最近更新 更多