Apple 플랫폼에서 Cloud Storage 참조 만들기

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

Firebase Realtime Database를 사용한 경우 이 경로는 매우 익숙할 것입니다. 하지만 파일 데이터는 Realtime Database아니라 Cloud Storage입니다.

참조 만들기

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

참조를 만들려면 FirebaseStorage 서비스를 사용하고 reference 메서드를 호출합니다.

Swift

// Get a reference to the storage service using the default Firebase App
let storage = Storage.storage()

// Create a storage reference from our storage service
let storageRef = storage.reference()
    

Objective-C

// Get a reference to the storage service using the default Firebase App
FIRStorage *storage = [FIRStorage storage];

// Create a storage reference from our storage service
FIRStorageReference *storageRef = [storage reference];
    

기존 참조에 child 메서드를 사용하여 'images/space.jpg'와 같이 트리에서 하위 위치를 가리키는 참조를 만들 수 있습니다.

Swift

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

// Child references can also take paths delimited by '/'
// spaceRef now points to "images/space.jpg"
// imagesRef still points to "images"
var spaceRef = storageRef.child("images/space.jpg")

// This is equivalent to creating the full reference
let storagePath = "\(your_firebase_storage_bucket)/images/space.jpg"
spaceRef = storage.reference(forURL: storagePath)
    

Objective-C

// Create a child reference
// imagesRef now points to "images"
FIRStorageReference *imagesRef = [storageRef child:@"images"];

// Child references can also take paths delimited by '/'
// spaceRef now points to "images/space.jpg"
// imagesRef still points to "images"
FIRStorageReference *spaceRef = [storageRef child:@"images/space.jpg"];

// This is equivalent to creating the full reference
spaceRef = [storage referenceForURL:@"gs://<your-firebase-storage-bucket>/images/space.jpg"];
     

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

Swift

// Parent allows us to move to the parent of a reference
// imagesRef now points to 'images'
let imagesRef = spaceRef.parent()

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

Objective-C

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

// Root allows us to move all the way back to the top of our bucket
// rootRef now points to the root
FIRStorageReference *rootRef = [spaceRef root];
    

child, parent, root는 각각 참조를 반환하므로 여러 번 연결할 수 있습니다. rootparent(nil)는 예외입니다.

Swift

// References can be chained together multiple times
// earthRef points to "images/earth.jpg"
let earthRef = spaceRef.parent()?.child("earth.jpg")

// nilRef is nil, since the parent of root is nil
let nilRef = spaceRef.root().parent()
    

Objective-C

// References can be chained together multiple times
// earthRef points to "images/earth.jpg"
FIRStorageReference *earthRef = [[spaceRef parent] child:@"earth.jpg"];

// nilRef is nil, since the parent of root is nil
FIRStorageReference *nilRef = [[spaceRef root] parent];
    

참조 속성

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

Swift

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

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

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

Objective-C

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

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

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

참조 제한사항

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

  1. reference.fullPath의 전체 길이는 UTF-8 인코딩 시 1~1,024바이트 사이여야 합니다.
  2. 캐리지 리턴 또는 라인 피드 문자는 사용할 수 없습니다.
  3. #, [, ], * 또는 ?는 다음과 잘 작동하지 않으므로 사용하지 않습니다. 기타 도구(예: Firebase Realtime Database) 또는 gsutil

전체 예시

Swift

// Points to the root reference
let storageRef = Storage.storage().reference()

// Points to "images"
let imagesRef = storageRef.child("images")

// Points to "images/space.jpg"
// Note that you can use variables to create child values
let fileName = "space.jpg"
let spaceRef = imagesRef.child(fileName)

// File path is "images/space.jpg"
let path = spaceRef.fullPath

// File name is "space.jpg"
let name = spaceRef.name

// Points to "images"
let images = spaceRef.parent()
    

Objective-C

// Points to the root reference
FIRStorageReference *storageRef = [[FIRStorage storage] reference];

// Points to "images"
FIRStorageReference *imagesRef = [storageRef child:@"images"];

// Points to "images/space.jpg"
// Note that you can use variables to create child values
NSString *fileName = @"space.jpg";
FIRStorageReference *spaceRef = [imagesRef child:fileName];

// File path is "images/space.jpg"
NSString *path = spaceRef.fullPath;

// File name is "space.jpg"
NSString *name = spaceRef.name;

// Points to "images"
imagesRef = [spaceRef parent];
    

다음으로 Cloud Storage파일을 업로드하는 방법을 알아보겠습니다.