222
schangxiang@126.com
2025-06-13 6a8393408d8cefcea02b7a598967de8dc1e565c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { ResultInfo } from "./commonInterface";
import * as fs from "fs";
import * as OSS from "ali-oss";
import { Context } from "egg";
let conf = {
  region: "oss-ap-southeast-2",
  endpoint: "oss-ap-southeast-2.aliyuncs.com",
  accessKeyId: "LTAIv74xeDXl5Qt6",
  accessKeySecret: "ThCk4hTFGpkLb1luzGUSHUV9dwJgry",
  bucket: "auod-bucket-wms"
};
const aliDomainHttps = "https://auod-bucket-wms.oss-ap-southeast-2.aliyuncs.com/";
const aliDomainHttp = "http://auod-bucket-wms.oss-ap-southeast-2.aliyuncs.com/";
 
/**
 * 上传阿里云
 * @param ctx Context
 * @param orginPath 本地绝对文件路径
 * @param aliPath 阿里云文件路径
 * @param overWrite 是否覆盖
 */
let aliOSSUpload = async (ctx: Context, orginPath: string, aliPath: string, overWrite: boolean = false) => {
  aliPath = aliPath.replace(/^\//, ""); // 移除开始/,阿里云oss不允许/开头
  var info: ResultInfo = {
    result: false
  };
 
  if (!fs.existsSync(orginPath)) {
    info.result = false;
    info.msg = "文件不存在";
    info.statusCode = 301;
    return info;
  }
 
  // 创建OSSClient实例。
  var client = new OSS(conf);
  try {
    try {
      var fileResult = await client.get(aliPath);
      if (!overWrite && fileResult.res.status == 200) {
        info.result = true;
        info.msg = "文件存在,不覆盖";
        info.statusCode = 303;
        return info;
      }
    } catch (error) {
      console.log("上传阿里云aliOSSUpload错误", error.message);
    }
 
    // 上传文件。
    let result = await client.put(aliPath, orginPath);
    console.log("Put object succeeded, ETag: {0} ", result.res.statusMessage);
    info.result = true;
    info.msg = result.res.statusMessage;
    info.statusCode = result.res.status;
  } catch (ex) {
    info.result = false;
    info.msg = ex.message;
    info.statusCode = 304;
    ctx.logger.info("上传AliOSS错误:" + ex.message + ", 文件路径:" + orginPath);
  }
 
  return info;
};
 
//#region aliOSSCopy
/**
 * 复制文件
 * @param ctx 上下文
 * @param orginPath 源文件
 * @param targetPath 目标文件
 */
let aliOSSCopy = async (ctx: Context, orginPath: string, targetPath: string) => {
  var info: ResultInfo = {
    result: false
  };
 
  orginPath = orginPath.replace(/\\$/, "");
  targetPath = targetPath.replace(/\\$/, "");
  targetPath = targetPath.replace(/^\//, "");
  var sourceObject = orginPath.replace(aliDomainHttps, "");
  sourceObject = orginPath.replace(aliDomainHttp, "");
  var targetObject = targetPath;
 
  // 创建OSSClient实例。
  var client = new OSS(conf);
  try {
    await client.copy(targetObject, sourceObject);
    info.result = true;
    info.statusCode = 200;
    info.msg = "复制成功";
  } catch (ex) {
    info.result = false;
    info.msg = ex.message;
    info.statusCode = 304;
    ctx.logger.info("复制AliOSS错误:" + ex.message + ", 文件路径:" + orginPath);
  }
 
  return info;
};
//#endregion
 
//#region aliOSSDelete
/// <summary>
/// 删除文件
/// </summary>
/// <param name="path">原路径</param>
/// <returns></returns>
let aliOSSDelete = async (ctx: Context, orginPath: string) => {
  var info: ResultInfo = {
    result: false
  };
 
  orginPath = orginPath.replace(/\\$/, "");
  var sourceObject = orginPath.replace(aliDomainHttps, "");
  sourceObject = orginPath.replace(aliDomainHttp, "");
 
  // 创建OSSClient实例。
  var client = new OSS(conf);
  try {
    // 文件
    await client.delete(sourceObject);
    info.result = true;
    info.statusCode = 200;
    info.msg = "删除成功";
  } catch (ex) {
    info.result = false;
    info.msg = ex.message;
    info.statusCode = 304;
    ctx.logger.info("删除AliOSS错误:" + ex.message + ", 文件路径:" + orginPath);
  }
 
  return info;
};
//#endregion
 
//#region download
/// <summary>
/// 下载文件
/// </summary>
/// <param name="path">原路径</param>
/// <param name="targetPath">目标路径</param>
/// <returns></returns>
let aliYunDownload = async (ctx: Context, orginPath: string, savePath: string) => {
  orginPath = orginPath.replace(/\\$/, "");
  var info: ResultInfo = {
    result: false
  };
  var sourceObject = orginPath.replace(aliDomainHttps, "");
  sourceObject = sourceObject.replace(aliDomainHttp, "");
  sourceObject = sourceObject.replace("?x-oss-process=style/600", "");
 
  // 创建OSSClient实例。
  var client = new OSS(conf);
  try {
    await client.get(sourceObject, savePath);
 
    //var obj = client.GetObject(bucketName, sourceObject);
    //using (var requestStream = obj.Content)
    //{
    //    byte[] buf = new byte[1024];
    //    var fs = File.Open(savePath, FileMode.OpenOrCreate);
    //    var len = 0;
    //    // 通过输入流将文件的内容读取到文件或者内存中。
    //    while ((len = requestStream.Read(buf, 0, 1024)) != 0)
    //    {
    //        fs.Write(buf, 0, len);
    //    }
    //    fs.Close();
    //}
 
    info.result = true;
    info.msg = "下载成功";
  } catch (ex) {
    info.result = false;
    info.msg = ex.message;
    info.statusCode = 304;
    ctx.logger.info("下载AliOSS错误:" + ex.message + ", 文件路径:" + orginPath);
  }
 
  return info;
};
//#endregion
 
export { aliOSSUpload, aliOSSCopy, aliOSSDelete, aliYunDownload };