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

85 lines
3.0 KiB
C#
Raw Normal View History

2024-04-10 13:55:27 +08:00
using System;
using System.IO;
using Qiniu.Storage;
using Qiniu.Util;
namespace Hncore.Infrastructure.Common
{
public class ImageCloudHelper
{
/// <summary>
///
/// </summary>
/// <param name="bytes"></param>
/// <returns>图片地址</returns>
public static string UploadImage(Stream stream, string prefix)
{
//todo
return "";
Mac mac = new Mac("3bmkJLD-inSGpQnLr_9UlommFT81B5L0ryesJLhS", "X22vza-l53jcZyi_fmaex88R065_Ip2_3j5Im0Se");
string bucket = "property";
// 上传策略,参见
// 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);
string jstr = putPolicy.ToJsonString();
string token = Auth.CreateUploadToken(mac, jstr);
FormUploader fu = new FormUploader(new Config()
{
Zone = Zone.ZONE_CN_East
});
string fileName = prefix + Guid.NewGuid() + ".jpg";
var result = fu.UploadStream(stream, fileName, token, null);
if (result.Code == 200)
{
return "http://propertyimages.etor.vip/" + fileName;
}
return null;
}
public static string UploadImage(string imageBase64, string prefix)
{
byte[] imageByte = Convert.FromBase64String(imageBase64);
var stream = new MemoryStream(imageByte);
return UploadImage(stream, prefix);
}
public static string GetToken(int expireInSeconds=3600)
{
Mac mac = new Mac("3bmkJLD-inSGpQnLr_9UlommFT81B5L0ryesJLhS", "X22vza-l53jcZyi_fmaex88R065_Ip2_3j5Im0Se");
string bucket = "property";
// 上传策略,参见
// 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;
}
}
}