Skip to content

Commit

Permalink
feat: list object functions support return restore info (#1339)
Browse files Browse the repository at this point in the history
* feat: list object functions support return restore info

* chore: lock nise version

---------

Co-authored-by: zhengzuoyu.zzy <[email protected]>
  • Loading branch information
YunZZY and zhengzuoyu.zzy authored Nov 19, 2024
1 parent 439ffde commit 986b743
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 23 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2449,6 +2449,9 @@ Success will return objects list on `objects` properties.
- size {Number} object size, e.g.: `344606`
- storageClass {String} storage class type, e.g.: `Standard`
- owner {Object} object owner, including `id` and `displayName`
- restoreInfo {Object|undefined} The restoration status of the object
- ongoingRequest {Boolean} Whether the restoration is complete
- expireDate {Date|undefined} The time before which the restored object can be read
- prefixes {Array<String>} prefix list
- isTruncated {Boolean} truncate or not
- nextMarker {String} next marker string
Expand Down Expand Up @@ -2515,6 +2518,9 @@ Success will return objects list on `objects` properties.
- size {Number} object size, e.g.: `344606`
- storageClass {String} storage class type, e.g.: `Standard`
- owner {Object|null} object owner, including `id` and `displayName`
- restoreInfo {Object|undefined} The restoration status of the object
- ongoingRequest {Boolean} Whether the restoration is complete
- expireDate {Date|undefined} The time before which the restored object can be read
- prefixes {Array<String>} prefix list
- isTruncated {Boolean} truncate or not
- nextContinuationToken {String} next continuation-token string
Expand Down Expand Up @@ -2595,6 +2601,9 @@ Success will return objects list on `objects` properties.
- versionId {String} object versionId
- storageClass {String} storage class type, e.g.: `Standard`
- owner {Object} object owner, including `id` and `displayName`
- restoreInfo {Object|undefined} The restoration status of the object
- ongoingRequest {Boolean} Whether the restoration is complete
- expireDate {Date|undefined} The time before which the restored object can be read
- deleteMarker {Array<ObjectDeleteMarker>} object delete marker info list
Each `ObjectDeleteMarker`
- name {String} object name on oss
Expand Down
9 changes: 7 additions & 2 deletions lib/browser/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { isBlob } = require('../common/utils/isBlob');
const { isFile } = require('../common/utils/isFile');
const { isBuffer } = require('../common/utils/isBuffer');
const { obj2xml } = require('../common/utils/obj2xml');
const { parseRestoreInfo } = require('../common/utils/parseRestoreInfo');

// var assert = require('assert');

Expand Down Expand Up @@ -191,6 +192,7 @@ proto.list = async function list(query, options) {
if (!Array.isArray(objects)) {
objects = [objects];
}

objects = objects.map(obj => ({
name: obj.Key,
url: that._objectUrl(obj.Key),
Expand All @@ -202,7 +204,8 @@ proto.list = async function list(query, options) {
owner: {
id: obj.Owner.ID,
displayName: obj.Owner.DisplayName
}
},
restoreInfo: parseRestoreInfo(obj.RestoreInfo)
}));
}
let prefixes = result.data.CommonPrefixes || null;
Expand Down Expand Up @@ -245,6 +248,7 @@ proto.listV2 = async function listV2(query, options = {}) {
if (!Array.isArray(objects)) {
objects = [objects];
}

objects = objects.map(obj => {
let owner = null;
if (obj.Owner) {
Expand All @@ -261,7 +265,8 @@ proto.listV2 = async function listV2(query, options = {}) {
type: obj.Type,
size: Number(obj.Size),
storageClass: obj.StorageClass,
owner
owner,
restoreInfo: parseRestoreInfo(obj.RestoreInfo)
};
});
}
Expand Down
4 changes: 3 additions & 1 deletion lib/common/object/getBucketVersions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const proto = exports;
const { isObject } = require('../utils/isObject');
const { isArray } = require('../utils/isArray');
const { parseRestoreInfo } = require('../utils/parseRestoreInfo');

proto.getBucketVersions = getBucketVersions;
proto.listObjectVersions = getBucketVersions;
Expand Down Expand Up @@ -43,7 +44,8 @@ async function getBucketVersions(query = {}, options = {}) {
owner: {
id: obj.Owner.ID,
displayName: obj.Owner.DisplayName
}
},
restoreInfo: parseRestoreInfo(obj.RestoreInfo)
}));
}
if (deleteMarker) {
Expand Down
6 changes: 6 additions & 0 deletions lib/common/utils/parseRestoreInfo.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface IRestoreInfo {
ongoingRequest: boolean;
expiryDate?: Date;
}
export declare const parseRestoreInfo: (originalRestoreInfo?: string | undefined) => IRestoreInfo | undefined;
export {};
18 changes: 18 additions & 0 deletions lib/common/utils/parseRestoreInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.parseRestoreInfo = void 0;
exports.parseRestoreInfo = originalRestoreInfo => {
let tempRestoreInfo;
if (originalRestoreInfo) {
tempRestoreInfo = {
ongoingRequest: originalRestoreInfo.includes('true')
};
if (!tempRestoreInfo.ongoingRequest) {
const matchArray = originalRestoreInfo.match(/expiry-date="(.*)"/);
if (matchArray && matchArray[1]) {
tempRestoreInfo.expiryDate = new Date(matchArray[1]);
}
}
}
return tempRestoreInfo;
};
24 changes: 24 additions & 0 deletions lib/common/utils/parseRestoreInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
interface IRestoreInfo {
ongoingRequest: boolean;
expiryDate?: Date;
}

export const parseRestoreInfo = (originalRestoreInfo?: string): IRestoreInfo | undefined => {
let tempRestoreInfo: IRestoreInfo | undefined;

if (originalRestoreInfo) {
tempRestoreInfo = {
ongoingRequest: originalRestoreInfo.includes('true')
};

if (!tempRestoreInfo.ongoingRequest) {
const matchArray = originalRestoreInfo.match(/expiry-date="(.*)"/);

if (matchArray && matchArray[1]) {
tempRestoreInfo.expiryDate = new Date(matchArray[1]);
}
}
}

return tempRestoreInfo;
};
9 changes: 7 additions & 2 deletions lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const pump = require('pump');
const { isBuffer } = require('./common/utils/isBuffer');
const { retry } = require('./common/utils/retry');
const { obj2xml } = require('./common/utils/obj2xml');
const { parseRestoreInfo } = require('./common/utils/parseRestoreInfo');

const proto = exports;

Expand Down Expand Up @@ -211,6 +212,7 @@ proto.list = async function list(query, options) {
if (!Array.isArray(objects)) {
objects = [objects];
}

objects = objects.map(obj => ({
name: obj.Key,
url: that._objectUrl(obj.Key),
Expand All @@ -222,7 +224,8 @@ proto.list = async function list(query, options) {
owner: {
id: obj.Owner.ID,
displayName: obj.Owner.DisplayName
}
},
restoreInfo: parseRestoreInfo(obj.RestoreInfo)
}));
}
let prefixes = result.data.CommonPrefixes || null;
Expand Down Expand Up @@ -276,6 +279,7 @@ proto.listV2 = async function listV2(query = {}, options = {}) {
displayName: obj.Owner.DisplayName
};
}

