|
20 | 20 | import com.google.cloud.secretmanager.v1.AccessSecretVersionResponse; |
21 | 21 | import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; |
22 | 22 | import com.google.cloud.secretmanager.v1.SecretVersionName; |
23 | | -import java.io.File; |
| 23 | +import com.google.cloud.storage.Blob; |
| 24 | +import com.google.cloud.storage.BlobId; |
| 25 | +import com.google.cloud.storage.Storage; |
| 26 | +import com.google.cloud.storage.StorageException; |
| 27 | +import com.google.cloud.storage.StorageOptions; |
24 | 28 | import java.io.IOException; |
25 | | -import java.nio.channels.FileChannel; |
26 | | -import java.nio.channels.ReadableByteChannel; |
27 | 29 | import java.nio.charset.StandardCharsets; |
28 | 30 | import java.nio.file.Files; |
29 | 31 | import java.nio.file.Path; |
30 | 32 | import java.nio.file.Paths; |
31 | | -import java.nio.file.StandardOpenOption; |
32 | 33 | import java.util.HashMap; |
33 | | -import java.util.HashSet; |
34 | 34 | import java.util.Map; |
35 | | -import java.util.Set; |
36 | 35 | import java.util.concurrent.ConcurrentHashMap; |
37 | 36 | import java.util.regex.Matcher; |
38 | 37 | import java.util.regex.Pattern; |
39 | | -import org.apache.beam.sdk.io.FileSystems; |
40 | 38 | import org.apache.beam.sdk.transforms.SerializableFunction; |
41 | 39 | import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions; |
42 | 40 | import org.slf4j.Logger; |
@@ -141,27 +139,33 @@ public T apply(Map<String, Object> config) { |
141 | 139 | */ |
142 | 140 | protected static synchronized String downloadGcsFile(String gcsFilePath, String outputFileString) |
143 | 141 | throws IOException { |
144 | | - // create the file only if it doesn't exist |
145 | | - if (!new File(outputFileString).exists()) { |
146 | | - Path outputFilePath = Paths.get(outputFileString); |
147 | | - Path parentDir = outputFilePath.getParent(); |
148 | | - if (parentDir != null) { |
149 | | - Files.createDirectories(parentDir); |
| 142 | + Path outputFilePath = Paths.get(outputFileString); |
| 143 | + // 1. Check if the file already exists to avoid doing duplicate work |
| 144 | + if (Files.exists(outputFilePath)) { |
| 145 | + System.out.println("File " + outputFileString + " already exists, skipping download."); |
| 146 | + return outputFileString; |
| 147 | + } |
| 148 | + if (outputFilePath.getParent() != null) { |
| 149 | + Files.createDirectories(outputFilePath.getParent()); |
| 150 | + } |
| 151 | + |
| 152 | + try { |
| 153 | + BlobId blobId = BlobId.fromGsUtilUri(gcsFilePath); |
| 154 | + Storage storage = StorageOptions.getDefaultInstance().getService(); |
| 155 | + Blob blob = storage.get(blobId); |
| 156 | + if (blob == null) { |
| 157 | + throw new IOException("File not found at GCS path: " + gcsFilePath); |
150 | 158 | } |
151 | 159 |
|
152 | | - LOG.info("Staging GCS file [{}] to [{}]", gcsFilePath, outputFileString); |
153 | | - Set<StandardOpenOption> options = new HashSet<>(2); |
154 | | - options.add(StandardOpenOption.CREATE); |
155 | | - options.add(StandardOpenOption.WRITE); |
| 160 | + // 2. Download the blob's content to the specified local file |
| 161 | + blob.downloadTo(outputFilePath); |
156 | 162 |
|
157 | | - // Copy the GCS file into a local file and will throw an I/O exception in case file not found. |
158 | | - try (ReadableByteChannel readerChannel = |
159 | | - FileSystems.open(FileSystems.matchSingleFileSpec(gcsFilePath).resourceId())) { |
160 | | - try (FileChannel writeChannel = FileChannel.open(outputFilePath, options)) { |
161 | | - writeChannel.transferFrom(readerChannel, 0, Long.MAX_VALUE); |
162 | | - } |
163 | | - } |
| 163 | + System.out.println("Downloaded " + gcsFilePath + " to " + outputFileString); |
| 164 | + } catch (StorageException e) { |
| 165 | + // Catch the StorageException and re-throw as a RuntimeException |
| 166 | + throw new RuntimeException("Failed to download file from GCS: " + e.getMessage(), e); |
164 | 167 | } |
| 168 | + |
165 | 169 | return outputFileString; |
166 | 170 | } |
167 | 171 |
|
|
0 commit comments