Skip to content

Latest commit

 

History

History

storage

Google Cloud Storage Kotlin Sample

Open in Cloud Shell

Description

This sample application demonstrates how to invoke the Google Cloud Storage API from Kotlin.

The app is a command-line tool that implements the following Google Cloud Storage operations:

  1. Create a bucket
  2. List buckets
  3. List blobs in a bucket
  4. Upload a file to a bucket
  5. Download a blob from a bucket
  6. Delete a blob
  7. Delete a bucket

Examples:

build/install/storage/bin/storage create my_awesome_bucket
build/install/storage/bin/storage upload resources/upload/dog.jpg my_awesome_bucket

Quickstart

Setup

Build

  • Clone the repository
    git clone https://github.com/GoogleCloudPlatform/kotlin-samples
    cd kotlin-samples/storage
  • Build the project with Gradle Wrapper:
    # run with "-info" flag to print potential errors
    ./gradlew installDist -info

You should now have a 'storage.jar' file under build/libs/

Running the sample

You can run Google Cloud Storage actions by supplying any of the below commands to the storage.jar file:

usage

Prints syntax of commands implemented in the sample

build/install/storage/bin/storage usage

create

Creates a new bucket

build/install/storage/bin/storage create <bucket>

info

Gives details about a bucket or a blob

  • To list all buckets in your storage
    build/install/storage/bin/storage info
  • To list all blobs in a bucket
    build/install/storage/bin/storage info <bucket>

upload

Uploads a file to a bucket

  • Letting the program infer blob name from localFilePath
    build/install/storage/bin/storage upload <localFilePath> <bucket>
  • Providing blob name
    build/install/storage/bin/storage upload <localFilePath> <bucket> <blob>

download

Downloads a blob from a bucket to your computer

build/install/storage/bin/storage download <bucket> <blob> <localFilePath>

delete

Deletes a bucket or a blob

  • To delete a bucket
    build/install/storage/bin/storage delete <bucket>
  • To delete a blob
    build/install/storage/bin/storage delete <bucket> <blob>

Head to storageTest.kt to see actual tests.

Contributing changes

Licensing