diff --git a/publish.yaml b/publish.yaml index e62ecbf..83c6f9a 100644 --- a/publish.yaml +++ b/publish.yaml @@ -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指定目录中。 @@ -94,7 +94,7 @@ Parameters: processedDir: title: 解压目标目录 type: string - default: dst + default: dst/ description: 将匹配到的文件解压到此目标目录下。为防止循环触发产生不必要的费用,建议您设置不同于前缀的目标目录。 retainFileName: title: 是否保留压缩文件名为路径目录 diff --git a/src/code/index.py b/src/code/index.py index a4085d4..c418286 100644 --- a/src/code/index.py +++ b/src/code/index.py @@ -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) @@ -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 ) ) @@ -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) @@ -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) @@ -152,3 +156,4 @@ def handler(event, context): LOGGER.error(e) finally: os.remove(tmpZipfile) + shutil.rmtree(tmpWorkDir) diff --git a/src/code/speed.py b/src/code/speed.py index 823c660..36b0452 100644 --- a/src/code/speed.py +++ b/src/code/speed.py @@ -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) diff --git a/src/s.yaml b/src/s.yaml index 85b4872..8ddaeaf 100644 --- a/src/s.yaml +++ b/src/s.yaml @@ -33,6 +33,7 @@ resources: environmentVariables: PROCESSED_DIR: '{{ processedDir }}' RETAIN_FILE_NAME: '{{ retainFileName }}' + WORK_DIR: "/tmp" internetAccess: true role: '{{ roleArn }}' functionName: '{{ functionName }}' diff --git a/src/variable.yaml b/src/variable.yaml index de360d1..c67353f 100644 --- a/src/variable.yaml +++ b/src/variable.yaml @@ -3,7 +3,7 @@ services: #服务变量 processedDir: title: 解压目标目录 type: string - default: dst + default: dst/ description: 将匹配到的文件解压到此目标目录下。为防止循环触发产生不必要的费用,建议您设置不同于前缀的目标目录。 retainFileName: title: 是否保留压缩文件名为路径目录