Issue
I am currently trying to save a word cloud I am generating in my AWS lambda function to my s3 bucket, my code executes gut gives "errorMessage": "Parameter validation failed:\nInvalid type for parameter Body, value: <wordcloud.wordcloud.WordCloud object at 0x7f36643fce10>, type: <class 'wordcloud.wordcloud.WordCloud'>, valid types: <class 'bytes'>, <class 'bytearray'>, file-like object",
as an error, I have looked online and cant seem to find the cause, do I need to to convert the plot to bytes to be able to store it in S3 like this?
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
import boto3
text = "cat cat cat dog dog dog test test hello one two three four five"
generate_word_cloud(text)
def generate_word_cloud(text):
wordcloud = WordCloud(width = 3000,
height = 2000,
random_state=1,
background_color='salmon',
colormap='Pastel1',
collocations=False,
stopwords = STOPWORDS).generate(text)
save_to_bucket(wordcloud)
def save_to_bucket(wordcloud):
#Save tweet list to an s3 bucket
BUCKET_NAME = "1706224-tweets"
FILE_NAME = "wordcloud.png"
s3 = boto3.resource('s3')
object = s3.Object(BUCKET_NAME, FILE_NAME)
object.put(Body=wordcloud)
Solution
In the end I used the /tmp storage from lambda to store the image temporarily, then uploaded the image after
wordcloud.to_file("/tmp/"+FILE_NAME)
s3 = boto3.client('s3')
s3.upload_file("/tmp/"+FILE_NAME,BUCKET_NAME,FILE_NAME)
Answered By - Charlie
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.