Files
juipnet/Infrastructure/Hncore.Infrastructure/Common/QiNiuCloudHelper.cs

119 lines
4.1 KiB
C#
Raw Normal View History

2024-04-10 13:55:27 +08:00
using System;
using System.Collections.Generic;
using System.IO;
using Hncore.Infrastructure.Extension;
using Qiniu.Http;
using Qiniu.Storage;
using Qiniu.Util;
namespace Hncore.Infrastructure.Common
{
public class QiNiuCloudHelper
{
static Mac mac = new Mac("3bmkJLD-inSGpQnLr_9UlommFT81B5L0ryesJLhS",
"X22vza-l53jcZyi_fmaex88R065_Ip2_3j5Im0Se");
static string bucket = "property";
/// <summary>
///
/// </summary>
/// <param name="bytes"></param>
/// <returns>图片地址</returns>
public static string UploadImage(Stream stream, string prefix, string persistentOps = "")
{
string fileName = prefix + Guid.NewGuid() + ".jpg";
// 上传策略,参见
// https://developer.qiniu.com/kodo/manual/put-policy
PutPolicy putPolicy = new PutPolicy();
// 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖),请使用 SCOPE = "BUCKET:KEY"
// putPolicy.Scope = bucket + ":" + saveKey;
putPolicy.Scope = bucket;
// 上传策略有效期(对应于生成的凭证的有效期)
putPolicy.SetExpires(3600);
if (!string.IsNullOrEmpty(persistentOps))
{
string saveAs = (bucket + ":" + fileName).ToBase64String()
.Replace("+", "-")
.Replace("/", "_");
putPolicy.PersistentOps = persistentOps + $"|saveas/{saveAs}";
putPolicy.PersistentPipeline = "face_image";
}
string jstr = putPolicy.ToJsonString();
string token = Auth.CreateUploadToken(mac, jstr);
ResumableUploader fu = new ResumableUploader(new Config()
{
Zone = Zone.ZONE_CN_East
});
var result = fu.UploadStream(stream, fileName, token, null);
if (result.Code == 200)
{
return "http://propertyimages.etor.vip/" + fileName;
}
LogHelper.Error("七牛上传图片失败", result.ToString());
return null;
}
public static string UploadImage(string imageBase64, string prefix, string persistentOps = "")
{
byte[] imageByte = Convert.FromBase64String(imageBase64);
var stream = new MemoryStream(imageByte);
return UploadImage(stream, prefix, persistentOps);
}
/// <summary>
/// 删除资源
/// </summary>
/// <param name="key"></param>
public static void Delete(string key)
{
Config config = new Config();
config.Zone = Zone.ZONE_CN_East;
BucketManager bucketManager = new BucketManager(mac, config);
var res = bucketManager.Delete(bucket, key);
}
public static List<string> Domains(string bucket)
{
Config config = new Config();
config.Zone = Zone.ZONE_CN_East;
BucketManager bucketManager = new BucketManager(mac, config);
return bucketManager.Domains("property").Result;
}
public static string GetToken(int expireInSeconds = 3600)
{
// 上传策略,参见
// https://developer.qiniu.com/kodo/manual/put-policy
PutPolicy putPolicy = new PutPolicy();
// 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖),请使用 SCOPE = "BUCKET:KEY"
// putPolicy.Scope = bucket + ":" + saveKey;
putPolicy.Scope = bucket;
// 上传策略有效期(对应于生成的凭证的有效期)
putPolicy.SetExpires(expireInSeconds);
putPolicy.ReturnBody =
"{\"key\":$(key),\"hash\":$(etag),\"mimeType\":$(mimeType),\"fname\":$(fname),\"fsize\":$(fsize),\"avinfo\":$(avinfo),\"ext\":$(ext),\"imageInfo\":$(imageInfo)}";
string jstr = putPolicy.ToJsonString();
string token = Auth.CreateUploadToken(mac, jstr);
return token;
}
}
}