Skip to content

Commit

Permalink
Merge pull request #20 from devsapp/fix/add-nas-tips
Browse files Browse the repository at this point in the history
add support nas tips
  • Loading branch information
rsonghuster authored Nov 15, 2024
2 parents 7c7cbd6 + e27ee82 commit 3d644c1
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
4 changes: 2 additions & 2 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-cap
Version: 0.0.7
Version: 0.0.8
Provider:
- 阿里云
Description: 本案例展示了如何将ZIP文件进行解压,快速创建并部署到云原生应用开发平台 CAP。匹配解压规则的ZIP文件在上传到OSS,会自动触发解压,解压完成后存储至OSS指定目录中。
Expand Down Expand Up @@ -94,7 +94,7 @@ Parameters:
processedDir:
title: 解压目标目录
type: string
default: dst
default: dst/
description: 将匹配到的文件解压到此目标目录下。为防止循环触发产生不必要的费用,建议您设置不同于前缀的目标目录。
retainFileName:
title: 是否保留压缩文件名为路径目录
Expand Down
15 changes: 10 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,11 @@ 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(f)
)
continue
zip_file.extract(file_info.filename, tmpWorkDir)
Expand All @@ -152,3 +156,4 @@ def handler(event, context):
LOGGER.error(e)
finally:
os.remove(tmpZipfile)
shutil.rmtree(tmpWorkDir)
4 changes: 3 additions & 1 deletion src/code/speed.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ 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
1 change: 1 addition & 0 deletions src/s.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ resources:
environmentVariables:
PROCESSED_DIR: '{{ processedDir }}'
RETAIN_FILE_NAME: '{{ retainFileName }}'
WORK_DIR: "/tmp"
internetAccess: true
role: '{{ roleArn }}'
functionName: '{{ functionName }}'
Expand Down
2 changes: 1 addition & 1 deletion src/variable.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ services: #服务变量
processedDir:
title: 解压目标目录
type: string
default: dst
default: dst/
description: 将匹配到的文件解压到此目标目录下。为防止循环触发产生不必要的费用,建议您设置不同于前缀的目标目录。
retainFileName:
title: 是否保留压缩文件名为路径目录
Expand Down

0 comments on commit 3d644c1

Please sign in to comment.