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: Apache Pekko Connectors Google Cloud Storage
Artifact
org.apache.pekko
pekko-connectors-google-cloud-storage
2.0.0-RC1
JDK versions
OpenJDK 17
OpenJDK 21
Scala versions2.13.18, 3.3.9
JPMS module namepekko.stream.connectors.google.cloud.storage
License
API documentation
Forums
Release notesGitHub releases
IssuesGithub issues
Sourceshttps://github.com/apache/pekko-connectors

Artifacts

sbt
val PekkoVersion = "2.0.0"
val PekkoHttpVersion = "2.0.1"
libraryDependencies ++= Seq(
  "org.apache.pekko" %% "pekko-connectors-google-cloud-storage" % "2.0.0-RC1",
  "org.apache.pekko" %% "pekko-stream" % PekkoVersion,
  "org.apache.pekko" %% "pekko-http" % PekkoHttpVersion,
  "org.apache.pekko" %% "pekko-http-spray-json" % PekkoHttpVersion
)
Maven
<properties>
  <pekko.version>2.0.0</pekko.version>
  <pekko.http.version>2.0.1</pekko.http.version>
  <scala.binary.version>2.13</scala.binary.version>
</properties>
<dependencies>
  <dependency>
    <groupId>org.apache.pekko</groupId>
    <artifactId>pekko-connectors-google-cloud-storage_${scala.binary.version}</artifactId>
    <version>2.0.0-RC1</version>
  </dependency>
  <dependency>
    <groupId>org.apache.pekko</groupId>
    <artifactId>pekko-stream_${scala.binary.version}</artifactId>
    <version>${pekko.version}</version>
  </dependency>
  <dependency>
    <groupId>org.apache.pekko</groupId>
    <artifactId>pekko-http_${scala.binary.version}</artifactId>
    <version>${pekko.http.version}</version>
  </dependency>
  <dependency>
    <groupId>org.apache.pekko</groupId>
    <artifactId>pekko-http-spray-json_${scala.binary.version}</artifactId>
    <version>${pekko.http.version}</version>
  </dependency>
</dependencies>
Gradle
def versions = [
  PekkoVersion: "2.0.0",
  PekkoHttpVersion: "2.0.1",
  ScalaBinary: "2.13"
]
dependencies {
  implementation "org.apache.pekko:pekko-connectors-google-cloud-storage_${versions.ScalaBinary}:2.0.0-RC1"
  implementation "org.apache.pekko:pekko-stream_${versions.ScalaBinary}:${versions.PekkoVersion}"
  implementation "org.apache.pekko:pekko-http_${versions.ScalaBinary}:${versions.PekkoHttpVersion}"
  implementation "org.apache.pekko:pekko-http-spray-json_${versions.ScalaBinary}:${versions.PekkoHttpVersion}"
}

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

