npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

payload-plugin-cloud-storage

v1.1.0

Published

Remote storage plugin for Payload CMS.

Downloads

5

Readme

Installation

npm i payload-plugin-cloud-storage
yarn add payload-plugin-cloud-storage

Basic Usage

1. Instantiate an adapter

Adapters encapsulate all vendor-specific configuration and API calls.

Currently we only support S3 or S3-compatible APIs (like DigitalOcean spaces) but this will change soon!

import { S3Adapter } from 'payload-plugin-cloud-storage';

const s3Adapter = new S3Adapter(
  {
    endpoint: `https://${process.env.SPACES_REGION}.digitaloceanspaces.com`,
    region: process.env.SPACES_REGION,
    credentials: {
      accessKeyId: process.env.SPACES_KEY,
      secretAccessKey: process.env.SPACES_SECRET,
    },
  },
  {
    bucket: process.env.SPACES_NAME,
    endpointUrl: `https://${process.env.SPACES_NAME}.${process.env.SPACES_REGION}.cdn.digitaloceanspaces.com`
  },
  // optional, use your own getEndpoint method
  (endpointUrl, file) => {
    return `${endpoint}/${data.filename}`
  }
)

2. Add the plugin to Payloads configuration

The plugin attaches itself to all collections that specify an upload key. The at it's most basic, the plugin will provide:

  • A beforeChange hook that pushes uploaded files to the relevant cloud storage.
  • An afterDelete hook that removes files from cloud storage after the document has been deleted in Payload.
  • An afterRead hook that adds returns an endpoint to the file for both the main file and each of the sizes.
  • A custom upload.adminThumbnail function. See admin thumbnails for a detailed explanation on what this function does.
const config = buildConfig({
  serverURL: 'http://localhost:3000',
  collections: [
    {
      slug: 'images',
      upload: true, // uploads being enabled is what enables this plugin on the collection
      fields: []
    }
  ],
  plugins: [
    cloudStorage(s3Adapter)
  ]
})

Referencing files uploaded by the plugin

By default the endpoint property is named cloudStorageUrl and it is added to both the main document and each of the image sizes on the collections afterRead hook.

{
  "id": "6110b3ba2ecab80501b31fa6",
  "width": 1247,
  "height": 968,
  "sizes": {
    "mobile": {
      "width": 1000,
      "height": 1000,
      "filename": "test-5-1000x1000.jpg",
      "mimeType": "image/jpeg",
      "filesize": 41329,
      "cloudStorageUrl": "https://brightvision.fra1.cdn.digitaloceanspaces.com/test-1000x1000.jpg"
    }
  },
  "filename": "test.jpg",
  "filesize": 142083,
  "mimeType": "image/jpeg",
  "createdAt": "2021-08-09T04:48:58.577Z",
  "updatedAt": "2021-08-09T04:57:16.992Z",
  "cloudStorageUrl": "https://brightvision.fra1.cdn.digitaloceanspaces.com/test.jpg"
}

Admin Thumbnails

The admin thumbnail function the plugin provides tries to transparently support the same functions that Payload itself does.

If your collection has an upload.adminThumbnail set as string, then it will try to pull the image from that size same as the default behavior. If somehow that size doesn't exist then it'll fallback to the main image.

If however your collection specifies a GetAdminThumbnail function then that will take precedence over the plugin provided function.

Extra Options

cloudStorage allows you to pass a second options parameter.

| Property | Required | Values | Description | |-------------------------|----------|----------------------------|-------------------------------------------------------------------------------------| | disableEndpointProperty | no | boolean | Disable the afterRead hook and the custom adminThumbnail function entirely. | | endpointPropertyName | no | string | Customize the name of the property that gets added in the plugins afterRead hook. | | disableLocalStorage | no | boolean | Passed through to uploads.disableLocalStorage. Defaults to true. |