File uploads for both Attachment fields and File collections are supported via the HTTP API. The method of invocation differs depending on the storage engine used by the Attachment field or File collection.
For built-in open-source storage engines in the project, such as S3, OSS, and COS, the HTTP API call is the same as the user interface upload function, and files are uploaded via the server. Calling the API requires passing a user-login-based JWT token through the Authorization request header; otherwise, access will be denied.
Initiate a create action on the attachments resource (attachments), send a POST request, and upload the binary content through the file field. After the call, the file will be uploaded to the default storage engine.
To upload a file to a different storage engine, you can use the attachmentField parameter to specify the storage engine configured for the collection field (if not configured, it will be uploaded to the default storage engine).
Uploading to a File collection will automatically generate a file record. Initiate a create action on the File collection resource, send a POST request, and upload the binary content through the file field.
When uploading to a File collection, there is no need to specify a storage engine; the file will be uploaded to the storage engine configured for that collection.
For S3-compatible storage engines provided through the commercial S3-Pro plugin, the HTTP API upload needs to be called in several steps.
Get Storage Engine Information
Initiate a getBasicInfo action on the storages collection (storages), carrying the storage name, to request the storage engine's configuration information.
Example of returned storage engine configuration information:
Get Presigned Information from the Service Provider
Initiate a createPresignedUrl action on the fileStorageS3 resource, send a POST request, and include file-related information in the body to obtain the presigned upload information.
Note:
- name: File name
- size: File size (in bytes)
- type: The MIME type of the file. You can refer to: Common MIME types
- storageId: The ID of the storage engine (the
idfield returned in the first step)- storageType: The type of the storage engine (the
typefield returned in the first step)Example request data:
The data structure of the obtained presigned information is as follows:
File Upload
Use the returned putUrl to initiate a PUT request and upload the file as the body.
Note:
- putUrl: The
putUrlfield returned in the previous step- file_path: The local path of the file to be uploaded
Example request data:
Create File Record
After a successful upload, initiate a create action on the attachments resource (attachments) by sending a POST request to create the file record.
Description of dependent data in data-raw:
- title: The
fileInfo.titlefield returned in the previous step- filename: The
fileInfo.keyfield returned in the previous step- extname: The
fileInfo.extnamefield returned in the previous step- path: Empty by default
- size: The
fileInfo.sizefield returned in the previous step- url: Empty by default
- mimetype: The
fileInfo.mimetypefield returned in the previous step- meta: The
fileInfo.metafield returned in the previous step- storageId: The
idfield returned in the first stepExample request data:
The first three steps are the same as for Attachment field uploads, but in the fourth step, you need to create a file record by initiating a create action on the File collection resource, sending a POST request, and uploading the file information via the body.
Description of dependent data in data-raw:
- title: The
fileInfo.titlefield returned in the previous step- filename: The
fileInfo.keyfield returned in the previous step- extname: The
fileInfo.extnamefield returned in the previous step- path: Empty by default
- size: The
fileInfo.sizefield returned in the previous step- url: Empty by default
- mimetype: The
fileInfo.mimetypefield returned in the previous step- meta: The
fileInfo.metafield returned in the previous step- storageId: The
idfield returned in the first stepExample request data: