forked from aliyun/ros-templates
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fc-web-file-backend-service.yml
540 lines (540 loc) · 17.1 KB
/
fc-web-file-backend-service.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
ROSTemplateFormatVersion: '2015-09-01'
Description:
en: This template is used to build website file processing services of the serverless
architecture. You can use APIs, SDKs, or Alibaba Cloud Management Console to upload
files to specified OSS buckets or update files. These files can be automatically
compressed or decompressed, and you can obtain information such as MD5 hashed
values and file metadata.
zh-cn: 使用Severless无服务器架构搭建Web网站文件处理服务,用户通过API、SDK或阿里云控制台上传或更新文件到指定OSS存储空间后,可自动实现文件压缩、文件解压、获取文件md5、获取文件元信息等功能。
Parameters:
BucketName:
Type: String
Label:
en: Bucket Name
zh-cn: 存储空间名称
Description:
en: The name must be 3 to 63 bytes in length, The name must start and end with
a lowercase letter or digit.The name can contain only lowercase letters, digits,
and hyphens (-).;<br><b>note:<font color='blue'>A bucket name must be globally
unique within OSS. Bucket names cannot be changed after the bucket is created.</b></font>
zh-cn: 长度为3~63个字符,必须以小写字母或数字开头和结尾,可以包含小写字母、数字和连字符(-);<br><b>注:<font color='blue'>需要全网唯一性,已经存在的不能在创建</b></font>
Default: storfiles
MinLength: 3
MaxLength: 63
BucketAccessControl:
Type: String
Label:
en: Access Control List
zh-cn: 读写权限
Description:
en: '<font color=''blue''><b>Optional values: </b></font><br>[private:<font
color=''green''>All access to the file requires authentication</font>]<br>[public-read:<font
color=''green''>File writes need to be authenticated;Files can be read anonymously</font>]<br>[public-read-write:<font
color=''green''>Anyone (including anonymous visitors) can perform read and
write operators on the files in the bucket</font>]'
zh-cn: '<font color=''blue''><b>可选值: </b></font><br>[private:<font color=''green''>对文件的所有访问操作需要进行身份验证</font>]<br>[public-read:<font
color=''green''>对文件写操作需要进行身份验证;可以对文件进行匿名读</font>]<br>[public-read-write:<font
color=''green''>所有人都可以对文件进行读写操作</font>]'
Default: private
AllowedValues:
- private
- public-read
- public-read-write
BucketCompressFolder:
Type: String
Label:
en: Compression Function Folder
zh-cn: 压缩函数文件目录
Description:
en: The compression function is triggered when a file is uploaded or updated
under this folder
zh-cn: 在该文件夹下上传或更新文件时会触发压缩函数执行
Default: source
BucketDeletionForce:
Type: Boolean
Label:
en: Support for Force Deletion
zh-cn: 支持强制删除
Description:
en: Do you want to force the deletion of files in OSS?
zh-cn: 是否强制删除OSS中的文件
Default: false
SlsProjectName:
Type: String
Label:
en: Project Name
zh-cn: 项目名称
Description:
en: 'Logging service project name; Supports only lowercase letters, numbers,
hyphens (-), and underscores, and must begin and end with lowercase letters
and numbers, with names 3-63 characters in length.<br><b>note: <font color=''blue''>project
name global unique</b></font>'
zh-cn: 日志服务项目名称;只支持小写字母、数字、连字符(-)和下划线(_),必须以小写字母和数字开头和结尾,名称长度为3-63个字符。<br><b>注:<font
color='blue'>项目名称全局唯一</b></font>
Default: sls-fc-storfiles
MinLength: 3
MaxLength: 63
SlsLogStoreName:
Type: String
Label:
en: Log Store Name
zh-cn: 日志库名称
Description:
en: Log store name; only lowercase letters, numbers, hyphens (-), and underscores
are supported, and must begin and end with lowercase letters and numbers,
with names 3-63 characters in length.
zh-cn: 日志库名称;Supports only lowercase letters, numbers, hyphens (-), and underscores,
and must begin and end with lowercase letters and numbers, with names 3-63
characters in length.
Default: logstore-fc-storfiles
MinLength: 3
MaxLength: 63
Resources:
OssBucket:
Type: ALIYUN::OSS::Bucket
Properties:
AccessControl:
Ref: BucketAccessControl
BucketName:
Ref: BucketName
DeletionForce:
Ref: BucketDeletionForce
StorageClass: Standard
Metadata:
ALIYUN::ROS::Designer:
id: cd35e0ea-fbac-48f7-a205-e867bc370b6a
RamRole:
Type: ALIYUN::RAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service:
- fc.aliyuncs.com
- oss.aliyuncs.com
- log.aliyuncs.com
Version: '1'
Policies:
- PolicyDocument:
Statement:
- Action:
- log:PostLogStoreLogs
Effect: Allow
Resource:
- acs:log:*:*:*
- Action:
- fc:*
Effect: Allow
Resource:
- '*'
- Action:
- oss:*
Effect: Allow
Resource:
- '*'
Version: '1'
PolicyName:
Fn::Join:
- '-'
- - StackId
- Ref: ALIYUN::StackId
RoleName:
Fn::Join:
- '-'
- - StackId
- Ref: ALIYUN::StackId
Metadata:
ALIYUN::ROS::Designer:
id: bb33f21c-5dbd-4ff3-aa81-5ab14ac89ca8
SlsProject:
Type: ALIYUN::SLS::Project
Properties:
Description: 创建函数计算服务FC-storfiles的日志服务
Name:
Ref: SlsProjectName
Metadata:
ALIYUN::ROS::Designer:
id: 12798b6b-6b44-4437-8fc5-66f03dc075ea
SlsLogStore:
Type: ALIYUN::SLS::Logstore
Properties:
AppendMeta: true
AutoSplit: true
EnableTracking: false
LogstoreName:
Ref: SlsLogStoreName
MaxSplitShard: 64
PreserveStorage: true
ProjectName:
Ref: SlsProjectName
ShardCount: 2
DependsOn: SlsProject
Metadata:
ALIYUN::ROS::Designer:
id: d05046bc-840b-460e-94fd-515a8186106f
FcService:
Type: ALIYUN::FC::Service
Properties:
Description: 网站文件处理函数计算服务
InternetAccess: true
Role:
Fn::GetAtt:
- RamRole
- Arn
ServiceName:
Fn::Join:
- '-'
- - StackId
- Ref: ALIYUN::StackId
- FC-Service
DependsOn:
- OssBucket
- RamRole
- SlsLogStore
- SlsProject
Metadata:
ALIYUN::ROS::Designer:
id: 29a76ead-b4c2-4642-921f-1a8740121524
FcCompress:
Type: ALIYUN::FC::Function
Properties:
Code:
SourceCode:
Fn::Join:
- ''
- - "# -*- coding: utf-8 -*- \nimport os\nimport oss2 \nimport zipfile\
\ \nimport shutil \nimport time \nimport json \n \ndef handler(event,\
\ context): \n evt = json.loads(event) \n creds = context.credentials\
\ \n auth = oss2.StsAuth(creds.accessKeyId, creds.accessKeySecret,\
\ creds.securityToken) \n endpoint = 'oss-{}.aliyuncs.com'.format(context.region)\
\ \n bucket = oss2.Bucket(auth, endpoint, evt['events'][0]['oss']['bucket']['name'])\
\ \n #your source list \n sourceFolder = \""
- Ref: BucketCompressFolder
- "/\"\n #zip name \n uid = 'ZIP123456' \n tmpdir = '/tmp/download/'\
\ \n \n os.system(\"rm -rf /tmp/*\") \n os.mkdir(tmpdir)\
\ \n \n #download \n for obj in oss2.ObjectIterator(bucket,\
\ prefix=sourceFolder, delimiter = '/') : \n millis = int(round(time.time()\
\ * 1000)) \n if not obj.is_prefix():\n bucket.get_object_to_file(obj.key\
\ , tmpdir + str(millis) + '.jpg') \n \n #zip file \n \
\ zipname = '/tmp/'+uid + '.zip' \n make_zip(tmpdir , zipname)\
\ \n \n #upload \n total_size = os.path.getsize(zipname)\
\ \n part_size = oss2.determine_part_size(total_size, preferred_size\
\ = 128 * 1024) \n \n key = uid + '.zip' \n upload_id =\
\ bucket.init_multipart_upload(key).upload_id \n \n with open(zipname,\
\ 'rb') as fileobj: \n parts = [] \n part_number =\
\ 1 \n offset = 0 \n while offset < total_size: \
\ \n num_to_upload = min(part_size, total_size - offset)\
\ \n result = bucket.upload_part(key, upload_id, part_number,oss2.SizedFileAdapter(fileobj,\
\ num_to_upload)) \n parts.append(oss2.models.PartInfo(part_number,\
\ result.etag)) \n offset += num_to_upload \n \
\ part_number += 1 \n \n bucket.complete_multipart_upload(key,\
\ upload_id, parts) \n \n return total_size \n \n\
\ \ndef make_zip(source_dir, output_filename): \n zipf = zipfile.ZipFile(output_filename,\
\ 'w') \n pre_len = len(os.path.dirname(source_dir)) \n \
\ for parent, dirnames, filenames in os.walk(source_dir): \n \
\ for filename in filenames: \n pathfile = os.path.join(parent,\
\ filename) \n arcname = pathfile[pre_len:].strip(os.path.sep)\
\ \n zipf.write(pathfile, arcname) \n zipf.close()"
FunctionName: compress
Handler: index.handler
Runtime: python2.7
ServiceName:
Fn::GetAtt:
- FcService
- ServiceName
DependsOn:
- FcService
Metadata:
ALIYUN::ROS::Designer:
id: 7809ddc6-a6ea-4397-bd40-4dfc4001059c
FcCompressTrigger:
Type: ALIYUN::FC::Trigger
Properties:
FunctionName:
Fn::GetAtt:
- FcCompress
- FunctionName
InvocationRole:
Fn::GetAtt:
- RamRole
- Arn
ServiceName:
Fn::GetAtt:
- FcService
- ServiceName
SourceArn:
Fn::Join:
- ':'
- - acs:oss
- Ref: ALIYUN::Region
- Ref: ALIYUN::TenantId
- Fn::GetAtt:
- OssBucket
- Name
TriggerConfig:
BucketName:
Ref: BucketName
Events:
- oss:ObjectCreated:PutObject
- oss:ObjectCreated:PostObject
Filter:
Key:
Prefix:
Fn::Join:
- ''
- - Ref: BucketCompressFolder
- /
Suffix: ''
TriggerName: compress_trigger
TriggerType: oss
DependsOn:
- FcCompress
- OssBucket
- RamRole
Metadata:
ALIYUN::ROS::Designer:
id: bb9818cb-2d78-49a3-9911-8248c6754a2e
FcNewFolder:
Type: ALIYUN::FC::Function
Properties:
Code:
SourceCode:
Fn::Join:
- ''
- - "# -*- coding: utf-8 -*- \n\nimport oss2 \nimport json \n \n\
def handler(event, context): \n creds = context.credentials \n\
\ auth = oss2.StsAuth(creds.accessKeyId, creds.accessKeySecret, creds.securityToken)\
\ \n endpoint = 'oss-{}.aliyuncs.com'.format(context.region) \n\
\ bucket = oss2.Bucket(auth, endpoint, \""
- Fn::GetAtt:
- OssBucket
- Name
- "\") \n sourceFolder = \""
- Ref: BucketCompressFolder
- |-
/"
bucket.put_object(sourceFolder, b'')
FunctionName: new_folder
Handler: index.handler
Runtime: python2.7
ServiceName:
Fn::GetAtt:
- FcService
- ServiceName
DependsOn: FcService
Metadata:
ALIYUN::ROS::Designer:
id: 019ef910-6e16-48e0-a43a-7477e1d9b622
FcNewFolderInvoker:
Type: ALIYUN::FC::FunctionInvoker
Properties:
ExecuteVersion: 1
FunctionName:
Fn::GetAtt:
- FcNewFolder
- FunctionName
ServiceName:
Fn::GetAtt:
- FcService
- ServiceName
DependsOn:
- FcNewFolder
- FcService
Metadata:
ALIYUN::ROS::Designer:
id: 023bdf89-e20e-404a-8300-7ced3ea4f0fd
Outputs:
FcServiceName:
Description:
en: Function calculates the service name
zh-cn: 函数计算服务名称
Value:
Fn::GetAtt:
- FcService
- ServiceName
OssBucketDomainName:
Description:
en: OSS Bucket domain name
zh-cn: 存储空间外网访问域名
Value:
Fn::GetAtt:
- OssBucket
- DomainName
OssBucketInternalDomainName:
Description:
en: OSS Bucket internal domain name
zh-cn: 存储空间内网访问域名
Value:
Fn::GetAtt:
- OssBucket
- InternalDomainName
OssBucketName:
Description:
en: OSS Bucket Name
zh-cn: 存储空间名称
Value:
Fn::GetAtt:
- OssBucket
- Name
SlsLogStoreName:
Description:
en: Log store name
zh-cn: 日志存储库名称
Value:
Fn::GetAtt:
- SlsLogStore
- LogstoreName
SlsProjectName:
Description:
en: Log service name
zh-cn: 日志服务名称
Value:
Fn::GetAtt:
- SlsProject
- Name
Metadata:
ALIYUN::ROS::Interface:
ParameterGroups:
- Parameters:
- BucketName
- BucketAccessControl
- BucketCompressFolder
- BucketDeletionForce
Label:
default: OSS
- Parameters:
- SlsProjectName
- SlsLogStoreName
Label:
default: SLS
TemplateTags:
- acs:solution:无服务计算:基于函数计算搭建网站文件处理
ALIYUN::ROS::Designer:
019ef910-6e16-48e0-a43a-7477e1d9b622:
position:
x: 396
y: 530
size:
height: 60
width: 60
z: 0
023bdf89-e20e-404a-8300-7ced3ea4f0fd:
position:
x: 245
y: 530
size:
height: 60
width: 60
z: 0
12798b6b-6b44-4437-8fc5-66f03dc075ea:
position:
x: 935
y: 402
size:
height: 60
width: 60
z: 0
24a0a4b8-c819-412b-aeb9-2c05d41ef484:
source:
id: 023bdf89-e20e-404a-8300-7ced3ea4f0fd
target:
id: 019ef910-6e16-48e0-a43a-7477e1d9b622
z: 1
29a76ead-b4c2-4642-921f-1a8740121524:
position:
x: 635
y: 402
size:
height: 60
width: 60
z: 0
3e5caf58-3785-4f0c-976c-94ee9364495b:
source:
id: 29a76ead-b4c2-4642-921f-1a8740121524
target:
id: d05046bc-840b-460e-94fd-515a8186106f
z: 1
7809ddc6-a6ea-4397-bd40-4dfc4001059c:
position:
x: 397
y: 402
size:
height: 60
width: 60
z: 0
83a43e4f-a44f-47ae-8293-bdcc0e7cca91:
source:
id: 7809ddc6-a6ea-4397-bd40-4dfc4001059c
target:
id: 29a76ead-b4c2-4642-921f-1a8740121524
z: 1
88ad4e7a-41dd-43a9-aa12-5d25a39087a5:
source:
id: 019ef910-6e16-48e0-a43a-7477e1d9b622
target:
id: 29a76ead-b4c2-4642-921f-1a8740121524
z: 1
9daedbeb-076d-4be5-a981-2aa3de9618e2:
source:
id: bb9818cb-2d78-49a3-9911-8248c6754a2e
target:
id: bb33f21c-5dbd-4ff3-aa81-5ab14ac89ca8
z: 1
a02d37c9-1734-4b7e-bb5c-9e676758a2cf:
source:
id: bb9818cb-2d78-49a3-9911-8248c6754a2e
target:
id: cd35e0ea-fbac-48f7-a205-e867bc370b6a
z: 1
b434c954-c17b-4fb6-ad9b-23d5d65685f0:
source:
id: bb9818cb-2d78-49a3-9911-8248c6754a2e
target:
id: 7809ddc6-a6ea-4397-bd40-4dfc4001059c
z: 1
bb33f21c-5dbd-4ff3-aa81-5ab14ac89ca8:
position:
x: 433
y: 185
size:
height: 60
width: 60
z: 0
bb9818cb-2d78-49a3-9911-8248c6754a2e:
position:
x: 249
y: 402
size:
height: 60
width: 60
z: 0
bdf84bb3-8038-4d17-b4fc-0e8aff323fb4:
source:
id: d05046bc-840b-460e-94fd-515a8186106f
target:
id: 12798b6b-6b44-4437-8fc5-66f03dc075ea
z: 1
bf665e33-9ab8-4f2e-87bc-94500c197635:
source:
id: 29a76ead-b4c2-4642-921f-1a8740121524
target:
id: bb33f21c-5dbd-4ff3-aa81-5ab14ac89ca8
z: 1
cd35e0ea-fbac-48f7-a205-e867bc370b6a:
position:
x: 80
y: 402
size:
height: 60
width: 60
z: 0
d05046bc-840b-460e-94fd-515a8186106f:
position:
x: 780
y: 402
size:
height: 60
width: 60
z: 0