Skip to content

Commit

Permalink
Merge pull request #19 from devsapp/add-nas-tips
Browse files Browse the repository at this point in the history
add support nas tips
  • Loading branch information
rsonghuster authored Nov 14, 2024
2 parents 029fc03 + 611ffa0 commit 8662703
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.s
.s
git-statistics.sh
6 changes: 3 additions & 3 deletions publish.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Edition: 3.0.0
Type: Project
Name: start-unzip-oss-v3
Version: 0.0.13
Version: 0.0.14
Provider:
- 阿里云
Description: 本案例是将解压zip文件工具 unzip,快速创建并部署到阿里云函数计算 FC。
Expand Down Expand Up @@ -86,12 +86,12 @@ Parameters:
prefix:
title: 前缀
type: string
default: src
default: src/
description: 配置触发函数计算的文件前缀。您上传指定前缀的ZIP文件或将ZIP文件上传至指定目录会触发函数计算。置空此项则匹配所有上传的ZIP文件,置空此项可能会触发循环执行,建议您配置文件前缀。
processedDir:
title: 解压目标目录
type: string
default: dst
default: dst/
description: 将匹配到的文件解压到此目标目录下。为防止循环触发产生不必要的费用,建议您设置不同于前缀的目标目录。
retainFileName:
title: 是否保留压缩文件名为路径目录
Expand Down
17 changes: 12 additions & 5 deletions src/code/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import logging
import zipfile
import chardet
import shutil

# Close the info log printed by the oss SDK
logging.getLogger("oss2.api").setLevel(logging.ERROR)
Expand Down Expand Up @@ -86,9 +87,11 @@ def handler(event, context):
object_sizeMB = evt["oss"]["object"]["size"] / 1024 / 1024
LOGGER.info("{} size is = {}MB".format(object_name, object_sizeMB))

if object_sizeMB > 10240 * 0.9:
WORK_DIR = os.environ.get("WORK_DIR", "/tmp")

if WORK_DIR == "/tmp" and object_sizeMB > 10240 * 0.9:
raise RuntimeError(
"{} size is too large; please use NAS, refer: https://github.com/zhaohang88/unzip-oss-nas".format(
"{} size is too large; Please use NAS and set the WORK_DIR environment variable to specify the NAS mount directory. For reference, see: https://help.aliyun.com/zh/functioncompute/fc-3-0/user-guide/configure-a-nas-file-system-1".format(
object_name
)
)
Expand Down Expand Up @@ -120,7 +123,7 @@ def handler(event, context):
newKeyPrefix = os.path.join(PROCESSED_DIR, zip_name)
newKeyPrefix = newKeyPrefix.replace(".zip", "/")

tmpWorkDir = "/tmp/{}".format(context.request_id)
tmpWorkDir = "{}/{}".format(WORK_DIR, context.request_id)
if not os.path.exists(tmpWorkDir):
os.makedirs(tmpWorkDir)

Expand All @@ -134,10 +137,13 @@ def handler(event, context):
continue
f_size = file_info.file_size
if (
object_sizeMB + f_size / 1024 / 1024 > 10240 * 0.99
WORK_DIR == "/tmp"
and object_sizeMB + f_size / 1024 / 1024 > 10240 * 0.99
): # if zip file + one file size > 0.99G, skip extract and upload
LOGGER.error(
"{} size is too large; skip extract and upload".format(f)
"{} size is too large; skip extract and upload. Please use NAS and set the WORK_DIR environment variable to specify the NAS mount directory. For reference, see: https://help.aliyun.com/zh/functioncompute/fc-3-0/user-guide/configure-a-nas-file-system-1".format(
file_info.filename
)
)
continue
zip_file.extract(file_info.filename, tmpWorkDir)
Expand All @@ -152,3 +158,4 @@ def handler(event, context):
LOGGER.error(e)
finally:
os.remove(tmpZipfile)
shutil.rmtree(tmpWorkDir)
3 changes: 2 additions & 1 deletion src/code/speed.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def handler(event, context):
newKeyPrefix = os.path.join(PROCESSED_DIR, zip_name)
newKeyPrefix = newKeyPrefix.replace(".zip", "/")

tmpWorkDir = "/tmp/{}".format(context.request_id)
WORK_DIR = os.environ.get("WORK_DIR", "/tmp")
tmpWorkDir = "{}/{}".format(WORK_DIR, context.request_id)
if not os.path.exists(tmpWorkDir):
os.makedirs(tmpWorkDir)

Expand Down
3 changes: 2 additions & 1 deletion src/s.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ access: '{{ access }}'
vars:
region: '{{ region }}'
resources:
fc-zip-oss-service:
fc-unzip-oss-service:
component: fc3
props:
region: ${vars.region}
Expand Down Expand Up @@ -36,6 +36,7 @@ resources:
environmentVariables:
PROCESSED_DIR: '{{ processedDir }}'
RETAIN_FILE_NAME: '{{ retainFileName }}'
WORK_DIR: "/tmp"
internetAccess: true
role: '{{ roleArn }}'
functionName: '{{ functionName }}'
Expand Down

0 comments on commit 8662703

Please sign in to comment.