Convert binary image to Base64 from Realm function

I am trying to read a binary (binData) image stored in a MongoDB document from inside a Realm function. My goal is to encode this binary file as Base64 (to send to AWS) using Buffer.from(file.buffer) but I get the following error: TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.

When I read the same document from Node.js (using a connection uri), Buffer.from(file.buffer) works fine.

My question is: how can I encode a binary image straight out of MongoDB from inside a Realm function?

Here is my code for reference:

exports = function(){
  var drawings = context.services.get("Cluster0").db("databaseName").collection("drawings");

  // get drawing document
  const query = { "_id": BSON.ObjectId("61c348229f2368fd6f2a2f95")};
  const projection = {
    "_id":1,
    "thumbnail" : 1,
  }
  
  drawings.findOne(query, projection)
  .then(doc => {
    const file = doc.thumbnail
    console.log('file', file) // this logs: [object Binary]
    console.log('typeof(file)', typeof(file)) // this logs: object
    const base64data = Buffer.from(file.buffer); // this throws the error, and I've also tried Buffer.from(file)
   })
};

Thanks in advance!

Hello @Laekipia,

Thanks for raising this query.

A binary field in a document is stored as BSON.Binary object so you would need to call its .toBase64() method to convert to Base64 to send to AWS.

You can read more on JSON and BSON.

I hope this answers your query.

Please let us know if you would have further questions.

Cheers,
Henna

1 Like

@henna.s, you’re an absolute legend! Thanks so much :pray:

1 Like

I’m trying to implement an app similar to the O’FISH app. My images are temporarily stored as binary data in MongoDB before being sent to S3 via a Realm trigger function.

I’ve written a realm function that uploads a file to my S3 bucket. This function works fine if the file is "hello world". However, I get Error: Unsupported body payload object when my file is a binary image queried from a MongoDB document.

Here is my Realm function:

exports = function(){
  var drawings = context.services.get("Cluster0").db("databaseName").collection("drawings");

  // get drawing document
  const query = { "_id": BSON.ObjectId("61c348229f2368fd6f2a2f95")};
  const projection = {
    "_id":1,
    "thumbnail" : 1,
  }
  drawings.findOne(query, projection)
  .then(doc => {
    const imageName = doc._id.toString()
    // THE UPLOAD TO S3 WORKS WITH LINE BELOW
    // const file = 'hello world'
    const file = doc.thumbnail
    console.log('file type', Object.prototype.toString.call(file)) // this logs: [object Binary]
   
    // call uploadImageToS3 function
    context.functions.execute("uploadImageToS3", imageName, file)
    .then (() => {
      console.log('Uploaded to S3');
    })
};

And here is my uploadImageToS3 function:


exports = async function(imageName, file) {
  
  const AWS = require('aws-sdk');

  AWS.config.update({
    accessKeyId :context.values.get("AWS_ACCESS_KEY_ID"),
    secretAccessKey : context.values.get("AWS_ACCESS_KEY_SECRET"),
    region: context.values.get("AWS_REGION")
  });
  
  const s3 = new AWS.S3({apiVersion: '2006-03-01'})
  const bucketName = context.values.get("AWS_BUCKET")
  
  return s3.upload({
        "Bucket": bucketName,
        "Key" : imageName,
        "Body": file,
        "ACL": "public-read",
        "ContentType": "image/jpeg",
        }).promise()
}

How do I upload a binary image (straight out of a MongoDB document) to S3?

Many thanks in advance!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.

Happy to Help :smiley:

I have combined the two posts as both were the same questions.

Cheers.