StorageClient is a client that helps me use a firebase-storage interface with ease.
In StorageClient, StorageReference is either Resource
or Folder
.
Resource
is a content that can store in FirebaseStorage, such as Text
, Image
, Video
, Audio
and so on.
Although Resource is content, Resouce is stored in Folder
as one of a resouce in single folder.
"My Photo" is Resource |
---|
All of Resource
s are in Folder
.
There are three kinds of Folder
.
HomeFolder
is at the real root of folder.
RootFolder
is a child of HomeFolder
.
Although HomeFolder
is ensured to be single, RootFolder
can be multiple.
So, this is root Folder
created by developers.
SubFolder
is a child of RootFolder
.
"StorageExample" is RootFolder |
---|
- Init
StorageClient
.
First of all, configure StorageClient.shared.storage
with own FirebaseStorage.Storage
.
// Option: If you want to select specific Storage, add this line.
StorageClient.shared.storage = Storage.storage()
- Create new
Resource
and upload.
It is possible to upload
new resource like the following.
Because all Resource
are under Folder
, it is also necessary to set parent folder at Resource.folder
.
func upload_photo() {
let folder = RootFolder(name: "Tests")
let text: String = "Test Test Test Test Text!!"
let data = text.data(using: .utf8)!
let resource = Resource(
name: "test",
folder: folder,
metadata: Resource.Metadata(contentType: .plain),
data: data
)
// Start uploading
resource.uploadWithData()
.sink { task in
switch task.status {
case .success:
print("OK")
case .fail(let error):
print(error)
case .progress(let fractionComplete):
print(fractionComplete)
default:
break
}
}
.store(in: &cancellables)
}
Documentation is work in progress.