Files
Upload, download, and manage files in your DYPAI storage buckets.
Uploading Files
From Browser (JavaScript)
// Get file from input
const fileInput = document.querySelector('input[type="file"]');
const file = fileInput.files[0];
// Upload to storage
const response = await fetch(
'https://your-project.dypai.io/storage/v1/object/avatars/' + file.name,
{
method: 'POST',
headers: {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': file.type
},
body: file
}
);
const data = await response.json();
console.log('Uploaded:', data.Key);With Path/Folder
// Upload to a specific folder
const userId = 'user-123';
const path = userId + '/profile.jpg';
const response = await fetch(
'https://your-project.dypai.io/storage/v1/object/avatars/' + path,
{
method: 'POST',
headers: {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': file.type
},
body: file
}
);Upsert (Update or Insert)
// Replace file if it exists
const response = await fetch(
'https://your-project.dypai.io/storage/v1/object/avatars/profile.jpg',
{
method: 'POST',
headers: {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': file.type,
'x-upsert': 'true' // Enable upsert
},
body: file
}
);Use
x-upsert: true to automatically replace existing files with the same name.Downloading Files
Public Files
// Direct URL for public files
const publicUrl = 'https://your-project.dypai.io/storage/v1/object/public/avatars/profile.jpg';
// Use in HTML
<img src={publicUrl} alt="Profile" />Private Files (Signed URLs)
// Create a signed URL for private files
const response = await fetch(
'https://your-project.dypai.io/storage/v1/object/sign/documents/report.pdf',
{
method: 'POST',
headers: {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
},
body: JSON.stringify({
expiresIn: 3600 // URL valid for 1 hour
})
}
);
const { signedURL } = await response.json();
console.log('Download at:', signedURL);Signed URLs are temporary and expire after the specified time.
Download with Authentication
// Download with auth header
const response = await fetch(
'https://your-project.dypai.io/storage/v1/object/documents/report.pdf',
{
headers: {
'Authorization': 'Bearer ' + accessToken
}
}
);
const blob = await response.blob();
const url = URL.createObjectURL(blob);
// Trigger download
const a = document.createElement('a');
a.href = url;
a.download = 'report.pdf';
a.click();Listing Files
// List files in a bucket
const response = await fetch(
'https://your-project.dypai.io/storage/v1/object/list/avatars',
{
method: 'POST',
headers: {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
},
body: JSON.stringify({
prefix: '', // Filter by path prefix
limit: 100, // Max results
offset: 0, // Pagination offset
sortBy: {
column: 'created_at',
order: 'desc'
}
})
}
);
const files = await response.json();
files.forEach(file => {
console.log(file.name, file.metadata.size, file.created_at);
});List Files in Folder
// List files in a specific folder
const response = await fetch(
'https://your-project.dypai.io/storage/v1/object/list/documents',
{
method: 'POST',
headers: {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
},
body: JSON.stringify({
prefix: 'invoices/2024/',
limit: 50
})
}
);Deleting Files
Delete Single File
// Delete a file
const response = await fetch(
'https://your-project.dypai.io/storage/v1/object/avatars/old-profile.jpg',
{
method: 'DELETE',
headers: {
'Authorization': 'Bearer ' + accessToken
}
}
);Delete Multiple Files
// Delete multiple files at once
const response = await fetch(
'https://your-project.dypai.io/storage/v1/object/avatars',
{
method: 'DELETE',
headers: {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
},
body: JSON.stringify({
prefixes: [
'temp/file1.jpg',
'temp/file2.jpg',
'temp/file3.jpg'
]
})
}
);Deleted files cannot be recovered. Consider implementing soft deletes for important files.
Moving/Renaming Files
// Move or rename a file
const response = await fetch(
'https://your-project.dypai.io/storage/v1/object/move',
{
method: 'POST',
headers: {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
},
body: JSON.stringify({
bucketId: 'documents',
sourceKey: 'drafts/report.pdf',
destinationKey: 'published/report-final.pdf'
})
}
);Copying Files
// Copy a file
const response = await fetch(
'https://your-project.dypai.io/storage/v1/object/copy',
{
method: 'POST',
headers: {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
},
body: JSON.stringify({
bucketId: 'templates',
sourceKey: 'invoice-template.pdf',
destinationKey: 'invoices/2024/invoice-001.pdf'
})
}
);File Metadata
// Get file metadata
const response = await fetch(
'https://your-project.dypai.io/storage/v1/object/info/avatars/profile.jpg',
{
headers: {
'Authorization': 'Bearer ' + accessToken
}
}
);
const metadata = await response.json();
console.log({
size: metadata.metadata.size,
mimeType: metadata.metadata.mimetype,
createdAt: metadata.created_at,
updatedAt: metadata.updated_at
});Image Transformations
Transform images on-the-fly:
// Resize image
const url = 'https://your-project.dypai.io/storage/v1/render/image/public/avatars/profile.jpg'
+ '?width=200&height=200';
// Other transformations
const resized = url + '?width=400';
const cropped = url + '?width=200&height=200&resize=cover';
const quality = url + '?quality=80';
const format = url + '?format=webp';| Parameter | Description | Example |
|---|---|---|
width | Resize width | ?width=200 |
height | Resize height | ?height=200 |
resize | Resize mode | ?resize=cover |
quality | JPEG quality (1-100) | ?quality=80 |
format | Output format | ?format=webp |
File Size Limits
| Plan | Default Max Size |
|---|---|
| Free | 50 MB per file |
| Pro | 500 MB per file |
| Enterprise | 5 GB per file |
Configure per-bucket file size limits for more control.