Skip to content

Commit 49e008c

Browse files
committed
use getBucket instead of getStorageLayout api
1 parent f1d1107 commit 49e008c

8 files changed

Lines changed: 35 additions & 142 deletions

File tree

java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageImpl.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
import com.google.storage.v2.DeleteBucketRequest;
9797
import com.google.storage.v2.DeleteObjectRequest;
9898
import com.google.storage.v2.GetBucketRequest;
99+
import com.google.storage.v2.Bucket;
99100
import com.google.storage.v2.GetObjectRequest;
100101
import com.google.storage.v2.ListBucketsRequest;
101102
import com.google.storage.v2.ListObjectsRequest;
@@ -222,23 +223,23 @@ public BucketMetadataCache getBucketMetadataCache() {
222223
}
223224

224225
@Override
225-
public com.google.cloud.Tuple<String, String> internalGetStorageLayout(String bucketName) {
226-
com.google.storage.v2.stub.StorageStub rawStub = storageClient.getStub();
227-
if (!(rawStub instanceof GrpcStorageOptions.AcoGrpcStorageStub)) {
228-
throw new RuntimeException("StorageStub is not an AcoGrpcStorageStub");
229-
}
230-
GrpcStorageOptions.AcoGrpcStorageStub stub = (GrpcStorageOptions.AcoGrpcStorageStub) rawStub;
231-
232-
GetStorageLayoutRequest request =
233-
GetStorageLayoutRequest.newBuilder()
234-
.setName(StorageLayoutName.of(getOptions().getProjectId(), bucketName).toString())
226+
public com.google.cloud.Tuple<String, String> internalGetBucket(String bucketName) {
227+
GetBucketRequest request =
228+
GetBucketRequest.newBuilder()
229+
.setName(bucketNameCodec.encode(bucketName))
235230
.build();
236-
237-
com.google.api.gax.grpc.GrpcCallContext merge = com.google.cloud.storage.Utils.merge(com.google.api.gax.grpc.GrpcCallContext.createDefault(), Retrying.newCallContext());
238-
239-
StorageLayout layout = stub.getStorageLayoutCallable().call(request, merge);
240-
241-
return com.google.cloud.Tuple.of(layout.getName(), layout.getLocation());
231+
GrpcCallContext merge = merge(GrpcCallContext.createDefault(), Retrying.newCallContext());
232+
Bucket bucket = storageClient.getBucketCallable().call(request, merge);
233+
String pNum = bucket.getProject();
234+
if (pNum == null || pNum.isEmpty()) {
235+
pNum = "projects/_";
236+
}
237+
String actualResource = pNum + "/buckets/" + bucketName;
238+
String actualLocation = bucket.getLocation();
239+
if (actualLocation == null || actualLocation.isEmpty()) {
240+
actualLocation = "global";
241+
}
242+
return com.google.cloud.Tuple.of(actualResource, actualLocation);
242243
}
243244

244245
@Override

java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java

Lines changed: 2 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@
4545
import com.google.api.gax.rpc.RequestParamsBuilder;
4646
import com.google.api.gax.rpc.RequestParamsExtractor;
4747
import com.google.api.gax.rpc.ServerStreamingCallable;
48-
import com.google.api.gax.rpc.UnaryCallable;
49-
import com.google.api.gax.grpc.GrpcCallSettings;
50-
import com.google.storage.control.v2.GetStorageLayoutRequest;
51-
import com.google.storage.control.v2.StorageLayout;
5248
import com.google.api.gax.rpc.internal.QuotaProjectIdHidingCredentials;
5349
import com.google.api.gax.tracing.ApiTracerFactory;
5450
import com.google.api.pathtemplate.PathTemplate;
@@ -955,10 +951,7 @@ public Storage create(StorageOptions options) {
955951
} else {
956952
LOGGER.config(
957953
"zero-copy protobuf deserialization unavailable, proceeding with default");
958-
StorageStubSettings baseSettings = (StorageStubSettings) storageSettings.getStubSettings();
959-
AcoStorageStubSettings.Builder acoStubBuilder = new AcoStorageStubSettings.Builder(baseSettings);
960-
AcoStorageSettings.Builder acoSettingsBuilder = new AcoStorageSettings.Builder(acoStubBuilder);
961-
AcoStorageClient client = new AcoStorageClient(new AcoStorageSettings(acoSettingsBuilder));
954+
StorageClient client = StorageClient.create(storageSettings);
962955
StorageDataClient dataClient =
963956
StorageDataClient.create(
964957
executor,
@@ -1110,94 +1103,6 @@ public InternalStorageSettings build() throws IOException {
11101103
}
11111104
}
11121105

1113-
static class AcoGrpcStorageStub extends GrpcStorageStub {
1114-
private final UnaryCallable<GetStorageLayoutRequest, StorageLayout> getStorageLayoutCallable;
1115-
1116-
AcoGrpcStorageStub(
1117-
StorageStubSettings settings,
1118-
ClientContext clientContext,
1119-
GrpcStubCallableFactory callableFactory)
1120-
throws IOException {
1121-
super(settings, clientContext, callableFactory);
1122-
1123-
MethodDescriptor<GetStorageLayoutRequest, StorageLayout> getStorageLayoutMethod =
1124-
MethodDescriptor.<GetStorageLayoutRequest, StorageLayout>newBuilder()
1125-
.setType(MethodDescriptor.MethodType.UNARY)
1126-
.setFullMethodName("google.storage.control.v2.StorageControl/GetStorageLayout")
1127-
.setRequestMarshaller(ProtoUtils.marshaller(GetStorageLayoutRequest.getDefaultInstance()))
1128-
.setResponseMarshaller(ProtoUtils.marshaller(StorageLayout.getDefaultInstance()))
1129-
.build();
1130-
1131-
GrpcCallSettings<GetStorageLayoutRequest, StorageLayout> transportSettings =
1132-
GrpcCallSettings.<GetStorageLayoutRequest, StorageLayout>newBuilder()
1133-
.setMethodDescriptor(getStorageLayoutMethod)
1134-
.setParamsExtractor(request -> ImmutableMap.of())
1135-
.build();
1136-
1137-
this.getStorageLayoutCallable =
1138-
callableFactory.createUnaryCallable(
1139-
transportSettings,
1140-
com.google.api.gax.rpc.UnaryCallSettings.<GetStorageLayoutRequest, StorageLayout>newUnaryCallSettingsBuilder().build(),
1141-
clientContext);
1142-
}
1143-
1144-
public UnaryCallable<GetStorageLayoutRequest, StorageLayout> getStorageLayoutCallable() {
1145-
return getStorageLayoutCallable;
1146-
}
1147-
}
1148-
1149-
private static final class AcoStorageStubSettings extends StorageStubSettings {
1150-
private AcoStorageStubSettings(Builder settingsBuilder) throws IOException {
1151-
super(settingsBuilder);
1152-
}
1153-
@Override
1154-
public StorageStub createStub() throws IOException {
1155-
if (!getTransportChannelProvider()
1156-
.getTransportName()
1157-
.equals(GrpcTransportChannel.getGrpcTransportName())) {
1158-
throw new UnsupportedOperationException(
1159-
String.format(
1160-
"Transport not supported: %s", getTransportChannelProvider().getTransportName()));
1161-
}
1162-
ClientContext clientContext = ClientContext.create(this);
1163-
return new AcoGrpcStorageStub(this, clientContext, new GrpcStorageCallableFactory());
1164-
}
1165-
private static final class Builder extends StorageStubSettings.Builder {
1166-
private Builder(StorageStubSettings settings) {
1167-
super(settings);
1168-
}
1169-
@Override
1170-
public AcoStorageStubSettings build() throws IOException {
1171-
return new AcoStorageStubSettings(this);
1172-
}
1173-
}
1174-
}
1175-
1176-
private static final class AcoStorageSettings extends StorageSettings {
1177-
private AcoStorageSettings(Builder settingsBuilder) throws IOException {
1178-
super(settingsBuilder);
1179-
}
1180-
private static final class Builder extends StorageSettings.Builder {
1181-
private Builder(StorageStubSettings.Builder stubSettings) {
1182-
super(stubSettings);
1183-
}
1184-
@Override
1185-
public AcoStorageSettings build() throws IOException {
1186-
return new AcoStorageSettings(this);
1187-
}
1188-
}
1189-
}
1190-
1191-
private static final class AcoStorageClient extends StorageClient {
1192-
private AcoStorageClient(StorageSettings settings) throws IOException {
1193-
super(settings);
1194-
}
1195-
@Override
1196-
public AcoGrpcStorageStub getStub() {
1197-
return (AcoGrpcStorageStub) super.getStub();
1198-
}
1199-
}
1200-
12011106
private static final class InternalStorageStubSettings extends StorageStubSettings {
12021107

12031108
private InternalStorageStubSettings(Builder settingsBuilder) throws IOException {
@@ -1236,7 +1141,7 @@ public InternalStorageStubSettings build() throws IOException {
12361141

12371142
// DanglingJavadocs are for breadcrumbs to source of copied generated code
12381143
@SuppressWarnings("DanglingJavadoc")
1239-
private static final class InternalZeroCopyGrpcStorageStub extends AcoGrpcStorageStub
1144+
private static final class InternalZeroCopyGrpcStorageStub extends GrpcStorageStub
12401145
implements AutoCloseable {
12411146

12421147
private static final RequestParamsExtractor<BidiReadObjectRequest>

java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/OtelStorageDecorator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,11 @@ void checkCacheAndTriggerFetch(String bucketName) {
126126
() -> {
127127
try {
128128
com.google.cloud.Tuple<String, String> layout =
129-
internal.internalGetStorageLayout(bucketName);
129+
internal.internalGetBucket(bucketName);
130130
cache.put(bucketName, new BucketMetadataCache.BucketMetadata(layout.x(), layout.y()));
131131
} catch (Exception e) {
132+
System.err.println("Background GetBucket failed: " + e.getMessage());
133+
e.printStackTrace();
132134
}
133135
});
134136
}

java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ public BucketMetadataCache getBucketMetadataCache() {
133133
}
134134

135135
@Override
136-
public com.google.cloud.Tuple<String, String> internalGetStorageLayout(String bucketName) {
137-
return storageRpc.getStorageLayout(bucketName);
136+
public com.google.cloud.Tuple<String, String> internalGetBucket(String bucketName) {
137+
return storageRpc.getBucketMetadata(bucketName);
138138
}
139139

140140
StorageImpl(HttpStorageOptions options, WriterFactory writerFactory, Retrier retrier) {

java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageInternal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ default BlobInfo internalObjectGet(BlobId blobId, Opts<ObjectSourceOpt> opts) {
4949
throw new UnsupportedOperationException("not implemented");
5050
}
5151

52-
default com.google.cloud.Tuple<String, String> internalGetStorageLayout(String bucketName) {
52+
default com.google.cloud.Tuple<String, String> internalGetBucket(String bucketName) {
5353
throw new UnsupportedOperationException("not implemented");
5454
}
5555

java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -584,30 +584,15 @@ public Bucket get(Bucket bucket, Map<Option, ?> options) {
584584
}
585585

586586
@Override
587-
public com.google.cloud.Tuple<String, String> getStorageLayout(String bucketName) {
587+
public com.google.cloud.Tuple<String, String> getBucketMetadata(String bucketName) {
588588
try {
589-
String url = options.getHost() + "/storage/v1/b/" + bucketName + "/storageLayout";
590-
com.google.api.client.http.GenericUrl genericUrl =
591-
new com.google.api.client.http.GenericUrl(url);
592-
com.google.api.client.http.HttpRequest request =
593-
storage.getRequestFactory().buildGetRequest(genericUrl);
594-
com.google.api.client.http.HttpResponse response = request.execute();
595-
String content = response.parseAsString();
596-
597-
String actualResource = "projects/_/buckets/" + bucketName;
598-
String actualLocation = "global";
599-
600-
com.google.api.client.json.JsonParser parser =
601-
storage.getJsonFactory().createJsonParser(content);
602-
@SuppressWarnings("unchecked")
603-
Map<String, Object> map = parser.parse(Map.class);
604-
if (map.containsKey("name")) {
605-
actualResource = (String) map.get("name");
589+
com.google.api.services.storage.model.Bucket bucket =
590+
storage.buckets().get(bucketName).execute();
591+
String actualResource = "projects/" + bucket.getProjectNumber() + "/buckets/" + bucketName;
592+
String actualLocation = bucket.getLocation();
593+
if (actualLocation == null) {
594+
actualLocation = "global";
606595
}
607-
if (map.containsKey("location")) {
608-
actualLocation = (String) map.get("location");
609-
}
610-
611596
return com.google.cloud.Tuple.of(actualResource, actualLocation);
612597
} catch (IOException e) {
613598
throw translate(e);

java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,11 +641,11 @@ TestIamPermissionsResponse testIamPermissions(
641641
ServiceAccount getServiceAccount(String projectId);
642642

643643
/**
644-
* Returns the storage layout for the specified bucket.
644+
* Returns the bucket resource id and location for the specified bucket.
645645
*
646646
* @throws StorageException upon failure
647647
*/
648-
com.google.cloud.Tuple<String, String> getStorageLayout(String bucketName);
648+
com.google.cloud.Tuple<String, String> getBucketMetadata(String bucketName);
649649

650650
@InternalApi
651651
Storage getStorage();

java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/testing/StorageRpcTestBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ public ServiceAccount getServiceAccount(String projectId) {
323323
}
324324

325325
@Override
326-
public com.google.cloud.Tuple<String, String> getStorageLayout(String bucketName) {
326+
public com.google.cloud.Tuple<String, String> getBucketMetadata(String bucketName) {
327327
throw new UnsupportedOperationException("Not implemented yet");
328328
}
329329

0 commit comments

Comments
 (0)