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

11

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