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 };
|