master
yutent 2024-08-22 16:55:59 +08:00
commit 870ec3ba08
4 changed files with 52 additions and 25 deletions

View File

@ -1,2 +1,27 @@
# alioss # alioss
迷你版 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的跨域配置

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 * @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) {
let time = new Date() let time = new Date()
let params = { let 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
} }
}) })

View File

@ -37,7 +37,7 @@ export function getExt(str = '') {
} }
export function getMimeType(name) { export function getMimeType(name) {
var ext = getExt(name) let ext = getExt(name)
return MIME_TYPES[ext] || DEFAULT_MIME_TYPE return MIME_TYPES[ext] || DEFAULT_MIME_TYPE
} }