Пример #1
0
        /// <summary>
        /// 用于配置依赖注入以在运行时根据依赖关系创建对象
        /// </summary>
        /// <param name="services"></param>
        public override void ConfigureServices(IServiceCollection services)
        {
            base.ConfigureServices(services);

            var cosConfig = new TencentCosConfig();

            Configuration.GetSection("TencentCos").Bind(cosConfig);

            services.AddSingleton <ITencentCosProvider>(o => new TencentCosProvider(cosConfig));
        }
Пример #2
0
        public TencentStorageTests()
        {
            var cosConfig = new TencentCosConfig
            {
                //这里使用自己的腾讯云相关配置
                //AppId = "",
                //SecretId = "",
                //SecretKey = "",
                //BucketName = "test",
                //Region = "ap-chengdu"
            };

            if (cosConfig.AppId == null)
            {
                cosConfig = ConfigHelper.LoadConfig <TencentCosConfig>("TencentStorage");
            }
            var tencentStorage = new TencentStorageProvider(cosConfig);

            StorageProvider = tencentStorage;
        }
        public TencentStorageTests()
        {
            var cosConfig = new TencentCosConfig
            {
                //这里使用自己的腾讯云相关配置
                AppId      = "",
                SecretId   = "",
                SecretKey  = "",
                BucketName = "test",
                Region     = "ap-chengdu"
            };

            //如果没填,尝试从配置文件加载
            if (string.IsNullOrWhiteSpace(cosConfig.AppId))
            {
                cosConfig = ConfigHelper.LoadConfig <TencentCosConfig>("TencentStorage");
            }
            var tencentStorage = new TencentStorageProvider(cosConfig);

            StorageProvider = tencentStorage;
        }
Пример #4
0
        /// <summary>
        /// 根据配置初始化存储提供程序
        /// </summary>
        public void Initialize()
        {
            //日志函数
            //void LogAction(string tag, string message)
            //{
            //    if (tag.Equals("error", StringComparison.CurrentCultureIgnoreCase))
            //        Logger.Error(message);
            //    else
            //        Logger.Debug(message);
            //}

            #region 配置存储程序
            switch (_appConfiguration.Configuration["StorageProvider:Type"])
            {
            case "LocalStorageProvider":
            {
                var rootPath = _appConfiguration.Configuration["StorageProvider:LocalStorageProvider:RootPath"];
                if (!rootPath.Contains(":"))
                {
                    rootPath = Path.Combine(_env.WebRootPath, rootPath);
                }

                if (!Directory.Exists(rootPath))
                {
                    Directory.CreateDirectory(rootPath);
                }

                StorageProvider = new LocalStorageProvider(rootPath, _appConfiguration.Configuration["StorageProvider:LocalStorageProvider:RootUrl"]);
                break;
            }

            case "AliyunOssStorageProvider":
            {
                AliyunOssConfig aliyunOssConfig;

                if (Convert.ToBoolean(_settingManager.GetSettingValueAsync(AppSettings.AliStorageManagement.IsEnabled).Result))
                {
                    aliyunOssConfig = new AliyunOssConfig()
                    {
                        AccessKeyId     = _settingManager.GetSettingValueAsync(AppSettings.AliStorageManagement.AccessKeyId).Result,
                        AccessKeySecret = _settingManager.GetSettingValueAsync(AppSettings.AliStorageManagement.AccessKeySecret).Result,
                        Endpoint        = _settingManager.GetSettingValueAsync(AppSettings.AliStorageManagement.EndPoint).Result
                    };
                }
                else
                {
                    aliyunOssConfig = new AliyunOssConfig()
                    {
                        AccessKeyId     = _appConfiguration.Configuration["StorageProvider:AliyunOssStorageProvider:AccessKeyId"],
                        AccessKeySecret = _appConfiguration.Configuration["StorageProvider:AliyunOssStorageProvider:AccessKeySecret"],
                        Endpoint        = _appConfiguration.Configuration["StorageProvider:AliyunOssStorageProvider:Endpoint"]
                    };
                }

                if (aliyunOssConfig.AccessKeyId.IsNullOrWhiteSpace())
                {
                    throw new UserFriendlyException("AliyunOssStorageProvider accessKeyId is null!");
                }
                if (aliyunOssConfig.AccessKeySecret.IsNullOrWhiteSpace())
                {
                    throw new UserFriendlyException("AliyunOssStorageProvider accessKeySecret is null!");
                }
                if (aliyunOssConfig.Endpoint.IsNullOrWhiteSpace())
                {
                    throw new UserFriendlyException("AliyunOssStorageProvider endpoint is null!");
                }

                StorageProvider = new AliyunOssStorageProvider(aliyunOssConfig);
                break;
            }

            case "TencentCosStorageProvider":
            {
                TencentCosConfig tencentCosConfig;

                if (Convert.ToBoolean(_settingManager.GetSettingValueAsync(AppSettings.TencentStorageManagement.IsEnabled).Result))
                {
                    tencentCosConfig = new TencentCosConfig()
                    {
                        AppId      = _settingManager.GetSettingValueAsync(AppSettings.TencentStorageManagement.AppId).Result,
                        BucketName = _settingManager.GetSettingValueAsync(AppSettings.TencentStorageManagement.BucketName).Result,
                        Region     = _settingManager.GetSettingValueAsync(AppSettings.TencentStorageManagement.Region).Result,
                        SecretId   = _settingManager.GetSettingValueAsync(AppSettings.TencentStorageManagement.SecretId).Result,
                        SecretKey  = _settingManager.GetSettingValueAsync(AppSettings.TencentStorageManagement.SecretKey).Result
                    };
                }
                else
                {
                    tencentCosConfig = new TencentCosConfig()
                    {
                        AppId      = _appConfiguration.Configuration["StorageProvider:TencentCosStorageProvider:AppId"],
                        BucketName = _appConfiguration.Configuration["StorageProvider:TencentCosStorageProvider:BucketName"],
                        Region     = _appConfiguration.Configuration["StorageProvider:TencentCosStorageProvider:Region"],
                        SecretId   = _appConfiguration.Configuration["StorageProvider:TencentCosStorageProvider:SecretId"],
                        SecretKey  = _appConfiguration.Configuration["StorageProvider:TencentCosStorageProvider:SecretKey"]
                    };
                }

                if (tencentCosConfig.AppId.IsNullOrWhiteSpace())
                {
                    throw new UserFriendlyException("TencentCosStorageProvider AppId is null!");
                }
                if (tencentCosConfig.BucketName.IsNullOrWhiteSpace())
                {
                    throw new UserFriendlyException("TencentCosStorageProvider BucketName is null!");
                }
                if (tencentCosConfig.Region.IsNullOrWhiteSpace())
                {
                    throw new UserFriendlyException("TencentCosStorageProvider Region is null!");
                }
                if (tencentCosConfig.SecretId.IsNullOrWhiteSpace())
                {
                    throw new UserFriendlyException("TencentCosStorageProvider SecretId is null!");
                }
                if (tencentCosConfig.SecretKey.IsNullOrWhiteSpace())
                {
                    throw new UserFriendlyException("TencentCosStorageProvider SecretKey is null!");
                }
                StorageProvider = new TencentStorageProvider(tencentCosConfig);
                break;
            }

            default:
                break;
            }
            #endregion
        }