1 | import CloudBase from '@cloudbase/manager-node'
|
2 | import { StorageService } from '@cloudbase/manager-node/types/storage'
|
3 | import { checkAndGetCredential, getProxy } from './utils'
|
4 | import { CloudBaseError } from './error'
|
5 |
|
6 | interface IStorageOptions {
|
7 | envId: string
|
8 | localPath: string
|
9 | cloudPath: string
|
10 | }
|
11 |
|
12 | interface IStorageCloudOptions {
|
13 | envId: string
|
14 | cloudPath: string
|
15 | cloudPaths?: string[]
|
16 | }
|
17 |
|
18 | async 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 |
|
30 | export 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 |
|
39 | export 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 |
|
48 | export 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 |
|
57 | export 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 |
|
67 | export 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 |
|
78 | export async function deleteDirectory(options: IStorageCloudOptions) {
|
79 | const { envId, cloudPath } = options
|
80 | const storageService = await getStorageService(envId)
|
81 |
|
82 | return storageService.deleteDirectory(cloudPath)
|
83 | }
|
84 |
|
85 | export async function list(options: IStorageCloudOptions) {
|
86 | const { envId, cloudPath } = options
|
87 | const storageService = await getStorageService(envId)
|
88 |
|
89 | return storageService.listDirectoryFiles(cloudPath)
|
90 | }
|
91 |
|
92 | export async function getUrl(options: IStorageCloudOptions) {
|
93 | const { envId, cloudPaths } = options
|
94 | const storageService = await getStorageService(envId)
|
95 |
|
96 | return storageService.getTemporaryUrl(cloudPaths)
|
97 | }
|
98 |
|
99 | export async function detail(options: IStorageCloudOptions) {
|
100 | const { envId, cloudPath } = options
|
101 | const storageService = await getStorageService(envId)
|
102 |
|
103 | return storageService.getFileInfo(cloudPath)
|
104 | }
|
105 |
|
106 | export async function getAcl(options) {
|
107 | const { envId } = options
|
108 | const storageService = await getStorageService(envId)
|
109 |
|
110 | return storageService.getStorageAcl()
|
111 | }
|
112 |
|
113 | export 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 | }
|