@did-space/s3-driver 0.2.173 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { ReadOptions, WriteOptions, SpaceOperatorProtocol, SpaceConfig, AppSpaceOptions, DeleteOptions, ListOptions, Object, Data, ListsOptions, GetHashOptions } from '@did-space/core';
2
+ import { ReadOptions, WriteOptions, SpaceOperatorProtocol, SpaceConfig, AppSpaceOptions, DeleteOptions, ListOptions, Object, Data, ListsOptions, GetHashOptions, WriteAsOwnerOptions, ReadAsOwnerOptions, DeleteAsOwnerOptions, ExistsAsOwnerOptions } from '@did-space/core';
3
3
  import { S3Client } from '@aws-sdk/client-s3';
4
4
  import { Stream } from 'stream';
5
5
  import { S3DriverOptions } from '../s3/options';
@@ -76,7 +76,7 @@ export declare class S3SpaceOperator implements SpaceOperatorProtocol {
76
76
  */
77
77
  lists(options: ListsOptions): Promise<Object[]>;
78
78
  list(options: ListOptions): Promise<Object>;
79
- writeAsOwner(key: string, data: Data): Promise<void>;
79
+ writeAsOwner(key: string, data: Data, options?: WriteAsOwnerOptions): Promise<void>;
80
80
  /**
81
81
  *
82
82
  * FIXME: @yejianchao 需要支持一下删除一个文件夹(非空)的操作
@@ -84,8 +84,8 @@ export declare class S3SpaceOperator implements SpaceOperatorProtocol {
84
84
  * @return {*} {Promise<void>}
85
85
  * @memberof S3SpaceOperator
86
86
  */
87
- deleteAsOwner(key: string): Promise<void>;
88
- readAsOwner(key: string): Promise<Stream>;
89
- existsAsOwner(key: string): Promise<boolean>;
87
+ deleteAsOwner(key: string, options?: DeleteAsOwnerOptions): Promise<void>;
88
+ readAsOwner(key: string, options?: ReadAsOwnerOptions): Promise<Stream>;
89
+ existsAsOwner(key: string, options?: ExistsAsOwnerOptions): Promise<boolean>;
90
90
  }
91
91
  export {};
@@ -28,6 +28,7 @@ const mime_types_1 = __importDefault(require("mime-types"));
28
28
  const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
29
29
  const debug_1 = __importDefault(require("debug"));
30
30
  const p_all_1 = __importDefault(require("p-all"));
31
+ const common_1 = require("../utils/common");
31
32
  const { version: s3ClientVersion } = require('@aws-sdk/client-s3/package.json');
32
33
  const { version: libStorageVersion } = require('@aws-sdk/lib-storage/package.json');
33
34
  const debug = (0, debug_1.default)('@did-space/s3-driver:S3SpaceOperator');
