Aydrian Howard

4 results

Using AWS Rekognition to Analyse and Tag Uploaded Images Using Stitch

Computers can now look at a video or image and know what’s going on and, sometimes, who’s in it. Amazon Web Service Rekognition gives your applications the eyes it needs to label visual content. In the following, you can see how to use Rekognition along with MongoDB Stitch to supplement new content with information as it is inserted into the database. You can easily detect labels or faces in images or videos in your MongoDB Stitch application using the built-in AWS service. Just add the AWS service and use the Stitch client to execute the AWS SES request right from your React.js application or create a Stitch function and Trigger. In a recent Stitchcraft live coding session on my Twitch channel , I wanted to tag an image using label detection . I set up a trigger that executed a function after an image was uploaded to my S3 bucket and its metadata was inserted into a collection. exports = function(changeEvent) { const aws = context.services.get('AWS'); const mongodb = context.services.get("mongodb-atlas"); const insertedPic = changeEvent.fullDocument; const args = { Image: { S3Object: { Bucket: insertedPic.s3.bucket, Name: insertedPic.s3.key } }, MaxLabels: 10, MinConfidence: 75.0 }; return aws.rekognition() .DetectLabels(args) .then(result => { return mongodb .db('data') .collection('picstream') .updateOne({_id: insertedPic._id}, {$set: {tags: result.Labels}}); }); }; With just a couple of service calls, I was able to take an image, stored in S3, analyse it with Rekognition, and add the tags to its document. Want to see how it all came together? Watch the recording on YouTube with the Github repo in the description. Follow me on Twitch to join me and ask questions live. -Aydrian Howard Developer Advocate NYC @aydrianh

October 25, 2018

Sending Emails With MongoDB & AWS SES

Email is king of communication. Most grandparents have an email account, so if you are looking for a communication channel with the most coverage, it’s going to be email. But sending an email from an application is not fun. Many transactional email services, for security reasons, will not allow you to make a request to their APIs from a front end application. This requires you to support a backend application to handle these transactions. If you are looking to host a simple Contact Form or share content from your website, setting up a Node.js application, configuring REST routes with Express.js, and deploying that somewhere would be overkill. The built-in AWS service from MongoDB Stitch makes it easy to send a transactional email using the Simple Email Service (SES) . Just add the AWS service , configure SES , and use the Stitch client to execute the AWS SES request right from your React.js application. I created the following function in a recent Stitchcraft live coding session on my Twitch channel . share = async (entry, email) => { const args = { Destination: { ToAddresses: [email] }, Message: { Body: { Html: { Charset: 'UTF-8', Data: ` <h1>Enjoy this pic!</h1> <img src="${entry.url}" /> ` } }, Subject: { Charset: 'UTF-8', Data: `Picture shared by ${entry.owner_name}` } }, Source: 'picstream@ses.aydrian.me' } const request = new AwsRequest.Builder() .withService('ses') .withAction('SendEmail') .withRegion('us-east-1') .withArgs(args) .build() return this.aws.execute(request) } With these few lines of code, I was able to take an image, stored in S3 and email it to the specified email. Wasn’t able to see it live? Watch the recording on YouTube with the Github repo in the description. Follow me on Twitch to join me and ask questions live. -Aydrian Howard Developer Advocate NYC @aydrianh

October 2, 2018

Reacting to Auth Events using Stitch Triggers

MongoDB Stitch makes it easy to add authentication to your application. Several authentication providers are available to be configured using the Stitch Admin Console. Recently, authorization triggers were added to Stitch. Functions can now be executed based on authorization events such as user creation, deletion, and login. During my Stitchcraft live coding sessions , I’ve been creating an Instagram-like application that uses Google Authentication . The Google authentication provider can be configured to return metadata with the authenticated user. I set up my provider to retrieve the user’s email, name, and profile picture. This works well as long as only the authenticated users need to see it. If you want other users to be able to access this data, you’re going to have to write it to a collection. Before authorization triggers, this could have been an arduous task. Now it’s as simple as executing a function to perform an insert on the CREATE operation. Because I wanted to also ensure that the data in this collection stayed up-to-date, I created authorization triggers for CREATE and LOGIN and pointed them to a single upsert function as seen below. exports = function(authEvent) { const mongodb = context.services.get("mongodb-atlas"); const users = mongodb.db('data').collection('users'); const { user, time } = authEvent; const newUser = { user_id: user.id, last_login: time, full_name: user.data.name, first_name: user.data.first_name, last_name: user.data.last_name, email: user.data.email, picture: user.data.picture }; return users.updateOne({user_id: user.id}, newUser, {upsert: true}); }; During the last Stitchcraft session, I set up this authorization trigger and a database trigger that watched for changes to the full_name field. Check out the recording with the GitHub repo linked in the description. Follow me on Twitch and be notified of future Stitchcraft live coding sessions. -Aydrian Howard Developer Advocate NYC @aydrianh Creating your first Stitch app? Start with one of the Stitch tutorials . Want to learn more about MongoDB Stitch? Read the white paper .

September 25, 2018