Skip to content

Commit

Permalink
Merge pull request #21 from shimohq/feat-aws-s3-env
Browse files Browse the repository at this point in the history
feat: 支持 AWS S3 环境变量覆盖文件配置
  • Loading branch information
luoyjx committed Apr 30, 2020
2 parents 53c2a0a + 09bc66e commit 745faf8
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 32 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@
"OSS_ID": "<Your accessKeyId>",
"OSS_SECRET": "<Your accessKeySecret>",
"OSS_BUCKET": "<Your bucket name>",
"OSS_ENDPOINT": "<oss endpoint>m"
"OSS_ENDPOINT": "<oss endpoint>",
},
"oss": {
"endpoint": "<oss endpoint>",
"accessKeyId": "<Your accessKeyId>",
"accessKeySecret": "<Your accessKeySecret>",
"bucket": "<Your bucket name>"
},
"aws": {
"accessKeyId": "< your access key ID>",
Expand Down
60 changes: 44 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"lodash.isnil": "^4.0.0",
"lodash.isplainobject": "^4.0.6",
"lodash.omitby": "^4.6.0",
"uuid": "^3.3.2"
"uuid": "^3.3.2",
"yn": "^4.0.0"
},
"devDependencies": {
"@types/jest": "^22.0.1",
Expand Down
19 changes: 5 additions & 14 deletions src/bufferSupport/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const omitBy = require('lodash.omitby');
import { sizeof, retryWrapper } from '../common';
import { MAX_RAW_PAYLOAD_SIZE } from '../constants';
import { getClientByType } from '../storage';
import { StorageEngine } from '../types';
import { loadConfigWithEnvs } from '../config';

export type AliyunCallback = (error: any, response: IReplyPayload) => any;
export type OSS_TYPE = 'oss' | 'aws';
Expand Down Expand Up @@ -35,27 +37,16 @@ export interface IReplyPayload {

export function initReceiver(
noOSS: boolean = false,
ossType: OSS_TYPE = 'oss',
ossType: StorageEngine = StorageEngine.ALIYUN_OSS,
ossThreshold: number = 0
): {
receive: (
event: Buffer | string | IReceiveParsedPayload
) => Promise<{ headers?: any; body: any; storeType?: string }>;
reply: replyFunc;
} {
const cwd = process.cwd();
const fcConfig = require(path.join(cwd, './.fc-config.json'));
let storageOptions = fcConfig[ossType] || {};

if (ossType === 'oss' && process.env['OSS_ID']) {
storageOptions = {
accessKeyId: process.env['OSS_ID'],
accessKeySecret: process.env['OSS_SECRET'],
bucket: process.env['OSS_BUCKET'],
endpoint: process.env['OSS_ENDPOINT'],
};
}

const fcConfig = loadConfigWithEnvs();
const storageOptions = fcConfig[ossType] || ({} as any);
const storageClient = getClientByType(ossType, storageOptions);

const receive = async (
Expand Down
57 changes: 57 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import * as path from 'path';
import yn = require('yn');

import { StorageEngine, IConfigurationV1 } from './types';

/**
* 加载配置并通过环境变量覆盖部分配置
*
* @param {StorageEngine} storageEngine 存储引擎类型: oss/aws
* @return {IConfigurationV1}
*/
export function loadConfigWithEnvs(
storageEngine: StorageEngine = StorageEngine.ALIYUN_OSS
): IConfigurationV1 {
const fcConfigWithEnvs = loadConfig();

if (storageEngine === StorageEngine.ALIYUN_OSS && process.env.OSS_ID) {
fcConfigWithEnvs.oss = {
accessKeyId: process.env.OSS_ID || 'no access id',
accessKeySecret: process.env.OSS_SECRET || 'no access key secret',
bucket: process.env.OSS_BUCKET || 'your bucket name',
endpoint: process.env.OSS_ENDPOINT || 'some oss endpoint',
};
} else if (storageEngine === StorageEngine.AWS_S3) {
fcConfigWithEnvs.aws = fcConfigWithEnvs.aws || ({} as any);
fcConfigWithEnvs.aws!.accessKeyId =
process.env.AWS_S3_ID || fcConfigWithEnvs.aws!.accessKeyId;
fcConfigWithEnvs.aws!.secretAccessKey =
process.env.AWS_S3_SECRET || fcConfigWithEnvs.aws!.secretAccessKey;
fcConfigWithEnvs.aws!.bucket =
process.env.AWS_S3_BUCKET_FC_TASK || fcConfigWithEnvs.aws!.bucket;
fcConfigWithEnvs.aws!.region =
process.env.AWS_S3_REGION || fcConfigWithEnvs.aws!.region;
fcConfigWithEnvs.aws!.endpoint =
process.env.AWS_S3_ENDPOINT || fcConfigWithEnvs.aws!.endpoint;
// 如果需要使用官方 S3,在环境变量中设为 false
fcConfigWithEnvs.aws!.s3ForcePathStyle = process.env.AWS_S3_FORCE_PATH_STYLE
? yn(process.env.AWS_S3_FORCE_PATH_STYLE)
: fcConfigWithEnvs.aws!.s3ForcePathStyle;
}

return fcConfigWithEnvs;
}

/**
* 从配置文件加载配置
*
* @return {IConfigurationV1}
*/
export function loadConfig(): IConfigurationV1 {
const cwd = process.cwd();
const fcConfig: IConfigurationV1 = require(path.join(
cwd,
'./.fc-config.json'
));
return fcConfig;
}
34 changes: 34 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { IOSSOptions } from 'awos-js/lib/oss';
import { IAWSOptions } from 'awos-js/lib/aws';

// 存储引擎类型
export enum StorageEngine {
ALIYUN_OSS = 'oss',
AWS_S3 = 'aws',
}

export interface IConfigurationV1 {
serviceName: string;
handler: string;
memorySize: number;
runtime: string;
timeout: number;
fc: IFcConfigurationV1;
aws?: IAWSOptions;
oss?: IOSSOptions;
EnvironmentVariables?: IEnvVars;
}

export interface IFcConfigurationV1 {
accountId: string;
accessKeyID: string;
accessKeySecret: string;
region: string;
timeout: number;
}

export interface IEnvVars {
[envKey: string]: string;
}

export type Nullable<T> = T | undefined;

0 comments on commit 745faf8

Please sign in to comment.