Single file upload
Uploading programmatically
Create Presigned URL
In order to upload a file programmatically, you need to have a presigned URL. A presigned URL is a URL that grants temporary access to a specific resource in a storage service, allowing users to upload or download a file without needing direct access to the service’s credentials.
You can create a presigned using the File Storage Action via the Upload Url.
The Upload type
needs to be set to Single file upload
, otherwise it will initiate a multipart upload, instead of creating a presigned url and the single file upload won’t work.
This will return the url that is then used for uploading the file to your bucket.
Step-By-Step Instructions
If you want to upload a file from your own application, you can do so by simply sending a put request to the presigend url you receive from the File Storage Action via Upload Url. The file needs to be set as request body and a Content-Type header with the respective file type needs to be set as well.
-
Get the File:
The example below uses a simple HTML file input element.
Example:const file = document.getElementById('fileInput').files[0]
-
Send a PUT request with the File to the presigned URL:
The example below uses the fetch API, but you can use any other method to send the request, such as Axios or XMLHttpRequest.
Theurl
that the request is sent to, is the url that you receive in the Create Presigned URL step. -
Error handling:
The example below uses a simple check if the response was ok and prints the error message to the console.
You can adjust this to your needs and add a custom retry logic if needed.