Direct dependencies
OrganizationArtifactVersion
org.apache.pekkopekko-connectors-google-common_2.132.0.0-RC1
org.apache.pekkopekko-http-spray-json_2.132.0.1
org.apache.pekkopekko-http_2.132.0.1
org.apache.pekkopekko-stream_2.132.0.0
org.scala-langscala-library2.13.18
Dependency tree
org.apache.pekko    pekko-connectors-google-common_2.13    2.0.0-RC1
    com.github.jwt-scala    jwt-json-common_2.13    12.0.0    Apache-2.0
        com.github.jwt-scala    jwt-core_2.13    12.0.0    Apache-2.0
            org.scala-lang    scala-library    2.13.18    Apache-2.0
        org.scala-lang    scala-library    2.13.18    Apache-2.0
    com.google.auth    google-auth-library-credentials    1.58.0    BSD New license
        org.jspecify    jspecify    1.0.1    The Apache License, Version 2.0
    com.google.auth    google-auth-library-oauth2-http    1.58.0    BSD New license
        com.google.api    api-common    2.74.0    BSD-3-Clause
            com.google.auto.value    auto-value-annotations    1.11.0    Apache 2.0
            com.google.code.findbugs    jsr305    3.0.2    The Apache Software License, Version 2.0
            com.google.errorprone    error_prone_annotations    2.50.0    Apache 2.0
            com.google.guava    guava    33.7.2-jre    Apache License, Version 2.0
                com.google.errorprone    error_prone_annotations    2.50.0    Apache 2.0
                com.google.guava    failureaccess    1.0.3    Apache License, Version 2.0
                com.google.guava    listenablefuture    9999.0-empty-to-avoid-conflict-with-guava    The Apache Software License, Version 2.0
                com.google.j2objc    j2objc-annotations    3.1    Apache License, Version 2.0
                org.jspecify    jspecify    1.0.1    The Apache License, Version 2.0
        com.google.auth    google-auth-library-credentials    1.58.0    BSD New license
            org.jspecify    jspecify    1.0.1    The Apache License, Version 2.0
        com.google.auto.value    auto-value-annotations    1.11.0    Apache 2.0
        com.google.code.findbugs    jsr305    3.0.2    The Apache Software License, Version 2.0
        com.google.code.gson    gson    2.14.0    Apache-2.0
            com.google.errorprone    error_prone_annotations    2.50.0    Apache 2.0
        com.google.errorprone    error_prone_annotations    2.50.0    Apache 2.0
        com.google.guava    guava    33.7.2-jre    Apache License, Version 2.0
            com.google.errorprone    error_prone_annotations    2.50.0    Apache 2.0
            com.google.guava    failureaccess    1.0.3    Apache License, Version 2.0
            com.google.guava    listenablefuture    9999.0-empty-to-avoid-conflict-with-guava    The Apache Software License, Version 2.0
            com.google.j2objc    j2objc-annotations    3.1    Apache License, Version 2.0
            org.jspecify    jspecify    1.0.1    The Apache License, Version 2.0
        com.google.http-client    google-http-client-gson    2.2.0    The Apache Software License, Version 2.0
            com.google.code.gson    gson    2.14.0    Apache-2.0
                com.google.errorprone    error_prone_annotations    2.50.0    Apache 2.0
            com.google.http-client    google-http-client    2.2.0    The Apache Software License, Version 2.0
                com.google.code.findbugs    jsr305    3.0.2    The Apache Software License, Version 2.0
                com.google.errorprone    error_prone_annotations    2.50.0    Apache 2.0
                com.google.guava    guava    33.7.2-jre    Apache License, Version 2.0
                    com.google.errorprone    error_prone_annotations    2.50.0    Apache 2.0
                    com.google.guava    failureaccess    1.0.3    Apache License, Version 2.0
                    com.google.guava    listenablefuture    9999.0-empty-to-avoid-conflict-with-guava    The Apache Software License, Version 2.0
                    com.google.j2objc    j2objc-annotations    3.1    Apache License, Version 2.0
                    org.jspecify    jspecify    1.0.1    The Apache License, Version 2.0
                com.google.j2objc    j2objc-annotations    3.1    Apache License, Version 2.0
                io.grpc    grpc-context    1.70.0    Apache 2.0
                    io.grpc    grpc-api    1.70.0    Apache 2.0
                io.opencensus    opencensus-api    0.31.1    The Apache License, Version 2.0
                    io.grpc    grpc-context    1.70.0    Apache 2.0
                        io.grpc    grpc-api    1.70.0    Apache 2.0
                io.opencensus    opencensus-contrib-http-util    0.31.1    The Apache License, Version 2.0
                    com.google.guava    guava    33.7.2-jre    Apache License, Version 2.0
                        com.google.errorprone    error_prone_annotations    2.50.0    Apache 2.0
                        com.google.guava    failureaccess    1.0.3    Apache License, Version 2.0
                        com.google.guava    listenablefuture    9999.0-empty-to-avoid-conflict-with-guava    The Apache Software License, Version 2.0
                        com.google.j2objc    j2objc-annotations    3.1    Apache License, Version 2.0
                        org.jspecify    jspecify    1.0.1    The Apache License, Version 2.0
                    io.opencensus    opencensus-api    0.31.1    The Apache License, Version 2.0
                        io.grpc    grpc-context    1.70.0    Apache 2.0
                            io.grpc    grpc-api    1.70.0    Apache 2.0
                org.apache.httpcomponents    httpclient    4.5.14    Apache License, Version 2.0
                    commons-codec    commons-codec    1.11    Apache License, Version 2.0
                    org.apache.httpcomponents    httpcore    4.4.16    Apache License, Version 2.0
                org.apache.httpcomponents    httpcore    4.4.16    Apache License, Version 2.0
        com.google.http-client    google-http-client    2.2.0    The Apache Software License, Version 2.0
            com.google.code.findbugs    jsr305    3.0.2    The Apache Software License, Version 2.0
            com.google.errorprone    error_prone_annotations    2.50.0    Apache 2.0
            com.google.guava    guava    33.7.2-jre    Apache License, Version 2.0
                com.google.errorprone    error_prone_annotations    2.50.0    Apache 2.0
                com.google.guava    failureaccess    1.0.3    Apache License, Version 2.0
                com.google.guava    listenablefuture    9999.0-empty-to-avoid-conflict-with-guava    The Apache Software License, Version 2.0
                com.google.j2objc    j2objc-annotations    3.1    Apache License, Version 2.0
                org.jspecify    jspecify    1.0.1    The Apache License, Version 2.0
            com.google.j2objc    j2objc-annotations    3.1    Apache License, Version 2.0
            io.grpc    grpc-context    1.70.0    Apache 2.0
                io.grpc    grpc-api    1.70.0    Apache 2.0
            io.opencensus    opencensus-api    0.31.1    The Apache License, Version 2.0
                io.grpc    grpc-context    1.70.0    Apache 2.0
                    io.grpc    grpc-api    1.70.0    Apache 2.0
            io.opencensus    opencensus-contrib-http-util    0.31.1    The Apache License, Version 2.0
                com.google.guava    guava    33.7.2-jre    Apache License, Version 2.0
                    com.google.errorprone    error_prone_annotations    2.50.0    Apache 2.0
                    com.google.guava    failureaccess    1.0.3    Apache License, Version 2.0
                    com.google.guava    listenablefuture    9999.0-empty-to-avoid-conflict-with-guava    The Apache Software License, Version 2.0
                    com.google.j2objc    j2objc-annotations    3.1    Apache License, Version 2.0
                    org.jspecify    jspecify    1.0.1    The Apache License, Version 2.0
                io.opencensus    opencensus-api    0.31.1    The Apache License, Version 2.0
                    io.grpc    grpc-context    1.70.0    Apache 2.0
                        io.grpc    grpc-api    1.70.0    Apache 2.0
            org.apache.httpcomponents    httpclient    4.5.14    Apache License, Version 2.0
                commons-codec    commons-codec    1.11    Apache License, Version 2.0
                org.apache.httpcomponents    httpcore    4.4.16    Apache License, Version 2.0
            org.apache.httpcomponents    httpcore    4.4.16    Apache License, Version 2.0
        org.jspecify    jspecify    1.0.1    The Apache License, Version 2.0
    org.apache.pekko    pekko-http-spray-json_2.13    2.0.1    Apache-2.0
        io.spray    spray-json_2.13    1.3.6    Apache 2
            org.scala-lang    scala-library    2.13.18    Apache-2.0
        org.apache.pekko    pekko-http_2.13    2.0.1    Apache-2.0
            org.apache.pekko    pekko-http-core_2.13    2.0.1    Apache-2.0
                org.apache.pekko    pekko-parsing_2.13    2.0.1    Apache-2.0
                    org.scala-lang    scala-library    2.13.18    Apache-2.0
                org.parboiled    parboiled_2.13    2.5.1    Apache-2.0
                    org.scala-lang    scala-library    2.13.18    Apache-2.0
                org.scala-lang    scala-library    2.13.18    Apache-2.0
            org.scala-lang    scala-library    2.13.18    Apache-2.0
        org.scala-lang    scala-library    2.13.18    Apache-2.0
    org.apache.pekko    pekko-http_2.13    2.0.1    Apache-2.0
        org.apache.pekko    pekko-http-core_2.13    2.0.1    Apache-2.0
            org.apache.pekko    pekko-parsing_2.13    2.0.1    Apache-2.0
                org.scala-lang    scala-library    2.13.18    Apache-2.0
            org.parboiled    parboiled_2.13    2.5.1    Apache-2.0
                org.scala-lang    scala-library    2.13.18    Apache-2.0
            org.scala-lang    scala-library    2.13.18    Apache-2.0
        org.scala-lang    scala-library    2.13.18    Apache-2.0
    org.apache.pekko    pekko-stream_2.13    2.0.0    Apache-2.0
        org.apache.pekko    pekko-actor_2.13    2.0.0    Apache-2.0
            com.typesafe    config    1.4.9    Apache-2.0
            org.scala-lang    scala-library    2.13.18    Apache-2.0
        org.apache.pekko    pekko-protobuf-v3_2.13    2.0.0    Apache-2.0
        org.reactivestreams    reactive-streams    1.0.4    MIT-0
        org.scala-lang    scala-library    2.13.18    Apache-2.0
    org.scala-lang    scala-library    2.13.18    Apache-2.0
