Install the SDK
npm install @jupiter-cloud/sdkInitialize the client
Initialize the Jupiter client with the API base URL and the Project ID shown in the Jupiter Dashboard. Storage is available from client.storage.
import { Jupiter } from '@jupiter-cloud/sdk' const client = new Jupiter( 'https://api.jupitercloud.co', 'project-id', 'optional-team-api-key')Create a bucket
A bucket groups objects and defines their location and upload rules. The name and location are required. The remaining settings are optional.
const result = await client.storage.createBucket({ name: 'my-first-bucket', location: 'weur', public: false, allowedMimeTypes: ['image/jpeg', 'image/png'], fileSizeLimit: 10 * 1024 * 1024, attributes: { purpose: 'user-uploads' }}) if (result.error) { throw result.error} console.log(result.data)Create a policy for the bucket
Attach a Jupiter Security Policy that permits authenticated users to upload and retrieve objects allowed by your application.
permit ( principal, action in [ JupiterStorage::Action::"UploadObject", JupiterStorage::Action::"GetObject" ], resource)when { context.auth.user.id == resource.owner_id};Upload the first object
Pass the bucket name, object key, and upload body in one object. In a browser, a File selected through an input can be used directly as the body.
const input = document.querySelector<HTMLInputElement>('#file-input')const selectedFile = input?.files?.[0] if (!selectedFile) { throw new Error('Select a file before uploading')} const result = await client.storage.uploadObject({ bucketName: 'my-first-bucket', key: selectedFile.name, body: selectedFile, contentType: selectedFile.type}) if (result.error) { throw result.error} console.log(result.data.object)console.log(result.data.uploaded)