Learn how to integrate Bolt.new AI with Backblaze B2 Cloud Storage using our easy step-by-step guide to streamline data management and boost efficiency.
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 in your Bolt.new AI project.
{
"dependencies": {
"backblaze-b2": "^1.2.0"
}
}
backblazeB2.ts
in your project’s src
folder (or equivalent folder where your TypeScript code is maintained).backblazeB2.ts
:
import B2 from 'backblaze-b2';
// Initialize the Backblaze B2 client using your environment variables.
// Ensure these environment variables (B2KEYID and B2APPKEY) are set in Bolt.new AI's secrets.
const b2 = new B2({
applicationKeyId: process.env.B2KEYID,
applicationKey: process.env.B2APPKEY,
});
// Function to authorize with Backblaze B2.
export async function authorize() {
try {
const authResponse = await b2.authorize();
console.log('Backblaze B2 authorized successfully');
return authResponse;
} catch (error) {
console.error('Authorization error:', error);
throw error;
}
}
// Function to upload a file to a specified bucket.
// bucketId: Your Backblaze bucket ID.
// fileName: The name you want to assign to the file in B2.
// fileContent: The file data in Buffer format.
export async function uploadFile(bucketId: string, fileName: string, fileContent: Buffer) {
try {
// Retrieve the upload URL and token.
const uploadUrlResponse = await b2.getUploadUrl({ bucketId });
const { uploadUrl, authorizationToken } = uploadUrlResponse.data;
// Upload the file.
const response = await b2.uploadFile({
uploadUrl,
uploadAuthToken: authorizationToken,
fileName,
data: fileContent
});
console.log('File uploaded successfully');
return response.data;
} catch (error) {
console.error('File upload error:', error);
throw error;
}
}
index.ts
or similar) in your project.backblazeB2.ts
:
import { authorize, uploadFile } from './backblazeB2';
async function initBackblaze() {
try {
await authorize();
// Optionally, call uploadFile here or later when needed.
// For example, to upload a file, you might do:
// const fileBuffer = Buffer.from('your file content here');
// await uploadFile('your-bucket-id', 'example.txt', fileBuffer);
} catch (error) {
console.error('Error initializing Backblaze integration:', error);
}
}
initBackblaze();
process.env.B2KEYID
and process.env.B2APPKEY
available to your TypeScript code.
uploadFile
to confirm that file operations work as expected.When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.