jeanbmar

payload s3 upload

by jeanbmar
Cloud storage
v1
^1.x.x
MIT

Overview

Summary generated from the README

This plugin stores files uploaded through Payload upload collections in Amazon S3 instead of on the server file system. It is registered in the Payload config with an S3 client, and each upload collection sets its own bucket, prefix and PutObject options. Resized image variants are uploaded too. It targets Payload 1.x.

Features

  • Uploads files to Amazon S3 instead of the local file system
  • Configures bucket, prefix and command options on each collection
  • Supports dynamic prefixes computed from the document
  • Automatically uploads resized image variants
  • Accepts any PutObjectCommandInput property such as ACL
  • Provides a typed S3UploadCollectionConfig for collections

Use cases

  • Store media uploads in S3 on a Payload 1.x project
  • Keep storage settings next to each collection instead of in the global config
  • Organise uploads into per-type bucket folders using dynamic prefixes

From the author

Send Payload CMS uploads to Amazon S3

Installation

npm i payload-s3-upload

70

Stars

11

Forks

5

Open Issues

3 years ago

Last Updated

273

Weekly Downloads

v2.1.1

npm Version

21.1 KB

Unpacked Size

1

Dependencies

GitHub stars

700 since Sep 6

Show data
GitHub stars by day (UTC)
DateStars
Dec 5, 202670
Dec 4, 202670
Dec 3, 202670
Dec 2, 202670
Dec 1, 202670
Nov 30, 202670
Nov 29, 202670
Nov 28, 202670
Nov 27, 202670
Nov 26, 202670
Nov 25, 202670
Nov 24, 202670
Nov 23, 202670
Nov 22, 202670
Nov 21, 202670
Nov 20, 202670
Nov 19, 202670
Nov 18, 202670
Nov 17, 202670
Nov 16, 202670
Nov 15, 202670
Nov 14, 202670
Nov 13, 202670
Nov 12, 202670
Nov 11, 202670
Nov 10, 202670
Nov 9, 202670
Nov 8, 202670
Nov 7, 202670
Nov 6, 202670
Nov 5, 202670
Nov 4, 202670
Nov 3, 202670
Nov 2, 202670
Nov 1, 202670
Oct 31, 202670
Oct 30, 202670
Oct 29, 202670
Oct 28, 202670
Oct 27, 202670
Oct 26, 202670
Oct 25, 202670
Oct 24, 202670
Oct 23, 202670
Oct 22, 202670
Oct 21, 202670
Oct 20, 202670
Oct 19, 202670
Oct 18, 202670
Oct 17, 202670
Oct 16, 202670
Oct 15, 202670
Oct 14, 202670
Oct 13, 202670
Oct 12, 202670
Oct 11, 202670
Oct 10, 202670
Oct 9, 202670
Oct 8, 202670
Oct 7, 202670
Oct 6, 202670
Oct 5, 202670
Oct 4, 202670
Oct 3, 202670
Oct 2, 202670
Oct 1, 202670
Sep 30, 202670
Sep 29, 202670
Sep 28, 202670
Sep 27, 202670
Sep 26, 202670
Sep 25, 202670
Sep 24, 202670
Sep 23, 202670
Sep 22, 202670
Sep 21, 202670
Sep 20, 202670
Sep 19, 202670
Sep 18, 202670
Sep 17, 202670
Sep 16, 202670
Sep 15, 202670
Sep 14, 202670
Sep 13, 202670
Sep 12, 202670
Sep 11, 202670
Sep 10, 202670
Sep 9, 202670
Sep 8, 202670
Sep 7, 202670
Sep 6, 202670
npm weekly downloads

270−292 since Sep 6

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 5, 2026270
Dec 4, 2026266
Dec 3, 2026284
Dec 2, 2026303
Dec 1, 2026302
Nov 30, 2026295
Nov 29, 2026284
Nov 28, 2026315
Nov 27, 2026319
Nov 26, 2026316
Nov 25, 2026298
Nov 24, 2026298
Nov 23, 2026330
Nov 22, 2026309
Nov 21, 2026306
Nov 20, 2026287
Nov 19, 2026302
Nov 18, 2026336
Nov 17, 2026350
Nov 16, 2026324
Nov 15, 2026351
Nov 14, 2026369
Nov 13, 2026420
Nov 12, 2026405
Nov 11, 2026370
Nov 10, 2026352
Nov 9, 2026339
Nov 8, 2026340
Nov 7, 2026305
Nov 6, 2026333
Nov 5, 2026359
Nov 4, 2026377
Nov 3, 2026413
Nov 2, 2026448
Nov 1, 2026429
Oct 31, 2026418
Oct 30, 2026340
Oct 29, 2026270
Oct 28, 2026215
Oct 27, 2026182
Oct 26, 2026135
Oct 25, 2026150
Oct 24, 2026197
Oct 23, 2026159
Oct 22, 2026214
Oct 21, 2026242
Oct 20, 2026237
Oct 19, 2026241
Oct 18, 2026260
Oct 17, 2026209
Oct 16, 2026235
Oct 15, 2026223
Oct 14, 2026257
Oct 13, 2026262
Oct 12, 2026281
Oct 11, 2026247
Oct 10, 2026256
Oct 9, 2026286
Oct 8, 2026281
Oct 7, 2026426
Oct 6, 2026423
Oct 5, 2026403
Oct 4, 2026386
Oct 3, 2026357
Oct 2, 2026322
Oct 1, 2026428
Sep 30, 2026249
Sep 29, 2026257
Sep 28, 2026390
Sep 27, 2026396
Sep 26, 2026435
Sep 25, 2026600
Sep 24, 2026547
Sep 23, 2026590
Sep 22, 2026757
Sep 21, 2026660
Sep 20, 2026676
Sep 19, 2026818
Sep 18, 2026716
Sep 17, 2026668
Sep 16, 2026765
Sep 15, 2026588
Sep 14, 2026553
Sep 13, 2026708
Sep 12, 2026567
Sep 11, 2026533
Sep 10, 2026683
Sep 9, 2026574
Sep 8, 2026585
Sep 7, 2026727
Sep 6, 2026562

