Developer Guide
Other documentationsDemoCommunityGitHub
  • Forest Admin
  • Getting Started
    • Quick start
    • Development workflow
  • Reference Guide
    • How it works
      • Environments
      • Developing on Forest Admin
        • Using branches
        • Deploying your changes
        • Forest CLI commands
          • init
          • login
          • branch
          • switch
          • set-origin
          • push
          • environments:reset
          • environments:create
          • deploy
          • schema:diff [beta]
        • Express packages
    • Models
      • Enrich your models
      • Relationships
        • Create a Smart relationship
          • GetIdsFromRequest
        • Smart Relationship Examples
          • Smart hasMany relationship in mongoDB
    • Actions
      • Create and manage Smart Actions
        • Use a Smart Action Form
        • Use a Smart Action Intent
      • Smart Action Examples
        • Calculate the distance between two string addresses
        • Impersonate a user
        • Create a record with a multiselect through a many-to-many relationship
        • Handle enums with alias labels in a smart action
        • Dropdown with list of values in smart action form
        • Custom dynamic dropdown in a form using smart collections
        • Refresh hasMany relationship in smart action
        • Smart segment to restrict access to an action on a record details view
        • BelongsToMany edition through smart collection
        • Upload files to amazon s3
        • Upload several files with the File Picker
        • Retrieve smart field info in a smart action
        • Smart action to create several records from the input of a single smart action form
        • Add many existing records at the same time (hasMany-belongsTo relationship)
        • Call a webhook with record ids
        • Bulk update records
    • Smart Fields
      • Smart Field Examples
        • Add an HTML credit card as a smart field in a summary view
        • Display field with complex info in html format (rich text editor)
        • Generate signed urls to display S3 files in a smart field
        • Print a status object in a single line field
        • Sort by smart field
        • Sort by smart field that includes value from a belongsTo relationship
        • Add fields destined to the create form
        • Add validation to a smart field edition
        • Display smart field as progress bar using rich text editor
        • Update point geometry field using a smart field and algolia api
    • Smart Collections
      • Examples
        • Create a Smart Collection with Amazon S3
        • Smart relationship between model and stripe cards
        • Create records from a Smart collection
        • Searchable smart collection with records fetched from hubspot API
      • Serializing your records
    • Routes
      • Default routes
      • Extend a route
      • Override a route
    • Integrations
      • Stripe
      • Mixpanel
      • Intercom
      • Elasticsearch
        • Interact with your Elasticsearch data
        • Elasticsearch service/utils
        • Another example
      • Zendesk
        • Authentication, Filtering & Sorting
        • Display Zendesk tickets
        • Display Zendesk users
        • View tickets related to a user
        • Bonus: Direct link to Zendesk + change priority of a ticket
      • Dwolla
        • Display Dwolla customers
        • Display Dwolla funding sources
        • Display Dwolla transfers
        • Link users and Dwolla customers
        • Dwolla Service
      • Razorpay
      • Hubspot
        • Create a Hubspot company
        • Display Hubspot companies
      • Twilio
        • Send an SMS with Twilio and Zapier
      • Azure Table Storage
      • Slack
        • Send Smart Action notifications to Slack
      • Algolia
        • Geocode an address with Algolia
    • Smart Views
      • Create a Map view
      • Create a Calendar view
      • Create a Shipping view
      • Create a Gallery view
      • Create a custom tinder-like validation view
      • Create a dynamic calendar view for an event-booking use case
      • Create a custom moderation view
    • Smart Segments
    • Scopes
      • Create a scope more than one level away based on a Smart field
      • Scope on a smart field extracting a json's column attribute
    • Performance
    • Charts
      • Create an API-based Chart
      • Create a Smart Chart
      • Create Charts with AWS Redshift
  • Extra help
    • Setup
      • Install
      • Connecting Forest Admin to Your Database (Forest Cloud)
      • Forest Admin IP white-listing (Forest Cloud)
      • Why HTTPS is necessary even locally
      • Troubleshooting
      • Prevent permission errors at installation
      • Deploy your admin backend to AWS
      • Deploy your admin backend on Heroku
      • Deploy your admin backend to Ubuntu server
      • Deploy your admin backend to Google Cloud Platform
      • Install Forest Admin on a remote machine
      • Use Forest Admin with a read-only database
      • Configuring CORS headers
      • Running Forest Admin on multiple servers
      • Flatten nested fields (MongoDB)
    • Upgrade
      • Upgrade notes (Rails)
        • Upgrade to v9
        • Upgrade to v8
        • Upgrade to v7
        • Upgrade to v6
        • Upgrade to v5
        • Upgrade to v4
        • Upgrade to v3
      • Upgrade notes (SQL, Mongodb)
        • Upgrade to v9
        • Upgrade to v8
        • Upgrade to v7
        • Upgrade to v6
        • Upgrade to v5
        • Upgrade to v4
        • Upgrade to v3
      • Update your models' definition
      • Monitor your Forest's status
      • Manage your Forest Admin environments programmatically
      • Changing your domain name
      • Migrate to the new role system
      • Push your new version to production
    • Databases
      • Use a demo SQL database
      • Use a demo MongoDB database
      • Populate a postgreSQL database on Heroku
      • Connect to a read replica database
      • Plug multiple schemas
      • Add new databases
      • Manage SQL views
    • Settings
      • Customize your /forest folder
      • Disable automatic Forest Admin schema update
      • Include/exclude models
      • Display extensive logs
      • Laravel specific settings
    • Releases Support
    • Other documentations
