// 获取七牛token
public ApiResponse GetQiniuToken(QiniuToken req)
{
try
{
Mac mac = new Mac(req.AccessKey, req.SecretKey);
PutPolicy putPolicy = new PutPolicy();
// putPolicy.Scope = req.Bucket + ":" + key;
putPolicy.Scope = req.Bucket;
putPolicy.SetExpires();
putPolicy.DeleteAfterDays = ;
string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());
return Success(token);
}
catch (Exception ex)
{
return Failed("查询七牛token时发生异常,错误描述:" + ex.Message);
}
}
【putPolicy.DeleteAfterDays = 1; 这是个坑,默认值为1,会导致使用接口上传的图片1天后默认删除,所以记得修改官方demo中的默认值。】
// 删除七牛云空间图片
public ApiResponse DelQiniuPic(QiniuDel req)
{
try
{
// 空间名
string Bucket = req.Bucket;
// 文件名
string Key = req.Key;
// 设置存储区域
Config config = new Config();
config.Zone = Zone.ZONE_CN_East;
Mac mac = new Mac(req.AccessKey, req.SecretKey);
BucketManager bucketManager = new BucketManager(mac, config);
HttpResult deleteRet = bucketManager.Delete(Bucket, Key);
Console.WriteLine("delete error: " + deleteRet.ToString());
if (deleteRet.Code != (int)HttpCode.OK)
{
Console.WriteLine("delete error: " + deleteRet.ToString());
}
return Success("已成功删除七牛云端图片!");
}
catch (Exception ex)
{
return Failed("删除七牛图片时发生异常,错误描述:" + ex.Message);
}
}
【此外,对于调用删除接口,链接地址任然能访问的情况,需要清空CDN缓存 https://portal.qiniu.com/cdn/refresh-prefetch】