shunping commented on code in PR #37502:
URL: https://github.com/apache/beam/pull/37502#discussion_r2784630677
##########
sdks/java/extensions/google-cloud-platform-core/src/main/java/org/apache/beam/sdk/extensions/gcp/util/GcsUtil.java:
##########
@@ -146,36 +188,86 @@ Integer getUploadBufferSizeBytes() {
}
public long fileSize(GcsPath path) throws IOException {
+ if (delegateV2 != null) {
+ return delegateV2.fileSize(path);
+ }
return delegate.fileSize(path);
}
+ /** @deprecated use {@link #getBlob(GcsPath)}. */
+ @Deprecated
public StorageObject getObject(GcsPath gcsPath) throws IOException {
return delegate.getObject(gcsPath);
}
+ /** @deprecated use {@link #getBlob(GcsPath, BlobGetOption...)}. */
+ @Deprecated
@VisibleForTesting
StorageObject getObject(GcsPath gcsPath, BackOff backoff, Sleeper sleeper)
throws IOException {
return delegate.getObject(gcsPath, backoff, sleeper);
}
+ public Blob getBlob(GcsPath gcsPath, BlobGetOption... options) throws
IOException {
+ if (delegateV2 != null) {
+ return delegateV2.getBlob(gcsPath, options);
+ }
+ throw new IOException("GcsUtil2 not initialized.");
Review Comment:
Fixed this and other places.
##########
sdks/java/extensions/google-cloud-platform-core/src/main/java/org/apache/beam/sdk/extensions/gcp/util/GcsUtilV2.java:
##########
@@ -0,0 +1,330 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.extensions.gcp.util;
+
+import static org.apache.beam.sdk.io.FileSystemUtils.wildcardToRegexp;
+import static
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkNotNull;
+
+import com.google.api.gax.paging.Page;
+import com.google.auto.value.AutoValue;
+import com.google.cloud.storage.Blob;
+import com.google.cloud.storage.Bucket;
+import com.google.cloud.storage.BucketInfo;
+import com.google.cloud.storage.Storage;
+import com.google.cloud.storage.Storage.BlobField;
+import com.google.cloud.storage.Storage.BlobGetOption;
+import com.google.cloud.storage.Storage.BlobListOption;
+import com.google.cloud.storage.Storage.BucketField;
+import com.google.cloud.storage.Storage.BucketGetOption;
+import com.google.cloud.storage.StorageBatch;
+import com.google.cloud.storage.StorageBatchResult;
+import com.google.cloud.storage.StorageException;
+import com.google.cloud.storage.StorageOptions;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.nio.file.AccessDeniedException;
+import java.nio.file.FileAlreadyExistsException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Pattern;
+import org.apache.beam.sdk.extensions.gcp.options.GcpOptions;
+import org.apache.beam.sdk.extensions.gcp.util.gcsfs.GcsPath;
+import org.apache.beam.sdk.options.DefaultValueFactory;
+import org.apache.beam.sdk.options.PipelineOptions;
+import
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.annotations.VisibleForTesting;
+import
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList;
+import
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Lists;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+class GcsUtilV2 {
+ private static final org.slf4j.Logger LOG =
org.slf4j.LoggerFactory.getLogger(GcsUtilV2.class);
+
+ public static class GcsUtilFactory implements DefaultValueFactory<GcsUtilV2>
{
+ @Override
+ public GcsUtilV2 create(PipelineOptions options) {
+ // GcsOptions gcsOptions = options.as(GcsOptions.class);
+ // Storage.Builder storageBuilder =
Transport.newStorageClient(gcsOptions);
+ return new GcsUtilV2(options);
+ }
+ }
+
+ private Storage storage;
+
+ /** Maximum number of items to retrieve per Objects.List request. */
+ private static final long MAX_LIST_BLOBS_PER_CALL = 1024;
+
+ /** Maximum number of requests permitted in a GCS batch request. */
+ private static final int MAX_REQUESTS_PER_BATCH = 100;
+
+ GcsUtilV2(PipelineOptions options) {
+ String projectId = options.as(GcpOptions.class).getProject();
+ storage =
StorageOptions.newBuilder().setProjectId(projectId).build().getService();
+ }
+
+ @SuppressWarnings({
+ "nullness" // For Creating AccessDeniedException and
FileAlreadyExistsException with null.
+ })
+ private IOException translateStorageException(GcsPath gcsPath,
StorageException e) {
+ switch (e.getCode()) {
+ case 403:
+ return new AccessDeniedException(gcsPath.toString(), null,
e.getMessage());
+ case 409:
+ return new FileAlreadyExistsException(gcsPath.toString(), null,
e.getMessage());
+ default:
+ return new IOException(e);
+ }
+ }
+
+ private IOException translateStorageException(
+ String bucketName, @Nullable String blobName, StorageException e) {
+ return translateStorageException(GcsPath.fromComponents(bucketName,
blobName), e);
+ }
+
+ public Blob getBlob(GcsPath gcsPath, BlobGetOption... options) throws
IOException {
+ try {
+ Blob blob = storage.get(gcsPath.getBucket(), gcsPath.getObject(),
options);
+ if (blob == null) {
+ throw new FileNotFoundException(
+ String.format("The specified file does not exist: %s",
gcsPath.toString()));
+ }
+ return blob;
+ } catch (StorageException e) {
+ throw translateStorageException(gcsPath, e);
+ }
+ }
+
+ public long fileSize(GcsPath gcsPath) throws IOException {
+ return getBlob(gcsPath, BlobGetOption.fields(BlobField.SIZE)).getSize();
+ }
+
+ /** A class that holds either a {@link Blob} or an {@link IOException}. */
+ // It is clear from the name that this class holds either Blob or
IOException.
+ @SuppressFBWarnings("NM_CLASS_NOT_EXCEPTION")
+ @AutoValue
+ public abstract static class BlobOrIOException {
+
+ /** Returns the {@link Blob}. */
+ public abstract @Nullable Blob blob();
+
+ /** Returns the {@link IOException}. */
+ public abstract @Nullable IOException ioException();
+
+ @VisibleForTesting
+ public static BlobOrIOException create(Blob blob) {
+ return new AutoValue_GcsUtilV2_BlobOrIOException(
+ checkNotNull(blob, "blob"), null /* ioException */);
+ }
+
+ @VisibleForTesting
+ public static BlobOrIOException create(IOException ioException) {
+ return new AutoValue_GcsUtilV2_BlobOrIOException(
+ null /* blob */, checkNotNull(ioException, "ioException"));
+ }
+ }
+
+ public List<BlobOrIOException> getBlobs(List<GcsPath> gcsPaths,
BlobGetOption... options)
+ throws IOException {
+
+ List<BlobOrIOException> results = new ArrayList<>();
+
+ for (List<GcsPath> pathPartition :
+ Lists.partition(Lists.newArrayList(gcsPaths), MAX_REQUESTS_PER_BATCH))
{
+
+ // Create a new empty batch every time
+ StorageBatch batch = storage.batch();
+ List<StorageBatchResult<Blob>> batchResultFutures = new ArrayList<>();
+
+ for (GcsPath path : pathPartition) {
+ batchResultFutures.add(batch.get(path.getBucket(), path.getObject(),
options));
+ }
+ batch.submit();
+
+ for (int i = 0; i < batchResultFutures.size(); i++) {
+ StorageBatchResult<Blob> future = batchResultFutures.get(i);
+ try {
+ Blob blob = future.get();
+ if (blob != null) {
+ results.add(BlobOrIOException.create(blob));
+ } else {
+ results.add(
+ BlobOrIOException.create(
+ new FileNotFoundException(
+ String.format(
+ "The specified file does not exist: %s",
gcsPaths.get(i).toString()))));
+ }
+ } catch (StorageException e) {
+
results.add(BlobOrIOException.create(translateStorageException(gcsPaths.get(i),
e)));
+ }
+ }
Review Comment:
Fixed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]