payloadcms / plugin-cloud-storage

The official cloud storage plugin for Payload
MIT License
131 stars 36 forks source link

Using env variables in prefix string comes out as "undefined" #52

Closed imCorfitz closed 1 year ago

imCorfitz commented 1 year ago

Update

This apparently is also an issue in other plugins. Using environment variables as part of the plugin configuration is not possible.


Original post

Following will return undefined/media as prefix in API calls, however images will be uploaded to the directory path in S3 using the prefix value from .env. So you will have to manually generate the file URL to inject the env variable (which works for some reason).

export default buildConfig({
  serverURL: process.env.PAYLOAD_PUBLIC_SERVER_URL,
  collections: [Page, Media],
  typescript: {
    outputFile: path.resolve(__dirname, 'payload-types.ts'),
  },
  graphQL: {
    schemaOutputFile: path.resolve(__dirname, 'generated-schema.graphql'),
  },
  plugins: [
    cloudStorage({
      collections: {
        media: {
          adapter, // see docs for the adapter you want to use
          disablePayloadAccessControl: true,
          prefix: `${process.env.S3_PREFIX}/media`,
          // generateFileURL: (args) => `https://s3.${process.env.S3_REGION}.amazonaws.com/${process.env.S3_BUCKET}/${process.env.S3_PREFIX}/media/${args.filename}`,
          disableLocalStorage: true,
        },
      },
    }),
  ],
});

Adapter for reference:

const adapter = s3Adapter({
  config: {
    forcePathStyle: true,
    region: process.env.S3_REGION,
    endpoint: `https://s3.${process.env.S3_REGION}.amazonaws.com`,
    credentials: {
      accessKeyId: process.env.S3_ACCESS_KEY_ID,
      secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
    },
    // ... Other S3 configuration
  },
  bucket: process.env.S3_BUCKET,
});

Because it returns undefined/media but the file is actually stored in my-prefix/media, then the media files aren't deleted automatically from the bucket when media items are deleted in cms.

imCorfitz commented 1 year ago

Closing this issue as I found the previous thread discussing this matter: https://github.com/payloadcms/payload/issues/1654#issuecomment-1356809053