Learn how to integrate Lovable with Backblaze B2 Cloud Storage using our simple, step-by-step guide for a seamless setup and efficient data management solution.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
package.json
file and add the following dependency entry:
{
"dependencies": {
"backblaze-b2": "^2.0.0"
}
}
src
folder called backblazeService.ts
. This file will contain all the functions and configurations necessary to interact with Backblaze B2.src/backblazeService.ts
:
import B2 from 'backblaze-b2';
export interface B2Config {
accountId: string;
applicationKey: string;
bucketId: string;
}
export class BackblazeService {
private b2: B2;
private bucketId: string;
constructor(config: B2Config) {
// Initialize the B2 client with your account credentials
this.b2 = new B2({
accountId: config.accountId,
applicationKey: config.applicationKey
});
this.bucketId = config.bucketId;
}
// Authorize with Backblaze B2
public async authorize(): Promise {
try {
await this.b2.authorize();
} catch (error) {
console.error('Authorization failed:', error);
throw error;
}
}
// Upload a file to the configured bucket
public async uploadFile(fileName: string, fileData: Buffer | Blob): Promise {
try {
// Get upload URL and auth token
const uploadUrlResponse = await this.b2.getUploadUrl({ bucketId: this.bucketId });
const { uploadUrl, authorizationToken } = uploadUrlResponse.data;
// Upload file
const uploadResponse = await this.b2.uploadFile({
uploadUrl,
uploadAuthToken: authorizationToken,
fileName,
data: fileData
});
return uploadResponse.data;
} catch (error) {
console.error('Upload failed:', error);
throw error;
}
}
}
src/fileUploader.ts
).
// Import the Backblaze service
import { BackblazeService, B2Config } from './backblazeService';
// Set up your Backblaze credentials
const b2Config: B2Config = {
accountId: 'YOURACCOUNTID',
applicationKey: 'YOURAPPLICATIONKEY',
bucketId: 'YOURBUCKETID'
};
// Initialize the Backblaze service instance
const backblazeService = new BackblazeService(b2Config);
// Authorize with Backblaze when your module initializes
backblazeService.authorize().then(() => {
console.log('Backblaze authorized successfully.');
}).catch((error) => {
console.error('Error during Backblaze authorization:', error);
});
YOURACCOUNTID
, YOURAPPLICATIONKEY
, and YOURBUCKETID
with your actual Backblaze credentials.
src/fileUploader.ts
) or any other appropriate module, add a function that uses the BackblazeService to upload a file. For example:
export async function handleFileUpload(file: File): Promise {
try {
// Convert the File (from an input element) into a Blob or Buffer
const fileData = await file.arrayBuffer();
const bufferData = Buffer.from(fileData);
// Call the uploadFile method
const response = await backblazeService.uploadFile(file.name, bufferData);
console.log('File uploaded successfully:', response);
} catch (error) {
console.error('Error during file upload:', error);
}
}
File
object (which you might get from an HTML file input) and uploads it to Backblaze. Ensure you adjust the logic if your file data is already in a suitable format.
src/components/UploadComponent.tsx
).handleFileUpload
function at the top of the component file:
import { handleFileUpload } from '../fileUploader';
handleFileUpload
when a file is selected. For example:
function onFileChange(event: React.ChangeEvent): void {
const files = event.target.files;
if (files && files[0]) {
handleFileUpload(files[0]);
}
}
<input type="file" onChange={onFileChange} />
package.json
and compile your TypeScript files.When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.