-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from shimohq/feat-aws-s3-env
feat: 支持 AWS S3 环境变量覆盖文件配置
- Loading branch information
Showing
6 changed files
with
149 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |