#Using upload prefix to create folders in storage

3 messages · Page 1 of 1 (latest)

signal vigil
#

I have a ProductImages collection, which is used by Products collection. I am using Vercel Bob storage for storing the images. I tried setting a beforeChange hook to set a dynamic prefix option using product.slug. This indeed places the image within the folder as intended but when it tries to fetch http://localhost:3000/api/productImages it raises a 404. But if I dont add the prefix and store it directly, it is able to fetch the image back successfully.

hooks: {
    beforeOperation: [
      async ({req, operation}) => {
        if((operation === 'create' || operation === 'update') && req.data) {
          const productId = req.data.product
          const product = await req.payload.findByID({
            collection: 'products',
            id: productId,
          })
          if (!product) return;

          const remoteFolder = `productImages/${product.store.slug}`;
          console.log(`Image will be stored in ${remoteFolder} folder`);
          req.data.prefix = remoteFolder;
        }
      }
    ]
  },

Am I missing something here, since the image is stored with the right folder structure as expected but it is not able to reference it properly or fetch it? Would it work properly with Folders?

signal vigil
#

Using upload prefix to create folders in storage