Uploading images/videos with functions

I know that we should use AWS or Firebase Storage for storing service.

I already read about uploading images with base64.

Is it possible to send video in any form to Realm function that would upload it in AWS for example?

What are best practices for images/videos and Functions?

Thanks in advance

Hi @Aleksandar_Dimitrijevic and welcome in the MongoDB Community :muscle: !

I did a project like this. I was just writing the base64 image to my MongoDB Atlas cluster but maybe this can be a starting point:

exports = function(payload, response) {
    const mongodb = context.services.get("mongodb-atlas");
    const pictocat = mongodb.db("stitch").collection("pictocat");
    
    var body = EJSON.parse(payload.body.text());
    body.dateInserted = new Date();
    pictocat.insertOne(body)
    .then(result => {
      response.setStatusCode(201);
    });
};

I was sending the pictures like this:

#!/usr/bin/env bash
for i in {1..15}
do
  start_json='{ "comment" : "This LOLCAT is awesome!", "picture" : { "$binary": { "base64": "';
  base64_image=$(base64 -w 0 lolcat$i.jpg);
  end_json='", "subType":"00"}}}';
  json="${start_json}${base64_image}${end_json}"

  curl -H "Content-Type: application/json" \
  --data-binary @- \
  https://webhooks.mongodb-stitch.com/api/client/v2.0/app/pictocat-kjvkr/service/REST/incoming_webhook/receive-cats?secret=test <<CURL_DATA
$json
CURL_DATA
done

As you can see, this script is a bit old because it was developed when MongoDB Realm was MongoDB Stitch so a few things might be different, but you get the idea.

To upload to S3, you now need to use the JS dependencies in Realm. They are touching this subject here.

Cheers,
Maxime.

1 Like

Personally, I would use the AWS SDK in realm to create a presigned URL to upload to from the client.

1 Like

Thanks for your answers.
A specific situation here is that our application must be offline first because it will be used in areas without any internet connection. So in that manner, we should probably provide videos and images with functions, so they could be executed to realm when the connection is gained.

@MaBeuLux88 I think that solution for images with S3 is okay. But is there any example with videos? How are they uploaded and sent to Realm functions from client(in my case specific -Android)?

@Adam_Holt thanks for your answer but in our case, functions are must if i read documentation correctly

I don’t have an example for videos :/.

But maybe you should have a look to the OFish app that we helped develop. They take the mobile devices on the ships where they are completely off the grid as well. They need to take pictures of the ship they inspect, etc. Maybe videos? Not sure.

But the concept here should be the same. I think they built a system to store the images on the mobile device until it’s able to sync them on S3.

@Andrew_Morgan I think you worked on that part of the project, no?

Cheers,
Maxime.

O-Fish stores the photos in the local Realm database. Once the document containing the image is synced to Atlas, a trigger fires which uploads it to S3 and replaces the image in the document with an S3 link. The updated document is then synced back to the mobile app and the binary image disappears from Realm. We keep a thumbnail image in Realm so that users can at least see something when looking at old reports when offline.

This article has more details… Realm Data and Partitioning Strategy Behind the WildAid O-FISH Mobile Apps

1 Like

@Andrew_Morgan @MaBeuLux88
Thanks for your responses, you helped me a lot.

1 Like