org.apache.pekko    pekko-http-spray-json_2.13    2.0.1    Apache-2.0
    io.spray    spray-json_2.13    1.3.6    Apache 2
        org.scala-lang    scala-library    2.13.18    Apache-2.0
    org.apache.pekko    pekko-http_2.13    2.0.1    Apache-2.0
        org.apache.pekko    pekko-http-core_2.13    2.0.1    Apache-2.0
            org.apache.pekko    pekko-parsing_2.13    2.0.1    Apache-2.0
                org.scala-lang    scala-library    2.13.18    Apache-2.0
            org.parboiled    parboiled_2.13    2.5.1    Apache-2.0
                org.scala-lang    scala-library    2.13.18    Apache-2.0
            org.scala-lang    scala-library    2.13.18    Apache-2.0
        org.scala-lang    scala-library    2.13.18    Apache-2.0
    org.scala-lang    scala-library    2.13.18    Apache-2.0
org.apache.pekko    pekko-http_2.13    2.0.1    Apache-2.0
    org.apache.pekko    pekko-http-core_2.13    2.0.1    Apache-2.0
        org.apache.pekko    pekko-parsing_2.13    2.0.1    Apache-2.0
            org.scala-lang    scala-library    2.13.18    Apache-2.0
        org.parboiled    parboiled_2.13    2.5.1    Apache-2.0
            org.scala-lang    scala-library    2.13.18    Apache-2.0
        org.scala-lang    scala-library    2.13.18    Apache-2.0
    org.scala-lang    scala-library    2.13.18    Apache-2.0
