Skip to content

Commit 5efbfd9

Browse files
committed
feat: init oss resources creation
Signed-off-by: seven <[email protected]>
1 parent f7d8de8 commit 5efbfd9

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

package-lock.json

Lines changed: 28 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"@alicloud/ros-cdk-apigateway": "^1.4.0",
5454
"@alicloud/ros-cdk-core": "^1.4.0",
5555
"@alicloud/ros-cdk-fc3": "^1.4.0",
56+
"@alicloud/ros-cdk-oss": "^1.4.0",
5657
"@alicloud/ros-cdk-ram": "^1.4.0",
5758
"@alicloud/ros20190910": "^3.5.0",
5859
"ajv": "^8.17.1",
@@ -67,8 +68,8 @@
6768
"devDependencies": {
6869
"@types/i18n": "^0.13.12",
6970
"@types/jest": "^29.5.13",
70-
"@types/node": "^22.7.4",
7171
"@types/lodash": "^4.17.12",
72+
"@types/node": "^22.7.4",
7273
"@typescript-eslint/eslint-plugin": "^8.8.0",
7374
"@typescript-eslint/parser": "^8.8.0",
7475
"eslint": "^8.57.1",

src/common/iacHelper.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ export const resolveCode = (location: string): string => {
99

1010
return fileContent.toString('base64');
1111
};
12+
export const readCodeSize = (location: string): number => {
13+
const filePath = path.resolve(process.cwd(), location);
14+
const stats = fs.statSync(filePath);
15+
return stats.size;
16+
};
1217

1318
const evalCtx = (value: string, ctx: ActionContext): string => {
1419
const containsStage = value.match(/\$\{ctx.\w+}/);

src/stack/iacStack.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { ActionContext, EventTypes, ServerlessIac } from '../types';
44
import * as fc from '@alicloud/ros-cdk-fc3';
55
import * as ram from '@alicloud/ros-cdk-ram';
66
import * as agw from '@alicloud/ros-cdk-apigateway';
7-
import { replaceReference, resolveCode } from '../common';
7+
import * as oss from '@alicloud/ros-cdk-oss';
8+
import { readCodeSize, replaceReference, resolveCode } from '../common';
89

910
export class IacStack extends ros.Stack {
1011
private readonly service: string;
@@ -41,7 +42,29 @@ export class IacStack extends ros.Stack {
4142
replaceReference(`${this.service} stack`, context),
4243
);
4344

45+
const codeSize = iac.functions.find(({ code }) => readCodeSize(code) > 15 * 1024 * 1024);
46+
if (codeSize) {
47+
// creat oss to store code
48+
new oss.Bucket(this, replaceReference(`${this.service}_code_object_store`, context), {
49+
bucketName: replaceReference(`${this.service}-code-object-store`, context),
50+
serverSideEncryptionConfiguration: { sseAlgorithm: 'KMS' },
51+
});
52+
}
53+
4454
iac.functions.forEach((fnc) => {
55+
if (codeSize && readCodeSize(fnc.code) > 15 * 1024 * 1024) {
56+
// upload code to oss
57+
new oss.RosBucketObject(
58+
this,
59+
replaceReference(`${this.service}_code_object`, context),
60+
{
61+
bucket: replaceReference(`${this.service}-code-object-store`, context),
62+
objectName: replaceReference(fnc.code, context),
63+
source: resolveCode(fnc.code),
64+
},
65+
true,
66+
);
67+
}
4568
new fc.RosFunction(
4669
this,
4770
fnc.key,

0 commit comments

Comments
 (0)