Skip to content

Commit

Permalink
Add StorageOptions to FirebaseOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
regine-chan committed Jun 8, 2023
1 parent 25dfab6 commit 44b1f0a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/main/java/com/google/firebase/FirebaseOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.api.client.util.Key;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.firestore.FirestoreOptions;
import com.google.cloud.storage.StorageOptions;
import com.google.common.base.Strings;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
Expand Down Expand Up @@ -86,6 +87,8 @@ public GoogleCredentials get() {
private final ThreadManager threadManager;
private final FirestoreOptions firestoreOptions;

private final StorageOptions storageOptions;

private FirebaseOptions(@NonNull final FirebaseOptions.Builder builder) {
this.databaseUrl = builder.databaseUrl;
this.credentialsSupplier = checkNotNull(
Expand Down Expand Up @@ -113,6 +116,7 @@ private FirebaseOptions(@NonNull final FirebaseOptions.Builder builder) {
checkArgument(builder.readTimeout >= 0);
this.readTimeout = builder.readTimeout;
this.firestoreOptions = builder.firestoreOptions;
this.storageOptions = builder.storageOptions;
}

/**
Expand Down Expand Up @@ -216,6 +220,10 @@ FirestoreOptions getFirestoreOptions() {
return firestoreOptions;
}

public StorageOptions getStorageOptions() {
return storageOptions;
}

/**
* Creates an empty builder.
*
Expand Down Expand Up @@ -251,6 +259,8 @@ public static final class Builder {
@Key("storageBucket")
private String storageBucket;

private StorageOptions storageOptions;

@Key("serviceAccountId")
private String serviceAccountId;
private Supplier<GoogleCredentials> credentialsSupplier;
Expand Down Expand Up @@ -290,6 +300,7 @@ public Builder(FirebaseOptions options) {
connectTimeout = options.connectTimeout;
readTimeout = options.readTimeout;
firestoreOptions = options.firestoreOptions;
storageOptions = options.storageOptions;
}

/**
Expand Down Expand Up @@ -495,6 +506,11 @@ public Builder setReadTimeout(int readTimeout) {
return this;
}

public Builder setStorageOptions(StorageOptions storageOptions) {
this.storageOptions = storageOptions;
return this;
}

/**
* Builds the {@link FirebaseOptions} instance from the previously set options.
*
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/google/firebase/cloud/StorageClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.cloud.firestore.FirestoreOptions;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
Expand Down Expand Up @@ -46,6 +47,9 @@ public class StorageClient {
StorageClient(FirebaseApp app, Storage storage) {
this.app = checkNotNull(app, "FirebaseApp must not be null");
this.storage = checkNotNull(storage, "Storage must not be null");

StorageOptions userOptions = app.getOptions().getStorageOptions();

}

public static StorageClient getInstance() {
Expand All @@ -56,7 +60,10 @@ public static synchronized StorageClient getInstance(FirebaseApp app) {
StorageClientService service = ImplFirebaseTrampolines.getService(app, SERVICE_ID,
StorageClientService.class);
if (service == null) {
Storage storage = StorageOptions.newBuilder()
StorageOptions userOptions = app.getOptions().getStorageOptions();
StorageOptions.Builder builder = userOptions != null ? userOptions.toBuilder() :
StorageOptions.newBuilder();
Storage storage = builder
.setCredentials(ImplFirebaseTrampolines.getCredentials(app))
.build()
.getService();
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/com/google/firebase/cloud/StorageClientIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.common.io.CharStreams;
import com.google.firebase.testing.IntegrationTestUtils;
import com.google.firebase.testing.ServiceAccount;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -88,4 +92,15 @@ private Blob createTextBlob(Bucket bucket, String contents) {
return bucket.create(fileName, contents.getBytes(), "text/plain");
}

@Test
public void testStorageOptions() throws IOException {
Storage storage = StorageOptions.newBuilder()
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
.setProjectId("explicit-project-id")
.setHost("explicit-host")
.build().getService();
assertEquals("explicit-project-id", storage.getOptions().getProjectId());
assertEquals("explicit-host", storage.getOptions().getHost());
}

}

0 comments on commit 44b1f0a

Please sign in to comment.