Powered by GitBook
On this page

Was this helpful?

  1. Reference Guide
  2. Actions
  3. Smart Action Examples

Upload files to amazon s3

PreviousBelongsToMany edition through smart collectionNextUpload several files with the File Picker

Last updated 1 year ago

Was this helpful?

Please be sure of your agent type and version and pick the right documentation accordingly.

This is the documentation of the forest-express-sequelize and forest-express-mongoose Node.js agents that will soon reach end-of-support.

forest-express-sequelize v9 and forest-express-mongoose v9 are replaced by v1.

Please check your agent type and version and read on or switch to the right documentation.

This is still the latest Ruby on Rails documentation of the forest_liana agent, you’re at the right place, please read on.

This is the documentation of the django-forestadmin Django agent that will soon reach end-of-support.

If you’re using a Django agent, notice that django-forestadmin v1 is replaced by v1.

If you’re using a Flask agent, go to the v1 documentation.

Please check your agent type and version and read on or switch to the right documentation.

This is the documentation of the forestadmin/laravel-forestadmin Laravel agent that will soon reach end-of-support.

If you’re using a Laravel agent, notice that forestadmin/laravel-forestadmin v1 is replaced by v3.

If you’re using a Symfony agent, go to the v1 documentation.

Please check your agent type and version and read on or switch to the right documentation.

Upload files to amazon s3

In this example we want to upload files (legal docs) for the companies collection that will be stored in Amazon S3 through a smart action. To do so we need to perform the following steps:

Declare the smart action

In the companies.js file of the Forest folder, add the following to enable the user to access the action in the UI (by declaring the name and type of the action) and open an input form when triggering the action (by declaring fields).

const { collection } = require('forest-express-sequelize');

