Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

How to change the URL of the file that is being uploaded

default discord avatar
mast19998 months ago

Well, I added a handler that gets the file and connects in to the response:


async (req: any, res: any, next: any) => {
  try {
    const { storageClient } = await getStorageClient(); // connect to where you want to get the image from
    // get the image here
    const Key = createKey({
      collection: "media",
      filename: req.params.filename,
    });

    const object = await storageClient.send(
      new GetObjectCommand({
        Bucket: BUCKET,
        Key,
      })
    );
// set the response up here
    res.set({
      "Content-Length": object.ContentLength,
       "Content-Type": object.ContentType,
        ETag: object.ETag,
      });

    if (object?.Body) {
      return (object.Body as any).pipe(res);
    }
// send the response
    return next();
    } catch (err: unknown) {
      req.payload.logger.error({
        err,
        msg: "Error getting file from cloud storage",
      });
      return next();
    }
}




I'm using payload version 2 as well.



I'm trying this locally.



I'm trying to write some custom logic for file uploads and I need to change the file URL that is going to be saved in the database.



I've written a onBeforeChange hook that uploads the image somewhere else and then updates the url for the data that I get in the arguments and then return it.


I tried looking at

payload/plugin-cloud

but I couldn't figure out why the images always point to localhost 😦

    Star on GitHub

    Star

    Chat on Discord

    Discord

    online

    Can't find what you're looking for?

    Get dedicated engineering support directly from the Payload team.