fun download()

in storage/src/main/kotlin/Storage.kt [120:138]


fun download(vararg params: String) { // 3 mandatory args expected: <bucket> <blob> <localFilePath>
    if (params.size < 3) {
        error("Bad input: command 'download' expects 3 mandatory arguments. \n $usage")
    }

    val bucketName = params[0]
    val bucket = storage.get(bucketName)
        ?: error("Bucket $bucketName does not exist! To see your existing buckets, use command 'info'. \n $usage")

    val blobName = params[1]
    val blob = bucket.get(blobName)
        ?: error("Blob $blobName does not exist! To see blobs in bucket $bucketName, use command 'info $bucketName'. \n $usage")

    val localFilePath = Paths.get(params[2])
    val writeTo = PrintStream(FileOutputStream(localFilePath.toFile()))
    writeTo.write(blob.getContent())
    writeTo.close()
    println("$blobName was successfully downloaded to $localFilePath.")
}