Google Cloud Storage

Google Cloud Storage allows world-wide storage and retrieval of any amount of data at any time.

Further information at the official Google Cloud Storage documentation website. This connector communicates to Cloud Storage via HTTP requests.

Project Info: Alpakka Google Cloud Storage
Artifact
com.lightbend.akka
akka-stream-alpakka-google-cloud-storage
1.1.2
JDK versions
OpenJDK 8
Scala versions2.12.7, 2.13.0
JPMS module nameakka.stream.alpakka.google.cloud.storage
License
Readiness level
Since 1.1.0, 2019-07-03
Home pagehttps://doc.akka.io/docs/alpakka/current
API documentation
Forums
Release notesIn the documentation
IssuesGithub issues
Sourceshttps://github.com/akka/alpakka

Artifacts

sbt
libraryDependencies += "com.lightbend.akka" %% "akka-stream-alpakka-google-cloud-storage" % "1.1.2"
Maven
<dependency>
  <groupId>com.lightbend.akka</groupId>
  <artifactId>akka-stream-alpakka-google-cloud-storage_2.12</artifactId>
  <version>1.1.2</version>
</dependency>
Gradle
dependencies {
  compile group: 'com.lightbend.akka', name: 'akka-stream-alpakka-google-cloud-storage_2.12', version: '1.1.2'
}

The table below shows direct dependencies of this module and the second tab shows all libraries it depends on transitively.

Direct dependencies
OrganizationArtifactVersionLicense
com.pauldijoujwt-core_2.123.0.1Apache-2.0
com.typesafe.akkaakka-http-spray-json_2.1210.1.10Apache-2.0
com.typesafe.akkaakka-http_2.1210.1.10Apache-2.0
com.typesafe.akkaakka-stream_2.122.5.23Apache License, Version 2.0
org.scala-langscala-library2.12.7BSD 3-Clause
Dependency tree
com.pauldijou    jwt-core_2.12    3.0.1    Apache-2.0
    org.scala-lang    scala-library    2.12.7    BSD 3-Clause
com.typesafe.akka    akka-http-spray-json_2.12    10.1.10    Apache-2.0
    com.typesafe.akka    akka-http_2.12    10.1.10    Apache-2.0
        com.typesafe.akka    akka-http-core_2.12    10.1.10    Apache-2.0
            com.typesafe.akka    akka-parsing_2.12    10.1.10    Apache-2.0
                org.scala-lang    scala-library    2.12.7    BSD 3-Clause
            org.scala-lang    scala-library    2.12.7    BSD 3-Clause
        org.scala-lang    scala-library    2.12.7    BSD 3-Clause
    io.spray    spray-json_2.12    1.3.5    Apache 2
        org.scala-lang    scala-library    2.12.7    BSD 3-Clause
    org.scala-lang    scala-library    2.12.7    BSD 3-Clause
com.typesafe.akka    akka-http_2.12    10.1.10    Apache-2.0
    com.typesafe.akka    akka-http-core_2.12    10.1.10    Apache-2.0
        com.typesafe.akka    akka-parsing_2.12    10.1.10    Apache-2.0
            org.scala-lang    scala-library    2.12.7    BSD 3-Clause
        org.scala-lang    scala-library    2.12.7    BSD 3-Clause
    org.scala-lang    scala-library    2.12.7    BSD 3-Clause
com.typesafe.akka    akka-stream_2.12    2.5.23    Apache License, Version 2.0
    com.typesafe.akka    akka-actor_2.12    2.5.23    Apache License, Version 2.0
        com.typesafe    config    1.3.3    Apache License, Version 2.0
        org.scala-lang.modules    scala-java8-compat_2.12    0.8.0    BSD 3-clause
            org.scala-lang    scala-library    2.12.7    BSD 3-Clause
        org.scala-lang    scala-library    2.12.7    BSD 3-Clause
    com.typesafe.akka    akka-protobuf_2.12    2.5.23    Apache License, Version 2.0
        org.scala-lang    scala-library    2.12.7    BSD 3-Clause
    com.typesafe    ssl-config-core_2.12    0.3.7    Apache-2.0
        com.typesafe    config    1.3.3    Apache License, Version 2.0
        org.scala-lang.modules    scala-parser-combinators_2.12    1.1.1    BSD 3-clause
            org.scala-lang    scala-library    2.12.7    BSD 3-Clause
        org.scala-lang    scala-library    2.12.7    BSD 3-Clause
    org.reactivestreams    reactive-streams    1.0.2    CC0
    org.scala-lang    scala-library    2.12.7    BSD 3-Clause
org.scala-lang    scala-library    2.12.7    BSD 3-Clause

Configuration

The settings for the Google Cloud Storage connector are read by default from alpakka.googlecloud.storage configuration section. If you use a non-standard configuration path or need multiple different configurations, please refer to the attributes section below to see how to apply different configuration to different parts of the stream. You’ll first need to prepare your credentials for access to google cloud storage. All of the available configuration settings can be found in the application.conf.

