Skip to content

Commit

Permalink
refactor: refactor configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
duanlv committed Jan 18, 2024
1 parent c99d2f8 commit 45159eb
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 257 deletions.
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "es5"
}
37 changes: 32 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ const AWOS = require('awos-js')
### for Aliyun OSS

```javascript
import { build } from 'awos-js'

// for v3.*.*
const client = build({
storageType: 'oss',
accessKeyID: "xxx",
accessKeySecret: "xxx",
bucket: "my_bucket",
endpoint: 'endpoint',
})

// for v2.*.*
const client = new AWOS.Client({
type: 'oss',
ossOptions: {
Expand All @@ -43,11 +55,26 @@ const client = new AWOS.Client({
})
```

### for Amazon S3(minio)
### for AWS-S3 / MINIO

```javascript
import { build } from 'awos-js'

// for v3.*.*
const client = build({
storageType: 'aws',
accessKeyID: "xxx",
accessKeySecret: "xxx",
// when use aws s3, endpoint is unnecessary and region must be set
endpoint: "https://xxxx.myminio.com",
bucket: "my_bucket",
// when use minio, S3ForcePathStyle must be set true
s3ForcePathStyle: true,
})

// For v2.*.*
const client = new AWOS.Client({
type: 'aws',
storageType: 'aws',
awsOptions: {
accessKeyId: 'accessKeyId',
secretAccessKey: 'secretAccessKey',
Expand Down Expand Up @@ -77,6 +104,9 @@ copy(key: string, source: string, options?: ICopyObjectOptions): Promise<void>;
```

### Change Log
- v3.0.0 / 2024-01-18
- **[Breaking]** refactor configuration options ⚠️
- support common prefix configuration

- v2.0.0 / 2020-06-18
- Breaking
Expand All @@ -94,6 +124,3 @@ copy(key: string, source: string, options?: ICopyObjectOptions): Promise<void>;

- v1.0.1 / 2019-03-19
- bug fix: oss listObject() should return [] when options.prefix not exist in the bucket; oss listObject() maxKeys not working



8 changes: 4 additions & 4 deletions __tests__/aws.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const _ = require('lodash');

const prefix = 'test-awos-multi';
const client = new AWS({
accessKeyId: process.env.AWS_ID!,
secretAccessKey: process.env.AWS_SECRET!,
accessKeyID: process.env.AWS_ID!,
accessKeySecret: process.env.AWS_SECRET!,
bucket: process.env.AWS_BUCKET!,
endpoint: process.env.ENDPOINT,
endpoint: process.env.ENDPOINT!,
s3ForcePathStyle: true,
prefix,
});
Expand Down Expand Up @@ -90,7 +90,7 @@ it('should copy() works fine', async () => {
});

it('should get() works fine', async () => {
const res = await client.get(key, ['length']) as IGetObjectResponse;
const res = (await client.get(key, ['length'])) as IGetObjectResponse;
expect(res.content).toEqual(content);
expect(res.meta.get('length')).toEqual(String(content.length));
expect(res.headers['content-type']).toEqual(contentType);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "awos-js",
"version": "2.1.4",
"version": "3.0.0",
"description": "AWOS: Wrapper For OSS And AWS(MINIO) SDK",
"main": "./lib/index.js",
"typings": "./lib/index.d.ts",
Expand All @@ -9,7 +9,7 @@
],
"scripts": {
"clean": "rimraf lib",
"format": "prettier --write \"{src,__tests__}/**/*.ts\" --single-quote --trailing-comma es5",
"format": "prettier --write \"{src,__tests__}/**/*.ts\"",
"lint": "tslint --force --format verbose \"src/**/*.ts\"",
"prebuild": "npm run clean && npm run format && npm run lint && echo Using TypeScript && tsc --version",
"prepublish": "npm run build",
Expand Down
134 changes: 0 additions & 134 deletions src/awos.ts

This file was deleted.

59 changes: 18 additions & 41 deletions src/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
IListObjectV2Output,
ICopyObjectOptions,
IHeadOptions,
ICommonClientOptions,
} from './types';
import * as _ from 'lodash';
import { AbstractClient } from './client';
Expand All @@ -25,17 +26,11 @@ const STANDARD_HEADERS_KEYMAP = {
LastModified: 'last-modified',
};

export interface IAWSOptions {
accessKeyId: string;
secretAccessKey: string;
bucket: string;
endpoint?: string;
shards?: string[];
export interface IAWSOptions extends ICommonClientOptions {
s3ForcePathStyle?: boolean;
region?: string;
signatureVersion?: string;
prefix?: string;
[key: string]: any;
signatureVersion?: string;
}

const DefaultSignatureVersion = 'v4';
Expand All @@ -44,53 +39,35 @@ export default class AWSClient extends AbstractClient {
private client: AWS.S3;

constructor(options: IAWSOptions) {
const bucket =
options.shards && options.shards.length
? options.shards
: [options.bucket];
super({
prefix: options.prefix || '',
bucket,
});
super(options);

const awsClientOptions: AWS.S3.Types.ClientConfiguration = {
accessKeyId: options.accessKeyID,
secretAccessKey: options.accessKeySecret,
signatureVersion: options.signatureVersion || DefaultSignatureVersion,
};
const s3ForcePathStyle = !!options.s3ForcePathStyle;

['accessKeyId', 'secretAccessKey', 'bucket'].forEach(key => {
assert(options[key], `options.${key} required`);
});

// use minio
if (s3ForcePathStyle) {
// minio
assert(
options.endpoint,
'options.endpoint is required when options.s3ForcePathStyle = true'
);
this.client = new AWS.S3({
accessKeyId: options.accessKeyId,
secretAccessKey: options.secretAccessKey,
endpoint: options.endpoint,
region: options.region || 'cn-north-1',
signatureVersion: options.signatureVersion || DefaultSignatureVersion,
s3ForcePathStyle,
});
}
// use aws s3
else {
awsClientOptions.endpoint = options.endpoint;
awsClientOptions.region = options.region || 'cn-north-1';
awsClientOptions.s3ForcePathStyle = true;
} else {
// aws s3
assert(
options.region,
'options.region is required when options.s3ForcePathStyle = false'
);
const s3Options: any = {
accessKeyId: options.accessKeyId,
secretAccessKey: options.secretAccessKey,
region: options.region,
signatureVersion: options.signatureVersion || DefaultSignatureVersion,
};
awsClientOptions.region = options.region;
if (options.endpoint) {
s3Options.endpoint = options.endpoint;
awsClientOptions.endpoint = options.endpoint;
}
this.client = new AWS.S3(s3Options);
}
this.client = new AWS.S3(awsClientOptions);
}

protected async _get(
Expand Down
Loading

0 comments on commit 45159eb

Please sign in to comment.