How to upload files to API
- Request a temporary URL for upload using
/file/upload/get-presigned-url
. This will return JSON withpresignedUrl
property that contains a link you should upload into usingPUT
andurl
property that contains a link to the uploaded file. - Upload your file using
PUT
to thispresignedUrl
link generated on the previous step. Once finished, your file can be accessed using the link fromurl
link from the step1
. - Congrats! Now you can access your uploaded file using that link from
url
param from step1
. Now you can call any API method with your link set asurl
param.
Security Note: Uploaded files are auto-removed within 60
minutes by default. You can also remove your file earlier using /file/delete
endpoint.
On-Prem Version only: Depending on your settings, your files are stored on your server or in the cloud storage of your choice or on your file server. If you use cloud storage (like AWS S3) then you need to set up an auto-removal policy for uploaded files or remove files using /file/delete
endpoint.
Example
! Don't forget to set x-api-key
url param or http header param (preferred) to API key, get yours here
curl --location --request GET [https://api.pdf.co/v1/file/upload/get-presigned-url?name=test.pdf&encrypt=true](https://api.pdf.co/v1/file/upload/get-presigned-url?name=test.pdf&encrypt=true)
--header 'x-api-key: YOUR_API_KEY'
200
{
"presignedUrl": "https://pdf-temp-files.s3-us-west-2.amazonaws.com/28e191b14f3a4f43b16fcef1d53d191e/test.pdf?X-Amz-Expires=900&....",
"url": "https://pdf-temp-files.s3-us-west-2.amazonaws.com/28e191b14f3a4f43b16fcef1d53d191e/test.pdf?X-Amz-Expires=....",
}
// Now use PUT to upload file to "presignedUrl" link:
curl --location --request PUT '<insert presignedUrl here>' \
--header 'x-api-key: YOUR_API_KEY' \
--header 'Content-Type: application/octet-stream' \
--data-binary '@./sample.pdf'
200
{
// 200 means all was uploaded OK
}
Now your file is available via the link from url
param from the first step.
IMPORTANT: don't forget to add Content-Type
header with applicaton/octet-stream
value. Otherwise your uploaded files can become damaged.
Incorrect
function uploadFile(file){
...
const formData = new FormData();
formData.append("file", file);
await axios({
method: 'PUT',
url,
data: formData,
headers: {'Content-Type': 'multipart/form-data'}
})
...
}
Correct
function uploadFile(file){
...
// upload file data directly
// [https://github.com/axios/axios#axiospatchurl-data-config](https://github.com/axios/axios#axiospatchurl-data-config)
await axios({
method: 'PUT',
url: url,
data: file,
headers: {'Content-Type': 'application/octet-stream'}
})
...
}