shunping commented on code in PR #37502:
URL: https://github.com/apache/beam/pull/37502#discussion_r2795112055


##########
sdks/java/extensions/google-cloud-platform-core/src/main/java/org/apache/beam/sdk/extensions/gcp/util/GcsUtilV2.java:
##########
@@ -0,0 +1,329 @@
+/*
+ * 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")

Review Comment:
   I followed a previous class in GcsUtilV1.java, i.e. 
`StorageObjectOrIOException`.
   
   
https://github.com/apache/beam/blob/aaecfa8f54583ccd227189bd2e8580dcee78f502/sdks/java/extensions/google-cloud-platform-core/src/main/java/org/apache/beam/sdk/extensions/gcp/util/GcsUtil.java#L366



-- 
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]

Reply via email to