Unity용 Cloud Storage로 Cloud Storage 참조 만들기

파일은 Cloud Storage 버킷에 저장됩니다. 이 버킷의 파일은 로컬 하드 디스크의 파일 시스템 또는 Firebase 실시간 데이터베이스의 데이터와 같이 계층 구조로 표시됩니다. 파일을 가리키는 참조를 만들면 앱에 액세스 권한이 부여됩니다. 이러한 참조를 사용하여 데이터 업로드 또는 다운로드, 메타데이터 가져오기 또는 업데이트, 파일 삭제 등을 수행할 수 있습니다. 참조는 특정 파일을 가리키거나 계층 구조에서 보다 상위 노드를 가리킬 수도 있습니다.

Firebase 실시간 데이터베이스를 사용해 보았다면 이러한 참조 경로에 익숙하실 겁니다. 하지만 파일 데이터는 실시간 데이터베이스가 아니라 Cloud Storage에 저장된다는 점에 주의하세요.

참조 만들기

파일 업로드, 다운로드, 삭제, 메타데이터 가져오기 또는 업데이트를 하려면 참조를 만듭니다. 참조는 클라우드의 파일을 가리키는 포인터로 생각하면 됩니다. 참조는 메모리에 부담을 주지 않으므로 원하는 만큼 만들 수 있으며 여러 작업에서 재사용할 수도 있습니다.

GetReferenceFromUrl() 메서드를 호출하고 gs://<your-cloud-storage-bucket> 형식의 URL을 전달하면 Firebase 앱의 Firebase.Storage.FirebaseStorage 서비스에서 참조가 생성됩니다. Firebase ConsoleStorage 섹션에서 이 URL을 확인할 수 있습니다.

// Get a reference to the storage service, using the default Firebase App
FirebaseStorage storage = FirebaseStorage.DefaultInstance;

// Create a storage reference from our storage service
StorageReference storageRef =
    storage.GetReferenceFromUrl("gs://<your-cloud-storage-bucket>");

기존 참조에 child 메서드를 사용하여 트리에서 하위 위치(예: 'images/space.jpg')에 대한 참조를 만들 수 있습니다.

// Create a child reference
// imagesRef now points to "images"
StorageReference imagesRef = storageRef.Child("images");

// Child references can also take paths delimited by '/' such as:
// "images/space.jpg".
StorageReference spaceRef = imagesRef.Child("space.jpg");
// spaceRef now points to "images/space.jpg"
// imagesRef still points to "images"

// This is equivalent to creating the full referenced
StorageReference spaceRefFull = storage.GetReferenceFromUrl(
    "gs://<your-cloud-storage-bucket>/images/space.jpg");

ParentRoot 메서드를 사용하여 파일 계층에서 상위로 탐색할 수도 있습니다. Parent는 한 단계 위로 탐색하며 Root는 맨 위로 탐색합니다.

// Parent allows us to move to the parent of a reference
// imagesRef now points to 'images'
StorageReference imagesRef = spaceRef.Parent;

// Root allows us to move all the way back to the top of our bucket
// rootRef now points to the root
StorageReference rootRef = spaceRef.Root;

Child, Parent, Root는 각각 참조를 반환하므로 여러 번 연결할 수 있습니다. 다만 RootParent는 잘못된 StorageReference이므로 예외입니다.

// References can be chained together multiple times
// earthRef points to "images/earth.jpg"
StorageReference earthRef =
    spaceRef.Parent.Child("earth.jpg");

// nullRef is null since the parent of root is an invalid StorageReference
StorageReference nullRef = spaceRef.Root.Parent;

참조 메서드

Path, Name, Bucket 속성으로 참조를 조사하여 참조가 가리키는 파일을 자세히 파악할 수 있습니다. 이러한 속성은 파일의 전체 경로, 이름 및 버킷을 가져옵니다.

// Reference's path is: "images/space.jpg"
// This is analogous to a file path on disk
string path = spaceRef.Path;

// Reference's name is the last segment of the full path: "space.jpg"
// This is analogous to the file name
string name = spaceRef.Name;

// Reference's bucket is the name of the storage bucket where files are stored
string bucket = spaceRef.Bucket;

참조 제한사항

참조 경로와 이름에는 유효한 Unicode 문자를 어떤 순서로든 포함할 수 있지만 다음을 비롯하여 몇 가지 제한사항이 있습니다.

  1. UTF-8 인코딩 시 총 reference.Path 길이는 1~1024바이트여야 합니다.
  2. 캐리지 리턴 또는 줄바꿈 문자는 사용할 수 없습니다.
  3. #, [, ], * 또는 ?Firebase 실시간 데이터베이스 또는 gsutil과 같은 다른 도구와 잘 호환되지 않으므로 사용하지 마세요.

전체 예시

FirebaseStorage storage = FirebaseStorage.DefaultInstance;

// Points to the root reference
StorageReference storageRef =
    storage.GetReferenceFromUrl("gs://<your-bucket-name>");

// Points to "images"
StorageReference imagesRef = storageRef.Child("images");

// Points to "images/space.jpg"
// Note that you can use variables to create child values
string filename = "space.jpg";
StorageReference spaceRef = imagesRef.Child(filename);

// File path is "images/space.jpg"
string path = spaceRef.Path;

// File name is "space.jpg"
string name = spaceRef.Name;

// Points to "images"
StorageReference imagesRef = spaceRef.Parent;

다음 단계

다음으로 Cloud Storage에 파일을 업로드하는 방법을 알아보세요.