הפקודה Cloud Storage for Firebase מאפשרת להעלות קבצים במהירות ובקלות לדלי Cloud Storage שסופק ומנוהל על ידי Firebase.
יצירת קובץ עזר
כדי להעלות קובץ, קודם יוצרים הפניה Cloud Storage לקובץ שרוצים להעלות.
כדי ליצור הפניה, אפשר להוסיף נתיבי צאצא לשורש של קטגוריית Cloud Storage, או ליצור הפניה מכתובת URL קיימת של Cloud Storage או של https://
שמפנה לאובייקט ב-Cloud Storage.gs://
// Create a root reference StorageReference storageRef = storage.RootReference; // Create a reference to "mountains.jpg" StorageReference mountainsRef = storageRef.Child("mountains.jpg"); // Create a reference to 'images/mountains.jpg' StorageReference mountainImagesRef = storageRef.Child("images/mountains.jpg"); // While the file names are the same, the references point to different files Assert.AreEqual(mountainsRef.Name, mountainImagesRef.Name); Assert.AreNotEqual(mountainsRef.Path, mountainImagesRef.Path);
אי אפשר להעלות נתונים עם הפניה לקטגוריית Cloud Storage הבסיסית. ההפניה צריכה להפנות לכתובת URL של אתר צאצא.
העלאת קבצים
אחרי שיש לכם הפניה, אתם יכולים להעלות קבצים ל-Cloud Storage בשתי דרכים:
- העלאה ממערך בייטים בזיכרון
- העלאה מנתיב קובץ שמייצג קובץ במכשיר
העלאה מנתונים בזיכרון
השיטה PutBytesAsync()
היא הדרך הפשוטה ביותר להעלות קובץ ל-Cloud Storage. הפונקציה PutBytesAsync()
מקבלת byte[] ומחזירה System.Task<Firebase.Storage.StorageMetadata>
שיכיל מידע על הקובץ כשהמשימה תושלם. אפשר גם להשתמש ב-IProgress<UploadState>
(בדרך כלל StorageProgress<UploadState>
) כדי לעקוב אחרי סטטוס ההעלאה.
// Data in memory var customBytes = new byte[] { /*...*/ }; // Create a reference to the file you want to upload StorageReference riversRef = storageRef.Child("images/rivers.jpg"); // Upload the file to the path "images/rivers.jpg" riversRef.PutBytesAsync(customBytes) .ContinueWith((Task<StorageMetadata> task) => { if (task.IsFaulted || task.IsCanceled) { Debug.Log(task.Exception.ToString()); // Uh-oh, an error occurred! } else { // Metadata contains file metadata such as size, content-type, and md5hash. StorageMetadata metadata = task.Result; string md5Hash = metadata.Md5Hash; Debug.Log("Finished uploading..."); Debug.Log("md5 hash = " + md5Hash); } });
העלאה מקובץ מקומי
אתם יכולים להעלות קבצים מקומיים במכשירים, כמו תמונות וסרטונים מהמצלמה, באמצעות השיטה PutFileAsync()
. PutFileAsync()
מקבלת string
שמייצג את הנתיב לקובץ ומחזירה System.Task<Firebase.Storage.StorageMetadata>
שיכיל מידע על הקובץ כשהמשימה תושלם. אפשר גם להשתמש ב-IProgress<UploadState>
(בדרך כלל StorageProgress<UploadState>
) כדי לעקוב אחרי סטטוס ההעלאה.
// File located on disk string localFile = "..."; // Create a reference to the file you want to upload StorageReference riversRef = storageRef.Child("images/rivers.jpg"); // Upload the file to the path "images/rivers.jpg" riversRef.PutFileAsync(localFile) .ContinueWith((Task<StorageMetadata> task) => { if (task.IsFaulted || task.IsCanceled) { Debug.Log(task.Exception.ToString()); // Uh-oh, an error occurred! } else { // Metadata contains file metadata such as size, content-type, and download URL. StorageMetadata metadata = task.Result; string md5Hash = metadata.Md5Hash; Debug.Log("Finished uploading..."); Debug.Log("md5 hash = " + md5Hash); } });
אם רוצים לעקוב באופן פעיל אחרי ההעלאה, אפשר להשתמש במחלקה StorageProgress
או במחלקה משלכם שמטמיעה את IProgress<UploadState>
, עם השיטות PutFileAsync()
או PutBytesAsync()
.
מידע נוסף זמין במאמר בנושא ניהול העלאות.
הוספת מטא-נתונים של קובץ
אפשר גם לכלול מטא-נתונים כשמעלים קבצים. המטא-נתונים האלה מכילים מאפיינים אופייניים של מטא-נתונים של קבצים, כמו Name
, Size
ו-ContentType
(שנקרא בדרך כלל סוג MIME). השיטה PutFileAsync()
מסיקה באופן אוטומטי את סוג התוכן מהסיומת של שם הקובץ, אבל אפשר לשנות את הסוג שזוהה אוטומטית על ידי ציון ContentType
במטא-נתונים. אם לא תספקו ContentType
ו-Cloud Storage לא יוכל להסיק ברירת מחדל מסיומת הקובץ, Cloud Storage ישתמש ב-application/octet-stream
. מידע נוסף על מטא-נתונים של קבצים זמין בקטע שימוש במטא-נתונים של קבצים.
// Create storage reference StorageReference mountainsRef = storageRef.Child("images/mountains.jpg"); byte[] customBytes = new byte[] { /*...*/ }; string localFile = "..."; // Create file metadata including the content type var newMetadata = new MetadataChange(); newMetadata.ContentType = "image/jpeg"; // Upload data and metadata mountainsRef.PutBytesAsync(customBytes, newMetadata, null, CancellationToken.None); // .ContinueWithOnMainThread(... // Upload file and metadata mountainsRef.PutFileAsync(localFile, newMetadata, null, CancellationToken.None); // .ContinueWithOnMainThread(...
מעקב אחר התקדמות ההעלאה
אתם יכולים לצרף מאזינים להעלאות כדי לעקוב אחרי ההתקדמות של ההעלאה. המאזין פועל לפי הממשק הסטנדרטי System.IProgress<T>
. אפשר להשתמש במופע של המחלקה StorageProgress
כדי לספק Action<T>
משלכם כקריאה חוזרת לסימון התקדמות.
// Start uploading a file var task = storageRef.Child("images/mountains.jpg") .PutFileAsync(localFile, null, new StorageProgress<UploadState>(state => { // called periodically during the upload Debug.Log(String.Format("Progress: {0} of {1} bytes transferred.", state.BytesTransferred, state.TotalByteCount)); }), CancellationToken.None, null); task.ContinueWithOnMainThread(resultTask => { if (!resultTask.IsFaulted && !resultTask.IsCanceled) { Debug.Log("Upload finished."); } });
טיפול בשגיאות
יכולות להיות כמה סיבות לשגיאות בהעלאה, כולל קובץ מקומי שלא קיים או משתמש שאין לו הרשאה להעלות את הקובץ הרצוי. מידע נוסף על שגיאות זמין בקטע Handle Errors במסמכים.
השלבים הבאים
אחרי שהעליתם קבצים, נלמד איך להוריד אותם מ-Cloud Storage.