Cloud Storage for Firebase ช่วยให้คุณอัปโหลดไฟล์ไปยังที่เก็บข้อมูล Cloud Storageที่ Firebase จัดเตรียมให้ และจัดการได้อย่างรวดเร็วและง่ายดาย
สร้างการอ้างอิง
หากต้องการอัปโหลดไฟล์ ขั้นตอนแรกให้สร้างCloud Storageข้อมูลอ้างอิง ไปยังไฟล์ที่ต้องการอัปโหลด
คุณสร้างการอ้างอิงได้โดยการต่อท้ายเส้นทางย่อยกับรูทของCloud Storage บัคเก็ต หรือจะสร้างการอ้างอิงจาก gs://
หรือ https://
URL ที่มีอยู่ซึ่งอ้างอิงออบเจ็กต์ใน Cloud Storage ก็ได้
// 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 ได้ 2 วิธี ดังนี้
- อัปโหลดจากอาร์เรย์ไบต์ในหน่วยความจำ
- อัปโหลดจากเส้นทางไฟล์ที่แสดงไฟล์ในอุปกรณ์
อัปโหลดจากข้อมูลในหน่วยความจำ
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(...
ตรวจสอบความคืบหน้าในการอัปโหลด
คุณสามารถแนบ Listener ไปกับการอัปโหลดเพื่อตรวจสอบความคืบหน้าของการอัปโหลดได้
โปรแกรมอ่านหน้าจอจะใช้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."); } });
การจัดการข้อผิดพลาด
การอัปโหลดอาจเกิดข้อผิดพลาดได้ด้วยเหตุผลหลายประการ เช่น ไม่มีไฟล์ในเครื่อง หรือผู้ใช้ไม่มีสิทธิ์อัปโหลด ไฟล์ที่ต้องการ ดูข้อมูลเพิ่มเติมเกี่ยวกับข้อผิดพลาดได้ในส่วน จัดการข้อผิดพลาด ของเอกสาร
ขั้นตอนถัดไป
ตอนนี้คุณได้อัปโหลดไฟล์แล้ว มาดูวิธีดาวน์โหลดไฟล์จาก Cloud Storage กัน