UNPKG

5.33 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11var __importDefault = (this && this.__importDefault) || function (mod) {
12 return (mod && mod.__esModule) ? mod : { "default": mod };
13};
14Object.defineProperty(exports, "__esModule", { value: true });
15const manager_node_1 = __importDefault(require("@cloudbase/manager-node"));
16const utils_1 = require("./utils");
17const error_1 = require("./error");
18function getStorageService(envId) {
19 return __awaiter(this, void 0, void 0, function* () {
20 const { secretId, secretKey, token } = yield utils_1.checkAndGetCredential(true);
21 const app = new manager_node_1.default({
22 secretId,
23 secretKey,
24 token,
25 envId,
26 proxy: utils_1.getProxy()
27 });
28 return app.storage;
29 });
30}
31function uploadFile(options) {
32 return __awaiter(this, void 0, void 0, function* () {
33 const { envId, localPath, cloudPath } = options;
34 const storageService = yield getStorageService(envId);
35 return storageService.uploadFile({
36 localPath,
37 cloudPath
38 });
39 });
40}
41exports.uploadFile = uploadFile;
42function uploadDirectory(options) {
43 return __awaiter(this, void 0, void 0, function* () {
44 const { envId, localPath, cloudPath } = options;
45 const storageService = yield getStorageService(envId);
46 return storageService.uploadDirectory({
47 localPath,
48 cloudPath
49 });
50 });
51}
52exports.uploadDirectory = uploadDirectory;
53function downloadFile(options) {
54 return __awaiter(this, void 0, void 0, function* () {
55 const { envId, localPath, cloudPath } = options;
56 const storageService = yield getStorageService(envId);
57 return storageService.downloadFile({
58 cloudPath,
59 localPath
60 });
61 });
62}
63exports.downloadFile = downloadFile;
64function downloadDirectory(options) {
65 return __awaiter(this, void 0, void 0, function* () {
66 const { envId, localPath, cloudPath } = options;
67 const storageService = yield getStorageService(envId);
68 return storageService.downloadDirectory({
69 cloudPath,
70 localPath
71 });
72 });
73}
74exports.downloadDirectory = downloadDirectory;
75function deleteFile(options) {
76 return __awaiter(this, void 0, void 0, function* () {
77 const { envId, cloudPath, cloudPaths } = options;
78 const storageService = yield getStorageService(envId);
79 if (cloudPaths === null || cloudPaths === void 0 ? void 0 : cloudPaths.length) {
80 return storageService.deleteFile(cloudPaths);
81 }
82 return storageService.deleteFile([cloudPath]);
83 });
84}
85exports.deleteFile = deleteFile;
86function deleteDirectory(options) {
87 return __awaiter(this, void 0, void 0, function* () {
88 const { envId, cloudPath } = options;
89 const storageService = yield getStorageService(envId);
90 return storageService.deleteDirectory(cloudPath);
91 });
92}
93exports.deleteDirectory = deleteDirectory;
94function list(options) {
95 return __awaiter(this, void 0, void 0, function* () {
96 const { envId, cloudPath } = options;
97 const storageService = yield getStorageService(envId);
98 return storageService.listDirectoryFiles(cloudPath);
99 });
100}
101exports.list = list;
102function getUrl(options) {
103 return __awaiter(this, void 0, void 0, function* () {
104 const { envId, cloudPaths } = options;
105 const storageService = yield getStorageService(envId);
106 return storageService.getTemporaryUrl(cloudPaths);
107 });
108}
109exports.getUrl = getUrl;
110function detail(options) {
111 return __awaiter(this, void 0, void 0, function* () {
112 const { envId, cloudPath } = options;
113 const storageService = yield getStorageService(envId);
114 return storageService.getFileInfo(cloudPath);
115 });
116}
117exports.detail = detail;
118function getAcl(options) {
119 return __awaiter(this, void 0, void 0, function* () {
120 const { envId } = options;
121 const storageService = yield getStorageService(envId);
122 return storageService.getStorageAcl();
123 });
124}
125exports.getAcl = getAcl;
126function setAcl(options) {
127 return __awaiter(this, void 0, void 0, function* () {
128 const { envId, acl } = options;
129 const validAcl = ['READONLY', 'PRIVATE', 'ADMINWRITE', 'ADMINONLY'];
130 if (!validAcl.includes(acl)) {
131 throw new error_1.CloudBaseError('非法的权限值,仅支持:READONLY, PRIVATE, ADMINWRITE, ADMINONLY');
132 }
133 const storageService = yield getStorageService(envId);
134 return storageService.setStorageAcl(acl);
135 });
136}
137exports.setAcl = setAcl;