File Upload တင်ခြင်း
File upload လုပ်ဖို့ Storage reference တစ်ခုတည်ဆောက်ပြီး uploadBytes ကိုသုံးပါတယ်။ File path ကို user ID နဲ့ခွဲထားရင် နောက်ပိုင်း manage လုပ်ရလွယ်ပါတယ်။
Code Example
javascript
import { getStorage, ref, uploadBytes } from 'firebase/storage';
import { getAuth } from 'firebase/auth';
const storage = getStorage();
const auth = getAuth();
async function uploadAvatar(file) {
const user = auth.currentUser;
if (!user) throw new Error('Login required');
const filePath = 'avatars/' + user.uid + '/' + file.name;
const fileRef = ref(storage, filePath);
await uploadBytes(fileRef, file);
console.log('Avatar uploaded:', filePath);
}ဒီ code က ဘာလုပ်တာလဲ?
avatars/userId/fileName ပုံစံနဲ့ path တည်ဆောက်ပြီး Storage ထဲကို file တင်ပါတယ်။ User ID ပါထားလို့ user တစ်ယောက်ချင်းစီရဲ့ files တွေကိုခွဲခြားလို့ရပါတယ်။
Common mistake
User ကတင်တဲ့ file.name တူနေရင် overwrite ဖြစ်နိုင်ပါတယ်။ Production မှာ timestamp သို့မဟုတ် random ID ထည့်ပြီး filename unique ဖြစ်အောင်လုပ်ပါ။