-
Notifications
You must be signed in to change notification settings - Fork 70
/
StorageClient.java
442 lines (419 loc) · 18.3 KB
/
StorageClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
* Copyright 2021 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
*
* https://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.storage.v2;
import com.google.api.core.BetaApi;
import com.google.api.gax.core.BackgroundResource;
import com.google.api.gax.rpc.ClientStreamingCallable;
import com.google.api.gax.rpc.ServerStreamingCallable;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.storage.v2.stub.StorageStub;
import com.google.storage.v2.stub.StorageStubSettings;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.annotation.Generated;
// AUTO-GENERATED DOCUMENTATION AND CLASS.
/**
* Service Description: ## API Overview and Naming Syntax
*
* <p>The GCS gRPC API allows applications to read and write data through the abstractions of
* buckets and objects. For a description of these abstractions please see
* https://cloud.google.com/storage/docs.
*
* <p>Resources are named as follows: - Projects are referred to as they are defined by the Resource
* Manager API, using strings like `projects/123456` or `projects/my-string-id`. - Buckets are named
* using string names of the form: `projects/{project}/buckets/{bucket}` For globally unique
* buckets, `_` may be substituted for the project. - Objects are uniquely identified by their name
* along with the name of the bucket they belong to, as separate strings in this API. For example:
*
* <p>ReadObjectRequest { bucket: 'projects/_/buckets/my-bucket' object: 'my-object' } Note that
* object names can contain `/` characters, which are treated as any other character (no special
* directory semantics).
*
* <p>This class provides the ability to make remote calls to the backing service through method
* calls that map to API methods. Sample code to get started:
*
* <pre>{@code
* try (StorageClient storageClient = StorageClient.create()) {
* StartResumableWriteRequest request =
* StartResumableWriteRequest.newBuilder()
* .setWriteObjectSpec(WriteObjectSpec.newBuilder().build())
* .setCommonObjectRequestParams(CommonObjectRequestParams.newBuilder().build())
* .setCommonRequestParams(CommonRequestParams.newBuilder().build())
* .build();
* StartResumableWriteResponse response = storageClient.startResumableWrite(request);
* }
* }</pre>
*
* <p>Note: close() needs to be called on the StorageClient object to clean up resources such as
* threads. In the example above, try-with-resources is used, which automatically calls close().
*
* <p>The surface of this class includes several types of Java methods for each of the API's
* methods:
*
* <ol>
* <li>A "flattened" method. With this type of method, the fields of the request type have been
* converted into function parameters. It may be the case that not all fields are available as
* parameters, and not every API method will have a flattened method entry point.
* <li>A "request object" method. This type of method only takes one parameter, a request object,
* which must be constructed before the call. Not every API method will have a request object
* method.
* <li>A "callable" method. This type of method takes no parameters and returns an immutable API
* callable object, which can be used to initiate calls to the service.
* </ol>
*
* <p>See the individual methods for example code.
*
* <p>Many parameters require resource names to be formatted in a particular way. To assist with
* these names, this class includes a format method for each type of name, and additionally a parse
* method to extract the individual identifiers contained within names that are returned.
*
* <p>This class can be customized by passing in a custom instance of StorageSettings to create().
* For example:
*
* <p>To customize credentials:
*
* <pre>{@code
* StorageSettings storageSettings =
* StorageSettings.newBuilder()
* .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
* .build();
* StorageClient storageClient = StorageClient.create(storageSettings);
* }</pre>
*
* <p>To customize the endpoint:
*
* <pre>{@code
* StorageSettings storageSettings = StorageSettings.newBuilder().setEndpoint(myEndpoint).build();
* StorageClient storageClient = StorageClient.create(storageSettings);
* }</pre>
*
* <p>Please refer to the GitHub repository's samples for more quickstart code snippets.
*/
@Generated("by gapic-generator-java")
public class StorageClient implements BackgroundResource {
private final StorageSettings settings;
private final StorageStub stub;
/** Constructs an instance of StorageClient with default settings. */
public static final StorageClient create() throws IOException {
return create(StorageSettings.newBuilder().build());
}
/**
* Constructs an instance of StorageClient, using the given settings. The channels are created
* based on the settings passed in, or defaults for any settings that are not set.
*/
public static final StorageClient create(StorageSettings settings) throws IOException {
return new StorageClient(settings);
}
/**
* Constructs an instance of StorageClient, using the given stub for making calls. This is for
* advanced usage - prefer using create(StorageSettings).
*/
@BetaApi("A restructuring of stub classes is planned, so this may break in the future")
public static final StorageClient create(StorageStub stub) {
return new StorageClient(stub);
}
/**
* Constructs an instance of StorageClient, using the given settings. This is protected so that it
* is easy to make a subclass, but otherwise, the static factory methods should be preferred.
*/
protected StorageClient(StorageSettings settings) throws IOException {
this.settings = settings;
this.stub = ((StorageStubSettings) settings.getStubSettings()).createStub();
}
@BetaApi("A restructuring of stub classes is planned, so this may break in the future")
protected StorageClient(StorageStub stub) {
this.settings = null;
this.stub = stub;
}
public final StorageSettings getSettings() {
return settings;
}
@BetaApi("A restructuring of stub classes is planned, so this may break in the future")
public StorageStub getStub() {
return stub;
}
// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
* Reads an object's data.
*
* <p>Sample code:
*
* <pre>{@code
* try (StorageClient storageClient = StorageClient.create()) {
* ReadObjectRequest request =
* ReadObjectRequest.newBuilder()
* .setBucket("bucket-1378203158")
* .setObject("object-1023368385")
* .setGeneration(305703192)
* .setReadOffset(-715377828)
* .setReadLimit(-164298798)
* .setIfGenerationMatch(-1086241088)
* .setIfGenerationNotMatch(1475720404)
* .setIfMetagenerationMatch(1043427781)
* .setIfMetagenerationNotMatch(1025430873)
* .setCommonObjectRequestParams(CommonObjectRequestParams.newBuilder().build())
* .setCommonRequestParams(CommonRequestParams.newBuilder().build())
* .setReadMask(FieldMask.newBuilder().build())
* .build();
* ServerStream<ReadObjectResponse> stream = storageClient.readObjectCallable().call(request);
* for (ReadObjectResponse response : stream) {
* // Do something when a response is received.
* }
* }
* }</pre>
*/
public final ServerStreamingCallable<ReadObjectRequest, ReadObjectResponse> readObjectCallable() {
return stub.readObjectCallable();
}
// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
* Stores a new object and metadata.
*
* <p>An object can be written either in a single message stream or in a resumable sequence of
* message streams. To write using a single stream, the client should include in the first message
* of the stream an `WriteObjectSpec` describing the destination bucket, object, and any
* preconditions. Additionally, the final message must set 'finish_write' to true, or else it is
* an error.
*
* <p>For a resumable write, the client should instead call `StartResumableWrite()` and provide
* that method an `WriteObjectSpec.` They should then attach the returned `upload_id` to the first
* message of each following call to `Create`. If there is an error or the connection is broken
* during the resumable `Create()`, the client should check the status of the `Create()` by
* calling `QueryWriteStatus()` and continue writing from the returned `persisted_size`. This may
* be less than the amount of data the client previously sent.
*
* <p>The service will not view the object as complete until the client has sent a
* `WriteObjectRequest` with `finish_write` set to `true`. Sending any requests on a stream after
* sending a request with `finish_write` set to `true` will cause an error. The client
* **should** check the response it receives to determine how much data the
* service was able to commit and whether the service views the object as complete.
*
* <p>Sample code:
*
* <pre>{@code
* try (StorageClient storageClient = StorageClient.create()) {
* ApiStreamObserver<WriteObjectRequest> responseObserver =
* new ApiStreamObserver<WriteObjectRequest>() {
* {@literal @}Override
* public void onNext(WriteObjectResponse response) {
* // Do something when a response is received.
* }
*
* {@literal @}Override
* public void onError(Throwable t) {
* // Add error-handling
* }
*
* {@literal @}Override
* public void onCompleted() {
* // Do something when complete.
* }
* };
* ApiStreamObserver<WriteObjectRequest> requestObserver =
* storageClient.writeObject().clientStreamingCall(responseObserver);
* WriteObjectRequest request =
* WriteObjectRequest.newBuilder()
* .setWriteOffset(-1559543565)
* .setObjectChecksums(ObjectChecksums.newBuilder().build())
* .setFinishWrite(true)
* .setCommonObjectRequestParams(CommonObjectRequestParams.newBuilder().build())
* .setCommonRequestParams(CommonRequestParams.newBuilder().build())
* .build();
* requestObserver.onNext(request);
* }
* }</pre>
*/
public final ClientStreamingCallable<WriteObjectRequest, WriteObjectResponse>
writeObjectCallable() {
return stub.writeObjectCallable();
}
// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
* Starts a resumable write. How long the write operation remains valid, and what happens when the
* write operation becomes invalid, are service-dependent.
*
* <p>Sample code:
*
* <pre>{@code
* try (StorageClient storageClient = StorageClient.create()) {
* StartResumableWriteRequest request =
* StartResumableWriteRequest.newBuilder()
* .setWriteObjectSpec(WriteObjectSpec.newBuilder().build())
* .setCommonObjectRequestParams(CommonObjectRequestParams.newBuilder().build())
* .setCommonRequestParams(CommonRequestParams.newBuilder().build())
* .build();
* StartResumableWriteResponse response = storageClient.startResumableWrite(request);
* }
* }</pre>
*
* @param request The request object containing all of the parameters for the API call.
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
*/
public final StartResumableWriteResponse startResumableWrite(StartResumableWriteRequest request) {
return startResumableWriteCallable().call(request);
}
// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
* Starts a resumable write. How long the write operation remains valid, and what happens when the
* write operation becomes invalid, are service-dependent.
*
* <p>Sample code:
*
* <pre>{@code
* try (StorageClient storageClient = StorageClient.create()) {
* StartResumableWriteRequest request =
* StartResumableWriteRequest.newBuilder()
* .setWriteObjectSpec(WriteObjectSpec.newBuilder().build())
* .setCommonObjectRequestParams(CommonObjectRequestParams.newBuilder().build())
* .setCommonRequestParams(CommonRequestParams.newBuilder().build())
* .build();
* ApiFuture<StartResumableWriteResponse> future =
* storageClient.startResumableWriteCallable().futureCall(request);
* // Do something.
* StartResumableWriteResponse response = future.get();
* }
* }</pre>
*/
public final UnaryCallable<StartResumableWriteRequest, StartResumableWriteResponse>
startResumableWriteCallable() {
return stub.startResumableWriteCallable();
}
// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
* Determines the `persisted_size` for an object that is being written, which can then be used as
* the `write_offset` for the next `Write()` call.
*
* <p>If the object does not exist (i.e., the object has been deleted, or the first `Write()` has
* not yet reached the service), this method returns the error `NOT_FOUND`.
*
* <p>The client **may** call `QueryWriteStatus()` at any time to determine how
* much data has been processed for this object. This is useful if the client is buffering data
* and needs to know which data can be safely evicted. For any sequence of `QueryWriteStatus()`
* calls for a given object name, the sequence of returned `persisted_size` values will be
* non-decreasing.
*
* <p>Sample code:
*
* <pre>{@code
* try (StorageClient storageClient = StorageClient.create()) {
* String uploadId = "uploadId1563990780";
* QueryWriteStatusResponse response = storageClient.queryWriteStatus(uploadId);
* }
* }</pre>
*
* @param uploadId Required. The name of the resume token for the object whose write status is
* being requested.
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
*/
public final QueryWriteStatusResponse queryWriteStatus(String uploadId) {
QueryWriteStatusRequest request =
QueryWriteStatusRequest.newBuilder().setUploadId(uploadId).build();
return queryWriteStatus(request);
}
// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
* Determines the `persisted_size` for an object that is being written, which can then be used as
* the `write_offset` for the next `Write()` call.
*
* <p>If the object does not exist (i.e., the object has been deleted, or the first `Write()` has
* not yet reached the service), this method returns the error `NOT_FOUND`.
*
* <p>The client **may** call `QueryWriteStatus()` at any time to determine how
* much data has been processed for this object. This is useful if the client is buffering data
* and needs to know which data can be safely evicted. For any sequence of `QueryWriteStatus()`
* calls for a given object name, the sequence of returned `persisted_size` values will be
* non-decreasing.
*
* <p>Sample code:
*
* <pre>{@code
* try (StorageClient storageClient = StorageClient.create()) {
* QueryWriteStatusRequest request =
* QueryWriteStatusRequest.newBuilder()
* .setUploadId("uploadId1563990780")
* .setCommonObjectRequestParams(CommonObjectRequestParams.newBuilder().build())
* .setCommonRequestParams(CommonRequestParams.newBuilder().build())
* .build();
* QueryWriteStatusResponse response = storageClient.queryWriteStatus(request);
* }
* }</pre>
*
* @param request The request object containing all of the parameters for the API call.
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
*/
public final QueryWriteStatusResponse queryWriteStatus(QueryWriteStatusRequest request) {
return queryWriteStatusCallable().call(request);
}
// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
* Determines the `persisted_size` for an object that is being written, which can then be used as
* the `write_offset` for the next `Write()` call.
*
* <p>If the object does not exist (i.e., the object has been deleted, or the first `Write()` has
* not yet reached the service), this method returns the error `NOT_FOUND`.
*
* <p>The client **may** call `QueryWriteStatus()` at any time to determine how
* much data has been processed for this object. This is useful if the client is buffering data
* and needs to know which data can be safely evicted. For any sequence of `QueryWriteStatus()`
* calls for a given object name, the sequence of returned `persisted_size` values will be
* non-decreasing.
*
* <p>Sample code:
*
* <pre>{@code
* try (StorageClient storageClient = StorageClient.create()) {
* QueryWriteStatusRequest request =
* QueryWriteStatusRequest.newBuilder()
* .setUploadId("uploadId1563990780")
* .setCommonObjectRequestParams(CommonObjectRequestParams.newBuilder().build())
* .setCommonRequestParams(CommonRequestParams.newBuilder().build())
* .build();
* ApiFuture<QueryWriteStatusResponse> future =
* storageClient.queryWriteStatusCallable().futureCall(request);
* // Do something.
* QueryWriteStatusResponse response = future.get();
* }
* }</pre>
*/
public final UnaryCallable<QueryWriteStatusRequest, QueryWriteStatusResponse>
queryWriteStatusCallable() {
return stub.queryWriteStatusCallable();
}
@Override
public final void close() {
stub.close();
}
@Override
public void shutdown() {
stub.shutdown();
}
@Override
public boolean isShutdown() {
return stub.isShutdown();
}
@Override
public boolean isTerminated() {
return stub.isTerminated();
}
@Override
public void shutdownNow() {
stub.shutdownNow();
}
@Override
public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
return stub.awaitTermination(duration, unit);
}
}