org.apache.pekko    pekko-stream_2.13    2.0.0    Apache-2.0
    org.apache.pekko    pekko-actor_2.13    2.0.0    Apache-2.0
        com.typesafe    config    1.4.9    Apache-2.0
        org.scala-lang    scala-library    2.13.18    Apache-2.0
    org.apache.pekko    pekko-protobuf-v3_2.13    2.0.0    Apache-2.0
    org.reactivestreams    reactive-streams    1.0.4    MIT-0
    org.scala-lang    scala-library    2.13.18    Apache-2.0
org.scala-lang    scala-library    2.13.18    Apache-2.0

Configuration

The Storage connector shares its basic configuration with all the Google connectors in Apache Pekko Connectors. Additional Storage-specific configuration settings can be found in its own reference.conf.

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
sourceval sink =
  GCStorage.resumableUpload(bucketName, fileName, ContentTypes.`text/plain(UTF-8)`, chunkSize, metadata)

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

val result: Future[StorageObject] = source.runWith(sink)
Java
source
final Sink<ByteString, CompletionStage<StorageObject>> sink = GCStorage.resumableUpload( bucketName(), fileName(), ContentTypes.TEXT_PLAIN_UTF8, chunkSize); final Source<ByteString, NotUsed> source = Source.from( List.of( ByteString.fromString(firstChunkContent), ByteString.fromString(secondChunkContent))); final CompletionStage<StorageObject> result = source.runWith(sink, system());

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.

If you need to download the specific version of the object in a bucket where object versioning is enabled, you can specify the generation.

Scala
source
val downloadSource: Source[Option[Source[ByteString, NotUsed]], NotUsed] = GCStorage.download(bucketName, fileName) val downloadGenerationSource: Source[Option[Source[ByteString, NotUsed]], NotUsed] = GCStorage.download(bucketName, fileName, Some(generation))
Java
source
final Source<Optional<Source<ByteString, NotUsed>>, NotUsed> downloadSource = GCStorage.download(bucketName(), fileName()); final Source<Optional<Source<ByteString, NotUsed>>, NotUsed> downloadGenerationSource = GCStorage.download(bucketName(), fileName(), generation());

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.

