Write To /tmp Directory In Aws Lambda With Python
Goal I'm trying to write a zip file to the /tmp folder in a python aws lambda, so I can extract manipulate before zipping, and placing it in s3 bucket. Problem Os Errno30 Read Only
Solution 1:
extractAll()
will extract files in the current directory, which is /var/task/test-deploy
in your case.
You need to use os.chdir()
to change the current directory:
import os, zipfile
os.chdir('/tmp')
with zipfile.ZipFile(source, 'r') asarchive:
archive.extractall()
Even better, you can create a temporary directory and extract the files there, to avoid polluting /tmp
:
import os, tempfile, zipfile
with tempfile.TemporaryDirectory() astmpdir:
os.chdir(tmpdir)
with zipfile.ZipFile(source, 'r') asarchive:
archive.extractall()
If you want to restore the current working directory after extracting the file, consider using this decorator:
import os, tempfile, zipfile, contextlib
@contextlib.context_manager
def temporary_work_dir():
old_work_dir = os.getcwd()
with tempfile.TemporaryDirectory() as tmp_dir:
os.chdir(tmp_dir)
try:
yieldfinally:
os.chdir(old_work_dir)
withtemporary_work_dir():
with zipfile.ZipFile(source, 'r') as archive:
archive.extractall()
Post a Comment for "Write To /tmp Directory In Aws Lambda With Python"