Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: listObjectsV2 #888

Merged
merged 3 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"useTabs": false,
"printWidth": 120,
"bracketSpacing": true,
"arrowParens": "avoid"
"arrowParens": "avoid",
"trailingComma": "none"
}
73 changes: 60 additions & 13 deletions lib/browser/object.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// const debug = require('debug')('ali-oss:object');
const fs = require('fs');
const copy = require('copy-to');
Expand All @@ -12,20 +11,19 @@ const { isBuffer } = require('../common/utils/isBuffer');

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


const proto = exports;

/**
* Object operations
*/

/**
* append an object from String(file path)/Buffer/ReadableStream
* @param {String} name the object key
* @param {Mixed} file String(file path)/Buffer/ReadableStream
* @param {Object} options
* @return {Object}
*/
* append an object from String(file path)/Buffer/ReadableStream
* @param {String} name the object key
* @param {Mixed} file String(file path)/Buffer/ReadableStream
* @param {Object} options
* @return {Object}
*/
proto.append = async function append(name, file, options) {
options = options || {};
if (options.position === undefined) options.position = '0';
Expand Down Expand Up @@ -155,7 +153,6 @@ proto.putStream = async function putStream(name, stream, options) {
return ret;
};


merge(proto, require('../common/object/copyObject'));
merge(proto, require('../common/object/getObjectTagging'));
merge(proto, require('../common/object/putObjectTagging'));
Expand Down Expand Up @@ -229,6 +226,56 @@ proto.list = async function list(query, options) {
};
};

proto.listV2 = async function listV2(query, options) {
const params = this._objectRequestParams('GET', '', options);
params.query = {
'list-type': 2,
...query
};
params.xmlResponse = true;
params.successStatuses = [200];

const result = await this.request(params);
let objects = result.data.Contents;
const that = this;
if (objects) {
if (!Array.isArray(objects)) {
objects = [objects];
}
objects = objects.map(obj => ({
name: obj.Key,
url: that._objectUrl(obj.Key),
lastModified: obj.LastModified,
etag: obj.ETag,
type: obj.Type,
size: Number(obj.Size),
storageClass: obj.StorageClass,
owner: obj.Owner
? {
id: obj.Owner.ID,
displayName: obj.Owner.DisplayName
}
: null
}));
}
let prefixes = result.data.CommonPrefixes || null;
if (prefixes) {
if (!Array.isArray(prefixes)) {
prefixes = [prefixes];
}
prefixes = prefixes.map(item => item.Prefix);
}
return {
res: result.res,
objects,
prefixes,
isTruncated: result.data.IsTruncated === 'true',
keyCount: result.data.KeyCount,
continuationToken: result.data.ContinuationToken || null,
nextContinuationToken: result.data.NextContinuationToken || null
};
};

/**
* Restore Object
* @param {String} name the object key
Expand Down Expand Up @@ -294,18 +341,18 @@ proto._convertMetaToHeaders = function _convertMetaToHeaders(meta, headers) {
return;
}

Object.keys(meta).forEach((k) => {
Object.keys(meta).forEach(k => {
headers[`x-oss-meta-${k}`] = meta[k];
});
};

proto._deleteFileSafe = function _deleteFileSafe(filepath) {
return new Promise((resolve) => {
fs.exists(filepath, (exists) => {
return new Promise(resolve => {
fs.exists(filepath, exists => {
if (!exists) {
resolve();
} else {
fs.unlink(filepath, (err) => {
fs.unlink(filepath, err => {
if (err) {
this.debug('unlink %j error: %s', filepath, err, 'error');
}
Expand Down
71 changes: 60 additions & 11 deletions lib/object.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

const debug = require('debug')('ali-oss:object');
const fs = require('fs');
const is = require('is-type-of');
Expand All @@ -17,12 +16,12 @@ const proto = exports;
*/

/**
* append an object from String(file path)/Buffer/ReadableStream
* @param {String} name the object key
* @param {Mixed} file String(file path)/Buffer/ReadableStream
* @param {Object} options
* @return {Object}
*/
* append an object from String(file path)/Buffer/ReadableStream
* @param {String} name the object key
* @param {Mixed} file String(file path)/Buffer/ReadableStream
* @param {Object} options
* @return {Object}
*/
proto.append = async function append(name, file, options) {
options = options || {};
if (options.position === undefined) options.position = '0';
Expand Down Expand Up @@ -226,6 +225,56 @@ proto.list = async function list(query, options) {
};
};

proto.listV2 = async function listV2(query, options) {
const params = this._objectRequestParams('GET', '', options);
params.query = {
'list-type': 2,
...query
};
params.xmlResponse = true;
params.successStatuses = [200];

const result = await this.request(params);
let objects = result.data.Contents;
const that = this;
if (objects) {
if (!Array.isArray(objects)) {
objects = [objects];
}
objects = objects.map(obj => ({
name: obj.Key,
url: that._objectUrl(obj.Key),
lastModified: obj.LastModified,
etag: obj.ETag,
type: obj.Type,
size: Number(obj.Size),
storageClass: obj.StorageClass,
owner: obj.Owner
? {
id: obj.Owner.ID,
displayName: obj.Owner.DisplayName
}
: null
}));
}
let prefixes = result.data.CommonPrefixes || null;
if (prefixes) {
if (!Array.isArray(prefixes)) {
prefixes = [prefixes];
}
prefixes = prefixes.map(item => item.Prefix);
}
return {
res: result.res,
objects,
prefixes,
isTruncated: result.data.IsTruncated === 'true',
keyCount: result.data.KeyCount,
continuationToken: result.data.ContinuationToken || null,
nextContinuationToken: result.data.NextContinuationToken || null
};
};

/**
* Restore Object
* @param {String} name the object key
Expand Down Expand Up @@ -303,18 +352,18 @@ proto._convertMetaToHeaders = function (meta, headers) {
return;
}

Object.keys(meta).forEach((k) => {
Object.keys(meta).forEach(k => {
headers[`x-oss-meta-${k}`] = meta[k];
});
};

proto._deleteFileSafe = function (filepath) {
return new Promise((resolve) => {
fs.exists(filepath, (exists) => {
return new Promise(resolve => {
fs.exists(filepath, exists => {
if (!exists) {
resolve();
} else {
fs.unlink(filepath, (err) => {
fs.unlink(filepath, err => {
if (err) {
debug('unlink %j error: %s', filepath, err);
}
Expand Down
Loading