Upload and Download Your First File
Use the JavaScript SDK for Object Storage to upload a file to a bucket and download it as a browser Blob containing the file bytes.
Before You Begin
Prepare the following:
- A project with a file system named
app-files. To create one, see Storage Setup and Configuration. - An initialized JavaScript
clientfor that project. If you enabled storage after registering your app, get the app configuration from the Oracle Backend with Supabase APIs Console again. See Initialize the JavaScript SDK. - A PNG image to select from the browser file input in the example.
- A page served over HTTPS or from localhost so the browser can use
crypto.randomUUID()to create an object name. - Oracle Deep Data Security (DDS) Data Grants that allow the active Data Role to upload to
uploads/withINSERTandUPDATEfor aputrequest and download from the same path withSELECT. A Data Role determines which grants apply to a request. The examples that follow use the authenticated Data Role, so sign in before you run the file example. See Storage Access Control with DDS.
For the upload permission, use the authenticated image upload grant example. It authorizes put requests under uploads/ when the content type matches image/%. To authorize the download from the same path, create a matching SELECT grant for the authenticated Data Role:
CREATE OR REPLACE DATA GRANT "quickstart image downloads" AS
SELECT ON BAAS_STORAGE_OBJECTS
WHERE object_name LIKE 'uploads/%'
AND content_type LIKE 'image/%'
TO BAAS_USER$AUTHENTICATED_ROLE;
Use the Data Role name created for your project’s schema. This read grant adapts the documented image read grant to the quickstart’s uploads/ path and authenticated role. The selected PNG has the image/png content type, which matches image/%.
Choose a Bucket and File Path
This quickstart uses BASE storage. The same SDK bucket-and-path pattern applies to a linked OCI Object Storage bucket.
The SDK selects a bucket with client.storage.from(bucketId). When you create a BASE file system named app-files in the Console, the generated app configuration uses app-files as its storage_bucket value. Pass the same name as bucketId.
The example creates a different path under uploads/ for each selected PNG and passes that path to both upload() and download(). The uploads/ prefix groups files and can be used in a Data Grant predicate. It doesn’t grant access by itself.
If you use another bucket name, replace app-files in the code with the storage_bucket value from your app configuration. If you choose another path, make sure your Data Grants cover it.
Upload and Download the File
Add this file input to your web page:
<label for="storage-file">Choose a PNG image</label>
<input id="storage-file" type="file" accept="image/png">
Add the following JavaScript to the same module that initializes client. Load that module with a <script type="module"> element after the file input, so client is in scope and the input exists when querySelector() runs. When you select a PNG image, the code gets its File, uploads it, and downloads the same path as a Blob.
const input = document.querySelector("#storage-file");
input.addEventListener("change", async () => {
const file = input.files?.[0];
if (!file) return;
if (file.type !== "image/png") {
console.error("Select a PNG image.");
return;
}
const files = client.storage.from("app-files");
try {
const path = `uploads/${crypto.randomUUID()}.png`;
const upload = await files.upload(path, file, {
contentType: file.type,
});
if (upload.error) throw upload.error;
const download = await files.download(path);
if (download.error) throw download.error;
console.log("Stored path:", path);
console.log("Downloaded bytes:", download.data.size);
} catch (error) {
console.error("Storage request failed:", error.message);
}
});
Storage methods return { data, error }. Check error before using data. If either result contains error, the code throws that RelationalStorageError, and the catch block displays its message. If a request is denied, check the active Data Role and the Data Grants for the operation, path, and content type. For more failure cases, see Errors and Troubleshooting.
Next Steps
For listing, metadata, signed URLs, and resumable uploads, see Using Storage SDK. To manage stored objects in the Oracle Backend with Supabase APIs Console, see Manage Files in the Console.