37
loading...
This website collects cookies to deliver better user experience
files.read
files.write
<dependencies>
section of your pom.xml
file.<dependencies>
<dependency>
<groupId>io.appwrite</groupId>
<artifactId>sdk-for-kotlin</artifactId>
<version>0.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.7</version>
</dependency>
</dependencies>
src/main/kotlin
and name it StorageCleaner.kt
.import io.appwrite.Client
import io.appwrite.services.Storage
import kotlin.system.exitProcess
fun main(args: Array<String>) {
val client = Client()
.setEndpoint(System.getenv("APPWRITE_ENDPOINT"))
.setProject(System.getenv("APPWRITE_FUNCTION_PROJECT_ID"))
.setKey(System.getenv("APPWRITE_API_KEY"))
val storage = Storage(client)
val daysToExpire = System.getenv("DAYS_TO_EXPIRE").toFloatOrNull()
if (daysToExpire == null) {
println("Unable to parse DAYS_TO_EXPIRE.")
exitProcess(1)
}
}
Models.kt
under src/main/kotlin
and add the following code.data class Permissions(val read: List<String>, val write: List<String>)
data class File(
val `$id`: String,
val name: String,
val `$permissions`: Permissions,
val dateCreated: Int,
val signature: String,
val mimeType: String,
val sizeOriginal: Int
)
data class FileList(val sum: Int, val files: List<File>?)
StorageCleaner.kt
.+import com.google.gson.Gson
import io.appwrite.Client
import io.appwrite.services.Storage
import kotlin.system.exitProcess
+suspend fun main(args: Array<String>) {
val client = Client()
.setEndpoint(System.getenv("APPWRITE_ENDPOINT"))
.setProject(System.getenv("APPWRITE_FUNCTION_PROJECT_ID"))
.setKey(System.getenv("APPWRITE_API_KEY"))
val storage = Storage(client)
val daysToExpire = System.getenv("DAYS_TO_EXPIRE").toFloatOrNull()
if (daysToExpire == null) {
println("Unable to parse DAYS_TO_EXPIRE.")
exitProcess(1)
}
+ val fileList = storage.listFiles("",100, 0, "DESC").body?.string() ?: ""
+ val files: FileList = Gson().fromJson(fileList, FileList::class.java)
}
daysToExpire
.import com.google.gson.Gson
import io.appwrite.Client
import io.appwrite.services.Storage
+ import java.util.*
import kotlin.system.exitProcess
suspend fun main(args: Array<String>) {
val client = Client()
.setEndpoint(System.getenv("APPWRITE_ENDPOINT"))
.setProject(System.getenv("APPWRITE_FUNCTION_PROJECT_ID"))
.setKey(System.getenv("APPWRITE_API_KEY"))
val storage = Storage(client)
val daysToExpire = System.getenv("DAYS_TO_EXPIRE").toFloatOrNull()
if (daysToExpire == null) {
println("Unable to parse DAYS_TO_EXPIRE.")
exitProcess(1)
}
val fileList = storage.listFiles("",100, 0, "DESC").body?.string() ?: ""
val files: FileList = Gson().fromJson(fileList, FileList::class.java)
+ var deletedFiles = 0
+ for( file in files.files!!) {
+ val diff: Long = Date().time/1000 - file.dateCreated
+ if (diff > daysToExpire * 24 * 60 * 60) {
+ storage.deleteFile(file.`$id`)
+ println("Deleted ${file.`$id`}")
+ deletedFiles++
+ }
+ }
+ println("Total files deleted: $deletedFiles")
}
.jar
. Fortunately, this can be done really easily using IntelliJ, so let's see how.Note: If you don't see the main class, try to follow the steps in this answer https://stackoverflow.com/questions/10654120/error-could-not-find-or-load-main-class-in-intellij-ide
src/main/kotlin/META-INF/MANIFEST.MF
with the following contents.Manifest-Version: 1.0
Main-Class: StorageCleanerKt
Build > Build Artifacts > Select your artifact from the list > Build
. You will find the output of this step in out/artifacts/mainModule_jar/mainModule.jar
docker run --rm --volume $(pwd):/usr/local/src:rw \
--env APPWRITE_ENDPOINT="http://192.168.1.35/v1" \
--env APPWRITE_FUNCTION_PROJECT_ID="60d31170f368f" \
--env DAYS_TO_EXPIRE="0.00001" \
--env APPWRITE_API_KEY="7e....879f" \
appwrite/runtime-for-java:11 \
java -jar out/artifacts/mainModule_jar/mainModule.jar
The APPWRITE_ENDPOINT is NOT localhost in this case since localhost is not accessible from the cloud functions runtime. You will need to set this to a publicly accessible IP of your Appwrite Server. You can find this by running hostname -I
in UNIX systems.
Total files deleted: 0
Create
.$ cd out/artifacts
$ tar -zcvf code.tar.gz mainModule_jar
mainModule_jar/
mainModule_jar/mainModule.jar
$ ls
code.tar.gz mainModule_jar
code.tar.gz
.Appwrite Dashboard > Functions > Overview > Deploy Tag
. In the dialog that pops up, upload the tarfile
we just created and java -jar mainModule.jar
for the entry point command. $ cd out/artifacts/
$ appwrite functions createTag --functionId=60d41cdbec776 --command="java -jar mainModule.jar" --code=mainModule_jar
$id : 60d46ee4506d4
functionId : 60d41cdbec776
dateCreated : 1624534756
command : java -jar mainModule.jar
size : 4381381
$ appwrite functions updateTag --functionId=60d41cdbec776 --tag=60d46ee4506d4
$id : 60d41cdbec776
$permissions :
name : Storage Test
dateCreated : 1624513755
dateUpdated : 1624534608
status : disabled
runtime : java-11
tag : 60d46ee4506d4
vars :
events : {}
schedule : 0 * * * *
scheduleNext :
schedulePrevious : 1624532406
timeout : 15