From 132e8b2819b3304cc01429889225b60f1fe30cc3 Mon Sep 17 00:00:00 2001 From: yutent Date: Fri, 23 Jun 2023 23:53:12 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=92=8C=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 13 +++++++++++++ package.json | 7 +++++-- src/index.js | 43 +++++++++++++++++++++---------------------- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 08cb752..0c7dfb7 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,15 @@ # alioss 迷你版 Alioss库。 体积虽小, 五脏俱全。 + + +## 版本 +> 2个版本, 基础版和完整版。 + +### 基础版 + +- `.auth(path)` 临时对文件签名(用于访问私有文件) +- `.policy(dir, size)` 生成policy签名 +- `.list({prefix, delimiter, max, token})` 列出文件 +- `.upload(auth, file, path)` 上传文件 +- `.copy(origin, target)` 复制文件 +- `.delete(path)` 删除文件 \ No newline at end of file diff --git a/package.json b/package.json index e151ae0..bc28b89 100644 --- a/package.json +++ b/package.json @@ -1,3 +1,6 @@ { - "name": "@bytedo/alioss" -} \ No newline at end of file + "name": "@bytedo/alioss", + "dependencies": { + "crypto.web.js": "^1.0.0" + } +} diff --git a/src/index.js b/src/index.js index d9848b5..06efdfc 100644 --- a/src/index.js +++ b/src/index.js @@ -4,22 +4,22 @@ * @date 2020/01/18 14:28:47 */ -import { hmac, base64encode } from 'crypto' +import { hmac, base64encode } from 'crypto.web.js' import xml2js from './lib/xml2js.js' -import { - APP_ID, - APP_KEY, - MIME_TYPES, - DEFAULT_MIME_TYPE -} from './lib/constants.js' +import { MIME_TYPES, DEFAULT_MIME_TYPE } from './lib/constants.js' import { getMimeType, fixFile, str2sign } from './lib/helper.js' export default class Alioss { + #appid = '' + #appkey = '' + #bucket = '' #domain = '' #api = '' - constructor(bucket, domain, region) { + constructor({ bucket, domain, region, appid, appkey } = {}) { + this.#appid = appid + this.#appkey = appkey this.#bucket = bucket this.#domain = domain this.#api = `https://${bucket}.${region}.aliyuncs.com` @@ -32,12 +32,12 @@ export default class Alioss { return hmac( 'SHA-1', `GET\n\n\n${time}\n/${this.#bucket}/${key}`, - APP_KEY, + this.#appkey, 'base64' ).then(signature => { - return `?OSSAccessKeyId=${APP_ID}&Expires=${time}&Signature=${encodeURIComponent( - signature - )}` + return `?OSSAccessKeyId=${ + this.#appid + }&Expires=${time}&Signature=${encodeURIComponent(signature)}` }) } @@ -46,7 +46,7 @@ export default class Alioss { * dir: 上传目录名 * size: 大小限制, 单位 MB 默认10MB */ - sign(dir = '', size = 10) { + policy(dir = '', size = 10) { var time = new Date() var params = { conditions: [ @@ -61,8 +61,8 @@ export default class Alioss { policy = JSON.stringify(params) policy = btoa(policy) - return hmac('SHA-1', policy, APP_KEY, 'base64').then(signature => { - return { policy, signature, id: APP_ID } + return hmac('SHA-1', policy, this.#appkey, 'base64').then(signature => { + return { policy, signature, id: this.#appid } }) } @@ -80,14 +80,14 @@ export default class Alioss { `GET\n\n\n${time}\nx-oss-date:${time}\n/${this.#bucket}/${ token ? '?continuation-token=' + token : '' }`, - APP_KEY, + this.#appkey, 'base64' ) .then(signature => fetch(this.#api + '?' + query.toParams(), { headers: { 'content-type': void 0, - authorization: `OSS ${APP_ID}:${signature}`, + authorization: `OSS ${this.#appid}:${signature}`, 'x-oss-date': time } }) @@ -137,14 +137,14 @@ export default class Alioss { headers, key: target }), - APP_KEY, + this.#appkey, 'base64' ) .then(signature => fetch(`${this.#api}/${target}`, { method: 'PUT', headers: { - authorization: `OSS ${APP_ID}:${signature}`, + authorization: `OSS ${this.#appid}:${signature}`, ...headers } }) @@ -158,14 +158,13 @@ export default class Alioss { return hmac( 'SHA-1', `DELETE\n\n\n${time}\nx-oss-date:${time}\n/${this.#bucket}/${key}`, - APP_KEY, + this.#appkey, 'base64' ).then(signature => fetch(`${this.#api}/${key}`, { method: 'DELETE', headers: { - 'content-type': void 0, - authorization: `OSS ${APP_ID}:${signature}`, + authorization: `OSS ${this.#appid}:${signature}`, 'x-oss-date': time } }) From 58e3caaf97afc3556f9995b44593ca3c76b74df4 Mon Sep 17 00:00:00 2001 From: yutent Date: Sat, 24 Jun 2023 00:20:20 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 0c7dfb7..6a37e0f 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,23 @@ ## 版本 > 2个版本, 基础版和完整版。 -### 基础版 +### 基础版 APIs -- `.auth(path)` 临时对文件签名(用于访问私有文件) -- `.policy(dir, size)` 生成policy签名 -- `.list({prefix, delimiter, max, token})` 列出文件 -- `.upload(auth, file, path)` 上传文件 -- `.copy(origin, target)` 复制文件 -- `.delete(path)` 删除文件 \ No newline at end of file +- `.auth(path)` 临时对文件签名(用于访问私有文件) +- `.policy(dir, size)` 生成policy签名 +- `.list({prefix, delimiter, max, token})` 列出文件 +- `.upload(auth, file, path)` 上传文件 +- `.createLink(origin, target)` 创建软链接 +- `.copy(origin, target)` 复制文件 +- `.delete(path)` 删除文件 + +### 完整版 APIs +除了拥有基础版所有的APIs之外, 还拥有以下APIs。 + +- `.setACL(path, acl)` 设置文件的ACL权限 +- `.getACL(path)` 获取文件的ACL权限 +- `.setBucketACL(bucket, acl)` 设置bucket的ACL权限 +- `.getBucketACL(bucket)` 获取bucket的ACL权限 +- `.setCORS(bucket, rules)` 设置bucket的跨域配置 +- `.getCORS(bucket)` 获取bucket的跨域配置 +- `.deleteCORS(bucket)` 删除bucket的跨域配置 \ No newline at end of file