Files
Uploading and managing files.
Info
File publicMeta
and privateMeta
fields support any kind of data formats encoded to byte arrays. Examples in this section use kotlinx-serialization-json-jvm
dependency for Kotlin and com.google.code.gson:gson
for Java to serialize object to JSON format.
Uploading Files
Uploading file to a Store in the most basic way:
val storeID = "STORE_ID"
val fileContent = "Text file content".encodeToByteArray()
val storeApi = connection.storeApi
val fileId = StoreFileStreamWriter.createFile(
storeApi,
storeID,
ByteArray(0),
ByteArray(0),
fileContent.size.toLong(),
).run {
this.write(fileContent)
this
}.close()
Getting Files
Define file item class with decoded privateMeta
.
data class FileItem(
val file: File,
val decodedPrivateMeta: FilePrivateMeta
)
Fetching the most recent files in given Store:
val storeId = "STORE_ID"
val startIndex = 0L
val pageSize = 100L
val storeApi = connection.storeApi
val filesPagingList = storeApi.listFiles(
storeId,
startIndex,
pageSize,
SortOrder.DESC
)
val files = filesPagingList.readItems.map {
FileItem(
it,
Json.decodeFromString(it.privateMeta.decodeToString())
)
}
Managing Files
val fileID = "FILE_ID"
val storeApi = connection.storeApi
val file: File = storeApi.getFile(fileID)
var filePrivateMeta: FilePrivateMeta = Json.decodeFromString(file.privateMeta.decodeToString())
filePrivateMeta = filePrivateMeta.copy(
name = "New file name"
)
storeApi.updateFileMeta(
fileID,
file.publicMeta,
Json.encodeToString(filePrivateMeta).encodeToByteArray(),
)