@@ -342,7 +343,7 @@ class S3SpaceOperator {
342
343
  var _a, e_3, _b, _c;
343
344
  var _d;
344
345
  return __awaiter(this, void 0, void 0, function* () {
345
- debug('_listsRecursiveAsOwner.before', `Prefix=${prefix}`);
346
+ debug('listsRecursiveAsOwner.before', `Prefix=${prefix}`);
346
347
  const objects = [];
347
348
  try {
348
349
  for (var _e = true, _f = __asyncValues((0, client_s3_1.paginateListObjectsV2)({ client: this.s3Client }, {
@@ -384,7 +385,7 @@ class S3SpaceOperator {
384
385
  }
385
386
  finally { if (e_3) throw e_3.error; }
386
387
  }
387
- debug('_listsRecursiveAsOwner.after', `Prefix=${prefix}`);
388
+ debug('listsRecursiveAsOwner.after', `Prefix=${prefix}`);
388
389
  return objects;
389
390
  });
390
391
  }
@@ -399,9 +400,10 @@ class S3SpaceOperator {
399
400
  */
400
401
  lists(options) {
401
402
  return __awaiter(this, void 0, void 0, function* () {
402
- const prefix = (0, path_1.join)(this.options.root, options.key);
403
+ const prefix = (0, common_1.getSafeKey)((options === null || options === void 0 ? void 0 : options.useGlobal) ? options.key : (0, path_1.join)(this.options.root, options.key));
403
404
  debug('lists.before', `Prefix=${prefix}`);
404
- if (!(yield this.existsAsOwner(options.key))) {
405
+ if (!(yield this.existsAsOwner(options.key, options))) {
406
+ debug('lists.before.existsAsOwner()', `does not exist: ${options.key}`);
405
407
  return [];
406
408
  }
407
409
  // 以 非递归的方式 && 显示文件夹 的方式列出当前文件
@@ -444,11 +446,15 @@ class S3SpaceOperator {
444
446
  };
445
447
  });
446
448
  }
447
- writeAsOwner(key, data) {
449
+ writeAsOwner(key, data, options) {
448
450
  return __awaiter(this, void 0, void 0, function* () {
451
+ debug('writeAsOwner.before', JSON.stringify({ key, options }));
452
+ const $key = (0, common_1.getSafeKey)((options === null || options === void 0 ? void 0 : options.useGlobal) ? key : (0, path_1.join)(this.options.root, key));
453
+ debug('writeAsOwner.$$key', $key);
449
454
  yield this.s3Client.send(new client_s3_1.PutObjectCommand({
450
455
  Bucket: this.options.bucket,
451
- Key: (0, path_1.join)(this.options.root, key),
456
+ // @note: 注意 key 不能以 / 开头
457
+ Key: $key,
452
458
  Body: data,
453
459
  }));
454
460
  });
@@ -460,10 +466,13 @@ class S3SpaceOperator {
460
466
  * @return {*} {Promise<void>}
461
467
  * @memberof S3SpaceOperator
462
468
  */
463
- deleteAsOwner(key) {
469
+ deleteAsOwner(key, options) {
464
470
  return __awaiter(this, void 0, void 0, function* () {
465
471
  debug('deleteAsOwner.before', `Bucket=${this.options.bucket} Key=${key}`);
472
+ const $key = (0, common_1.getSafeKey)((options === null || options === void 0 ? void 0 : options.useGlobal) ? key : (0, path_1.join)(this.options.root, key));
473
+ debug('deleteAsOwner.$key', $key);
466
474
  if (key.endsWith('/')) {
475
+ // FIXME: 这个 key 怎么取,后续处理一下,对于新的存储改进暂无影响,但是目前改动的话,会对旧的存储产生影响 @jianchao
467
476
  const objects = yield this.listsRecursiveAsOwner(key);
468
477
  const actions = objects.map((x) => {
469
478
  return () => __awaiter(this, void 0, void 0, function* () {
@@ -479,7 +488,7 @@ class S3SpaceOperator {
479
488
  yield this.s3Client.send(new client_s3_1.DeleteObjectCommand({
480
489
  Bucket: this.options.bucket,
481
490
  // @note: 注意 key 不能以 / 开头
482
- Key: key,
491
+ Key: $key,
483
492
  }));
484
493
  }));
485
494
  yield (0, p_all_1.default)(actions, { concurrency: process.env.DRIVER_CONCURRENCY ? +process.env.DRIVER_CONCURRENCY : 32 });
@@ -487,28 +496,37 @@ class S3SpaceOperator {
487
496
  else {
488
497
  yield this.s3Client.send(new client_s3_1.DeleteObjectCommand({
489
498
  Bucket: this.options.bucket,
490
- Key: (0, path_1.join)(this.options.root, key),
499
+ Key: $key,
491
500
  }));
492
501
  }
493
502
  debug('deleteAsOwner.after', `Key=${key}`);
494
503
  });
495
504
  }
496
- readAsOwner(key) {
505
+ readAsOwner(key, options) {
497
506
  return __awaiter(this, void 0, void 0, function* () {
507
+ debug('readAsOwner.before', JSON.stringify({ key, options }));
508
+ const $key = (0, common_1.getSafeKey)((options === null || options === void 0 ? void 0 : options.useGlobal) ? key : (0, path_1.join)(this.options.root, key));
509
+ debug('readAsOwner.$key', $key);
498
510
  const output = yield this.s3Client.send(new client_s3_1.GetObjectCommand({
499
511
  Bucket: this.options.bucket,
500
- Key: (0, path_1.join)(this.options.root, key),
512
+ Key: $key,
501
513
  ResponseContentType: 'application/octet-stream',
502
514
  }));
503
515
  return output.Body;
504
516
  });
505
517
  }
506
- existsAsOwner(key) {
518
+ existsAsOwner(key, options) {
507
519
  var _a;
508
520
  return __awaiter(this, void 0, void 0, function* () {
509
- const $key = (0, path_1.join)(this.options.root, key);
510
- const isDir = $key.endsWith('/');
521
+ debug('existsAsOwner.before', JSON.stringify(Object.assign(Object.assign({}, options), { key })));
511
522
  try {
523
+ const $key = (0, common_1.getSafeKey)((options === null || options === void 0 ? void 0 : options.useGlobal) ? key : (0, path_1.join)(this.options.root, key));
524
+ if ($key === '') {
525
+ // 表示根目录,根目录始终存在的
526
+ return true;
527
+ }
528
+ const isDir = $key.endsWith('/');
529
+ debug('existsAsOwner.before.$$key', $key);
512
530
  // 特别地,如果这是一个 folder 的话,那么需要使用 ListObjectsCommand 做特殊判断
513
531
  if (isDir) {
514
532
  const output = yield this.s3Client.send(new client_s3_1.ListObjectsCommand({
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @description s3 的 key 不能以 / 开头
3
+ * @export
4
+ * @param {string} key
5
+ * @return {string}
6
+ */
7
+ export declare function getSafeKey(key: string): string;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSafeKey = void 0;
4
+ /**
5
+ * @description s3 的 key 不能以 / 开头
6
+ * @export
7
+ * @param {string} key
8
+ * @return {string}
9
+ */
10
+ function getSafeKey(key) {
11
+ return key.replace(/^\/*/, '');
12
+ }
13
+ exports.getSafeKey = getSafeKey;
@@ -1,2 +1,3 @@
1
1
  export * from './stream-to-string';
2
2
  export * from './timeout';
3
+ export * from './common';
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./stream-to-string"), exports);
18
18
  __exportStar(require("./timeout"), exports);
19
+ __exportStar(require("./common"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@did-space/s3-driver",
3
- "version": "0.2.173",
3
+ "version": "0.3.1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -34,7 +34,7 @@
34
34
  "dependencies": {
35
35
  "@aws-sdk/client-s3": "3.370.0",
36
36
  "@aws-sdk/lib-storage": "3.370.0",
37
- "@did-space/core": "0.2.173",
37
+ "@did-space/core": "0.3.1",
38
38
  "debug": "^4.3.4",
39
39
  "js-yaml": "^4.1.0",
40
40
  "lodash": "^4.17.21",
@@ -57,5 +57,5 @@
57
57
  "ts-jest": "^28.0.6",
58
58
  "typescript": "^4.9.5"
59
59
  },
60
- "gitHead": "eec3f7428872bd94393e2ffca9759caac0e1afa7"
60
+ "gitHead": "054a38c39a923ce79045ab3640f5611b62ea19bc"
61
61
  }