#Can I use a field's value in S3 path ? Using hook or generateFileURL ?

3 messages · Page 1 of 1 (latest)

weak wasp
#

Question in title says it all.
currently here's my config :

  plugins: [
    s3Storage({
      collections: {
        fichiers: {
          disableLocalStorage: true,
          disablePayloadAccessControl: true,
          prefix: "media/files",
          generateFileURL: async (doc) => {
            console.log("logging doc", doc)
            return `${process.env.R2_ENDPOINT}/${process.env.R2_BUCKET_NAME}/${doc.prefix}/${doc.filename}`
          },
        }
      },
      bucket: process.env.R2_BUCKET_NAME,
      config: {
        endpoint: process.env.R2_ENDPOINT, 
        credentials: {
          accessKeyId: process.env.R2_ACCESS_KEY_ID,
          secretAccessKey: process.env.R2_SECRET_ACCESS_KEY,
        },
        region: process.env.R2_REGION,
      },
    }),
  ],

when logging doc, I can see a "fields" array, but it seems populated with the field's config rather than field's content.

I tried using something like

  hooks: {
    afterRead: [
      async ({doc}) => {
        return {
          ...doc,
          prefix: `${doc.prefix}/${doc.linkedTournage}/${doc.filename}`
        }
      }
    ]
  },

or

  hooks: {
    afterRead: [
      async ({doc}) => {
        return {
          ...doc,
          filename: `${doc.linkedTournage}/${doc.filename}`
        }
      }
    ]
  },

but it didn't have an effect on the generated path.
linkedTournage is a relationship but when doc is logged in that hook I can see the id as string which is expected.
i can see the id being added to the title of the doc but I want the path to change on my R2. any suggestions ?
maybe I'm using the wrong hook ?

Thanks in advance

strong wharfBOT
weak wasp
#

I managed to do it !
Like this

  hooks: {
    beforeChange: [
      async ({data}) => {
        return {
          ...data,
          prefix: `${data.prefix}/${data.linkedTournage}/${data.filename}`
        }
      }
    ]
  },

you're welcome, future payload user