|
| 1 | +package com.coreoz.plume.file.service; |
| 2 | + |
| 3 | +import com.coreoz.plume.file.configuration.FileStorageGcpConfigurationService; |
| 4 | +import com.coreoz.plume.file.services.storage.FileStorageService; |
| 5 | +import com.google.auth.Credentials; |
| 6 | +import com.google.cloud.storage.Blob; |
| 7 | +import com.google.cloud.storage.BlobId; |
| 8 | +import com.google.cloud.storage.BlobInfo; |
| 9 | +import com.google.cloud.storage.Storage; |
| 10 | +import com.google.cloud.storage.StorageException; |
| 11 | +import com.google.cloud.storage.StorageOptions; |
| 12 | +import jakarta.inject.Inject; |
| 13 | +import jakarta.inject.Singleton; |
| 14 | +import lombok.extern.slf4j.Slf4j; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.nio.channels.Channels; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Optional; |
| 21 | + |
| 22 | +@Slf4j |
| 23 | +@Singleton |
| 24 | +public class FileStorageGcpService implements FileStorageService { |
| 25 | + |
| 26 | + private final Storage storage; |
| 27 | + private final String bucketName; |
| 28 | + private final String bucketBasePath; |
| 29 | + |
| 30 | + @Inject |
| 31 | + public FileStorageGcpService( |
| 32 | + FileStorageGcpConfigurationService configurationService, |
| 33 | + Credentials credentials |
| 34 | + ) { |
| 35 | + this.storage = StorageOptions.newBuilder() |
| 36 | + .setProjectId(configurationService.gcpProjectId()) |
| 37 | + .setCredentials(credentials) |
| 38 | + .build() |
| 39 | + .getService(); |
| 40 | + this.bucketName = configurationService.gcpBucketName(); |
| 41 | + this.bucketBasePath = normalizeBasePath(configurationService.gcpBucketBasePath()); |
| 42 | + if (this.storage == null || this.bucketName == null) { |
| 43 | + throw new IllegalStateException("GCP Storage or bucket name is not configured properly"); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void add(String fileUniqueName, InputStream fileData) throws IOException { |
| 49 | + BlobInfo blobInfo = BlobInfo.newBuilder(bucketName, objectName(fileUniqueName)).build(); |
| 50 | + storage.createFrom(blobInfo, fileData); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public Optional<InputStream> fetch(String fileUniqueName) { |
| 55 | + Blob blob = storage.get(BlobId.of(bucketName, objectName(fileUniqueName))); |
| 56 | + if (blob == null) { |
| 57 | + return Optional.empty(); |
| 58 | + } |
| 59 | + return Optional.of( |
| 60 | + Channels.newInputStream(blob.reader()) |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void deleteAll(List<String> fileUniqueNames) { |
| 66 | + for (String fileUniqueName : fileUniqueNames) { |
| 67 | + BlobId blobId = BlobId.of(bucketName, objectName(fileUniqueName)); |
| 68 | + try { |
| 69 | + storage.delete(blobId); |
| 70 | + } catch (StorageException e) { |
| 71 | + logger.warn("Failed to delete file: {}", fileUniqueName, e); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Construct the object name by combining the base path and the file unique name. |
| 78 | + * |
| 79 | + * @param fileUniqueName the unique name of the file |
| 80 | + * @return the full object name |
| 81 | + */ |
| 82 | + private String objectName(String fileUniqueName) { |
| 83 | + return bucketBasePath + fileUniqueName; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Normalize the base path to ensure it ends with a slash if not empty. |
| 88 | + * |
| 89 | + * @param basePath the base path to normalize |
| 90 | + * @return the normalized base path |
| 91 | + */ |
| 92 | + private static String normalizeBasePath(String basePath) { |
| 93 | + if (basePath == null || basePath.isBlank()) { |
| 94 | + return ""; |
| 95 | + } |
| 96 | + return basePath.endsWith("/") ? basePath : basePath + "/"; |
| 97 | + } |
| 98 | +} |
0 commit comments