Log in with your Google account if you’re not already logged in.
Navigate to the Service Account Tab:
Choose the Firebase project for which you want to create a service account.
Click on the gear icon nect to ‘Project Overview’ to open the ‘Project settings’.
In the Project settings, go to the ‘Service accounts’ tab.
Generate New Private Key:
You’ll find an option to generate a new private key in the Firebase Admin SDK section of the Service Accounts tab.
Click on ‘Generate new private key’, and a prompt will appear warning you to keep your private key secure and not to share it. Comfirm to proceed.
Once you confirm, a JSON file will be downloaded to your computer. This is your serviceAccountKey.json file. Store this file securely. It gives administrative access to your Firebase project, so it should never be shared or exposed publicly.
Firestore
Export Data from Firestore Emulator
Make sure your Firestore emulator is running
Use the firebase emulators: export [export_directory] command to export your data. This will save your emulator data (including Firestore data) to the specified directory.
Sample Script
A sample script that reads all collections in Firestore Emulator, and stores it into a JSON file:
// Initialize Firebase Admin SDK admin.initializeApp({ projectId: 'xxxx-xxx', // Your Product ID ...firebaseConfig, credential: admin.credential.applicationDefault(), });
// Connect to Firestore Emulator const db = admin.firestore(); db.settings({ host: 'localhost:8080', // Replace with your Firestore emulator host and port ssl: false, });
// Function to get all documents from a collection asyncfunctiongetAllDocuments(collectionRef) { const snapshot = await collectionRef.get(); const documents = {}; snapshot.forEach((doc) => { documents[doc.id] = doc.data(); }); return documents; }
// Function to read all collections and their documents asyncfunctionreadAllCollections() { const collections = {}; const collectionsSnapshot = await db.listCollections(); for (const collection of collectionsSnapshot) { const documents = awaitgetAllDocuments(collection); collections[collection.id] = documents; } return collections; }
// Function to save data to JSON file functionsaveDataToFile(data) { const exportDir = './export';
// Create export directory if it doesn't exist if (!fs.existsSync(exportDir)) { fs.mkdirSync(exportDir); }
// Function to upload file to Firebase Storage asyncfunctionuploadFile(filePath) { const destination = path.basename(filePath); // Or specify your desired path in the bucket
// Uploads a local file to the bucket await bucket.upload(filePath, { destination, public: true, // if you want the file to be publicly accessible, set this to true metadata: { cacheControl: 'public, max-age=31536000', // example metadata }, });
console.log(`${filePath} uploaded to ${bucketName}/${destination}`); }
// Example usage const localFilePath = './path/to/local/file'; // Path to the file you want to upload uploadFile(localFilePath) .then(() =>console.log('File uploaded successfully')) .catch(err =>console.error('Error uploading file:', err));