UNPKG

3.56 kBPlain TextView Raw
1import CloudBase from '@cloudbase/manager-node'
2import { StorageService } from '@cloudbase/manager-node/types/storage'
3import { checkAndGetCredential, getProxy } from './utils'
4import { CloudBaseError } from './error'
5
6interface IStorageOptions {
7 envId: string
8 localPath: string
9 cloudPath: string
10}
11
12interface IStorageCloudOptions {
13 envId: string
14 cloudPath: string
15 cloudPaths?: string[]
16}
17
18async function getStorageService(envId: string): Promise<StorageService> {
19 const { secretId, secretKey, token } = await checkAndGetCredential(true)
20 const app = new CloudBase({
21 secretId,
22 secretKey,
23 token,
24 envId,
25 proxy: getProxy()
26 })
27 return app.storage
28}
29
30export async function uploadFile(options: IStorageOptions) {
31 const { envId, localPath, cloudPath } = options
32 const storageService = await getStorageService(envId)
33 return storageService.uploadFile({
34 localPath,
35 cloudPath
36 })
37}
38
39export async function uploadDirectory(options: IStorageOptions) {
40 const { envId, localPath, cloudPath } = options
41 const storageService = await getStorageService(envId)
42 return storageService.uploadDirectory({
43 localPath,
44 cloudPath
45 })
46}
47
48export async function downloadFile(options: IStorageOptions) {
49 const { envId, localPath, cloudPath } = options
50 const storageService = await getStorageService(envId)
51 return storageService.downloadFile({
52 cloudPath,
53 localPath
54 })
55}
56
57export async function downloadDirectory(options: IStorageOptions) {
58 const { envId, localPath, cloudPath } = options
59 const storageService = await getStorageService(envId)
60
61 return storageService.downloadDirectory({
62 cloudPath,
63 localPath
64 })
65}
66
67export async function deleteFile(options: IStorageCloudOptions) {
68 const { envId, cloudPath, cloudPaths } = options
69 const storageService = await getStorageService(envId)
70
71 if (cloudPaths?.length) {
72 return storageService.deleteFile(cloudPaths)
73 }
74
75 return storageService.deleteFile([cloudPath])
76}
77
78export async function deleteDirectory(options: IStorageCloudOptions) {
79 const { envId, cloudPath } = options
80 const storageService = await getStorageService(envId)
81
82 return storageService.deleteDirectory(cloudPath)
83}
84
85export async function list(options: IStorageCloudOptions) {
86 const { envId, cloudPath } = options
87 const storageService = await getStorageService(envId)
88
89 return storageService.listDirectoryFiles(cloudPath)
90}
91
92export async function getUrl(options: IStorageCloudOptions) {
93 const { envId, cloudPaths } = options
94 const storageService = await getStorageService(envId)
95
96 return storageService.getTemporaryUrl(cloudPaths)
97}
98
99export async function detail(options: IStorageCloudOptions) {
100 const { envId, cloudPath } = options
101 const storageService = await getStorageService(envId)
102
103 return storageService.getFileInfo(cloudPath)
104}
105
106export async function getAcl(options) {
107 const { envId } = options
108 const storageService = await getStorageService(envId)
109
110 return storageService.getStorageAcl()
111}
112
113export async function setAcl(options) {
114 const { envId, acl } = options
115 const validAcl = ['READONLY', 'PRIVATE', 'ADMINWRITE', 'ADMINONLY']
116 if (!validAcl.includes(acl)) {
117 throw new CloudBaseError('非法的权限值,仅支持:READONLY, PRIVATE, ADMINWRITE, ADMINONLY')
118 }
119 const storageService = await getStorageService(envId)
120
121 return storageService.setStorageAcl(acl)
122}