update
commit
870ec3ba08
25
README.md
25
README.md
|
@ -1,2 +1,27 @@
|
|||
# alioss
|
||||
迷你版 Alioss库。 体积虽小, 五脏俱全。
|
||||
|
||||
|
||||
## 版本
|
||||
> 2个版本, 基础版和完整版。
|
||||
|
||||
### 基础版 APIs
|
||||
|
||||
- `.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的跨域配置
|
|
@ -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
|
||||
*/
|
||||
|
||||
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) {
|
||||
let time = new Date()
|
||||
let 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
|
||||
}
|
||||
})
|
||||
|
|
|
@ -37,7 +37,7 @@ export function getExt(str = '') {
|
|||
}
|
||||
|
||||
export function getMimeType(name) {
|
||||
var ext = getExt(name)
|
||||
let ext = getExt(name)
|
||||
return MIME_TYPES[ext] || DEFAULT_MIME_TYPE
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue