更新代码和文档
parent
6f9c0b0f4a
commit
132e8b2819
13
README.md
13
README.md
|
@ -1,2 +1,15 @@
|
||||||
# alioss
|
# alioss
|
||||||
迷你版 Alioss库。 体积虽小, 五脏俱全。
|
迷你版 Alioss库。 体积虽小, 五脏俱全。
|
||||||
|
|
||||||
|
|
||||||
|
## 版本
|
||||||
|
> 2个版本, 基础版和完整版。
|
||||||
|
|
||||||
|
### 基础版
|
||||||
|
|
||||||
|
- `.auth(path)` 临时对文件签名(用于访问私有文件)
|
||||||
|
- `.policy(dir, size)` 生成policy签名
|
||||||
|
- `.list({prefix, delimiter, max, token})` 列出文件
|
||||||
|
- `.upload(auth, file, path)` 上传文件
|
||||||
|
- `.copy(origin, target)` 复制文件
|
||||||
|
- `.delete(path)` 删除文件
|
|
@ -1,3 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@bytedo/alioss"
|
"name": "@bytedo/alioss",
|
||||||
|
"dependencies": {
|
||||||
|
"crypto.web.js": "^1.0.0"
|
||||||
|
}
|
||||||
}
|
}
|
43
src/index.js
43
src/index.js
|
@ -4,22 +4,22 @@
|
||||||
* @date 2020/01/18 14:28:47
|
* @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 xml2js from './lib/xml2js.js'
|
||||||
import {
|
import { MIME_TYPES, DEFAULT_MIME_TYPE } from './lib/constants.js'
|
||||||
APP_ID,
|
|
||||||
APP_KEY,
|
|
||||||
MIME_TYPES,
|
|
||||||
DEFAULT_MIME_TYPE
|
|
||||||
} from './lib/constants.js'
|
|
||||||
import { getMimeType, fixFile, str2sign } from './lib/helper.js'
|
import { getMimeType, fixFile, str2sign } from './lib/helper.js'
|
||||||
|
|
||||||
export default class Alioss {
|
export default class Alioss {
|
||||||
|
#appid = ''
|
||||||
|
#appkey = ''
|
||||||
|
|
||||||
#bucket = ''
|
#bucket = ''
|
||||||
#domain = ''
|
#domain = ''
|
||||||
#api = ''
|
#api = ''
|
||||||
|
|
||||||
constructor(bucket, domain, region) {
|
constructor({ bucket, domain, region, appid, appkey } = {}) {
|
||||||
|
this.#appid = appid
|
||||||
|
this.#appkey = appkey
|
||||||
this.#bucket = bucket
|
this.#bucket = bucket
|
||||||
this.#domain = domain
|
this.#domain = domain
|
||||||
this.#api = `https://${bucket}.${region}.aliyuncs.com`
|
this.#api = `https://${bucket}.${region}.aliyuncs.com`
|
||||||
|
@ -32,12 +32,12 @@ export default class Alioss {
|
||||||
return hmac(
|
return hmac(
|
||||||
'SHA-1',
|
'SHA-1',
|
||||||
`GET\n\n\n${time}\n/${this.#bucket}/${key}`,
|
`GET\n\n\n${time}\n/${this.#bucket}/${key}`,
|
||||||
APP_KEY,
|
this.#appkey,
|
||||||
'base64'
|
'base64'
|
||||||
).then(signature => {
|
).then(signature => {
|
||||||
return `?OSSAccessKeyId=${APP_ID}&Expires=${time}&Signature=${encodeURIComponent(
|
return `?OSSAccessKeyId=${
|
||||||
signature
|
this.#appid
|
||||||
)}`
|
}&Expires=${time}&Signature=${encodeURIComponent(signature)}`
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ export default class Alioss {
|
||||||
* dir: 上传目录名
|
* dir: 上传目录名
|
||||||
* size: 大小限制, 单位 MB 默认10MB
|
* size: 大小限制, 单位 MB 默认10MB
|
||||||
*/
|
*/
|
||||||
sign(dir = '', size = 10) {
|
policy(dir = '', size = 10) {
|
||||||
var time = new Date()
|
var time = new Date()
|
||||||
var params = {
|
var params = {
|
||||||
conditions: [
|
conditions: [
|
||||||
|
@ -61,8 +61,8 @@ export default class Alioss {
|
||||||
|
|
||||||
policy = JSON.stringify(params)
|
policy = JSON.stringify(params)
|
||||||
policy = btoa(policy)
|
policy = btoa(policy)
|
||||||
return hmac('SHA-1', policy, APP_KEY, 'base64').then(signature => {
|
return hmac('SHA-1', policy, this.#appkey, 'base64').then(signature => {
|
||||||
return { policy, signature, id: APP_ID }
|
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}/${
|
`GET\n\n\n${time}\nx-oss-date:${time}\n/${this.#bucket}/${
|
||||||
token ? '?continuation-token=' + token : ''
|
token ? '?continuation-token=' + token : ''
|
||||||
}`,
|
}`,
|
||||||
APP_KEY,
|
this.#appkey,
|
||||||
'base64'
|
'base64'
|
||||||
)
|
)
|
||||||
.then(signature =>
|
.then(signature =>
|
||||||
fetch(this.#api + '?' + query.toParams(), {
|
fetch(this.#api + '?' + query.toParams(), {
|
||||||
headers: {
|
headers: {
|
||||||
'content-type': void 0,
|
'content-type': void 0,
|
||||||
authorization: `OSS ${APP_ID}:${signature}`,
|
authorization: `OSS ${this.#appid}:${signature}`,
|
||||||
'x-oss-date': time
|
'x-oss-date': time
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -137,14 +137,14 @@ export default class Alioss {
|
||||||
headers,
|
headers,
|
||||||
key: target
|
key: target
|
||||||
}),
|
}),
|
||||||
APP_KEY,
|
this.#appkey,
|
||||||
'base64'
|
'base64'
|
||||||
)
|
)
|
||||||
.then(signature =>
|
.then(signature =>
|
||||||
fetch(`${this.#api}/${target}`, {
|
fetch(`${this.#api}/${target}`, {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: {
|
headers: {
|
||||||
authorization: `OSS ${APP_ID}:${signature}`,
|
authorization: `OSS ${this.#appid}:${signature}`,
|
||||||
...headers
|
...headers
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -158,14 +158,13 @@ export default class Alioss {
|
||||||
return hmac(
|
return hmac(
|
||||||
'SHA-1',
|
'SHA-1',
|
||||||
`DELETE\n\n\n${time}\nx-oss-date:${time}\n/${this.#bucket}/${key}`,
|
`DELETE\n\n\n${time}\nx-oss-date:${time}\n/${this.#bucket}/${key}`,
|
||||||
APP_KEY,
|
this.#appkey,
|
||||||
'base64'
|
'base64'
|
||||||
).then(signature =>
|
).then(signature =>
|
||||||
fetch(`${this.#api}/${key}`, {
|
fetch(`${this.#api}/${key}`, {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
headers: {
|
headers: {
|
||||||
'content-type': void 0,
|
authorization: `OSS ${this.#appid}:${signature}`,
|
||||||
authorization: `OSS ${APP_ID}:${signature}`,
|
|
||||||
'x-oss-date': time
|
'x-oss-date': time
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue