platformOS Community

Programatically upload an existing asset to a record?

Matt Jones Apr 01 2021 at 15:59

I'm working on a migration project where I have 200 .pdf files from another platform and I want to upload them all to their own secure S3 link- storing that link as an "upload" type property on a record- so when needed I can generate a secure link with an expiry time.

I know how to do this by manually uploading each one to an individual form, but would it be possible to do this programatically to save me time?

Pawel Apr 01 2021 at 22:58

The best way to migrate data, files, schemas etc. is to use:

  • pos-cli uploads push - to send zipped with files to our storage
  • pos-cli data import - to send schema and models that will reference those files

Not only it is the fastest way, it is probably the easiest. This is what we do in our template:

Push

pos-cli uploads push <YOUR_ENV_NAME> --path=pdfs.zip

Files will land on the storage under path instances/:instance_id/property_uploads/<your path> because .zip file will be extracted in place and kept the structure inside of it.
So you can structure directories to nest files under files/pdf/2020/ and it will respect it and extract it to instances/<instance_id>/property_uploads/files/pdf/2020/myfile.pdf

Import

pos-cli data import <YOUR_ENV_NAME> --path=data.zip --zip

You need to create schema and records in it to connect references to your files. Your schema (ie. pdfs must include property (ie. file) with type upload. upload_url of that record will be url of your file (which you already know what will be, because it it deterministic).

Each record will look more or less something like this:

id,properties,model_schema,created_at,updated_at
{"file":{"path":"files/pdf/2020/myfile.pdf","versions":{},"extension":"pdf","file_name":"file.pdf"},"label":"my pdf file from 2020" },pdfs,2021-03-31 13:17:29 UTC,2021-03-31 13:17:29 UTC

Read more on uploads here: https://documentation.platformos.com/developer-guide/user-uploads/user-uploads

You can take a look at our template repo to see how we did it (to generate seeds you need to clone the repo):
https://github.com/mdyd-dev/product-marketplace-template/tree/master/seed

  • Matt Jones Apr 06 2021 at 08:09
    Thanks Pawel, that's a really helpful answer!
Please sign in or fill up your profile to answer a question