I ran into a little issue today parsing a S3 SQS event that was sent to Lambda via a SQS trigger. I assumed the incoming event to Lambda was 100% of type dict. Given this, I assumed I could pull the bucket name and key using this syntax.
bucketname = event['Records']['body']['Records']['s3']['bucket']['name']
objectname = event['Records']['body']['Records']['s3']['object']['key']
As it turns out the incoming event is not 100% of type dict and I got the following error.
string indices must be integers
The Records after the body ([‘Records’][‘body’]) are of type str. Below is the updated code to pull the bucket name and key from the incoming event.
event_body_records_string = event['Records']['body']
event_body_records_dict = json.loads(event_body_records_string)
bucketname = event_body_records_dict['Records']['s3']['bucaket']['name']
objectname = event_body_records_dict['Records']['s3']['object']['key']
Now everything works out great!!!
I tried to install the Slate python package via PIP today. No good, I ran into the following error.
- Command “python setup.py egg_info” failed with error code 1 in…
The work-around was to bypass https://pypi.org/ and install directly from guthub.
When using the “receive_message” Python Boto function to pull message(s) from a SQS queue, you will always get a response back when the command completes. However, how do you determine if the response you got back actually contains a valid message?
response = sqs.receive_message
if 'Messages' in response:
print("Message on the queue to process")
print("No messages on the queue to process")
Thats about it!!
I just got my Blink(1) in the mail today. Its a cool indicator light that hooks into your USB port.
There is a ton of potential with this device. You can utilize the Blink1Control2 application to setup event polling triggers or just play around with the colors. (e.g. Show red color when a specific email is received)
Or you can enable your device to listen on a specific port, which would allow you to update the device remotely via a curl command. (e.g. Jenkin job failure) You can find the URL summary here.
Another option to using the light is to integrate it into a program, which would allow for more customizations. Pretty easy to do with Python. The Blink Python lib can be found here.
You can do some pretty cool things with tesseract-ocr. Using PyOCR, which is a wrapper for Tesseract, you can generate text from an image using Tesseract.
from wand.image import Image
from PIL import Image as PI
tool = pyocr.get_available_tools()
lang = tool.get_available_languages()
txt_list = tool.image_to_string(
outputFile = open('output.txt', 'w')
for item in txt_list:
outputFile.write("%s" % item)
Another use case I was working on today was rendering the text in a PDF file using Tesseract. I was converting the PDF to an image file first, then performing the above actions to read the text from the new image.
Here are a couple valuable resources I used to complete this little test.
- Installing Tesseract on a RHEL system – http://www.keienberg.com/install-tesseract-3-04-centos-7/ (link)
- Installing PyOCR and other image conversion tools – https://pythontips.com/2016/02/25/ocr-on-pdf-files-using-python/ (link)
Getting all the prerequisites installed was by far the hardest part on this effort.
You can speed up the use of Python Virtual Environments by using “virtualenvwrapper“.
If you are working on Windows, you can use the port “virtualenvwrapper-win“.
Here is an example convenience command provided by this Windows port.
- mkvirtualenv <project_name>
- Combines virtualenv, activate, and other stuff….
Check out this link for a full list of convenience commands provided by this package.
Below are a couple of problems I ran into when writing a Python 2.7 Lambda function that created a file and then uploaded it to S3. (s3.upload_file)
- The file I was creating and writing to in the function was empty in S3 after the upload.
- Turns out I needed the “( )” braces on the Python “close” command. Silly issue, but took my like 20 minutes to figure out….
- In your Lambda function, you need to create your files under /tmp, which is your functions ephemeral storage.
- fileName = ‘/tmp/’ + name