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
Discord
online
Get dedicated engineering support directly from the Payload team.