Local API, Seed Uploads without Physical Files

default discord avatar
zoul0813
2 months ago
5

I am currently writing a migration script to move data out of WordPress and into Payload. I'm currently exporting all of the WP Data into JSON files and then have a

seed.ts

script that uses the Payload Local API.



This is working great for things like Posts, Pages, Taxonomy, etc ...



The problem I'm having is with my images. I would like to be able to run this seed script over and over, as I continue to expand on what is being exported and how it's being implemented in Payload.



The problem I have is that when I try to seed images into an upload collection, it requires the physical file. In my case, I'm moving these files to S3 and using the

cloud-storage

plugin. I'd like to just call

payload.create({collection: 'images', data: imageData})

without needing to actually upload or process the physical files ... as I've already processed them (and we're talking about 7GB+ of media assets)



What I'm hoping for, is a flag I can pass to

payload.create

that tells it to ignore the file processing, and just add the record to MongoDB



I attempted to use

overwriteExistingFiles: false

... my collection also does not have versions enabled - so passing

draft: true

won't work ... and I'm thinking even if I had versions enabled, the result would be a drafted upload that can't be attached to my other documents until it's published, which will likely require the physical file again?



Any assistance on how to best approach this would be greatly appreciated, thanks.

  • discord user avatar
    dribbens
    Payload Team
    2 months ago

    We don't currently have an option that the files wouldn't be written.



    If this is a one time operational task, I would comment out the use of the cloud-storage plugin from your config and enable

    disableLocaleStorage

    on your upload. Then you should be able to call create without actually making any files. If this is something you often do, then you'll want a way to set an env variable or something in your own code that you can conditionally.



    Payload will still need to read the files to populate the database with things like image width and height. Also if you've configured image resizing in your collection, it will make resized files with generated filenames that may be different from your source.



    I hope this helps!

  • default discord avatar
    zoul0813
    2 months ago

    @dribbens thanks - unfortunately, Payload appears to require physical access to the file - even if I disable local storage. I'm unable to call

    payload.create

    and just feed it the raw information ...



    One idea that I'm considering at the moment is having two separate collections, and dynamically returning one or the other based on whether I'm seeding data or launching Payload.



    If I'm seeding, I'll just return a generic collection that isn't upload enabled, but defines all the same fields as an upload collection (filename, filesize, mimetype, width, height) and then I can just seed the data as if it were a regular document ... then when I launch Payload, I return the upload enabled version of the same collection.



    I'm not sure if swapping out collection configs like this dynamically will cause problems ... but so far, it looks like this thought may work.

  • discord user avatar
    dribbens
    Payload Team
    2 months ago

    I don't think it will cause you any issues having the upload feature toggle on/off this way. If you have the data for those extra fields, adding them manually is a good option.

  • default discord avatar
    zoul0813
    2 months ago

    FYI, I've tracked all of this down to

    generateFileData

    which is being passed a "throwOnMissingFile" argument that is set to whether or not the document is a draft ... my images are not version enabled with drafts enabled, so generateFileData throws an exception ...



    What I think could happen here, is generateFileData could look at the data that came in ... and if the file doesn't exist, but the file information is provided ... it can just use that?



    @dribbens swapping out the collection with a non-upload with some simple logic seems to have solved the problem ... seems a bit janky, but it works - thanks!

  • discord user avatar
    dribbens
    Payload Team
    2 months ago

    It does seem less than ideal. An alternative approach would be to use

    payload.collections[slug].Model

    directly. That would let you do your DB inserts without relying on Payload's file handling, the risk there is that you may have hooks and other behavior in your collection config that wouldn't be called.

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.