If you need the specific version of the object metadata in a bucket where object versioning is enabled, you can specify the generation.

Scala
source
val getObjectSource: Source[Option[StorageObject], NotUsed] = GCStorage.getObject(bucketName, fileName) val getObjectGenerationSource: Source[Option[StorageObject], NotUsed] = GCStorage.getObject(bucketName, fileName, Some(generation))
Java
source
final Source<Optional<StorageObject>, NotUsed> getObjectSource = GCStorage.getObject(bucketName(), fileName()); final Source<Optional<StorageObject>, NotUsed> getObjectGenerationSource = GCStorage.getObject(bucketName(), fileName(), generation());

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.

To get a list of both live and archived versions of all objects in a bucket where object versioning is enabled, the versions has to be set to true

Scala
source
val listSource: Source[StorageObject, NotUsed] = GCStorage.listBucket(bucketName, Some(folder)) val listVersionsSource: Source[StorageObject, NotUsed] = GCStorage.listBucket(bucketName, Some(folder), versions)
Java
source
final Source<StorageObject, NotUsed> listSource = GCStorage.listBucket(bucketName(), folder); final Source<StorageObject, NotUsed> listVersionsSource = GCStorage.listBucket(bucketName(), folder, versions);

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
source
val result: Future[StorageObject] = GCStorage.rewrite(bucketName, fileName, rewriteBucketName, fileName).run()
Java
source
final CompletionStage<StorageObject> result = GCStorage.rewrite(bucketName(), fileName(), rewriteBucketName, fileName()).run(system());

Apply Google Cloud Storage settings to a part of the stream

It is possible to make one part of the stream use different GoogleSettings 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 GoogleSettings instance or a custom config path to a graph using attributes from GoogleAttributes:

Scala
source
val newSettings = GoogleSettings(system).withProjectId("projectId") val listSource: Source[StorageObject, NotUsed] = GCStorage.listBucket(bucketName, None).withAttributes(GoogleAttributes.settings(newSettings))
Java
source
final GoogleSettings newSettings = GoogleSettings.create(system()).withProjectId("projectId"); final Source<StorageObject, NotUsed> listSource = GCStorage.listBucket(bucketName()).withAttributes(GoogleAttributes.settings(newSettings));

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
source
implicit val sampleAttributes: Attributes = GoogleAttributes.settings(sampleSettings) val createBucketResponse: Future[Bucket] = GCStorage.createBucket(bucketName, location) val createBucketSourceResponse: Source[Bucket, NotUsed] = GCStorage.createBucketSource(bucketName, location)
Java
source
final Attributes sampleAttributes = GoogleAttributes.settings(sampleSettings); final CompletionStage<Bucket> createBucketResponse = GCStorage.createBucket(bucketName(), location, actorSystem, sampleAttributes); final Source<Bucket, NotUsed> createBucketSourceResponse = GCStorage.createBucketSource(bucketName(), location);

Delete bucket

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

Scala
source
implicit val sampleAttributes: Attributes = GoogleAttributes.settings(sampleSettings) val deleteBucketResponse: Future[Done] = GCStorage.deleteBucket(bucketName) val deleteBucketSourceResponse: Source[Done, NotUsed] = GCStorage.deleteBucketSource(bucketName)
Java
source
final Attributes sampleAttributes = GoogleAttributes.settings(sampleSettings); final CompletionStage<Done> deleteBucketResponse = GCStorage.deleteBucket(this.bucketName(), actorSystem, sampleAttributes); final Source<Done, NotUsed> deleteBucketSourceResponse = GCStorage.deleteBucketSource(this.bucketName());

Get bucket

To get a bucket you need to specify its name.

Scala
source
implicit val sampleAttributes: Attributes = GoogleAttributes.settings(sampleSettings) val getBucketResponse: Future[Option[Bucket]] = GCStorage.getBucket(bucketName) val getBucketSourceResponse: Source[Option[Bucket], NotUsed] = GCStorage.getBucketSource(bucketName)
Java
source
final Attributes sampleAttributes = GoogleAttributes.settings(sampleSettings); final CompletionStage<Optional<Bucket>> getBucketResponse = GCStorage.getBucket(this.bucketName(), actorSystem, 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.