return {
name: obj.Key,
url: that._objectUrl(obj.Key),
Expand All @@ -284,7 +288,8 @@ proto.listV2 = async function listV2(query = {}, options = {}) {
type: obj.Type,
size: Number(obj.Size),
storageClass: obj.StorageClass,
owner
owner,
restoreInfo: parseRestoreInfo(obj.RestoreInfo)
};
});
}
Expand Down
50 changes: 32 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
"lint-staged": "^12.4.1",
"mm": "^2.0.0",
"mocha": "^9.1.2",
"nise": "5.1.4",
"nyc": "^15.1.0",
"prettier": "^3.0.0",
"promise-polyfill": "^6.0.2",
Expand Down
40 changes: 40 additions & 0 deletions test/browser/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,26 @@ describe('browser', () => {
assert(!result.isTruncated);
assert.equal(result.prefixes, null);
});

it('should list files with restore info', async () => {
const testFile = `${listPrefix}restoreInfoTest.txt`;
await client.put(testFile, Buffer.from('test'), {
headers: {
'x-oss-storage-class': 'Archive'
}
});
await client.restore(testFile);

const listResult = await client.list({
prefix: testFile
});
assert.strictEqual(listResult.res.status, 200);
assert.strictEqual(listResult.objects.length, 1);
assert.strictEqual(listResult.objects[0].restoreInfo.ongoingRequest, true);
assert.strictEqual(listResult.objects[0].restoreInfo.expiryDate, undefined);

await client.delete(testFile);
});
});

describe('listV2()', () => {
Expand Down Expand Up @@ -673,6 +693,26 @@ describe('browser', () => {
} while (nextContinuationToken);
assert.strictEqual(keyCount, 6);
});

it('should list files with restore info', async () => {
const testFile = `${listPrefix}restoreInfoTest.txt`;
await store.put(testFile, Buffer.from('test'), {
headers: {
'x-oss-storage-class': 'Archive'
}
});
await store.restore(testFile);

const listResult = await store.listV2({
prefix: testFile
});
assert.strictEqual(listResult.res.status, 200);
assert.strictEqual(listResult.objects.length, 1);
assert.strictEqual(listResult.objects[0].restoreInfo.ongoingRequest, true);
assert.strictEqual(listResult.objects[0].restoreInfo.expiryDate, undefined);

await store.delete(testFile);
});
});

describe('get()', () => {
Expand Down
18 changes: 18 additions & 0 deletions test/node/multiversion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,24 @@ describe('test/multiversion.test.js', () => {
assert(false, err.message);
}
});

it('should list files with restore info', async () => {
const testFile = 'restoreInfoTest.txt';
await store.put(testFile, Buffer.from('test'), {
headers: {
'x-oss-storage-class': 'Archive'
}
});
await store.restore(testFile);

const listResult = await store.getBucketVersions({
prefix: testFile
});
assert.strictEqual(listResult.res.status, 200);
assert.strictEqual(listResult.objects.length, 1);
assert.strictEqual(listResult.objects[0].restoreInfo.ongoingRequest, true);
assert.strictEqual(listResult.objects[0].restoreInfo.expiryDate, undefined);
});
});

describe('putBucketLifecycle() getBucketLifecycle()', async () => {
Expand Down
Loading

0 comments on commit 986b743

Please sign in to comment.