0

I have a rar file, which I want to unrar in AWS lambda using python. I learned about unrar library in python. I have created the layer for unrar in AWS. But now when I execute the code

from unrar import rarfile
rar = rarfile.RarFile('file.rar')
rar.extractall()

I got the following error

Couldn't find path to unrar library.

I read the solution. According to the solution, I have to set the environment variable. can I use this solution for AWS Lambda also? and what other solutions can be possible.

Vikku
  • 31
  • 4
  • 1
    How did you install `unrar` (and the `unrar` native library you need to compile by hand) into the AWS Lambda environment..? – AKX Sep 21 '22 at 05:13
  • with this command `docker run -v "$PWD":/var/task "lambci/lambda:build-python3.7" /bin/sh -c "pip install unrar -t python/lib/python3.7/site-packages/; exit"` then I zipped it and upload to layer – Vikku Sep 21 '22 at 05:50
  • 1
    So you didn't install the native library at all? That's likely why it isn't there. – AKX Sep 21 '22 at 05:59
  • okk! so how to install native unrar library in lambda? – Vikku Sep 21 '22 at 07:30
  • The library installation instructions are on the unrar PyPI page. Now you'll just have to come up with a build environment that's ABI compatible with Lambda. Good luck! – AKX Sep 21 '22 at 07:38
  • Thank you @AKX. I figured out, how unrar will work with lambda environment. – Vikku Sep 28 '22 at 01:40

1 Answers1

0

Firstly, I followed documentation of unrar. Steps I followed

  1. download source file, unzip it. (it will create a folder say unrar)
  2. cd unrar (go to folder)
  3. make lib && make install-lib
  4. it will create libunrar.so file at /usr/lib location in linux.

Python unrar could not find this file in Linux path, which is why It was throwing Couldn't find path to unrar library.

Now, I created a lambda layer for this file.

  1. create a folder named lib and copy/paste this libunrar.so in that folder.
  2. create a zip of lib and upload it to a lambda layer
  3. attach this layer to your lambda

Finally, set an environment variable in lambda UNRAR_LIB_PATH to libunrar.so

Vikku
  • 31
  • 4