更新代码和文档

master^2
yutent 2023-06-23 23:53:12 +08:00
parent 6f9c0b0f4a
commit 132e8b2819
3 changed files with 39 additions and 24 deletions

View File

@ -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)` 删除文件

View File

@ -1,3 +1,6 @@
{
"name": "@bytedo/alioss"
"name": "@bytedo/alioss",
"dependencies": {
"crypto.web.js": "^1.0.0"
}
}

View File

@ -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
}
})