Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mall.oytour.com
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
黄奎
mall.oytour.com
Commits
eea559ec
Commit
eea559ec
authored
Jun 11, 2020
by
黄奎
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新增上传文件到腾讯OSS
parent
6bd8cfaf
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
202 additions
and
11 deletions
+202
-11
Mall.ThirdCore.csproj
Mall.ThirdCore/Mall.ThirdCore.csproj
+1
-0
OssService.cs
Mall.ThirdCore/Oss/OssService.cs
+1
-8
TencentOss.cs
Mall.ThirdCore/Oss/TencentOss.cs
+154
-0
FileController.cs
Mall.WebApi/Controllers/File/FileController.cs
+46
-3
No files found.
Mall.ThirdCore/Mall.ThirdCore.csproj
View file @
eea559ec
...
...
@@ -9,6 +9,7 @@
<PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.10.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="RabbitMQ.Client" Version="6.0.0" />
<PackageReference Include="Tencent.QCloud.Cos.Sdk" Version="5.4.10" />
</ItemGroup>
<ItemGroup>
...
...
Mall.ThirdCore/Oss/OssService.cs
View file @
eea559ec
...
...
@@ -10,14 +10,7 @@ namespace Mall.ThirdCore.Oss
/// <returns></returns>
public
static
IOssService
Get
()
{
if
(
Config
.
FileService
==
"1"
)
{
return
AliOssService
.
Instance
();
}
else
{
return
OytourOssService
.
Instance
();
}
return
TencentOss
.
Instance
();
}
}
}
Mall.ThirdCore/Oss/TencentOss.cs
0 → 100644
View file @
eea559ec
using
COSXML
;
using
COSXML.Auth
;
using
COSXML.Model.Object
;
using
COSXML.Model.Bucket
;
using
COSXML.CosException
;
using
Newtonsoft.Json.Linq
;
using
System.IO
;
using
COSXML.Utils
;
using
System.Collections.Generic
;
using
System
;
namespace
Mall.ThirdCore.Oss
{
/// <summary>
/// 腾讯OSS服务
/// </summary>
public
class
TencentOss
:
IOssService
{
private
string
endpoint
=
""
;
private
string
accessKeyId
=
""
;
private
string
accessKeySecret
=
""
;
private
string
bucketName
=
""
;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="endpoint"></param>
/// <param name="accessKeyId"></param>
/// <param name="accessKeySecret"></param>
/// <param name="bucketName"></param>
public
TencentOss
(
string
endpoint
,
string
accessKeyId
,
string
accessKeySecret
,
string
bucketName
)
{
this
.
endpoint
=
endpoint
;
this
.
accessKeyId
=
accessKeyId
;
this
.
accessKeySecret
=
accessKeySecret
;
this
.
bucketName
=
bucketName
;
}
/// <summary>
/// 创建实例
/// </summary>
/// <returns></returns>
public
static
TencentOss
Instance
()
{
string
endpoint
=
"ap-chengdu"
;
string
accessKeyId
=
"AKIDDPnbIzi8C1eqEOPP8dw6MNAg9H9ldDKd"
;
string
accessKeySecret
=
"PdcLtOjslUzNFYdU4OSI1fKtdHpFT2Ob"
;
string
bucketName
=
"viitto-1301420277"
;
return
new
TencentOss
(
endpoint
,
accessKeyId
,
accessKeySecret
,
bucketName
);
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="stream">文件流</param>
/// <returns></returns>
public
OssResult
UploadFile
(
string
filePath
,
System
.
IO
.
Stream
stream
)
{
OssResult
ossResult
=
new
OssResult
();
CosXmlConfig
config
=
new
CosXmlConfig
.
Builder
()
.
SetConnectionTimeoutMs
(
60000
)
//设置连接超时时间,单位毫秒,默认45000ms
.
SetReadWriteTimeoutMs
(
40000
)
//设置读写超时时间,单位毫秒,默认45000ms
.
IsHttps
(
true
)
//设置默认 HTTPS 请求
.
SetAppid
(
"1301420277"
)
//设置腾讯云账户的账户标识 APPID
.
SetRegion
(
this
.
endpoint
)
//设置一个默认的存储桶地域
.
Build
();
string
secretId
=
this
.
accessKeyId
;
//云 API 密钥 SecretId
string
secretKey
=
this
.
accessKeySecret
;
//云 API 密钥 SecretKey
long
durationSecond
=
600
;
//每次请求签名有效时长,单位为秒
QCloudCredentialProvider
qCloudCredentialProvider
=
new
DefaultQCloudCredentialProvider
(
secretId
,
secretKey
,
durationSecond
);
CosXml
cosXml
=
new
CosXmlServer
(
config
,
qCloudCredentialProvider
);
try
{
string
fileExtention
=
System
.
IO
.
Path
.
GetExtension
(
filePath
);
string
bucket
=
this
.
bucketName
;
//存储桶,格式:BucketName-APPID
string
key
=
@"/SaleBefore/Goods/"
+
DateTime
.
Now
.
Ticks
.
ToString
()+
fileExtention
;
//对象在存储桶中的位置,即称对象键
PutObjectRequest
request
=
new
PutObjectRequest
(
bucket
,
key
,
filePath
);
//设置签名有效时长
request
.
SetSign
(
TimeUtils
.
GetCurrentTime
(
TimeUnit
.
SECONDS
),
600
);
//设置进度回调
request
.
SetCosProgressCallback
(
delegate
(
long
completed
,
long
total
)
{
//Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
});
//执行请求
PutObjectResult
result
=
cosXml
.
PutObject
(
request
);
//对象的 eTag
ossResult
.
data
=
key
;
ossResult
.
resultCode
=
1
;
ossResult
.
message
=
"上传成功"
;
}
catch
(
COSXML
.
CosException
.
CosClientException
clientEx
)
{
ossResult
.
resultCode
=
0
;
ossResult
.
message
=
clientEx
.
Message
;
}
catch
(
COSXML
.
CosException
.
CosServerException
serverEx
)
{
ossResult
.
resultCode
=
0
;
ossResult
.
message
=
serverEx
.
Message
;
}
return
ossResult
;
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public
OssResult
DeleteFile
(
string
fileName
)
{
OssResult
result
=
new
OssResult
();
return
result
;
}
/// <summary>
/// 批量删除文件
/// </summary>
/// <param name="fileNameList"></param>
/// <returns></returns>
public
OssResult
DeleteFile
(
List
<
string
>
fileNameList
)
{
OssResult
result
=
new
OssResult
();
return
result
;
}
/// <summary>
/// 拷贝文件
/// </summary>
/// <param name="sourceFileName">源文件</param>
/// <param name="targetFileName">目标文件</param>
/// <returns></returns>
public
OssResult
FileCopy
(
string
sourceFileName
,
string
targetFileName
)
{
OssResult
result
=
new
OssResult
();
return
result
;
}
/// <summary>
/// 判断文件是否存在
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public
bool
FileIsExist
(
string
fileName
)
{
return
false
;
}
}
}
\ No newline at end of file
Mall.WebApi/Controllers/File/FileController.cs
View file @
eea559ec
...
...
@@ -8,6 +8,7 @@ using System.Linq;
using
Mall.WebApi.Filter
;
using
Mall.Common.API
;
using
Mall.Common.Plugin
;
using
Mall.ThirdCore.Oss
;
namespace
Mall.WebApi.Controllers.File
{
...
...
@@ -64,9 +65,6 @@ namespace Mall.WebApi.Controllers.File
}
}
/// <summary>
/// 上传文件到本地临时文件
/// </summary>
...
...
@@ -112,5 +110,50 @@ namespace Mall.WebApi.Controllers.File
}
}
/// <summary>
/// 文件上传
/// </summary>
/// <param name="request"></param>
/// <param name="files"></param>
/// <returns></returns>
public
virtual
ApiResult
UploadTencent
()
{
try
{
var
files
=
Request
.
Form
.
Files
;
if
(
files
.
Count
==
0
)
{
return
new
ApiResult
{
resultCode
=
(
int
)
ResultCode
.
Fail
,
message
=
"未选择文件"
,
data
=
""
};
}
string
filename
=
files
[
0
].
FileName
;
string
fileExtention
=
System
.
IO
.
Path
.
GetExtension
(
files
[
0
].
FileName
);
string
path
=
Guid
.
NewGuid
().
ToString
()
+
fileExtention
;
string
basepath
=
AppContext
.
BaseDirectory
;
string
path_server
=
basepath
+
"\\SaleBefore\\Goods\\"
+
path
;
if
(!
Directory
.
Exists
(
basepath
+
"\\SaleBefore\\Goods\\"
))
{
Directory
.
CreateDirectory
(
basepath
+
"\\SaleBefore\\Goods\\"
);
}
using
(
FileStream
fstream
=
new
FileStream
(
path_server
,
FileMode
.
OpenOrCreate
,
FileAccess
.
ReadWrite
))
{
files
[
0
].
CopyTo
(
fstream
);
}
IOssService
ossService
=
OssService
.
Get
();
OssResult
result
=
ossService
.
UploadFile
(
path_server
,
null
);
try
{
System
.
IO
.
File
.
Delete
(
path_server
);
}
catch
{
}
return
ApiResult
.
Success
(
""
,
Common
.
Config
.
GetFileUrl
(
result
.
data
.
ToString
()));
}
catch
(
Exception
ex
)
{
LogHelper
.
Write
(
ex
,
"FileUpload"
);
return
ApiResult
.
Failed
();
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment