|
@@ -3,9 +3,10 @@ import {Resp} from "@util/Resp";
|
|
import {Stream} from "stream";
|
|
import {Stream} from "stream";
|
|
import {Logger} from "@util/Logger";
|
|
import {Logger} from "@util/Logger";
|
|
import {IOssConfig} from "@util/Config";
|
|
import {IOssConfig} from "@util/Config";
|
|
-
|
|
|
|
|
|
+const { default: axios } = require("axios");
|
|
|
|
+const fs = require("fs");
|
|
const OSS = require('ali-oss');
|
|
const OSS = require('ali-oss');
|
|
-
|
|
|
|
|
|
+const { STS } = require("ali-oss");
|
|
export type AliOssTag = string;
|
|
export type AliOssTag = string;
|
|
|
|
|
|
|
|
|
|
@@ -21,7 +22,10 @@ export class AliOss implements IOss {
|
|
this.client = new OSS({
|
|
this.client = new OSS({
|
|
endpoint: config.end_point,
|
|
endpoint: config.end_point,
|
|
accessKeyId: config.access_key,
|
|
accessKeyId: config.access_key,
|
|
- accessKeySecret: config.secret_key
|
|
|
|
|
|
+ accessKeySecret: config.secret_key,
|
|
|
|
+ region: config.region,
|
|
|
|
+ authorizationV4: true,
|
|
|
|
+ secure: this.config.use_ssl
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
@@ -50,6 +54,31 @@ export class AliOss implements IOss {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ object_exists(bucket: string, name: string): Promise<boolean> {
|
|
|
|
+ return new Promise<boolean>(async (resolve, reject) => {
|
|
|
|
+ try {
|
|
|
|
+ let client = new OSS({
|
|
|
|
+ bucket: bucket,
|
|
|
|
+ endpoint: this.config.end_point,
|
|
|
|
+ accessKeyId: this.config.access_key,
|
|
|
|
+ accessKeySecret: this.config.secret_key,
|
|
|
|
+ authorizationV4: true,
|
|
|
|
+ secure: this.config.use_ssl
|
|
|
|
+ });
|
|
|
|
+ await client.head(name, {});
|
|
|
|
+ resolve(true);
|
|
|
|
+ } catch (error: any) {
|
|
|
|
+ Logger.error(error);
|
|
|
|
+ // 指定的存储空间不存在。
|
|
|
|
+ if (error.name === 'NoSuchKey') {
|
|
|
|
+ resolve(false);
|
|
|
|
+ } else {
|
|
|
|
+ reject(Resp.gen_err(Resp.InternalServerError, error.message));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
make_bucket(bucket: string): Promise<void> {
|
|
make_bucket(bucket: string): Promise<void> {
|
|
return new Promise<void>(async (resolve, reject) => {
|
|
return new Promise<void>(async (resolve, reject) => {
|
|
try {
|
|
try {
|
|
@@ -83,7 +112,9 @@ export class AliOss implements IOss {
|
|
bucket: bucket,
|
|
bucket: bucket,
|
|
endpoint: this.config.end_point,
|
|
endpoint: this.config.end_point,
|
|
accessKeyId: this.config.access_key,
|
|
accessKeyId: this.config.access_key,
|
|
- accessKeySecret: this.config.secret_key
|
|
|
|
|
|
+ accessKeySecret: this.config.secret_key,
|
|
|
|
+ authorizationV4: true,
|
|
|
|
+ secure: this.config.use_ssl
|
|
});
|
|
});
|
|
Logger.info({
|
|
Logger.info({
|
|
bucket: bucket,
|
|
bucket: bucket,
|
|
@@ -104,10 +135,13 @@ export class AliOss implements IOss {
|
|
return new Promise<void>(async (resolve, reject) => {
|
|
return new Promise<void>(async (resolve, reject) => {
|
|
try {
|
|
try {
|
|
let client = new OSS({
|
|
let client = new OSS({
|
|
|
|
+ region: this.config.region,
|
|
bucket: bucket,
|
|
bucket: bucket,
|
|
- endpoint: this.config.end_point,
|
|
|
|
|
|
+ // endpoint: this.config.end_point,
|
|
accessKeyId: this.config.access_key,
|
|
accessKeyId: this.config.access_key,
|
|
- accessKeySecret: this.config.secret_key
|
|
|
|
|
|
+ accessKeySecret: this.config.secret_key,
|
|
|
|
+ authorizationV4: true,
|
|
|
|
+ secure: this.config.use_ssl
|
|
});
|
|
});
|
|
await client.delete(objectName);
|
|
await client.delete(objectName);
|
|
resolve();
|
|
resolve();
|
|
@@ -121,10 +155,13 @@ export class AliOss implements IOss {
|
|
return new Promise<void>(async (resolve, reject) => {
|
|
return new Promise<void>(async (resolve, reject) => {
|
|
try {
|
|
try {
|
|
let client = new OSS({
|
|
let client = new OSS({
|
|
|
|
+ region: this.config.region,
|
|
bucket: bucket,
|
|
bucket: bucket,
|
|
- endpoint: this.config.end_point,
|
|
|
|
|
|
+ // endpoint: this.config.end_point,
|
|
accessKeyId: this.config.access_key,
|
|
accessKeyId: this.config.access_key,
|
|
- accessKeySecret: this.config.secret_key
|
|
|
|
|
|
+ accessKeySecret: this.config.secret_key,
|
|
|
|
+ authorizationV4: true,
|
|
|
|
+ secure: this.config.use_ssl
|
|
|
|
|
|
});
|
|
});
|
|
await client.copy(dest_object_name, src_object_name);
|
|
await client.copy(dest_object_name, src_object_name);
|
|
@@ -139,12 +176,16 @@ export class AliOss implements IOss {
|
|
return new Promise<string>(async (resolve, reject) => {
|
|
return new Promise<string>(async (resolve, reject) => {
|
|
try {
|
|
try {
|
|
let client = new OSS({
|
|
let client = new OSS({
|
|
|
|
+ region: this.config.region,
|
|
bucket: bucket,
|
|
bucket: bucket,
|
|
- endpoint: this.config.end_point,
|
|
|
|
|
|
+ // endpoint: this.config.end_point,
|
|
accessKeyId: this.config.access_key,
|
|
accessKeyId: this.config.access_key,
|
|
- accessKeySecret: this.config.secret_key
|
|
|
|
|
|
+ accessKeySecret: this.config.secret_key,
|
|
|
|
+ authorizationV4: true,
|
|
|
|
+ secure: this.config.use_ssl
|
|
});
|
|
});
|
|
let url = client.signatureUrl(objectName, {expires: expiry});
|
|
let url = client.signatureUrl(objectName, {expires: expiry});
|
|
|
|
+
|
|
resolve(url);
|
|
resolve(url);
|
|
} catch (e: any) {
|
|
} catch (e: any) {
|
|
reject(Resp.gen_err(Resp.InternalServerError, `获取图片url出错, ${e.message}`));
|
|
reject(Resp.gen_err(Resp.InternalServerError, `获取图片url出错, ${e.message}`));
|
|
@@ -152,19 +193,63 @@ export class AliOss implements IOss {
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ //https://help.aliyun.com/zh/oss/use-cases/node-js?spm=a2c4g.11186623.0.0.53324cf5Ov5aix
|
|
|
|
+ //https://help.aliyun.com/zh/oss/use-cases/uploading-objects-to-oss-directly-from-clients/?spm=a2c4g.11186623.0.i1#36c322a137x9q
|
|
presignedPostPolicy(bucket: string, objectName: string, expiry: number, prefix?: string): Promise<any> {
|
|
presignedPostPolicy(bucket: string, objectName: string, expiry: number, prefix?: string): Promise<any> {
|
|
- return new Promise<string>(async (resolve, reject) => {
|
|
|
|
|
|
+ return new Promise<any>(async (resolve, reject) => {
|
|
try {
|
|
try {
|
|
- let client = new OSS({
|
|
|
|
|
|
+ /*let client = new OSS({
|
|
|
|
+ region: this.config.region,
|
|
bucket: bucket,
|
|
bucket: bucket,
|
|
- endpoint: this.config.end_point,
|
|
|
|
|
|
+ // endpoint: this.config.end_point,
|
|
accessKeyId: this.config.access_key,
|
|
accessKeyId: this.config.access_key,
|
|
- accessKeySecret: this.config.secret_key
|
|
|
|
|
|
+ accessKeySecret: this.config.secret_key,
|
|
|
|
+ authorizationV4: true,
|
|
|
|
+ secure: this.config.use_ssl
|
|
|
|
+ // cname: true
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ let url = await client.signatureUrlV4('POST', expiry, {
|
|
|
|
+ method: 'POST',
|
|
|
|
+ headers: {
|
|
|
|
+ "Content-Type": "multipart/form-data",
|
|
|
|
+ }
|
|
|
|
+ }, objectName);
|
|
|
|
+
|
|
|
|
+ const file = fs.readFileSync("/Users/chengang/Downloads/test.docx");
|
|
|
|
+ axios({
|
|
|
|
+ url,
|
|
|
|
+ headers: {
|
|
|
|
+ "Content-Type": "multipart/form-data",
|
|
|
|
+ },
|
|
|
|
+ method: "POST",
|
|
|
|
+ data: file,
|
|
|
|
+ })
|
|
|
|
+ .then((r: any) => console.log(r))
|
|
|
|
+ .catch((e: any) => console.log(e));*/
|
|
|
|
+ let result = await this.get_token(bucket, expiry);
|
|
|
|
+ result.formData.key = objectName;
|
|
|
|
+ if (this.config.callback_url) {
|
|
|
|
+ const callback = {
|
|
|
|
+ callbackUrl: this.config.callback_url,
|
|
|
|
+ callbackBody:// 设置回调的内容,例如文件ETag、资源类型mimeType等。
|
|
|
|
+ "filename=${object}&size=${size}&mimeType=${mimeType}",
|
|
|
|
+ callbackBodyType: "application/x-www-form-urlencoded",
|
|
|
|
+ }
|
|
|
|
+ result.formData.callback = Buffer.from(JSON.stringify(callback)).toString("base64");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ resolve({
|
|
|
|
+ // postURL: url,
|
|
|
|
+ // headers: {
|
|
|
|
+ // method: 'POST',
|
|
|
|
+ // "Content-Type": "multipart/form-data",
|
|
|
|
+ // },
|
|
|
|
+ // formData
|
|
|
|
+ ...result
|
|
});
|
|
});
|
|
- let url = client.signatureUrl(objectName, {expires: expiry, method: 'POST'});
|
|
|
|
- resolve(url);
|
|
|
|
} catch (e: any) {
|
|
} catch (e: any) {
|
|
- reject(Resp.gen_err(Resp.InternalServerError, `获取图片url出错, ${e.message}`));
|
|
|
|
|
|
+ reject(Resp.gen_err(Resp.InternalServerError, `获取oss上传url出错, ${e.message}`));
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
@@ -180,7 +265,42 @@ export class AliOss implements IOss {
|
|
|
|
|
|
stat_object(bucket: string, object_name: string): Promise<IObjectStat | undefined> {
|
|
stat_object(bucket: string, object_name: string): Promise<IObjectStat | undefined> {
|
|
return new Promise<IObjectStat | undefined>(async (resolve, _reject) => {
|
|
return new Promise<IObjectStat | undefined>(async (resolve, _reject) => {
|
|
- return resolve(undefined);
|
|
|
|
|
|
+ try {
|
|
|
|
+ let client = new OSS({
|
|
|
|
+ bucket: bucket,
|
|
|
|
+ endpoint: this.config.end_point,
|
|
|
|
+ accessKeyId: this.config.access_key,
|
|
|
|
+ accessKeySecret: this.config.secret_key,
|
|
|
|
+ authorizationV4: true,
|
|
|
|
+ secure: this.config.use_ssl
|
|
|
|
+ });
|
|
|
|
+ let head = await client.head(object_name, {});
|
|
|
|
+ resolve({
|
|
|
|
+ size: head.res.headers['content-length'],
|
|
|
|
+ etag: head.res.headers['etag']
|
|
|
|
+ });
|
|
|
|
+ } catch (error: any) {
|
|
|
|
+ // Logger.error(error);
|
|
|
|
+ resolve(undefined);
|
|
|
|
+ }
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ private async get_token(bucket: string, expiry: number) {
|
|
|
|
+ const date = new Date();
|
|
|
|
+ date.setSeconds(date.getSeconds() + expiry);
|
|
|
|
+ const policy = {
|
|
|
|
+ expiration: date.toISOString(),
|
|
|
|
+ conditions: [
|
|
|
|
+ {bucket}
|
|
|
|
+ ]
|
|
|
|
+ }
|
|
|
|
+ // 调用SDK获取签名。
|
|
|
|
+ const formData = await this.client.calculatePostSignature(policy);
|
|
|
|
+ const host = `https://${bucket}.${this.config.region}.aliyuncs.com`.toString();
|
|
|
|
+ return {
|
|
|
|
+ postURL: host,
|
|
|
|
+ formData
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|