1. 在appsettings.json中添加存储配置
json
//存储配置
"Storage": [
{
"Name": "qiniu",
"UploadTokenUrl": "https://localhost:5001/storage/uptoken?provider=qiniu",
"AccessKey": "your ak",
"SecretKey": "your sk",
"Domain": "http://res.tthongbao.cn", //云存储域名
"Bucket": "rsyunpan",
"Zone": "ZONE_CN_South", //华东 ZONE_CN_East 华北 ZONE_CN_North 华南 ZONE_CN_South 北美 ZONE_US_North
"UploadTokenExpireTime": 30, // 上传token有效时间单位:秒
"DownloadTokenExpireTime": 5, //下载token有效时间
"ManageTokenExpireTime": 5 //管理token有效时间
},
{
"Name": "qiniu",
"UploadTokenUrl": "https://localhost:5001/storage/uptoken?provider=qiniu",
"AccessKey": "your ak",
"SecretKey": "your sk",
"Domain": "http://res.rscode.cn", //云存储域名
"Bucket": "res-rscode-cn",
"Zone": "ZONE_CN_East", //华东 ZONE_CN_East 华北 ZONE_CN_North 华南 ZONE_CN_South 北美 ZONE_US_North
"UploadTokenExpireTime": 30, // 上传token有效时间单位:秒
"DownloadTokenExpireTime": 5, //下载token有效时间
"ManageTokenExpireTime": 5 //管理token有效时间
}
]
2. 配置服务
csharp
services.AddQiniuStorage(Configuration);
3. 调用IStorageProvider
3.1注入
csharp
IEnumerable<IStorageProvider> providers
3.2实例存储对象
csharp
var qiniu = providers.FirstOrDefault(s => s.StorageName == "qiniu")
3.3指定存储区
csharp
qiniu.UseBucket("rsyunpan");
实例代码
csharp
[Authorize]
[HttpGet("uptoken")]
public async Task<object> UpToken([FromServices] IEnumerable<IStorageProvider> providers,string key, string provider = "qiniu")
{
var userId = User.Claims.FirstOrDefault(x => x.Type == "UserId")?.Value;
var qiniu = providers.FirstOrDefault(s => s.StorageName == provider);
qiniu.UseBucket("rsyunpan");
var tokenInfo =await qiniu.GetUploadTokenInfoAsync(key,DateTime.Now.AddMinutes(5));
return tokenInfo;
}
七牛图缩图 https://developer.qiniu.com/dora/1279/basic-processing-images-imageview2