Each point is npm's rolling 7-day download count.

Health Score

48/100Fair

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/393192786-root.svg)](https://payloaddirectory.dev/plugins/393192786-root)
HTML
<a href="https://payloaddirectory.dev/plugins/393192786-root"><img src="https://payloaddirectory.dev/badge/393192786-root.svg" alt="Payload Directory"></a>
Updated Aug 9, 2023Created Aug 5, 2021Published May 16, 2023

README

Upload files to S3 in Payload CMS

This plugin sends uploaded files to Amazon S3 instead of writing them to the server file system.
Resized images are properly supported.

Why should I use this module?

Payload team supports an official cloud storage plugin, different from this one.

The main difference is that this plugin allows configuring collection logic on the collection itself.

Payload implementation requires to define collection-specific stuff from plugins inside the global payload configuration file, which is (imho) bad design.

Install

npm install payload-s3-upload --legacy-peer-deps

Payload requires legacy-peer-deps because of conflicts on React and GraphQL dependencies (see Payload docs).

Getting Started

Enable plugin in Payload CMS config

import { S3Client } from '@aws-sdk/client-s3';
import { buildConfig } from 'payload/config';
import s3Upload from 'payload-s3-upload';

export default buildConfig({
  // ...
  plugins: [
    s3Upload(new S3Client({
      region: process.env.AWS_REGION,
      credentials: {
        accessKeyId: process.env.AWS_KEY,
        secretAccessKey: process.env.AWS_SECRET,
      },
    })),
  ],
});

Configure your upload collections

import { S3UploadCollectionConfig } from 'payload-s3-upload';

const Media: S3UploadCollectionConfig = {
  slug: 'media',
  upload: {
    staticURL: '/assets',
    staticDir: 'assets',
    disableLocalStorage: true,
    s3: {
      bucket: 'my-bucket',
      prefix: 'images/xyz', // files will be stored in bucket folder images/xyz
      // prefix: ({ doc }) => `assets/${doc.type}`, // dynamic prefixes are possible too
      commandInput: {
        // optionally, use here any valid PutObjectCommandInput property
        // https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/interfaces/putobjectcommandinput.html
        ACL: 'public-read',  
      },
    },
    adminThumbnail: ({ doc }) =>
      `https://my-bucket.s3.eu-west-3.amazonaws.com/images/xyz/${doc.filename}`,
  },
  // create a field to access uploaded files in s3 from payload api
  fields: [
    {
      name: 'url',
      type: 'text',
      access: {
        create: () => false,
      },
      admin: {
        disabled: true,
      },
      hooks: {
        afterRead: [
          ({ data: doc }) =>
            `https://my-bucket.s3.eu-west-3.amazonaws.com/images/${doc.type}/${doc.filename}`,
        ],
      },
    },
  ],
};

export default Media;

Recipe for handling different sizes

This plugin automatically uploads image variants in S3.

However, in order to retrieve correct URLs for the different sizes in the API, additional hooks should be implemented.

import { S3UploadCollectionConfig } from 'payload-s3-upload';

const Media: S3UploadCollectionConfig = {
  slug: 'media',
  upload: {
    // ...
    imageSizes: [
      {
        name: 'thumbnail',
        width: 400,
        height: 300,
        crop: 'center'
      },
      {
        name: 'card',
        width: 768,
        height: 1024,
        crop: 'center'
      },
      {
        name: 'tablet',
        width: 1024,
        height: null,
        crop: 'center'
      }
    ],
    adminThumbnail: 'thumbnail',
  },
  hooks: {
    afterRead: [
      ({ doc }) => {
        // add a url property on the main image
        doc.url = `${myBucketUrl}/${doc.filename}`

        // add a url property on each imageSize
        Object.keys(doc.sizes)
          .forEach(k => doc.sizes[k].url = `${myBucketUrl}/${doc.sizes[k].filename}`)
      }
    ]
  },
  fields: []
};

export default Media;

Working Example

Please refer to the test files!

Similar plugins