Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set StorageOptions in FirebaseOptions #826

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 18 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,8 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.cloud.firestore.Firestore;
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 All @@ -27,6 +29,7 @@
import com.google.firebase.FirebaseApp;
import com.google.firebase.ImplFirebaseTrampolines;
import com.google.firebase.internal.FirebaseService;
import com.google.firebase.internal.NonNull;

/**
* StorageClient provides access to Google Cloud Storage APIs. You can specify a default cloud
Expand Down Expand Up @@ -56,8 +59,12 @@ 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))
.setProjectId(ImplFirebaseTrampolines.getProjectId(app))
.build()
.getService();
StorageClient client = new StorageClient(app, storage);
Expand All @@ -66,6 +73,16 @@ public static synchronized StorageClient getInstance(FirebaseApp app) {
return service.getInstance();
}

@NonNull
public static Storage getStorage(FirebaseApp app) {
return getInstance(app).storage;
}

@NonNull
public static Storage getStorage() {
return getStorage(FirebaseApp.getInstance());
}

/**
* Returns the default cloud storage bucket associated with the current app. This is the bucket
* configured via {@link com.google.firebase.FirebaseOptions} when initializing the app. If
Expand Down
7 changes: 6 additions & 1 deletion src/test/java/com/google/firebase/cloud/StorageClientIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@
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.FirebaseApp;
import com.google.firebase.FirebaseOptions;
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 @@ -87,5 +93,4 @@ private Blob createTextBlob(Bucket bucket, String contents) {
String fileName = "data_" + System.currentTimeMillis() + ".txt";
return bucket.create(fileName, contents.getBytes(), "text/plain");
}

}
34 changes: 34 additions & 0 deletions src/test/java/com/google/firebase/cloud/StorageClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@

package com.google.firebase.cloud;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.ImplFirebaseTrampolines;
import com.google.firebase.TestOnlyImplFirebaseTrampolines;
import com.google.firebase.testing.ServiceAccount;
import java.io.IOException;
Expand Down Expand Up @@ -126,4 +130,34 @@ public void testBucket() throws IOException {
assertSame(mockBucket, client.bucket());
assertSame(mockBucket, client.bucket("mock-bucket-name"));
}

@Test
public void testAbsentStorageOptions() throws Exception {
String projectId = "test-proj";
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
.setProjectId(projectId)
.build();

FirebaseApp.initializeApp(options);
Storage storage = StorageClient.getStorage();
assertEquals(projectId, storage.getOptions().getProjectId());
assertEquals("https://storage.googleapis.com", storage.getOptions().getHost());
}

@Test
public void testStorageOptions() throws IOException {
FirebaseApp app = FirebaseApp.initializeApp(FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
.setProjectId("explicit-project-id")
.setStorageOptions(StorageOptions.newBuilder()
.setHost("explicit-host")
.build())
.build());
Storage storage1 = StorageClient.getStorage(app);
assertEquals("explicit-project-id", storage1.getOptions().getProjectId());
assertEquals("explicit-host", storage1.getOptions().getHost());

assertSame(storage1, StorageClient.getStorage());
}
}