ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Firebase]Cloud Storage(1) - Create a Reference
    Firebase(Android)/CloudStorage 2021. 7. 27. 20:33

    Cloud Storage Bucket에 존재하는 파일, 데이터에 접근하기 위해서는 인증된 사용자만 가능하다. 그렇기 때문에 Cloud Storage의 Security Rules을 모두 접근가능하게 변경하고 테스트 개발을 진행해야 한다.

     

    https://firebase.google.com/docs/storage/android/create-reference?authuser=0 

     

    Android에서 Cloud Storage 참조 만들기  |  Firebase

    파일은 Cloud Storage 버킷에 저장됩니다. 이 버킷의 파일은 로컬 하드 디스크의 파일 시스템 또는 Firebase 실시간 데이터베이스의 데이터와 같이 계층 구조로 표시됩니다. 파일을 가리키는 참조를

    firebase.google.com

    https://firebase.google.com/docs/reference/android/com/google/firebase/storage/package-summary

     

    com.google.firebase.storage  |  Firebase

    firebase.ml.naturallanguage.translate

    firebase.google.com

    Firebase Cloud Storage의 api는 Firebase Realtime Database의 api와 사용법이 비슷하다.

     

    • FirebaseStorage  클라우드 스토리지 클래스

    -public static FirebaseStorage getInstance ()

    default FirebaseApp에 해당하는 FirebaseStorage 인스턴스 리턴

    FirebaseStorage storage=FirebaseStorage.getInstance();
    //default FirebaseApp에 해당하는 cloud storage인스턴스 반환

    -public StorageReference getReference ()

    클라우드 스토리지 내부의 루트 위치 인스턴스 리턴

    StorageReference rootRef=storage.getReference();
    //클라우드 스토리지의 root레벨 위치를 참조하는 reference인스턴스 리턴

    -public StorageReference getReference (String location)

    클라우드 스토리지 내부의 특정 위치에 해당하는 인스턴스 리턴


    • StorageReference 클라우드 스토리지 내부의 특정 위치를 참조할 수 있는 클래스

    StorageReference를 가지고 특정위치로 이동

     

    -public StorageReference child (String pathString)

    인스턴스의 위치 기준으로 pathString에 해당하는 위치 인스턴스 리턴

     pathString의 가장 처음, 가장 마지막 슬래시는 제거된다. 연속된 /는 하나의 /로 취급된다.
     
    child("/foo/bar")     path = foo/bar
    child("foo/bar/")     path = foo/bar
    child =("foo///bar")    path = foo/bar

     

     

    루트- 이미지를 보관하는 곳- 여러 이미지들   이 있다고 가정하면

    // Create a child reference
    // imagesRef now points to "images"
    StorageReference imagesRef = rootRef.child("images");
    
    // Child references can also take paths
    // spaceRef now points to "images/space.jpg
    // imagesRef still points to "images"
    StorageReference spaceRef = rootRef.child("images/space.jpg");

     

    -public StorageReference getParent ()

    해당 위치 reference의 부모 reference리턴

    해당 위치 reference가 루트일 경우 getParent하면 null을 리턴한다. (루트의 부모는 없으므로)

     

    -public StorageReference getRoot ()

    루트 reference리턴

     

     

    StorageReference가 가지고 있는 property출력

     

    -getPath(), getName(), getBucket()  모두 String 리턴

    // Reference's path is: "images/space.jpg"
    // This is analogous to a file path on disk
    spaceRef.getPath();
    
    // Reference's name is the last segment of the full path: "space.jpg"
    // This is analogous to the file name
    spaceRef.getName();
    
    // Reference's bucket is the name of the storage bucket that the files are stored in
    spaceRef.getBucket();

    Full Example

    //default firebase app's cloud storage
    FirebaseStorage storage = FirebaseStorage.getInstance();
    
    // Points to the root reference
    storageRef = storage.getReference();
    
    // Points to "images"
    imagesRef = storageRef.child("images");
    
    // Points to "images/space.jpg"
    // Note that you can use variables to create child values
    String fileName = "space.jpg";
    spaceRef = imagesRef.child(fileName);
    
    // File path is "images/space.jpg"
    String path = spaceRef.getPath();
    
    // File name is "space.jpg"
    String name = spaceRef.getName();
    
    // Bukcet string that file was contained
    String bucket = spaceRef.getBucket();
    
    // Points to "images"
    imagesRef = spaceRef.getParent();

     

    댓글

Designed by Tistory.