使用 python 写入 aws lambda 中的 /tmp 目录
Write to /tmp directory in aws lambda with python
目标
我正在尝试将一个 zip 文件写入 python aws lambda 中的 /tmp 文件夹,这样我就可以在压缩之前提取 manipulate,并将其放入 s3 存储桶中。
问题
Os Errno30 只读文件系统
此代码在我的计算机上进行了本地测试,以确保文件在我将其上传到 aws 之前写入我的工作目录。这是我正在尝试使用的代码。
file = downloadFile() #This is api call that returns binary zip object
newFile = open('/tmp/myZip.zip','wb')
newFile.write(file)
extractAll('/tmp/myZip.zip')
这是试图提取 zip 文件的代码
def extractAll(self,source):
with zipfile.ZipFile(source, 'r') as archive:
archive.extractall()
痕迹在这里
[Errno 30] Read-only file system: '/var/task/test-deploy': OSError
Traceback (most recent call last):
File "/var/task/web.py", line 31, in web
performAction(bb, eventBody)
File "/var/task/src/api/web.py", line 42, in performAction
zipHelper.extractAll('/tmp/myZip.zip')
File "/var/task/src/shared/utils/zipfilehelper.py", line 24, in extractAll
archive.extractall()
File "/var/lang/lib/python3.6/zipfile.py", line 1491, in extractall
self.extract(zipinfo, path, pwd)
File "/var/lang/lib/python3.6/zipfile.py", line 1479, in extract
return self._extract_member(member, path, pwd)
File "/var/lang/lib/python3.6/zipfile.py", line 1538, in _extract_member
os.mkdir(targetpath)
OSError: [Errno 30] Read-only file system: '/var/task/test-deploy'
extractAll()
将提取当前目录中的文件,在您的情况下为 /var/task/test-deploy
。
您需要使用os.chdir()
更改当前目录:
import os, zipfile
os.chdir('/tmp')
with zipfile.ZipFile(source, 'r') as archive:
archive.extractall()
更好的是,您可以 create a temporary directory 并在那里提取文件,以避免污染 /tmp
:
import os, tempfile, zipfile
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
with zipfile.ZipFile(source, 'r') as archive:
archive.extractall()
如果你想在解压文件后恢复当前工作目录,可以考虑使用这个装饰器:
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:
yield
finally:
os.chdir(old_work_dir)
with temporary_work_dir():
with zipfile.ZipFile(source, 'r') as archive:
archive.extractall()
目标
我正在尝试将一个 zip 文件写入 python aws lambda 中的 /tmp 文件夹,这样我就可以在压缩之前提取 manipulate,并将其放入 s3 存储桶中。
问题
Os Errno30 只读文件系统
此代码在我的计算机上进行了本地测试,以确保文件在我将其上传到 aws 之前写入我的工作目录。这是我正在尝试使用的代码。
file = downloadFile() #This is api call that returns binary zip object
newFile = open('/tmp/myZip.zip','wb')
newFile.write(file)
extractAll('/tmp/myZip.zip')
这是试图提取 zip 文件的代码
def extractAll(self,source):
with zipfile.ZipFile(source, 'r') as archive:
archive.extractall()
痕迹在这里
[Errno 30] Read-only file system: '/var/task/test-deploy': OSError
Traceback (most recent call last):
File "/var/task/web.py", line 31, in web
performAction(bb, eventBody)
File "/var/task/src/api/web.py", line 42, in performAction
zipHelper.extractAll('/tmp/myZip.zip')
File "/var/task/src/shared/utils/zipfilehelper.py", line 24, in extractAll
archive.extractall()
File "/var/lang/lib/python3.6/zipfile.py", line 1491, in extractall
self.extract(zipinfo, path, pwd)
File "/var/lang/lib/python3.6/zipfile.py", line 1479, in extract
return self._extract_member(member, path, pwd)
File "/var/lang/lib/python3.6/zipfile.py", line 1538, in _extract_member
os.mkdir(targetpath)
OSError: [Errno 30] Read-only file system: '/var/task/test-deploy'
extractAll()
将提取当前目录中的文件,在您的情况下为 /var/task/test-deploy
。
您需要使用os.chdir()
更改当前目录:
import os, zipfile
os.chdir('/tmp')
with zipfile.ZipFile(source, 'r') as archive:
archive.extractall()
更好的是,您可以 create a temporary directory 并在那里提取文件,以避免污染 /tmp
:
import os, tempfile, zipfile
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
with zipfile.ZipFile(source, 'r') as archive:
archive.extractall()
如果你想在解压文件后恢复当前工作目录,可以考虑使用这个装饰器:
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:
yield
finally:
os.chdir(old_work_dir)
with temporary_work_dir():
with zipfile.ZipFile(source, 'r') as archive:
archive.extractall()