อัปโหลดไฟล์ด้วย Cloud Storage สำหรับ Unity

Cloud Storage for Firebase ช่วยให้คุณอัปโหลดไฟล์ไปยังที่เก็บข้อมูล Cloud Storage ที่ให้บริการและจัดการโดย Firebase ได้อย่างรวดเร็วและง่ายดาย

สร้างการอ้างอิง

หากต้องการอัปโหลดไฟล์ ก่อนอื่นให้สร้างข้อมูลอ้างอิง Cloud Storage ไปยังไฟล์ที่ต้องการอัปโหลด

คุณสร้างข้อมูลอ้างอิงได้โดยการเพิ่มเส้นทางย่อยต่อท้ายรูทของที่เก็บข้อมูล Cloud Storage หรือสร้างข้อมูลอ้างอิงจาก URL ของ gs:// หรือ https:// ที่มีอยู่ซึ่งอ้างอิงออบเจ็กต์ใน 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 วิธีดังนี้

  1. อัปโหลดจากอาร์เรย์ไบต์ในหน่วยความจำ
  2. อัปโหลดจากเส้นทางไฟล์ที่แสดงถึงไฟล์ในอุปกรณ์

อัปโหลดจากข้อมูลในหน่วยความจำ

เมธอด PutBytesAsync() เป็นวิธีที่ง่ายที่สุดในการอัปโหลดไฟล์ไปยัง Cloud Storage PutBytesAsync() ใช้เวลา 1 ไบต์[] และแสดงผล 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 ลงในการอัปโหลดเพื่อตรวจสอบความคืบหน้าของการอัปโหลดได้ Listener ทำตามอินเทอร์เฟซ System.IProgress<T> มาตรฐาน คุณใช้อินสแตนซ์ของคลาส StorageProgress เพื่อระบุ Action<T> ของคุณเองเป็น Callback สำหรับเครื่องหมายแสดงความคืบหน้าได้

// 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 กัน