A random image generator using Unsplash API

For a side project I am currently working on I needed a simple random image generator. I was recommended to take a look at  unsplash.com/random, and it was exactly what I wanted!

But, it worked not exactly how I expected.

It opens images in the browser perfectly fine, but the  Unsplash random image API returns a JSON object with lots of extra information about a resource, instead of a binary blob.

The example below is not the complete JSON object from a response. It is a part that includes URLs to different sizes of the image.

{
 "id": "oyF9pqUz7hg",
 "created_at": "2021-02-13T03:42:49-05:00",
 "urls": {
   "raw": "https://images.unsplash.com/photo-1613205300259-6104fcc6ebfb?ixid=MnwyMTM2Njh8MHwxfHJhbmRvbXx8fHx8fHx8fDE2MTUzOTI0ODE&ixlib=rb-1.2.1",
   "full": "https://images.unsplash.com/photo-1613205300259-6104fcc6ebfb?crop=entropy&cs=srgb&fm=jpg&ixid=MnwyMTM2Njh8MHwxfHJhbmRvbXx8fHx8fHx8fDE2MTUzOTI0ODE&ixlib=rb-1.2.1&q=85",
   "regular": "https://images.unsplash.com/photo-1613205300259-6104fcc6ebfb?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwyMTM2Njh8MHwxfHJhbmRvbXx8fHx8fHx8fDE2MTUzOTI0ODE&ixlib=rb-1.2.1&q=80&w=1080",
   "small": "https://images.unsplash.com/photo-1613205300259-6104fcc6ebfb?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwyMTM2Njh8MHwxfHJhbmRvbXx8fHx8fHx8fDE2MTUzOTI0ODE&ixlib=rb-1.2.1&q=80&w=400",
   "thumb": "https://images.unsplash.com/photo-1613205300259-6104fcc6ebfb?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwyMTM2Njh8MHwxfHJhbmRvbXx8fHx8fHx8fDE2MTUzOTI0ODE&ixlib=rb-1.2.1&q=80&w=200"
 }
}

I can request one of these images and then format it to a binary response. So I decided to create a Lambda REST API Serverless application that fetches a resource from Unsplash and returns a binary representation of the image instead of a JSON object.

Here you can learn from my experience on how to configure a Lambda function that returns a jpeg image accessible via static URL provided by Amazon API Gateway.

#aws #image #lambda #binary

Serve images with AWS Lambda and Node.js
1.35 GEEK