Wednesday, 15 July 2020

Build Your Own Twitter Bot - Reading Images from S3


<< Selecting a Tweet                                            Table of Contents                                                     Deployment >>

In this post, I will show you how to read an image from an S3 bucket so that we can attach it to our Tweet.

In S3, create a new bucket called dynamodb-occulttwitterbot-imagestore

In our TwitterBotService project, create a class called S3ImageService and add the following method:



We use the TransferUtility class to open a Stream to the image located in our bucket. We then read this Stream into a MemoryStream instance.

We use the MemoryStream so that we can handle the file in memory, there is no need to save it as a physical file. We simply want to read the file from S3 and upload it straight to Twitter.


We will use the Tweetinvi library for uploading an image to Twitter. We will use Upload.UploadBinary which accepts a byte array. This allows us to use MemoryStream.ToArray() to return us the full byte array of the whole image.

In our OccultTwitterBot project, we use the S3ImageService to get our image stream:



The ImageKey is an attribute in our DynamoDb table which holds the name of the image.

<< Selecting a Tweet                                            Table of Contents                                                     Deployment >>

No comments:

Post a Comment