|
|
using System;
|
|
|
using System.IO;
|
|
|
using Qiniu.Http;
|
|
|
using Qiniu.Storage;
|
|
|
using Qiniu.Util;
|
|
|
using Microsoft.Extensions.Options;
|
|
|
using System.Threading.Tasks;
|
|
|
using Common.Extensions;
|
|
|
|
|
|
namespace Common.Tools.UpLoad
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 七牛云存储上传实现类
|
|
|
/// </summary>
|
|
|
public class UploadQiniu:IUploadService
|
|
|
{
|
|
|
|
|
|
private QiniuConfig _QnConfig;
|
|
|
private Config qiniuconfig = new Config { Zone = Zone.ZONE_CN_East };//上传的部分配置
|
|
|
public UploadQiniu()
|
|
|
{
|
|
|
_QnConfig =sysOptionConfig.Qiniuconfig;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///异步 上传数据流
|
|
|
/// </summary>
|
|
|
/// <param name="Filestream"></param>
|
|
|
/// <param name="qiniukey"></param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<bool> UploadStreamAsync(Stream Filestream, string qiniukey)
|
|
|
{
|
|
|
return await Task.Run(()=>UploadStream(Filestream,qiniukey));
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///上传数据流到
|
|
|
/// </summary>
|
|
|
/// <param name="Filestream"></param>
|
|
|
/// <returns></returns>
|
|
|
public bool UploadStream(Stream Filestream,string qiniukey)
|
|
|
{
|
|
|
if (Filestream == null || qiniukey.IsNull())
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
// 生成(上传)凭证时需要使用此Mac
|
|
|
// 这个示例单独使用了一个Settings类,其中包含AccessKey和SecretKey
|
|
|
// 实际应用中,请自行设置您的AccessKey和SecretKey
|
|
|
Mac mac = new Mac(_QnConfig.QiniuAccessKey, _QnConfig.QiniuSecretKey);
|
|
|
string bucket = _QnConfig.Qiniubucket;
|
|
|
string saveKey = qiniukey;//创建的骑牛key
|
|
|
if (saveKey.Substring(0, 1) == "/")
|
|
|
{
|
|
|
saveKey = saveKey.Substring(1, saveKey.Length - 1);
|
|
|
}
|
|
|
//byte[] data = System.Text.Encoding.UTF8.GetBytes("Hello World!");
|
|
|
// 上传策略,参见
|
|
|
// https://developer.qiniu.com/kodo/manual/put-policy
|
|
|
PutPolicy putPolicy = new PutPolicy();
|
|
|
// 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖),请使用 SCOPE = "BUCKET:KEY"
|
|
|
// putPolicy.Scope = bucket + ":" + saveKey;
|
|
|
|
|
|
putPolicy.Scope = bucket;
|
|
|
//putPolicy.Scope = "BUCKET:KEY";
|
|
|
// 上传策略有效期(对应于生成的凭证的有效期)
|
|
|
putPolicy.SetExpires(3600);
|
|
|
// 上传到云端多少天后自动删除该文件,如果不设置(即保持默认默认)则不删除
|
|
|
// putPolicy.DeleteAfterDays = 1;
|
|
|
// 生成上传凭证,参见
|
|
|
// https://developer.qiniu.com/kodo/manual/upload-token
|
|
|
string jstr = putPolicy.ToJsonString();
|
|
|
string token = Auth.CreateUploadToken(mac, jstr);
|
|
|
FormUploader fu = new FormUploader(qiniuconfig);
|
|
|
HttpResult result = fu.UploadStream(Filestream, saveKey, token, new PutExtra());
|
|
|
if (result.Code == 200)
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
string msg = result.Text;
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///异步上传字节
|
|
|
/// </summary>
|
|
|
/// <param name="data"></param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<bool> UploadDataAsync(byte[] data, string qiniukey)
|
|
|
{
|
|
|
return await UploadStreamAsync(new MemoryStream(data), qiniukey) ;
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 上传字节
|
|
|
/// </summary>
|
|
|
/// <param name="data"></param>
|
|
|
/// <returns></returns>
|
|
|
public bool UploadData(byte[] data, string qiniukey)
|
|
|
{
|
|
|
return UploadStream(new MemoryStream(data), qiniukey);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 上传文件到七牛云
|
|
|
/// </summary>
|
|
|
/// <param name="filePath">要上传的文件路径</param>
|
|
|
/// <param name="qiniukey">生成的文件名称</param>
|
|
|
public bool UploadFile(string filePath, string qiniukey)
|
|
|
{
|
|
|
return UploadStream(new MemoryStream(File.ReadAllBytes(filePath)), qiniukey);
|
|
|
}
|
|
|
|
|
|
public async Task<bool> UploadFileAsync(string filePath, string qiniukey)
|
|
|
{
|
|
|
return await UploadStreamAsync(new MemoryStream(File.ReadAllBytes(filePath)),qiniukey);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|