|
| 1 | +package com.gotocompany.firehose.sink.common.blobstorage.cos; |
| 2 | + |
| 3 | +import com.gotocompany.firehose.config.CloudObjectStorageConfig; |
| 4 | +import com.gotocompany.firehose.sink.common.blobstorage.BlobStorage; |
| 5 | +import com.gotocompany.firehose.sink.common.blobstorage.BlobStorageException; |
| 6 | +import com.gotocompany.firehose.sink.common.blobstorage.cos.auth.TencentCredentialManager; |
| 7 | +import com.gotocompany.firehose.sink.common.blobstorage.cos.service.TencentObjectOperations; |
| 8 | +import com.qcloud.cos.COSClient; |
| 9 | +import com.qcloud.cos.ClientConfig; |
| 10 | +import com.qcloud.cos.exception.CosClientException; |
| 11 | +import com.qcloud.cos.exception.CosServiceException; |
| 12 | +import com.qcloud.cos.model.BucketReplicationConfiguration; |
| 13 | +import com.qcloud.cos.model.ReplicationRule; |
| 14 | +import com.qcloud.cos.region.Region; |
| 15 | +import org.slf4j.Logger; |
| 16 | +import org.slf4j.LoggerFactory; |
| 17 | + |
| 18 | +public class CloudObjectStorage implements BlobStorage { |
| 19 | + private static final Logger LOGGER = LoggerFactory.getLogger(CloudObjectStorage.class); |
| 20 | + |
| 21 | + private final TencentObjectOperations tencentObjectOperations; |
| 22 | + private final TencentCredentialManager credentialManager; |
| 23 | + private final COSClient cosClient; |
| 24 | + private final CloudObjectStorageConfig config; |
| 25 | + |
| 26 | + public CloudObjectStorage(CloudObjectStorageConfig config) { |
| 27 | + this.config = config; |
| 28 | + this.credentialManager = new TencentCredentialManager(config); |
| 29 | + ClientConfig clientConfig = createDefaultClientConfig(config); |
| 30 | + this.cosClient = new COSClient(credentialManager.getCredentials(), clientConfig); |
| 31 | + this.tencentObjectOperations = new TencentObjectOperations(cosClient, config); |
| 32 | + checkBucket(); |
| 33 | + logRetentionPolicy(); |
| 34 | + } |
| 35 | + |
| 36 | + CloudObjectStorage(CloudObjectStorageConfig config, TencentCredentialManager credentialManager, COSClient cosClient) { |
| 37 | + this.config = config; |
| 38 | + this.credentialManager = credentialManager; |
| 39 | + this.cosClient = cosClient; |
| 40 | + this.tencentObjectOperations = new TencentObjectOperations(cosClient, config); |
| 41 | + checkBucket(); |
| 42 | + logRetentionPolicy(); |
| 43 | + } |
| 44 | + |
| 45 | + private static ClientConfig createDefaultClientConfig(CloudObjectStorageConfig config) { |
| 46 | + ClientConfig clientConfig = new ClientConfig(new Region(config.getCosRegion())); |
| 47 | + clientConfig.setMaxErrorRetry(config.getCosRetryMaxAttempts()); |
| 48 | + clientConfig.setConnectionTimeout(config.getCosConnectionTimeoutMS().intValue()); |
| 49 | + clientConfig.setSocketTimeout(config.getCosSocketTimeoutMS().intValue()); |
| 50 | + return clientConfig; |
| 51 | + } |
| 52 | + |
| 53 | + void checkBucket() { |
| 54 | + String bucketName = config.getCosBucketName(); |
| 55 | + if (bucketName == null || bucketName.trim().isEmpty()) { |
| 56 | + throw new IllegalArgumentException("Bucket name cannot be null or empty"); |
| 57 | + } |
| 58 | + String region = config.getCosRegion(); |
| 59 | + if (region == null || region.trim().isEmpty()) { |
| 60 | + throw new IllegalArgumentException("Region cannot be null or empty"); |
| 61 | + } |
| 62 | + try { |
| 63 | + if (!cosClient.doesBucketExist(bucketName)) { |
| 64 | + LOGGER.error("Bucket does not exist: {}", bucketName); |
| 65 | + LOGGER.error("Please create COS bucket before running firehose: {}", bucketName); |
| 66 | + throw new IllegalArgumentException("COS Bucket not found: " + bucketName); |
| 67 | + } |
| 68 | + LOGGER.info("Successfully verified COS bucket exists: {}", bucketName); |
| 69 | + } catch (CosServiceException e) { |
| 70 | + LOGGER.error("Failed to check bucket existence: {} - {} ({})", |
| 71 | + bucketName, e.getErrorMessage(), e.getStatusCode(), e); |
| 72 | + throw new IllegalArgumentException("Failed to verify COS bucket: " + e.getMessage(), e); |
| 73 | + } catch (CosClientException e) { |
| 74 | + LOGGER.error("Client error while checking bucket: {}", bucketName, e); |
| 75 | + throw new IllegalArgumentException("Failed to verify COS bucket due to client error", e); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private void logRetentionPolicy() { |
| 80 | + String bucketName = config.getCosBucketName(); |
| 81 | + try { |
| 82 | + BucketReplicationConfiguration replication = cosClient.getBucketReplicationConfiguration(bucketName); |
| 83 | + if (replication != null && replication.getRules() != null) { |
| 84 | + LOGGER.info("Retention Policy for bucket: {}", bucketName); |
| 85 | + for (ReplicationRule rule : replication.getRules()) { |
| 86 | + LOGGER.info("Rule ID: {}, Status: {}", rule.getID(), rule.getStatus()); |
| 87 | + } |
| 88 | + } else { |
| 89 | + LOGGER.info("No retention policy configured for bucket: {}", bucketName); |
| 90 | + } |
| 91 | + } catch (CosServiceException e) { |
| 92 | + LOGGER.warn("Unable to fetch retention policy for bucket {}: {} ({})", |
| 93 | + bucketName, e.getErrorMessage(), e.getStatusCode()); |
| 94 | + } catch (CosClientException e) { |
| 95 | + LOGGER.warn("Client error while fetching retention policy for bucket {}: {}", |
| 96 | + bucketName, e.getMessage()); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public void store(String objectName, String filePath) throws BlobStorageException { |
| 101 | + LOGGER.info("Storing file to COS: {} -> {}", filePath, objectName); |
| 102 | + tencentObjectOperations.uploadObject(objectName, filePath); |
| 103 | + } |
| 104 | + |
| 105 | + public void store(String objectName, byte[] content) throws BlobStorageException { |
| 106 | + if (content == null) { |
| 107 | + throw new IllegalArgumentException("Content cannot be null"); |
| 108 | + } |
| 109 | + LOGGER.info("Storing content to COS: {} ({} bytes)", objectName, content.length); |
| 110 | + tencentObjectOperations.uploadObject(objectName, content); |
| 111 | + } |
| 112 | +} |
0 commit comments