/// <summary> /// 加载七牛云存储 /// </summary> /// <param name="services"></param> /// <param name="configuration"></param> public static IServiceCollection AddQiNiuStorage(this IServiceCollection services, IConfiguration configuration) { StartUp.Run(); var section = configuration.GetSection(nameof(QiNiuStorageConfig)); if (section == null) { throw new BusinessException <string>("七牛云存储配置异常", HttpStatus.Err.Name); } QiNiuStorageConfig qiNiuStorageConfig = new QiNiuStorageConfig(section.GetValue <string>("AccessKey"), section.GetValue <string>("SecretKey"), section.GetValue <ZoneEnum>("DefaultZones"), section.GetValue <string>("DefaultHost"), section.GetValue <string>("DefaultBucket")) { IsUseHttps = section.GetValue <bool>("IsUseHttps"), UseCdnDomains = section.GetValue <bool>("UseCdnDomains"), IsAllowOverlap = section.GetValue <bool>("IsAllowOverlap"), PersistentNotifyUrl = section.GetValue <string>("PersistentNotifyUrl"), PersistentPipeline = section.GetValue <string>("PersistentPipeline"), ChunkUnit = ChunkUnit .FromValue <ChunkUnit>((section .GetValue <string>("ChunkUnit").ConvertToInt(ChunkUnit.U2048K.Id))) }; qiNiuStorageConfig.SetCallBack(section.GetValue <string>("CallbackBodyType").ConvertToInt(CallbackBodyType.Json.Id), section.GetValue <string>("CallbackHost"), section.GetValue <string>("CallbackUrl"), section.GetValue <string>("CallbackBody")); configuration.GetSection(nameof(QiNiuStorageConfig)).Bind(qiNiuStorageConfig); qiNiuStorageConfig.SetCallBackState(!string.IsNullOrEmpty(qiNiuStorageConfig.CallbackUrl) && !string.IsNullOrEmpty(qiNiuStorageConfig.CallbackHost)); return(AddQiNiuStorage(services, () => qiNiuStorageConfig)); }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddCors(options => { options.AddPolicy("CorsPolicy", builder => builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader()); }); services.AddQiNiuStorage(() => { var config = new QiNiuStorageConfig("accessKey", "secretKey", ZoneEnum.ZoneCnSouth, "访问图片域名", "空间名"); config.SetCallBack(EInfrastructure.Core.Configuration.Ioc.Plugs.Storage.Enumerations .CallbackBodyType.Json.Id, "回调地址域名,后不加/", "回调地址相对路径,前需要加/", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"fsiz\":$(fsize),\"bucket\":\"$(bucket)\",\"name\":\"$(x:name)\",\"mimeType\":\"$(mimeType)\"}"); return(config); }); serviceProvider = EInfrastructure.Core.AutoFac.AspNetCore.AutofacAutoRegister.Use(services, (builder) => { }); return(serviceProvider); }