Create your own serverless function

Last Updated: Feb 21, 2019 02:55AM PST

Serverless functions can be used for two main tasks:

1. Create your own data flow for anonymization, tiering, migration, analytics, etc
2. Automate NooBaa management activities for self-service such as account creation, granting permissions, etc.

Creating a new function is easy. 

Navigate to the functions window

Create serverless function

Type the function name and description



Write your code and provide the function name to execute





Set timeout and memory, we recommend leaving the default values.



Done. The function is ready. We just need to test it.



Open the function and click on Invoke tab



Provide the parameters. Please note that when triggered from a bucket event, the trigger will send the arguments as an array of objects in the following format:


 {
  "Records": [{
    "s3": {
      "bucket": {
        "name": "BUCKETNAME"
      },
      "object": {
        "key": "OBJECTNAME"
      }
    }
  }]
}




If everything is fine, you should see the green popup on the left bottom side.

Now connect the function to a bucket. Navigate to the target bucket, click on Triggers tab and provide the new function details. 



Example of a function that accesses the object metadata can be found below:

/* Copyright (C) 2016 NooBaa */
'use strict';

/* sample event - trigger structure:
{
    "Records": [{
        "s3": {
            "bucket": { "name": "first.bucket" },
            "object": { "key": "Apple.jpg" }
        }
    }]
}
*/

const AWS = require('aws-sdk');

exports.handler = async function(event, context, callback) {
    try {
        const s3 = new AWS.S3();
        const bucket = event.Records[0].s3.bucket.name;
        const key = event.Records[0].s3.object.key;
        const r = await s3.headObject({
            Bucket: bucket,
            Key: key,
        }).promise();
        return callback(null, JSON.stringify(r));
    } catch (err) {
        return callback(err);
    }
};
Did you find this article helpful?
195e69fe2c7a9be6b2b2aa6b87f970548@noobaa.desk-mail.com
https://cdn.desk.com/
false
desk
Loading
seconds ago
a minute ago
minutes ago
an hour ago
hours ago
a day ago
days ago
about
false
Invalid characters found
/customer/en/portal/articles/autocomplete