Skip to content
Gravity Logo / Docs Gravity home
Browse documentation
Documentation/Gravity Server

File Uploads

You can upload files via the React form using input type of file. The file component supports multiple file uploads using a drag-and-drop interface.

Info

User avatars demonstrate a full working example of uploading files from the client and storing them in an S3 bucket.

<Form inputs={{ 
  avatar: {
    label: 'Profile Picture',
    type: 'file', 
    required: false, 
    max: 1,
 },
}}/>

Send authenticated multipart requests to POST /api/utility/upload. The route uses Multer memory storage and the limits from config.upload. The controller passes each buffered file and the authenticated account ID to the S3 helper.

Uploading Files to Amazon S3

Gravity includes a helper for interacting with S3, so you can manage your S3 buckets and files in a few lines of code.

1. Add AWS Credentials to .env

You need to add the following settings to your environment to use the S3 helper:

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
S3_REGION=
S3_BUCKET=

2. Import The Helper

Import the helper anywhere in your project and call one of the helper methods.

const s3 = require('./helper/s3');

// list the buckets
await s3.bucket();

// list the items in the bucket
await s3.bucket.items(bucketName);

// create a new bucket
await s3.bucket.create(bucketName);

// delete a  bucket
await s3.bucket.delete(bucketName);

// upload a file or buffer and return the S3 url
await s3.upload({ bucket: bucketName, account: accountId, file });

// delete a file using a filename or S3 url
await s3.delete({ bucket: bucketName, filename });

// get a signed url for a file
await s3.signedURL({ bucket: bucketName, filename, expires });