Write to /tmp directory in aws lambda with python
Asked Answered
D

1

26

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 FileSystem

This code was tested locally on my computer to make sure the file would write to my working directory before I uploaded it to aws. This is the code i'm trying to use.

file = downloadFile() #This is api call that returns binary zip object
newFile = open('/tmp/myZip.zip','wb')
newFile.write(file)
extractAll('/tmp/myZip.zip')

here is the code that is trying to extract the zip file

def extractAll(self,source):
        with zipfile.ZipFile(source, 'r') as archive:
            archive.extractall()

here is the trace

[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'
Dimpledimwit answered 21/1, 2018 at 6:25 Comment(6)
In the first line, where are you downloading this file from/to? Lambda has temporary storage only at /tmp. Make sure you are downloading it to /tmp.Occurrence
How do you know downloadFile is trying to store in /tmp/ ?Posology
downloadFile() is getting a binary object from another apiDimpledimwit
I was assuming '/tmp/myZip.zip' the /tmp/ in my destination would resolve to the tmp storage for the lambdaDimpledimwit
Please add the full error traceback and the code of downliadFile() to your question!Lyricism
So actually looks like it is breaking on the extractDimpledimwit
D
31

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') as archive:
    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() as tmpdir:
    os.chdir(tmpdir)
    with zipfile.ZipFile(source, 'r') as archive:
        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:
            yield
        finally:
            os.chdir(old_work_dir)

with temporary_work_dir():
    with zipfile.ZipFile(source, 'r') as archive:
        archive.extractall()
Devilment answered 21/1, 2018 at 7:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.