Commit 4935a06b authored by 黄奎's avatar 黄奎

11

parent fc8c509a
......@@ -838,6 +838,7 @@ namespace Mall.Common
ossObj["accessKeyId"] = ReadConfigKey("tencentOSS", "accessKeyId");
ossObj["accessKeySecret"] = ReadConfigKey("tencentOSS", "accessKeySecret");
ossObj["domain"] = ReadConfigKey("tencentOSS", "domain");
ossObj["fileStoragePath"] = ReadConfigKey("tencentOSS", "fileStoragePath");
return ossObj;
}
}
......
......@@ -8,9 +8,9 @@ namespace Mall.ThirdCore.Oss
/// 创建上传实例
/// </summary>
/// <returns></returns>
public static IOssService GetTencent(string endpoint = null, string accessKeyId = null, string accessKeySecret = null, string bucketName = null)
public static IOssService GetTencent(string endpoint = null, string accessKeyId = null, string accessKeySecret = null, string bucketName = null,string fileStoragePath=null)
{
return TencentOss.Instance(endpoint, accessKeyId, accessKeySecret, bucketName);
return TencentOss.Instance(endpoint, accessKeyId, accessKeySecret, bucketName,fileStoragePath);
}
/// <summary>
......
......@@ -20,6 +20,10 @@ namespace Mall.ThirdCore.Oss
private string accessKeyId = "";
private string accessKeySecret = "";
private string bucketName = "";
/// <summary>
/// 文件存储位置
/// </summary>
private string fileStoragePath = "";
/// <summary>
/// 构造函数
......@@ -28,25 +32,28 @@ namespace Mall.ThirdCore.Oss
/// <param name="accessKeyId"></param>
/// <param name="accessKeySecret"></param>
/// <param name="bucketName"></param>
public TencentOss(string endpoint, string accessKeyId, string accessKeySecret, string bucketName)
/// <param name="fileStoragePath">文件存储位置</param>
public TencentOss(string endpoint, string accessKeyId, string accessKeySecret, string bucketName, string fileStoragePath)
{
this.endpoint = endpoint;
this.accessKeyId = accessKeyId;
this.accessKeySecret = accessKeySecret;
this.bucketName = bucketName;
this.fileStoragePath = fileStoragePath;
}
/// <summary>
/// 创建实例
/// </summary>
/// <returns></returns>
public static TencentOss Instance(string endpoint, string accessKeyId, string accessKeySecret, string bucketName)
public static TencentOss Instance(string endpoint, string accessKeyId, string accessKeySecret, string bucketName,string fileStoragePath)
{
endpoint ??= "ap-chengdu";
accessKeyId ??= "AKIDDPnbIzi8C1eqEOPP8dw6MNAg9H9ldDKd";
accessKeySecret ??= "PdcLtOjslUzNFYdU4OSI1fKtdHpFT2Ob";
bucketName ??= "viitto-1301420277";
return new TencentOss(endpoint, accessKeyId, accessKeySecret, bucketName);
fileStoragePath ??= "/SaleBefore/Goods/";
return new TencentOss(endpoint, accessKeyId, accessKeySecret, bucketName, fileStoragePath);
}
/// <summary>
......@@ -76,8 +83,7 @@ namespace Mall.ThirdCore.Oss
{
string fileExtention = System.IO.Path.GetExtension(filePath);
string bucket = this.bucketName; //存储桶,格式:BucketName-APPID
string key = @"/SaleBefore/Goods/" + DateTime.Now.Ticks.ToString() + fileExtention; //对象在存储桶中的位置,即称对象键
string key = fileStoragePath + DateTime.Now.Ticks.ToString() + fileExtention; //对象在存储桶中的位置,即称对象键
PutObjectRequest request = new PutObjectRequest(bucket, key, filePath);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
......
......@@ -172,8 +172,9 @@ namespace Mall.WebApi.Controllers.File
{
fileModel.Bucket = Common.Config.TencentOSS.GetStringValue("bucket");
}
string fileStorePath = Common.Config.TencentOSS.GetStringValue("fileStoragePath");
IsDefault = false;
IOssService ossService = OssService.GetTencent(fileModel.Region, fileModel.SecretId, fileModel.SecretKey, fileModel.Bucket);
IOssService ossService = OssService.GetTencent(fileModel.Region, fileModel.SecretId, fileModel.SecretKey, fileModel.Bucket, fileStorePath);
OssResult result = ossService.UploadFile(path_server, null);
try
{
......
......@@ -114,7 +114,9 @@
"bucket": "viitto-1301420277",
"accessKeyId": "AKIDDPnbIzi8C1eqEOPP8dw6MNAg9H9ldDKd",
"accessKeySecret": "PdcLtOjslUzNFYdU4OSI1fKtdHpFT2Ob",
"domain": "https://viitto-1301420277.cos.ap-chengdu.myqcloud.com"
"domain": "https://viitto-1301420277.cos.ap-chengdu.myqcloud.com",
//腾讯云文件存储路径
"fileStoragePath": "/SaleBefore/Goods/"
},
//阿里OSS配置
"aliOSS": {
......
......@@ -20,7 +20,8 @@ namespace TestCore
string SecretId = "AKIDDPnbIzi8C1eqEOPP8dw6MNAg9H9ldDKd";
string SecretKey = "PdcLtOjslUzNFYdU4OSI1fKtdHpFT2Ob";
string Bucket = "viitto-1301420277";
IOssService ossService = OssService.GetTencent(Region, SecretId, SecretKey, Bucket);
string fileStoragePath = string.Format("/{0}Mall/SaleBefore/Goods/", DateTime.Now.ToString("yyyy"));
IOssService ossService = OssService.GetTencent(Region, SecretId, SecretKey, Bucket, fileStoragePath);
OssResult result = ossService.UploadFile(filePath, null);
return result;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment