| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using System;
- using System.Threading;
- using System.Linq;
- using System.IO;
- using Aliyun.OSS;
- using Aliyun.OSS.Common;
- using System.Text.RegularExpressions;
- using Common;
- using Infrastructure;
- using Infrastructure.Model;
- namespace Util
- {
- public class OssHelper
- {
- public string key = ""; //阿里云AccessKey ID
- public string secret = ""; //阿里云Access Key Secret
- public string PathName = "Omega";
- public string endpoint = ""; //endpoint
- public string bucketName = "";
- public string SourceHost = "";
- public bool OssStatus = false;
- public readonly static OssHelper Instance = new OssHelper();
- private OssHelper()
- {
- var options = App.OptionsSetting;
- OssConfigs ossConfigs = options.OssConfigs;
- key = ossConfigs.Key;
- secret = ossConfigs.Secret;
- endpoint = ossConfigs.Endpoint;
- bucketName = ossConfigs.BucketName;
- SourceHost = "https://" + ossConfigs.BucketName + "." + ossConfigs.Endpoint + "/";
- }
- /// <summary>
- /// 上传文件
- /// </summary>
- /// <returns></returns>
- public void Upload(string dataFilePath)
- {
- var client = new OssClient(OssHelper.Instance.endpoint, OssHelper.Instance.key, OssHelper.Instance.secret);
- if(dataFilePath.Substring(dataFilePath.LastIndexOf("/")).Contains("."))
- {
- if(dataFilePath.Contains("net7.0/"))
- {
- string localFile = dataFilePath.Substring(dataFilePath.IndexOf("net7.0//") + 8).Replace("\\", "/");
- ScanQueue(localFile, dataFilePath, client);
- }
- else
- {
- ScanQueue(dataFilePath, dataFilePath, client);
- }
- }
- else
- {
- System.IO.FileSystemInfo info = new System.IO.DirectoryInfo(dataFilePath);
- scan(info, client);
- }
- }
- private void scan(System.IO.FileSystemInfo info, OssClient client)
- {
- if (!info.Exists) return;
- System.IO.DirectoryInfo dir = info as System.IO.DirectoryInfo;
- //不是目录
- if (dir == null) return;
- System.IO.FileSystemInfo[] files = dir.GetFileSystemInfos();
- for (int i = 0; i < files.Length; i++)
- {
- System.IO.FileInfo file = files[i] as System.IO.FileInfo;
- //是文件
- if (file != null)
- {
- string localFile = file.FullName.Substring(file.FullName.IndexOf("net7.0//") + 8).Replace("\\", "/");
- ScanQueue(localFile, file.FullName, client);
- }
- else scan(files[i], client);
- }
- }
- //上传
- public void ScanQueue(string data, string FileFullName, OssClient client)
- {
- // 上传文件。
- string localFile = Function.getPath(data);
- string filePath = data.TrimStart('/');
- var result = client.PutObject(bucketName, filePath, localFile, new ObjectMetadata()
- {
- ExpirationTime = DateTime.Parse("2050-12-31 23:59:59")
- });
- if (!string.IsNullOrEmpty(result.ETag))
- {
- if (result.ETag.Length == 32)
- {
- if (File.Exists(localFile))
- {
- File.Delete(localFile);
- }
- }
- }
- if (File.Exists(FileFullName))
- {
- File.Delete(FileFullName);
- }
- }
- }
- }
|