Woodshop for old agent generation
Try the new agent generation
  • What is woodshop
  • How to's
    • Smart Relationship
      • GetIdsFromRequest
    • Smart views
      • Display a calendar view
      • Create a custom tinder-like validation view
      • Create a custom moderation view
      • Create a dynamic calendar view for an event-booking use case
    • Configure environment variables
      • NodeJS/Express projects
    • Elasticsearch Integration
      • Interact with your Elasticsearch data
      • Elasticsearch service/utils
      • Another example
    • Zendesk Integration
      • 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 integration
      • Display Dwolla customers
      • Display Dwolla funding sources
      • Display Dwolla transfers
      • Link users and Dwolla customers
      • Dwolla service
    • Make filters case insensitive
    • Use Azure Table Storage
    • Create multiple line charts
    • Create Charts with AWS Redshift
    • View soft-deleted records
    • Send Smart Action notifications to Slack
    • Authenticate a Forest Admin API against an OAuth protected API Backend
    • Translate your project into TypeScript
      • V8
        • Migrate Mongoose files
        • Migrate Sequelize files
      • v7
        • Migrate Mongoose files
        • Migrate Sequelize files
      • v6
    • Geocode an address with Algolia
    • Display/edit a nested document
    • Send an SMS with Zapier
    • Hash a password with bcrypt
    • Display a customized response
    • Search on a smart field with two joints
    • Override the count route
    • Make a field readOnly with Sequelize
    • Hubspot integration
      • Create a Hubspot company
      • Display Hubspot companies
    • Impersonate a user
    • Import data from a CSV file
    • Import data from a JSON file
    • Load smart fields using hook
    • Pre-fill a form with data from a relationship
    • Re-use a smart field logic
    • Link to record info in a smart view
    • Display data in html format
    • Upload files to AWS S3
    • Display AWS S3 files from signed URLs
    • Prevent record update
    • Display, search and update attributes from a JSON field
    • Add many existing records at the same time (hasMany-belongsTo relationship)
    • Track users’ logs with morgan
    • Search on relationship fields by default
    • Export related data as CSV
    • Run automated tests
  • Forest Admin Documentation
Powered by GitBook
On this page
  • Requirements
  • How it works
  • Directory: /models
  • Directory: /services
  • Directory: /forest

Was this helpful?

  1. How to's

Display AWS S3 files from signed URLs

PreviousUpload files to AWS S3NextPrevent record update

Last updated 3 years ago

Was this helpful?

This example explains how to display pictures stored in an AWS S3 bucket, from signed URLs.

Below we have a collection places with a field pictures which is an array of strings containing the names of files stored in an S3 bucket on AWS.

We use then a smart field s3Pictures that is an array of return values from calls made to S3 to get signed URLs from the files present in pictures field.

Requirements

  • An admin backend running on forest-express-sequelize

  • An AWS S3 bucket with access credentials

How it works

Directory: /models

This directory contains the places.js file where the places model is declared.

places.js
module.exports = (sequelize, DataTypes) => {
  const { Sequelize } = sequelize;
    const Places = sequelize.define('places', {
    name: {
      type: DataTypes.STRING,
    },
    address: {
      type: DataTypes.STRING,
    },
    pictures: {
      type: DataTypes.ARRAY(DataTypes.STRING),
    },
  }, {
    tableName: 'places',
    underscored: true,
    timestamps: false,
  });

  return Places;
};

Directory: /services

This directory contains an s3-helper.js file, where the method to get the signed URL from S3 is defined. We use the aws-sdk npm package to connect to the bucket storing the pictures.

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

// Retrieving the AWS credentials from `.env` file
AWS.config.update({
  region: process.env.AWS_REGION,
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
});

function getS3SignedUrlById(fileId) {
  const s3Bucket = new AWS.S3({ params: { Bucket: process.env.S3_BUCKET } });
  return s3Bucket.getSignedUrl("getObject", {
    Key: fileId,
    Expires: 60 * process.env.AWS_S3_URL_EXPIRE_MINS,
  });
}

module.exports = getS3SignedUrlById;

Directory: /forest

This directory contains the places.js file where the smart field s3Pictures is declared.

In the get function, we iterate on the pictures array to get the signed URL for each file name and then return an array with the signed URLs.

places.js
const { collection } = require('forest-express-sequelize');
const getSignedUrlById = require('../services/s3-helper');

collection('places', {
  actions: [],
  fields: [
    {
      field: 's3Pictures',
      type: ['String'],
      get: (place) => {
        if (place.pictures) {
          const s3pictures = place.pictures.map((pic) => getS3SignedUrlById(pic))
          return Promise.all(s3pictures)
        }
        return null;
      },
    },
  ],
  segments: [],
});

The npm package

You need to configure your AWS credentials inside your app to get access to your bucket. You can read more about it in the AWS documentation .

You can then use the to preview the pictures.

aws-sdk
here
file viewer widget