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 do you upload an image using the payload client?

default discord avatar
teamkubelast year
11

I just can't get it to work. I am always getting TypeError: Cannot read properties of null (reading 'width')


I am uploading like this:



const payload = await getPayloadClient();



const media = await payload.create({


collection: "media",


data: {


alt: fileData.name


},


file: fileData


});



Seems like payload is treating the file as an image (and trying to access width). I am passing the data in this format (as required/specified by the payload client)



export type File = {


data: Buffer;


mimetype: string;


name: string;


size: number;


};

  • discord user avatar
    denolfe
    last year

    Where are you reading in

    fileData

    ? Can you show that code? There is an example of how to do this in our uploads tests here:

    https://github.com/payloadcms/payload/blob/master/test/uploads/config.ts#L218
  • default discord avatar
    teamkubelast year

    This is my Next.js route. I got it to work now, but somehow it only works when using local file upload. Once i enable the cloud storage plugin that is needed for production the file is not ending up in its supposed S3 bucket



    it fails with this error message

    Screenshot_2023-06-30_at_14.10.08.png
    Screenshot_2023-06-30_at_14.42.50.png
  • discord user avatar
    denolfe
    last year

    Can you show me where you're configuring the cloud storage plugin?

  • default discord avatar
    teamkubelast year


    File uploads work when using the payload admin dashboard or when the plugin ist disabled. It

    only

    fails when uploading through the payload client when using the cloud storage plugin

    Screenshot_2023-06-30_at_14.57.21.png
  • default discord avatar
    notchrlast year

    Looks like



    An issue here

    https://github.com/payloadcms/plugin-cloud-storage/blob/3d5858ca6d45d73c22e9bb97c2b443758d93400b/src/adapters/s3/handleUpload.ts#L34


    Since you're using s3... also here:

    https://github.com/payloadcms/plugin-cloud-storage/blob/3d5858ca6d45d73c22e9bb97c2b443758d93400b/src/adapters/s3/index.ts#L29


    So something with the intilization of your storage client is messed up



    This could be due to incorrect env vars



    Unlikely an issue with how Payload sets up the client in the plugin, but possible

  • discord user avatar
    denolfe
    last year

    @teamkube It definitely looks like your adapter may not be getting configured properly. I don't see the code for your adapter above, just the plugin. Can you provide that?

  • default discord avatar
    teamkubelast year

    Not doing anything fancy in the adapter



    the thing is when uploading through the cms admin panel it also works



    it only happens when using the api + cloud



    so i dont think there is something fundamentally wrong with the adapter?

    Screenshot_2023-07-03_at_10.27.11.png
  • discord user avatar
    denolfe
    last year

    Yes, that does narrow it down a bit. I don't see anything wrong with your adapter. Are you deploying this somewhere, or is this behavior showing locally?

  • default discord avatar
    teamkubelast year

    we deploy to vercel but this also happens when we use the s3 adapter locally on our development machines (all team members)



    seems to be a general issue with upload through the api + cloud plugin & s3Adapter

  • discord user avatar
    denolfe
    last year

    That definitely seems odd. Can you do a writeup as an issue here?

    https://github.com/payloadcms/plugin-cloud-storage/issues

    I can look into recreating.

  • default discord avatar
    teamkubelast year

    Jup, setting up a minimal reproducable example atm

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.