HOCON

privateKey ="""-----BEGIN PRIVATE KEY----- MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAMwkmdwrWp+LLlsf bVE+neFjZtUNuaD4/tpQ2UIh2u+qU6sr4bG8PPuqSdrt5b0/0vfMZA11mQWmKpg5 PK98kEkhbSvC08fG0TtpR9+vflghOuuvcw6kCniwNbHlOXnE8DwtKQp1DbTUPzMD hhsIjJaUtv19Xk7gh4MqYgANTm6lAgMBAAECgYEAwBXIeHSKxwiNS8ycbg//Oq7v eZV6j077bq0YYLO+cDjSlYOq0DSRJTSsXcXvoE1H00aM9mUq4TfjaGyi/3SzxYsr rSzu/qpYC58MJsnprIjlLgFZmZGe5MOSoul/u6JsBTJGkYPV0xGrtXJY103aSYzC xthpY0BHy9eO9I/pNlkCQQD/64g4INAiBdM4R5iONQvh8LLvqbb8Bw4vVwVFFnAr YHcomxtT9TunMad6KPgbOCd/fTttDADrv54htBrFGXeXAkEAzDTtisPKXPByJnUd jKO2oOg0Fs9IjGeWbnkrsN9j0134ldARE+WbT5S8G5EFo+bQi4ffU3+Y/4ly6Amm OAAzIwJBANV2GAD5HaHDShK/ZTf4dxjWM+pDnSVKnUJPS039EUKdC8cK2RiGjGNA v3jdg1Tw2cE1K8QhJwN8qOFj4JBWVbECQQCwcntej9bnf4vi1wd1YnCHkJyRqQIS 7974DhNGfYAQPv5w1JwtCRSuKuJvH1w0R1ijd//scjCNfQKgpNXPRbzpAkAQ8MFA MLpOLGqezUQthJWmVtnXEXaAlb3yFSRTZQVEselObiIc6EvYzNXv780IDT4pyKjg 8DS9i5jJDIVWr7mA -----END PRIVATE KEY----- """ privateKey = ${?GC_STORAGE_PRIVATE_KEY} alpakka.google.cloud.storage { project-id = "projectId" client-email = "[email protected]" private-key = ${privateKey} base-url = "https://www.googleapis.com/" // default base-path = "/storage/v1" // default token-url = "https://www.googleapis.com/oauth2/v4/token" // default token-scope = "https://www.googleapis.com/auth/devstorage.read_write" // default }

Store a file in Google Cloud Storage

A file can be uploaded to Google Cloud Storage by creating a source of ByteStringByteString and running that with a sink created from GCStorage.resumableUploadGCStorage.resumableUpload.

Scala
val sink =
  GCStorage.resumableUpload(bucketName, fileName, ContentTypes.`text/plain(UTF-8)`, chunkSize)

val source = Source(
  List(ByteString(firstChunkContent), ByteString(secondChunkContent))
)

val result: Future[StorageObject] = source.runWith(sink)
Java

final Sink<ByteString, CompletionStage<StorageObject>> sink = GCStorage.resumableUpload( bucketName(), fileName(), ContentTypes.TEXT_PLAIN_UTF8, chunkSize); final Source<ByteString, NotUsed> source = Source.from( Lists.newArrayList( ByteString.fromString(firstChunkContent), ByteString.fromString(secondChunkContent))); final CompletionStage<StorageObject> result = source.runWith(sink, materializer);

Download a file from Google Cloud Storage

A source for downloading a file can be created by calling GCStorage.downloadGCStorage.download. It will emit an OptionOptional that will hold file’s data or will be empty if no such file can be found.

Scala

val downloadSource: Source[Option[Source[ByteString, NotUsed]], NotUsed] = GCStorage.download(bucketName, fileName) val Some(data: Source[ByteString, _]): Option[Source[ByteString, NotUsed]] = downloadSource.runWith(Sink.head).futureValue val result: Future[Seq[String]] = data.map(_.utf8String).runWith(Sink.seq)
Java

final Source<Optional<Source<ByteString, NotUsed>>, NotUsed> downloadSource = GCStorage.download(bucketName(), fileName()); final Source<ByteString, NotUsed> data = downloadSource .runWith(Sink.head(), materializer) .toCompletableFuture() .get(5, TimeUnit.SECONDS) .get(); final CompletionStage<List<String>> resultCompletionStage = data.map(ByteString::utf8String).runWith(Sink.seq(), materializer); final List<String> result = resultCompletionStage.toCompletableFuture().get(5, TimeUnit.SECONDS);

Access object metadata without downloading object from Google Cloud Storage

If you do not need object itself, you can query for only object metadata using a source from GCStorage.getObjectGCStorage.getObject.

Scala

val getObjectSource: Source[Option[StorageObject], NotUsed] = GCStorage.getObject(bucketName, fileName)
Java

final Source<Optional<StorageObject>, NotUsed> getObjectSource = GCStorage.getObject(this.bucketName(), this.fileName());

List bucket contents

To get a list of all objects in a bucket, use GCStorage.listBucketGCStorage.listBucket. When run, this will give a stream of StorageObject.

Scala

val listSource: Source[StorageObject, NotUsed] = GCStorage.listBucket(bucketName, Some(folder))
Java

final Source<StorageObject, NotUsed> listSource = GCStorage.listBucket(this.bucketName(), folder);

Rewrite (multi part)

Copy an Google Clouds Storage object from source bucket to target bucket using GCStorage.rewriteGCStorage.rewrite. When run, this will emit a single StorageObject with the information about the copied object.

Scala

val result: Future[StorageObject] = GCStorage.rewrite(bucketName, fileName, rewriteBucketName, fileName).run
Java

final CompletionStage<StorageObject> result = GCStorage.rewrite(bucketName(), fileName(), rewriteBucketName, fileName()) .run(materializer);

Apply Google Cloud Storage settings to a part of the stream

It is possible to make one part of the stream use different GCStorageSettings from the rest of the graph. This can be useful, when one stream is used to copy files across regions with different service accounts. You can attach a custom GCStorageSettings instance or a custom config path to a graph using attributes from GCStorageAttributes:

Scala

val newBasePathSettings = GCStorageExt(this.system).settings.withBasePath("/storage/v1") val listSource: Source[StorageObject, NotUsed] = GCStorage.listBucket(bucketName, None).withAttributes(GCStorageAttributes.settings(newBasePathSettings))
Java

final GCStorageSettings newBasePathSettings = GCStorageExt.get(this.system()).settings().withBasePath("/storage/v1"); final Source<StorageObject, NotUsed> listSource = GCStorage.listBucket(this.bucketName()) .withAttributes(GCStorageAttributes.settings(newBasePathSettings));

Bucket management

Bucket management API provides functionality for both Sources and Futures / CompletionStages. In case of the Future API user can specify attributes to the request in the method itself and as for Sources it can be done via method .withAttributes. For more information about attributes see: GCStorageAttributes and Attributes

Make bucket

In order to create a bucket in Google Cloud Storage you need to specify it’s unique name. This value has to be set accordingly to the requirements. The bucket will be created in the given location.

Scala

implicit val sampleAttributes: Attributes = GCStorageAttributes.settings(sampleSettings) val createBucketResponse: Future[Bucket] = GCStorage.createBucket(bucketName, location) val createBucketSourceResponse: Source[Bucket, NotUsed] = GCStorage.createBucketSource(bucketName, location)
Java

final Attributes sampleAttributes = GCStorageAttributes.settings(sampleSettings); final CompletionStage<Bucket> createBucketResponse = GCStorage.createBucket(this.bucketName(), location, materializer, sampleAttributes); final Source<Bucket, NotUsed> createBucketSourceResponse = GCStorage.createBucketSource(this.bucketName(), location);

Delete bucket

To delete a bucket you need to specify its name and the bucket needs to be empty.

Scala

implicit val sampleAttributes: Attributes = GCStorageAttributes.settings(sampleSettings) val deleteBucketResponse: Future[Done] = GCStorage.deleteBucket(bucketName) val deleteBucketSourceResponse: Source[Done, NotUsed] = GCStorage.deleteBucketSource(bucketName)
Java

final Attributes sampleAttributes = GCStorageAttributes.settings(sampleSettings); final CompletionStage<Done> deleteBucketResponse = GCStorage.deleteBucket(this.bucketName(), materializer, sampleAttributes); final Source<Done, NotUsed> deleteBucketSourceResponse = GCStorage.deleteBucketSource(this.bucketName());

Get bucket

To get a bucket you need to specify its name.

Scala

implicit val sampleAttributes: Attributes = GCStorageAttributes.settings(sampleSettings) val getBucketResponse: Future[Option[Bucket]] = GCStorage.getBucket(bucketName) val getBucketSourceResponse: Source[Option[Bucket], NotUsed] = GCStorage.getBucketSource(bucketName)
Java

final Attributes sampleAttributes = GCStorageAttributes.settings(sampleSettings); final CompletionStage<Optional<Bucket>> getBucketResponse = GCStorage.getBucket(this.bucketName(), materializer, sampleAttributes); final Source<Optional<Bucket>, NotUsed> getBucketSourceResponse = GCStorage.getBucketSource(this.bucketName());

Running the example code

The code in this guide is part of runnable tests of this project. You are welcome to edit the code and run it in sbt.

Scala
sbt
> google-cloud-storage/test
Java
sbt
> google-cloud-storage/test

Some test code requires access to Google cloud storage, to run them you will need to configure a project and pub/sub in google cloud and provide your own credentials.

Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.