অ্যাডমিন SDK-এর সাহায্যে, আপনি সম্পূর্ণ অ্যাডমিন প্রিভিলেজ সহ অথবা আরও সূক্ষ্ম সীমিত প্রিভিলেজ সহ রিয়েলটাইম ডেটাবেসের ডেটা পড়তে ও লিখতে পারবেন। এই ডকুমেন্টে, আমরা আপনাকে Firebase Realtime Database অ্যাক্সেস করার জন্য আপনার প্রোজেক্টে ফায়ারবেস অ্যাডমিন SDK যোগ করার প্রক্রিয়াটি ধাপে ধাপে দেখাব।
অ্যাডমিন এসডিকে সেটআপ
আপনার সার্ভারে ফায়ারবেস রিয়েলটাইম ডেটাবেস ব্যবহার শুরু করতে, আপনাকে প্রথমে আপনার পছন্দের ভাষায় ফায়ারবেস অ্যাডমিন এসডিকে সেট আপ করতে হবে।
অ্যাডমিন এসডিকে প্রমাণীকরণ
Firebase Admin SDK ব্যবহার করে কোনো সার্ভার থেকে Firebase Realtime Database অ্যাক্সেস করার আগে, আপনাকে অবশ্যই Firebase-এর সাথে আপনার সার্ভারটিকে প্রমাণীকরণ করতে হবে। যখন আপনি একটি সার্ভার প্রমাণীকরণ করেন, তখন ক্লায়েন্ট অ্যাপের মতো ব্যবহারকারী অ্যাকাউন্টের ক্রেডেনশিয়াল দিয়ে সাইন ইন করার পরিবর্তে, আপনি একটি সার্ভিস অ্যাকাউন্ট দিয়ে প্রমাণীকরণ করেন, যা Firebase-এর কাছে আপনার সার্ভারকে শনাক্ত করে।
Firebase Admin SDK ব্যবহার করে প্রমাণীকরণের মাধ্যমে আপনি দুই ধরনের অ্যাক্সেস পেতে পারেন:
| ফায়ারবেস অ্যাডমিন এসডিকে প্রমাণীকরণ অ্যাক্সেস স্তর | |
|---|---|
| প্রশাসনিক সুযোগ-সুবিধা | একটি প্রোজেক্টের Realtime Database সম্পূর্ণ পঠন ও লিখন অ্যাক্সেস। ডেটা মাইগ্রেশন বা পুনর্গঠনের মতো প্রশাসনিক কাজ সম্পন্ন করতে সতর্কতার সাথে ব্যবহার করুন, যেগুলোর জন্য আপনার প্রোজেক্টের রিসোর্সগুলিতে অবাধ অ্যাক্সেস প্রয়োজন। |
| সীমিত সুবিধা | একটি প্রোজেক্টের Realtime Database অ্যাক্সেস, যা শুধুমাত্র আপনার সার্ভারের প্রয়োজনীয় রিসোর্সের মধ্যেই সীমাবদ্ধ। সুনির্দিষ্ট অ্যাক্সেস প্রয়োজনীয়তা রয়েছে এমন প্রশাসনিক কাজ সম্পন্ন করতে এই স্তরটি ব্যবহার করুন। উদাহরণস্বরূপ, যখন একটি সামারাইজেশন জব চালানো হয় যা পুরো ডেটাবেস থেকে ডেটা রিড করে, তখন আপনি একটি রিড-অনলি সিকিউরিটি রুল সেট করে এবং তারপর সেই রুল দ্বারা সীমিত প্রিভিলেজ সহ অ্যাডমিন SDK ইনিশিয়ালাইজ করে অনিচ্ছাকৃত রাইট থেকে সুরক্ষা দিতে পারেন। |
অ্যাডমিন অধিকার দিয়ে প্রমাণীকরণ করুন
যখন আপনি আপনার Firebase প্রোজেক্টে Editor রোল থাকা কোনো সার্ভিস অ্যাকাউন্টের ক্রেডেনশিয়াল ব্যবহার করে Firebase Admin SDK চালু করেন, তখন সেই ইনস্ট্যান্সটি আপনার প্রোজেক্টের Realtime Database সম্পূর্ণ রিড এবং রাইট অ্যাক্সেস পায়।
জাভা
// Initialize the SDK with Application Default Credentials FirebaseOptions options = FirebaseOptions.builder() .setCredentials(GoogleCredentials.getApplicationDefault()) // The database URL depends on the location of the database .setDatabaseUrl("https://DATABASE_NAME.firebaseio.com") .build(); FirebaseApp.initializeApp(options); // As an admin, the app has access to read and write all data, regardless of Security Rules DatabaseReference ref = FirebaseDatabase.getInstance() .getReference("restricted_access/secret_document"); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { Object document = dataSnapshot.getValue(); System.out.println(document); } @Override public void onCancelled(DatabaseError error) { } });
নোড.জেএস
import { initializeApp } from 'firebase-admin/app'; import { getDatabase } from 'firebase-admin/database'; // Initialize the app with Application Default Credentials, granting admin privileges initializeApp({ // The database URL depends on the location of the database databaseURL: "https://DATABASE_NAME.firebaseio.com" }); // As an admin, the app has access to read and write all data, regardless of Security Rules const db = getDatabase(); const ref = db.ref("restricted_access/secret_document"); ref.once("value", (snapshot) => { console.log(snapshot.val()); });
পাইথন
import firebase_admin from firebase_admin import credentials from firebase_admin import db # Fetch the service account key JSON file contents cred = credentials.Certificate('path/to/serviceAccountKey.json') # Initialize the app with a service account, granting admin privileges firebase_admin.initialize_app(cred, { 'databaseURL': 'https://databaseName.firebaseio.com' }) # As an admin, the app has access to read and write all data, regradless of Security Rules ref = db.reference('restricted_access/secret_document') print(ref.get())
যান
ctx := context.Background() conf := &firebase.Config{ DatabaseURL: "https://databaseName.firebaseio.com", } // Fetch the service account key JSON file contents opt := option.WithCredentialsFile("path/to/serviceAccountKey.json") // Initialize the app with a service account, granting admin privileges app, err := firebase.NewApp(ctx, conf, opt) if err != nil { log.Fatalln("Error initializing app:", err) } client, err := app.Database(ctx) if err != nil { log.Fatalln("Error initializing database client:", err) } // As an admin, the app has access to read and write all data, regradless of Security Rules ref := client.NewRef("restricted_access/secret_document") var data map[string]interface{} if err := ref.Get(ctx, &data); err != nil { log.Fatalln("Error reading from database:", err) } fmt.Println(data)
সীমিত অধিকার সহ প্রমাণীকরণ করুন
একটি উত্তম অনুশীলন হিসেবে, একটি সার্ভিসের শুধুমাত্র তার প্রয়োজনীয় রিসোর্সগুলিতেই অ্যাক্সেস থাকা উচিত। একটি Firebase অ্যাপ ইনস্ট্যান্স কোন কোন রিসোর্স অ্যাক্সেস করতে পারবে তার উপর আরও সূক্ষ্ম নিয়ন্ত্রণ পেতে, আপনার সার্ভিসকে উপস্থাপন করার জন্য আপনার সিকিউরিটি রুলস -এ একটি অনন্য আইডেন্টিফায়ার ব্যবহার করুন। তারপর উপযুক্ত নিয়ম সেট আপ করুন যা আপনার সার্ভিসকে তার প্রয়োজনীয় রিসোর্সগুলিতে অ্যাক্সেস দেবে। উদাহরণস্বরূপ:
{
"rules": {
"public_resource": {
".read": true,
".write": true
},
"some_resource": {
".read": "auth.uid === 'my-service-worker'",
".write": false
},
"another_resource": {
".read": "auth.uid === 'my-service-worker'",
".write": "auth.uid === 'my-service-worker'"
}
}
} এরপর, আপনার সার্ভারে, Firebase অ্যাপটি ইনিশিয়ালাইজ করার সময়, আপনার ডাটাবেস রুলস দ্বারা ব্যবহৃত auth অবজেক্টটিকে ওভাররাইড করতে databaseAuthVariableOverride অপশনটি ব্যবহার করুন। এই কাস্টম auth অবজেক্টে, uid ফিল্ডটিকে সেই আইডেন্টিফায়ারে সেট করুন যা আপনি আপনার সিকিউরিটি রুলসে সার্ভিসটিকে উপস্থাপন করতে ব্যবহার করেছেন।
জাভা
// Initialize the app with a custom auth variable, limiting the server's access Map<String, Object> auth = new HashMap<String, Object>(); auth.put("uid", "my-service-worker"); FirebaseOptions options = FirebaseOptions.builder() .setCredentials(GoogleCredentials.getApplicationDefault()) // The database URL depends on the location of the database .setDatabaseUrl("https://DATABASE_NAME.firebaseio.com") .setDatabaseAuthVariableOverride(auth) .build(); FirebaseApp.initializeApp(options); // The app only has access as defined in the Security Rules DatabaseReference ref = FirebaseDatabase .getInstance() .getReference("/some_resource"); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { String res = dataSnapshot.getValue(); System.out.println(res); } });
নোড.জেএস
import { initializeApp } from 'firebase-admin/app'; import { getDatabase } from 'firebase-admin/database'; // Initialize the app with a custom auth variable, limiting the server's access initializeApp({ // The database URL depends on the location of the database databaseURL: "https://DATABASE_NAME.firebaseio.com", databaseAuthVariableOverride: { uid: "my-service-worker" } }); // The app only has access as defined in the Security Rules const db = getDatabase(); const ref = db.ref("/some_resource"); ref.once("value", (snapshot) => { console.log(snapshot.val()); });
পাইথন
import firebase_admin from firebase_admin import credentials from firebase_admin import db # Fetch the service account key JSON file contents cred = credentials.Certificate('path/to/serviceAccountKey.json') # Initialize the app with a custom auth variable, limiting the server's access firebase_admin.initialize_app(cred, { 'databaseURL': 'https://databaseName.firebaseio.com', 'databaseAuthVariableOverride': { 'uid': 'my-service-worker' } }) # The app only has access as defined in the Security Rules ref = db.reference('/some_resource') print(ref.get())
যান
ctx := context.Background() // Initialize the app with a custom auth variable, limiting the server's access ao := map[string]interface{}{"uid": "my-service-worker"} conf := &firebase.Config{ DatabaseURL: "https://databaseName.firebaseio.com", AuthOverride: &ao, } // Fetch the service account key JSON file contents opt := option.WithCredentialsFile("path/to/serviceAccountKey.json") app, err := firebase.NewApp(ctx, conf, opt) if err != nil { log.Fatalln("Error initializing app:", err) } client, err := app.Database(ctx) if err != nil { log.Fatalln("Error initializing database client:", err) } // The app only has access as defined in the Security Rules ref := client.NewRef("/some_resource") var data map[string]interface{} if err := ref.Get(ctx, &data); err != nil { log.Fatalln("Error reading from database:", err) } fmt.Println(data)
কিছু ক্ষেত্রে, আপনি অ্যাডমিন SDK-গুলোকে একটি প্রমাণীকরণবিহীন ক্লায়েন্ট হিসেবে কাজ করানোর জন্য সীমিত করতে চাইতে পারেন। ডাটাবেস অথোরাইজেশন ভেরিয়েবল ওভাররাইডের জন্য ' null মান প্রদান করে আপনি এটি করতে পারেন।
জাভা
// Initialize the app with Application Default Credentials FirebaseOptions options = FirebaseOptions.builder() .setCredentials(GoogleCredentials.getApplicationDefault()) // The database URL depends on the location of the database .setDatabaseUrl("https://DATABASE_NAME.firebaseio.com") .setDatabaseAuthVariableOverride(null) .build(); FirebaseApp.initializeApp(options); // The app only has access to public data as defined in the Security Rules DatabaseReference ref = FirebaseDatabase .getInstance() .getReference("/public_resource"); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { String res = dataSnapshot.getValue(); System.out.println(res); } });
নোড.জেএস
import { initializeApp } from 'firebase-admin/app'; import { getDatabase } from 'firebase-admin/database'; // Initialize the app with a null auth variable, limiting the server's access initializeApp({ // The database URL depends on the location of the database databaseURL: "https://DATABASE_NAME.firebaseio.com", databaseAuthVariableOverride: null }); // The app only has access to public data as defined in the Security Rules const db = getDatabase(); const ref = db.ref("/public_resource"); ref.once("value", (snapshot) => { console.log(snapshot.val()); });
পাইথন
import firebase_admin from firebase_admin import credentials from firebase_admin import db # Fetch the service account key JSON file contents cred = credentials.Certificate('path/to/serviceAccountKey.json') # Initialize the app with a None auth variable, limiting the server's access firebase_admin.initialize_app(cred, { 'databaseURL': 'https://databaseName.firebaseio.com', 'databaseAuthVariableOverride': None }) # The app only has access to public data as defined in the Security Rules ref = db.reference('/public_resource') print(ref.get())
যান
ctx := context.Background() // Initialize the app with a nil auth variable, limiting the server's access var nilMap map[string]interface{} conf := &firebase.Config{ DatabaseURL: "https://databaseName.firebaseio.com", AuthOverride: &nilMap, } // Fetch the service account key JSON file contents opt := option.WithCredentialsFile("path/to/serviceAccountKey.json") app, err := firebase.NewApp(ctx, conf, opt) if err != nil { log.Fatalln("Error initializing app:", err) } client, err := app.Database(ctx) if err != nil { log.Fatalln("Error initializing database client:", err) } // The app only has access to public data as defined in the Security Rules ref := client.NewRef("/some_resource") var data map[string]interface{} if err := ref.Get(ctx, &data); err != nil { log.Fatalln("Error reading from database:", err) } fmt.Println(data)
পরবর্তী পদক্ষেপ
- Realtime Database জন্য ডেটা কীভাবে গঠন করতে হয় তা শিখুন।
- একাধিক ডাটাবেস ইনস্ট্যান্স জুড়ে ডেটা স্কেল করুন ।
- তথ্য সংরক্ষণ করুন।
- তথ্য উদ্ধার করুন।
- Firebase কনসোলে আপনার ডাটাবেস দেখুন।