Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,9 @@ private Tuple<StorageSettings, Opts<UserProject>> resolveSettingsAndOpts() throw
.setCredentialsProvider(credentialsProvider)
.setClock(getClock());

if (this.getUniverseDomain() != null) {
builder.setUniverseDomain(this.getUniverseDomain());
String configuredUniverseDomain = getConfiguredUniverseDomain();
if (configuredUniverseDomain != null) {
builder.setUniverseDomain(configuredUniverseDomain);
}

// this MUST come after credentials, service options set value has higher priority than creds
Expand Down Expand Up @@ -352,7 +353,7 @@ private Tuple<StorageSettings, Opts<UserProject>> resolveSettingsAndOpts() throw
ChannelConfigurator.lift(channelProviderBuilder.getChannelConfigurator()),
endpoint,
this.getProjectId(),
this.getUniverseDomain(),
configuredUniverseDomain,
!grpcClientMetricsManuallyEnabled);
channelProviderBuilder.setChannelConfigurator(channelConfigurator);
}
Expand Down Expand Up @@ -427,6 +428,11 @@ public GrpcStorageOptions.Builder toBuilder() {
return new GrpcStorageOptions.Builder(this);
}

@Override
boolean usesDefaultHost() {
return getHost().equals("https://storage." + getResolvedUniverseDomain());
}

@Override
public int hashCode() {
return Objects.hash(
Expand Down Expand Up @@ -473,7 +479,7 @@ && isEnableOtelDebugMetrics() == that.isEnableOtelDebugMetrics()
* @since 2.14.0
*/
public static GrpcStorageOptions.Builder newBuilder() {
return new GrpcStorageOptions.Builder().setHost(DEFAULT_HOST);
return new GrpcStorageOptions.Builder();
}

/**
Expand Down Expand Up @@ -852,10 +858,18 @@ public GrpcStorageOptions.Builder setMetricInterval(Duration metricInterval) {
*/
@Override
public GrpcStorageOptions build() {
String universeDomain =
MoreObjects.firstNonNull(
resolveBuilderUniverseDomain(), Credentials.GOOGLE_DEFAULT_UNIVERSE);
String resolvedHost = "https://storage." + universeDomain;
if (!isHostManuallySet()) {
setResolvedHostInternal(resolvedHost);
return new GrpcStorageOptions(this, defaults());
}
GrpcStorageOptions options = new GrpcStorageOptions(this, defaults());
// TODO: Remove when https://github.com/googleapis/sdk-platform-java/issues/2911 is resolved
if (options.getUniverseDomain() != null) {
this.setHost("https://storage." + options.getUniverseDomain());
// The former default host literal counts as unset.
if (DEFAULT_HOST.equals(options.getHost())) {
setResolvedHostInternal(resolvedHost);
return new GrpcStorageOptions(this, defaults());
}
Comment thread
nidhiii-27 marked this conversation as resolved.
Resolved
return options;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.storage;

import static com.google.common.truth.Truth.assertThat;

import com.google.api.gax.rpc.EndpointContext;
import com.google.auth.Credentials;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.storage.v2.StorageSettings;
import java.util.HashMap;
import java.util.Map;
import org.junit.After;
import org.junit.Test;

public final class GrpcStorageUniverseDomainTest {

private static final String CUSTOM_UNIVERSE = "custom.example.com";

@After
public void tearDown() {
StorageOptions.resetEnvResolverForTesting();
}

@Test
public void resolvesDefaultHostAndChannelSettingsFromConfiguredUniverseDomain() throws Exception {
Map<String, String> env = new HashMap<>();
env.put(EndpointContext.GOOGLE_CLOUD_UNIVERSE_DOMAIN, CUSTOM_UNIVERSE);
StorageOptions.setEnvResolverForTesting(env::get);

Credentials customCreds =
GoogleCredentials.newBuilder()
.setAccessToken(new AccessToken("test-token", null))
.setUniverseDomain(CUSTOM_UNIVERSE)
.build();
GrpcStorageOptions grpcOptions =
StorageOptions.grpc()
.setProjectId("test-proj")
.setCredentials(customCreds)
.setEnableGrpcClientMetrics(false)
.build();
assertThat(grpcOptions.getUniverseDomain()).isNull();
assertThat(grpcOptions.getHost()).isEqualTo("https://storage.custom.example.com");

StorageSettings settings = grpcOptions.getStorageSettings();
assertThat(settings.getEndpoint()).isEqualTo("storage.custom.example.com:443");
assertThat(settings.getUniverseDomain()).isEqualTo(CUSTOM_UNIVERSE);

Credentials explicitCreds =
GoogleCredentials.newBuilder()
.setAccessToken(new AccessToken("test-token", null))
.setUniverseDomain("explicit.example.com")
.build();
GrpcStorageOptions explicitOptions =
StorageOptions.grpc()
.setProjectId("test-proj")
.setCredentials(explicitCreds)
.setUniverseDomain("explicit.example.com")
.setEnableGrpcClientMetrics(false)
.build();
assertThat(explicitOptions.getUniverseDomain()).isEqualTo("explicit.example.com");
assertThat(explicitOptions.getHost()).isEqualTo("https://storage.explicit.example.com");

StorageSettings explicitSettings = explicitOptions.getStorageSettings();
assertThat(explicitSettings.getEndpoint()).isEqualTo("storage.explicit.example.com:443");
assertThat(explicitSettings.getUniverseDomain()).isEqualTo("explicit.example.com");
}

@Test
public void hostResolutionWithExplicitUniverseDomain() {
GrpcStorageOptions customHostGrpc =
StorageOptions.grpc()
.setProjectId("test-proj")
.setHost("http://localhost:8080")
.setUniverseDomain("explicit.example.com")
.build();
assertThat(customHostGrpc.getHost()).isEqualTo("http://localhost:8080");
Comment thread
nidhiii-27 marked this conversation as resolved.
Resolved

GrpcStorageOptions rebuiltCustomHostGrpc =
customHostGrpc.toBuilder().setUniverseDomain("other.example.com").build();
assertThat(rebuiltCustomHostGrpc.getHost()).isEqualTo("http://localhost:8080");

GrpcStorageOptions defaultHostGrpc =
StorageOptions.grpc()
.setProjectId("test-proj")
.setHost("https://storage.googleapis.com")
.setUniverseDomain("explicit.example.com")
.build();
assertThat(defaultHostGrpc.getHost()).isEqualTo("https://storage.explicit.example.com");

GrpcStorageOptions rebuiltGrpc =
StorageOptions.grpc()
.setProjectId("test-proj")
.setUniverseDomain("first.example.com")
.build()
.toBuilder()
.setUniverseDomain("explicit.example.com")
.build();
assertThat(rebuiltGrpc.getHost()).isEqualTo("https://storage.explicit.example.com");
}
}
Loading