Skip to content

Commit

Permalink
feat: listObjectsV2 (#888)
Browse files Browse the repository at this point in the history
* feat: listObjectsV2

* style: trailingComma none
  • Loading branch information
beajer authored Dec 1, 2020
1 parent 29a9ac6 commit f75ad23
Show file tree
Hide file tree
Showing 5 changed files with 720 additions and 294 deletions.
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

0 comments on commit f75ad23

Please sign in to comment.