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.
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
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?
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.