collection('companies', {
  actions: [{
    name: 'Upload Legal Docs',
    type: 'single',
    fields: [{
      field: 'Certificate of Incorporation',
      description: 'The legal document relating to the formation of a company or corporation.',
      type: 'File',
      isRequired: true
    }, {
      field: 'Proof of address',
      description: '(Electricity, Gas, Water, Internet, Landline & Mobile Phone Invoice / Payment Schedule) no older than 3 months of the legal representative of your company',
      type: 'File',
      isRequired: true
    }, {
      field: 'Company bank statement',
      description: 'PDF including company name as well as IBAN',
      type: 'File',
      isRequired: true
    }, {
      field: 'Valid proof of ID',
      description: 'ID card or passport if the document has been issued in the EU, EFTA, or EEA / ID card or passport + resident permit or driving license if the document has been issued outside the EU, EFTA, or EEA of the legal representative of your company',
      type: 'File',
      isRequired: true
    }],
});

Implement the logic of the smart action

To implement the logic that will be called upon when the action is triggered and the corresponding endpoint is called by the browser, the following has been added to the file companies.js in the routes folder.

const express = require('express');
const S3Helper = require('../services/s3-helper');
const router = express.Router();

function uploadLegalDoc(companyId, doc, field) {
  const id = uuid();

  return new S3Helper().upload(doc, `livedemo/legal/${id}`)
    .then(() => models.companies.findById(companyId))
    .then((company) => {
      company[field] = id;
      return company.save();
    })
    .then((company) => models.documents.create({
      file_id: company[field],
      is_verified: true,
    }));
}

router.post('/actions/upload-legal-docs',
  (req, res) => {
    // Get the current company id
    let companyId = req.body.data.attributes.ids[0];

    // Get the values of the input fields entered by the admin user.
    let attrs = req.body.data.attributes.values;
    let certificate_of_incorporation = attrs['Certificate of Incorporation'];
    let proof_of_address = attrs['Proof of address'];
    let company_bank_statement = attrs['Company bank statement'];
    let passport_id = attrs['Valid proof of id'];

    // The business logic of the Smart Action. We use the function
    // UploadLegalDoc to upload them to our S3 repository. You can see the full
    // implementation on our Forest Live Demo repository on Github.
    return P.all([
      uploadLegalDoc(companyId, certificate_of_incorporation, 'certificate_of_incorporation_id'),
      uploadLegalDoc(companyId, proof_of_address, 'proof_of_address_id'),
      uploadLegalDoc(companyId, company_bank_statement,'bank_statement_id'),
      uploadLegalDoc(companyId, passport_id, 'passport_id'),
    ])
    .then(() => {
      // Once the upload is finished, send a success message to the admin user in the UI.
      res.send({ success: 'Legal documents are successfully uploaded.' });
    });
  });

  ...

module.exports = router;

The file required where the S3 helper is defined has been added to a services folder, as services/s3-helper.js.

const P = require('bluebird');
const parseDataUri = require('parse-data-uri');
const AWS = require('aws-sdk');
const filesize = require('filesize');

function S3Helper() {
  function mapAttrs(file) {
    return {
      id: file.Key.replace('livedemo/legal/', ''),
      url: `https://s3-eu-west-1.amazonaws.com/${process.env.S3_BUCKET}/${file.Key}`,
      last_modified: file.LastModified,
      size: filesize(file.Size),
    };
  }

  this.upload = (rawData, filename) => {
    return new P((resolve, reject) => {
      // Create the S3 client.
      let s3Bucket = new AWS.S3({ params: { Bucket: process.env.S3_BUCKET } });
      let parsed = parseDataUri(rawData);
      let base64Image = rawData.replace(
        /^data:(image|application)\/\w+;base64,/,
        ''
      );

      let data = {
        Key: filename,
        Body: new Buffer(base64Image, 'base64'),
        ContentEncoding: 'base64',
        ContentDisposition: 'inline',
        ContentType: parsed.mimeType,
        ACL: 'public-read',
      };

      // Upload the image.
      s3Bucket.upload(data, function (err, response) {
        if (err) {
          return reject(err);
        }
        return resolve(response);

        return models.companies
          .findById(companyId)
          .then((company) => {
            company.certificate_of_incorporation_id = certificateId;
            return company.save();
          })
          .then(() => {
            res.send({ success: 'Legal documents are successfully uploaded.' });
          });
      });
    });
  };

  this.files = (prefix) => {
    const s3 = new AWS.S3();

    let files = [];

    return new P((resolve, reject) => {
      return s3
        .listObjects({
          Bucket: process.env.S3_BUCKET,
          Prefix: prefix,
        })
        .on('success', function handlePage(r) {
          files.push(...r.data.Contents);

          if (r.hasNextPage()) {
            r.nextPage().on('success', handlePage).send();
          } else {
            return resolve(files.map((f) => mapAttrs(f)));
          }
        })
        .on('error', (err) => {
          reject(err);
        })
        .send();
    });
  };

  this.file = (key) => {
    const s3 = new AWS.S3();

    let files = [];

    return new P((resolve, reject) => {
      return s3
        .listObjects({
          Bucket: process.env.S3_BUCKET,
          Prefix: key,
        })
        .on('success', (file) => {
          return resolve(mapAttrs(file.data.Contents[0]));
        })
        .on('error', (err) => {
          reject(err);
        })
        .send();
    });
  };

  this.deleteFile = (key) => {
    const s3 = new AWS.S3();

    return new P((resolve, reject) => {
      return s3
        .deleteObjects({
          Bucket: process.env.S3_BUCKET,
          Delete: {
            Objects: [{ Key: key }],
          },
        })
        .on('success', () => resolve())
        .on('error', (err) => reject(err))
        .send();
    });
  };

  this.updateFile = (key, newKey) => {
    const s3 = new AWS.S3();

    return new P((resolve, reject) => {
      return s3
        .copyObject({
          Bucket: process.env.S3_BUCKET,
          CopySource: process.env.S3_BUCKET + '/' + key,
          Key: newKey,
          MetadataDirective: 'REPLACE',
        })
        .on('success', (file) => {
          return this.deleteFile(key).then(() => {
            return resolve(this.file(newKey));
          });
        })
        .on('error', (err) => reject(err))
        .send();
    });
  };
}

module.exports = S3Helper;
@forestadmin/agent
forestadmin-agent-django
forestadmin-agent-flask
forestadmin/laravel-forestadmin
forestadmin/symfony-forestadmin