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: add data indexing API #1255

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
7 changes: 5 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/* eslint max-len: [0] */
// Ignore checking the js file generated by ts
const buckets = ['dataIndex.js'];
const ignorePatterns = ['lib/common/utils/*.js'].concat(buckets.map(item => `lib/common/bucket/${item}`));

module.exports = {
ignorePatterns,
extends: ['airbnb', 'eslint-config-ali/typescript', 'prettier'],
parserOptions: {
ecmaFeatures: {
Expand All @@ -16,7 +20,6 @@ module.exports = {
},
rules: {
indent: ['error', 2],
// override default options
'no-underscore-dangle': [0],
'no-plusplus': [0],
'no-return-await': [0],
Expand Down
128 changes: 128 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ All operation use es7 async/await to implement. All api is async function.
- [.extendBucketWorm(name, wormId, days[, options])](#extendBucketWormname-wormId-days-options)
- [.getBucketWorm(name[, options])](#getBucketWormname-options)
- [.initiateBucketWorm(name, days[, options])](#initiateBucketWormname-days-options)
- Data Indexing
- [.openMetaQuery(bucketName[, options])](#openMetaQueryBucketName-options)
- [.getMetaQueryStatus(bucketName[, options])](#getMetaQueryStatusBucketName-options)
- [.doMetaQuery(bucketName, queryParam[, options])](#doMetaQueryBucketName-queryParam-options)
- [.closeMetaQuery(bucketName[, options])](#closeMetaQueryBucketName-options)

- [Object Operations](#object-operations)
- [.list(query[, options])](#listquery-options)
Expand Down Expand Up @@ -1653,6 +1658,129 @@ Success will return:

---

### .openMetaQuery(bucketName[, options])

Enables the metadata management feature for a bucket.

parameters:

- bucketName {String} bucket name
- [options] {Object} optional args
shungang marked this conversation as resolved.
Show resolved Hide resolved

Success will return:

- status {Number} response status
- res {Object} response info

---

### .getMetaQueryStatus(bucketName[, options])

Queries the information about the metadata index library of a bucket.

parameters:

- bucketName {String} bucket name
- [options] {Object} optional args
shungang marked this conversation as resolved.
Show resolved Hide resolved

Success will return:

- status {Number} response status
- res {Object} response info
- phase {String} the scan type
- state {String} he status of the metadata index library
- createTime {Date} the time when the metadata index library was created
- updateTime {Date} the time when the metadata index library was updated

---

### .doMetaQuery(bucketName, queryParam[, options])

Query files (Objects) that meet the specified conditions and list file information according to the specified fields and sorting method.
shungang marked this conversation as resolved.
Show resolved Hide resolved

parameters:

- bucketName {String} the bucket name
- queryParam {Object} query parameters
- nextToken {String} The token that is used for the next query when the total number of objects exceeds the value of MaxResults. The object information is returned in alphabetical order starting from the value of NextToken. When this operation is called for the first time, set this field to null.
- maxResults {Number} The maximum number of objects to return. Valid values: 0 to 100. If this parameter is not set or is set to 0, 100 objects are returned.
- query {Object} query criteria
- field {String} Field name. Please refer to Appendix: <a href="https://www.alibabacloud.com/help/en/oss/developer-reference/appendix-supported-fields-and-operators" target="_blank">Support List for Fields and Operators</a>.
- value {String} The value of the field.
- operation {String} The operators. Valid values: eq (equal to), gt (greater than), gte (greater than or equal to), lt (less than), lte (less than or equal to), match (fuzzy query), prefix (prefix query), and (AND), or (OR), and not (NOT).
- subQueries {Array} The subquery conditions. Options that are included in this element are the same as those of simple query. You must set subquery conditions only when Operation is set to AND, OR, or NOT.
- sort {String} The field based on which the results are sorted. For more information about the fields that can be sorted. Please refer to Appendix: <a href="https://www.alibabacloud.com/help/en/oss/developer-reference/appendix-supported-fields-and-operators" target="_blank">Support List for Fields and Operators</a>.
- order {String} `asc` | `desc` The order in which you want to sort the queried data. Default value: desc.
- aggregations {Object} The container for the information about aggregate operations.
- field {String} The name of the field. For more information about supported fields and supported operators. Please refer to Appendix: <a href="https://www.alibabacloud.com/help/en/oss/developer-reference/appendix-supported-fields-and-operators" target="_blank">Support List for Fields and Operators</a>.
- operation {String} The operator for aggregate operations. Valid values:min,max,average,sum,count,distinct,group.
- [options] {Object} optional parameters
- [timeout] {Number} the operation timeout (ms)

Success will return:

- status {Number} response status
- res {Object} response info, including
- status {Number} response status
- headers {Object} response headers
- size {Number} response size
- rt {Number} request total use time (ms)
- nextToken {String} the token that is used for the next query when the total number of objects exceeds the value of MaxResults
- files {Array} the container for the information about objects
- aggregations {Array} the container for the information about aggregate operations

---

example:

```js
const result = await store.put('test.txt', 'hello world');
console.log(result);
```

---

###.get(name[, options])

Get the object content.

parameters:

- name {String} object name store on OSS

---

example:

```js
const queryParam = {
maxResults: 2,
query: { operation: 'and', subQueries: [{ field: 'Size', value: '1048575', operation: 'lt' }] },
sort: 'Size',
order: 'asc'
};
const result = await store.doMetaQuery(bucket, queryParam);
console.log(result);
```

---

### .closeMetaQuery(bucketName[, options])

Disables the metadata management feature for a bucket.

parameters:

- bucketName {String} bucket name
- [options] {Object} optional args
shungang marked this conversation as resolved.
Show resolved Hide resolved

Success will return:

- status {Number} response status
- res {Object} response info

---

## Object Operations

All operations function return Promise, except `signatureUrl`.
Expand Down
3 changes: 3 additions & 0 deletions lib/browser/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ merge(proto, require('../common/bucket/getBucketWebsite'));
merge(proto, require('../common/bucket/putBucketWebsite'));
merge(proto, require('../common/bucket/deleteBucketWebsite'));

// bucket data index
merge(proto, require('../common/bucket/dataIndex'));

// lifecycle
merge(proto, require('../common/bucket/getBucketLifecycle'));
merge(proto, require('../common/bucket/putBucketLifecycle'));
Expand Down
59 changes: 59 additions & 0 deletions lib/common/bucket/dataIndex.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
export declare function openMetaQuery(
this: any,
bucketName: string,
options?: {}
): Promise<{
res: any;
status: any;
}>;
export declare function getMetaQueryStatus(
this: any,
bucketName: string,
options?: {}
): Promise<{
res: any;
status: any;
phase: any;
state: any;
createTime: any;
updateTime: any;
}>;
interface ISubQuerie {
field?: string;
value?: string;
operation: string;
subQueries?: ISubQuerie[];
}
interface IAggregation {
field: string;
operation: string;
}
interface IMetaQuery {
nextToken?: string;
maxResults?: number;
query: ISubQuerie;
sort?: string;
order?: 'asc' | 'desc';
aggregations?: IAggregation[];
}
export declare function doMetaQuery(
this: any,
bucketName: string,
queryParam: IMetaQuery,
options?: {}
): Promise<{
res: any;
status: any;
nextToken: any;
files: any;
aggregations: any;
}>;
export declare function closeMetaQuery(
this: any,
bucketName: string,
options?: {}
): Promise<{
res: any;
status: any;
}>;
export {};
146 changes: 146 additions & 0 deletions lib/common/bucket/dataIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.closeMetaQuery = exports.doMetaQuery = exports.getMetaQueryStatus = exports.openMetaQuery = void 0;
/* eslint-disable max-len */
// https://help.aliyun.com/zh/oss/developer-reference/data-indexing
const checkBucketName_1 = require('../utils/checkBucketName');
const obj2xml_1 = require('../utils/obj2xml');
const formatObjKey_1 = require('../utils/formatObjKey');
async function openMetaQuery(bucketName, options = {}) {
checkBucketName_1.checkBucketName(bucketName);
const params = this._bucketRequestParams('POST', bucketName, { metaQuery: '', comp: 'add' }, options);
const result = await this.request(params);
if (result.status === 200) {
return {
res: result.res,
status: result.status
};
}
throw await this.requestError(result);
}
exports.openMetaQuery = openMetaQuery;
async function getMetaQueryStatus(bucketName, options = {}) {
checkBucketName_1.checkBucketName(bucketName);
const params = this._bucketRequestParams('GET', bucketName, 'metaQuery', options);
const result = await this.request(params);
if (result.status === 200) {
const data = await this.parseXML(result.data);
return {
res: result.res,
status: result.status,
phase: data.Phase,
state: data.State,
createTime: data.CreateTime,
updateTime: data.UpdateTime
};
}
throw await this.requestError(result);
}
exports.getMetaQueryStatus = getMetaQueryStatus;
async function doMetaQuery(bucketName, queryParam, options = {}) {
checkBucketName_1.checkBucketName(bucketName);
const params = this._bucketRequestParams('POST', bucketName, { metaQuery: '', comp: 'query' }, options);
const { aggregations } = queryParam;
let Aggregations;
if (aggregations && aggregations.length > 0) {
Aggregations = {
Aggregation: aggregations.map(item => ({ Field: item.field, Operation: item.operation }))
};
}
const paramXMLObj = {
MetaQuery: {
NextToken: queryParam.nextToken,
MaxResults: queryParam.maxResults,
Query: JSON.stringify(formatObjKey_1.formatObjKey(queryParam.query, 'firstUpperCase')),
Sort: queryParam.sort,
Order: queryParam.order,
Aggregations
}
};
params.mime = 'xml';
params.content = obj2xml_1.obj2xml(paramXMLObj, { headers: true, firstUpperCase: true });
const result = await this.request(params);
if (result.status === 200) {
const { NextToken, Files, Aggregations: aggRes } = await this.parseXML(result.data);
let files;
if (Files && Files.File) {
const getFileObject = item => {
var _a, _b;
return {
fileName: item.Filename,
size: item.Size,
fileModifiedTime: item.FileModifiedTime,
ossObjectType: item.OSSObjectType,
ossStorageClass: item.OSSStorageClass,
objectACL: item.ObjectACL,
eTag: item.ETag,
ossTaggingCount: item.OSSTaggingCount,
ossTagging:
(_a = item.OSSTagging) === null || _a === void 0
? void 0
: _a.map(tagging => ({
key: tagging.Key,
value: tagging.Value
})),
ossUserMeta:
(_b = item.OSSUserMeta) === null || _b === void 0
? void 0
: _b.map(meta => ({
key: meta.Key,
value: meta.Value
})),
ossCRC64: item.OSSCRC64,
serverSideEncryption: item.ServerSideEncryption,
serverSideEncryptionCustomerAlgorithm: item.ServerSideEncryptionCustomerAlgorithm
};
};
if (Files.File instanceof Array) {
files = Files.File.map(getFileObject);
} else {
files = [getFileObject(Files.File)];
}
}
let aggList;
if (aggRes) {
const getAggregationObject = item => {
var _a;
return {
field: item.Field,
operation: item.Operation,
value: item.Value,
groups:
(_a = item.Groups) === null || _a === void 0
? void 0
: _a.map(group => ({
value: group.Value,
count: group.Count
}))
};
};
if (aggRes.Aggregation instanceof Array) {
aggList = aggRes.Aggregation.map(getAggregationObject);
} else {
aggList = [getAggregationObject(aggRes.Aggregation)];
}
}
return {
res: result.res,
status: result.status,
nextToken: NextToken,
files,
aggregations: aggList
};
}
throw await this.requestError(result);
}
exports.doMetaQuery = doMetaQuery;
async function closeMetaQuery(bucketName, options = {}) {
checkBucketName_1.checkBucketName(bucketName);
const params = this._bucketRequestParams('POST', bucketName, { metaQuery: '', comp: 'delete' }, options);
const result = await this.request(params);
return {
res: result.res,
status: result.status
};
}
exports.closeMetaQuery = closeMetaQuery;
Loading
Loading