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

6.x to 7.x Feature Migration #1023

Merged
merged 21 commits into from
Dec 21, 2021
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
2 changes: 2 additions & 0 deletions .github/workflows/codeCov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ jobs:
ALI_SDK_STS_SECRET: ${{secrets.ALI_SDK_STS_SECRET}}
steps:
- uses: actions/checkout@v2
with:
ref: 7.x
- name: test and report upload
run: |
npm install
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v2

with:
ref: 7.x
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
Expand Down
10 changes: 10 additions & 0 deletions .github/workflows/node_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ jobs:

steps:
- uses: actions/checkout@v2
with:
ref: 7.x
- name: browser build and test
run: |
curl cip.cc
Expand Down Expand Up @@ -67,6 +69,8 @@ jobs:

steps:
- uses: actions/checkout@v2
with:
ref: 7.x
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
Expand Down Expand Up @@ -100,6 +104,8 @@ jobs:

steps:
- uses: actions/checkout@v2
with:
ref: 7.x
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
Expand Down Expand Up @@ -132,6 +138,8 @@ jobs:

steps:
- uses: actions/checkout@v2
with:
ref: 7.x
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
Expand Down Expand Up @@ -162,6 +170,8 @@ jobs:
node-version: [14.x]
steps:
- uses: actions/checkout@v2
with:
ref: 7.x
- name: clean all bucket
uses: actions/setup-node@v2
with:
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/snyk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ jobs:
environment: ali_oss_AK
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/checkoutV2
with:
ref: 7.x
- run: npm install
- name: Run Snyk to check for vulnerabilities
uses: snyk/actions/node@master
Expand Down
72 changes: 62 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ All operation use es7 async/await to implement. All api is async function.
- [Node Usage](#node-usage)
- [Browser Usage](#browser-usage)
- [Data Regions](#data-regions)
- [Create Account](#create-acount)
- [Create Account](#create-account)
- [Create A Bucket Instance](#create-a-bucket-instance)
- [oss(options)](#ossoptions)
- [Bucket Operations](#bucket-operations)
Expand Down Expand Up @@ -100,9 +100,9 @@ All operation use es7 async/await to implement. All api is async function.
- [.getBucketRequestPayment(bucketName[, options])](#getbucketrequestpaymentbucketname-options)
- [.putBucketRequestPayment(bucketName, payer[, options])](#putBucketRequestpaymentbucketname-payer-options)
- BucketEncryption
- [.putBucketEncryption(name[, options])](#putbucketencryptionbucketname-options)
- [.getBucketEncryption(name)](#getbucketencryptionbucketname-options)
- [.deleteBucketEncryption(name)](#deletebucketencryptionbucketname-options)
- [.putBucketEncryption(name[, rules])](#putbucketencryptionname-rules)
- [.getBucketEncryption(name)](#getbucketencryptionname)
- [.deleteBucketEncryption(name)](#deletebucketencryptionname)
- tagging
- [.putBucketTags(name, tag[, options])](#putBucketTagsname-tag-options)
- [.getBucketTags(name, [, options])](#getBucketTagsname-options)
Expand Down Expand Up @@ -155,7 +155,7 @@ All operation use es7 async/await to implement. All api is async function.
- [.completeMultipartUpload(name, uploadId, parts[, options])](#completemultipartuploadname-uploadid-parts-options)
- [.multipartUpload(name, file[, options])](#multipartuploadname-file-options)
- [.multipartUploadCopy(name, sourceData[, options])](#multipartuploadcopyname-sourcedata-options)
- [.listParts(name, uploadId[, query, options])](#listparts-name-uploadid-query-options)
- [.listParts(name, uploadId[, query, options])](#listpartsname-uploadid-query-options)
- [.listUploads(query[, options])](#listuploadsquery-options)
- [.abortMultipartUpload(name, uploadId[, options])](#abortmultipartuploadname-uploadid-options)
- [.calculatePostSignature(policy)](#calculatePostSignaturepolicy)
Expand Down Expand Up @@ -291,7 +291,7 @@ And see the build artifacts under `dist/`.

## Data Regions

[OSS current data regions](https://help.aliyun.com/document_detail/oss/user_guide/endpoint_region.html).
[OSS current data regions](https://help.aliyun.com/document_detail/31837.html).

region | country | city | endpoint | internal endpoint
--- | --- | --- | --- | ---
Expand Down Expand Up @@ -324,6 +324,9 @@ options:
- accessKeySecret {String} access secret you create
- [stsToken] {String} used by temporary authorization, detail [see](https://www.alibabacloud.com/help/doc-detail/32077.htm)
- [refreshSTSToken] {Function} used by auto set `stsToken`、`accessKeyId`、`accessKeySecret` when sts info expires. return value must be object contains `stsToken`、`accessKeyId`、`accessKeySecret`
[refreshSTSTokenInterval] {number} use time (ms) of refresh STSToken interval it should be
less than sts info expire interval, default is 300000ms(5min)
when sts info expires. return value must be object contains `stsToken`、`accessKeyId`、`accessKeySecret`
- [bucket] {String} the default bucket you want to access
If you don't have any bucket, please use `putBucket()` create one first.
- [endpoint] {String} oss region domain. It takes priority over `region`. Set as extranet domain name, intranet domain name, accelerated domain name, etc. according to different needs. please see [endpoints](https://www.alibabacloud.com/help/doc-detail/31837.htm)
Expand All @@ -340,7 +343,7 @@ options:
`fetch` mode ,else `XMLHttpRequest`
- [enableProxy] {Boolean}, Enable proxy request, default is false.
- [proxy] {String | Object}, proxy agent uri or options, default is null.
- [retryMax] {Number}, used by auto retry send request count when request error is net error or timeout.
- [retryMax] {Number}, used by auto retry send request count when request error is net error or timeout. **_NOTE:_** Not support `put` with stream, `putStream`, `append` with stream because the stream can only be consumed once

example:

Expand Down Expand Up @@ -377,6 +380,37 @@ const store = new OSS({
endpoint: 'your custome domain',
});
```
4. use STS and refreshSTSToken
```js
const OSS = require('ali-oss');
const store = new OSS({
accessKeyId: 'your STS key',
accessKeySecret: 'your STS secret',
stsToken: 'your STS token',
refreshSTSToken: async () => {
const info = await fetch('you sts server');
return {
accessKeyId: info.accessKeyId,
accessKeySecret: info.accessKeySecret,
stsToken: info.stsToken
}
},
refreshSTSTokenInterval: 300000
});
```

5. retry request with stream
```js
for (let i = 0; i <= store.options.retryMax; i++) {
try {
const result = await store.putStream("<example-object>", fs.createReadStream("<example-path>"));
console.log(result);
break; // break if success
} catch (e) {
console.log(e);
}
}
```

## Bucket Operations

Expand All @@ -400,6 +434,7 @@ Success will return buckets list on `buckets` properties.
- name {String} bucket name
- region {String} bucket store data region, e.g.: `oss-cn-hangzhou-a`
- creationDate {String} bucket create GMT date, e.g.: `2015-02-19T08:39:44.000Z`
- storageClass {String} e.g.: `Standard`, `IA`, `Archive`
- owner {Object} object owner, including `id` and `displayName`
- isTruncated {Boolean} truncate or not
- nextMarker {String} next marker string
Expand Down Expand Up @@ -1577,6 +1612,7 @@ parameters:
- 'Content-Encoding' object content encoding for download, e.g.: `Content-Encoding: gzip`
- 'Expires' expires time for download, an absolute date and time. e.g.: `Tue, 08 Dec 2020 13:49:43 GMT`
- See more: [PutObject](https://help.aliyun.com/document_detail/31978.html#title-yxe-96d-x61)
- [disabledMD5] {Boolean} default true, it just work in Browser. if false,it means that MD5 is automatically calculated for uploaded files. **_NOTE:_** Synchronous computing tasks will block the main process

Success will return the object information.

Expand Down Expand Up @@ -2487,7 +2523,7 @@ parameters:
- [content-type] {String} set the response content type
- [content-disposition] {String} set the response content disposition
- [cache-control] {String} set the response cache control
- See more: https://help.aliyun.com/document_detail/oss/api-reference/object/GetObject.html
- See more: <https://help.aliyun.com/document_detail/31980.html>
- [callback] {Object} set the callback for the operation
- url {String} set the url for callback
- [host] {String} set the host for callback
Expand Down Expand Up @@ -2633,6 +2669,7 @@ parameters:
- [options] {Object} optional parameters
- [timeout] {Number} the operation timeout
- [versionId] {String} the version id of history object
- [type] {String} the default type is Archive

Success will return:

Expand All @@ -2644,12 +2681,26 @@ Success will return:

example:

- Restore an object
- Restore an object with Archive type

```js
const result = await store.restore('ossdemo.txt');
console.log(result.status);
```
- Restore an object with ColdArchive type

```js
const result = await store.restore('ossdemo.txt',{type:'ColdArchive'});
console.log(result.status);
```


- Days for unfreezing Specifies the days for unfreezing

```js
const result = await store.restore('ossdemo.txt',{type:'ColdArchive',Days:2});
console.log(result.status);
```

- Restore an history object

Expand Down Expand Up @@ -3028,6 +3079,7 @@ parameters:
- 'Expires' expires time for download, an absolute date and time. e.g.: `Tue, 08 Dec 2020 13:49:43 GMT`
- **NOTE**: Some headers are [disabled in browser][disabled-browser-headers]
- [timeout] {Number} Milliseconds before a request is considered to be timed out
- [disabledMD5] {Boolean} default true, it just work in Browser. if false,it means that MD5 is automatically calculated for uploaded files. **_NOTE:_** Synchronous computing tasks will block the main process

Success will return:

Expand Down Expand Up @@ -4388,5 +4440,5 @@ SecurityTokenExpiredError | SecurityTokenExpired | 403 | sts Security Token Expi
[generator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*
[oss-sts]: https://help.aliyun.com/document_detail/oss/practice/ram_guide.html
[browser-sample]: https://github.com/rockuw/oss-in-browser
[oss-multipart]: https://help.aliyun.com/document_detail/oss/api-reference/multipart-upload/InitiateMultipartUpload.html
[oss-multipart]: https://help.aliyun.com/document_detail/31992.html
[disabled-browser-headers]: https://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader%28%29-method
18 changes: 17 additions & 1 deletion lib/browser/client/_createStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,26 @@ exports._createStream = void 0;
const isBlob_1 = require("../../common/utils/isBlob");
const isFile_1 = require("../../common/utils/isFile");
const isBuffer_1 = require("../../common/utils/isBuffer");
const getBuffer = file => {
// Some browsers do not support Blob.prototype.arrayBuffer, such as IE
if (file.arrayBuffer)
return file.arrayBuffer();
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (e) => {
var _a;
resolve((_a = e.target) === null || _a === void 0 ? void 0 : _a.result);
};
reader.onerror = (e) => {
reject(e);
};
reader.readAsArrayBuffer(file);
});
};
async function _createStream(file, start, end) {
if (isBlob_1.isBlob(file) || isFile_1.isFile(file)) {
const _file = file.slice(start, end);
const fileContent = await _file.arrayBuffer();
const fileContent = await getBuffer(_file);
return Buffer.from(fileContent);
}
else if (isBuffer_1.isBuffer(file)) {
Expand Down
1 change: 1 addition & 0 deletions lib/browser/object/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const isBuffer_1 = require("../../common/utils/isBuffer");
* @return {Object}
*/
async function put(name, file, options = {}) {
options.disabledMD5 = options.disabledMD5 === undefined ? true : !!options.disabledMD5;
let content;
name = objectName_1.objectName(name);
if (isBuffer_1.isBuffer(file)) {
Expand Down
1 change: 1 addition & 0 deletions lib/browser/object/putStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ async function putStream(name, stream, options = {}) {
const params = this._objectRequestParams(method, name, options);
encodeCallback_1.encodeCallback(params, options);
params.mime = options.mime;
params.disabledMD5 = options.disabledMD5;
if (stream.pipe) {
params.stream = stream;
}
Expand Down
1 change: 1 addition & 0 deletions lib/common/bucket/listBuckets.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ async function listBuckets(query = {}, options = {}) {
name: item.Name,
region: item.Location,
creationDate: item.CreationDate,
storageClass: item.StorageClass,
StorageClass: item.StorageClass,
tag: formatTag_1.formatTag(item),
}));
Expand Down
8 changes: 5 additions & 3 deletions lib/common/client/_createRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ function _createRequest(params) {
date = +new Date() + this.options.amendTimeSkewed;
}
const headers = {
'x-oss-date': dateformat_1.default(date, 'UTC:ddd, dd mmm yyyy HH:MM:ss \'GMT\''),
'x-oss-user-agent': this.userAgent
'x-oss-date': dateformat_1.default(date, 'UTC:ddd, dd mmm yyyy HH:MM:ss \'GMT\'')
};
if (typeof window !== 'undefined') {
headers['x-oss-user-agent'] = this.userAgent;
}
if (this.userAgent.includes('nodejs')) {
headers['User-Agent'] = this.userAgent;
}
Expand All @@ -55,7 +57,7 @@ function _createRequest(params) {
delHeader(headers, 'Content-Type');
}
if (params.content) {
if (!headers['Content-MD5']) {
if (!params.disabledMD5) {
headers['Content-MD5'] = crypto_1.default
.createHash('md5')
.update(Buffer.from(params.content, 'utf8'))
Expand Down
4 changes: 4 additions & 0 deletions lib/common/client/initOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ function initOptions(options) {
if (!options || !options.accessKeyId || !options.accessKeySecret) {
throw new Error('require accessKeyId, accessKeySecret');
}
if (options.stsToken && !options.refreshSTSToken && !options.refreshSTSTokenInterval) {
console.warn("It's recommended to set 'refreshSTSToken' and 'refreshSTSTokenInterval' to refresh stsToken、accessKeyId、accessKeySecret automatically when sts info expires");
}
if (options.bucket) {
checkBucketName_1.checkBucketName(options.bucket);
}
Expand All @@ -61,6 +64,7 @@ function initOptions(options) {
useFetch: false,
headerEncoding: 'utf-8',
amendTimeSkewed: 0,
refreshSTSTokenInterval: 60000 * 5,
refreshSTSToken: null,
enableProxy: false,
proxy: null,
Expand Down
4 changes: 4 additions & 0 deletions lib/common/client/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const debug_1 = __importDefault(require("debug"));
const parseXML_1 = require("../utils/parseXML");
const retry_1 = require("../utils/retry");
const setSTSToken_1 = require("../utils/setSTSToken");
const isFunction_1 = require("../utils/isFunction");
const debug = debug_1.default('ali-oss');
/**
* request oss server
Expand All @@ -28,6 +29,9 @@ const debug = debug_1.default('ali-oss');
* @api private
*/
async function _request(params) {
if (this.options.stsToken && isFunction_1.isFunction(this.options.refreshSTSToken)) {
await setSTSToken_1.setSTSToken.call(this);
}
const reqParams = this._createRequest(params);
const isNode = this._getUserAgent().includes('nodejs');
if (!isNode && !this.options.useFetch) {
Expand Down
2 changes: 2 additions & 0 deletions lib/common/multipart/handleUploadPart.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const isStreamLike = (stream) => {
* @param {Object} options
*/
async function handleUploadPart(name, uploadId, partNo, data, options = {}) {
options.disabledMD5 = options.disabledMD5 === undefined ? true : !!options.disabledMD5;
const opt = {};
copy_to_1.default(options, false).to(opt);
opt.headers = opt.headers || {};
Expand All @@ -34,6 +35,7 @@ async function handleUploadPart(name, uploadId, partNo, data, options = {}) {
else {
params.content = data.stream;
}
params.disabledMD5 = opt.disabledMD5;
params.successStatuses = [200];
const result = await this.request(params);
if (!result.res.headers.etag) {
Expand Down
6 changes: 5 additions & 1 deletion lib/common/multipart/resumeMultipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ async function resumeMultipart(checkpoint, options = {}) {
}
else {
// upload in parallel
const jobErr = await _parallel_1._parallel.call(this, todo, parallel, uploadPartJob);
const jobErr = await _parallel_1._parallel.call(this, todo, parallel, value => new Promise((resolve, reject) => {
uploadPartJob(value).then(() => {
resolve();
}).catch(reject);
}));
if (this.isCancel()) {
uploadPartJob = null;
throw _makeCancelEvent_1._makeCancelEvent();
Expand Down
4 changes: 2 additions & 2 deletions lib/common/object/restore.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { MultiVersionCommonOptions } from '../../types/params';
/**
* Restore Object
* @param {String} name the object key
* @param {Object} options
* @param {Object} options {type : Archive or ColdArchive}
* @returns {{res}}
*/
export declare function restore(this: any, name: string, options?: MultiVersionCommonOptions): Promise<{
export declare function restore(this: any, name: string, options: MultiVersionCommonOptions): Promise<{
res: any;
}>;
Loading