Skip to content

Commit caf2a84

Browse files
authored
fix outdated code (#66)
1 parent 85b031f commit caf2a84

File tree

8 files changed

+114
-131
lines changed

8 files changed

+114
-131
lines changed

asset-manager/web/pom.xml

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
<properties>
1313
<aws-sdk.version>2.25.13</aws-sdk.version>
14-
<version.spring.cloud.azure>5.19.0</version.spring.cloud.azure>
1514
</properties>
1615

1716
<artifactId>assets-manager-web</artifactId>
@@ -28,22 +27,12 @@
2827
<artifactId>spring-boot-starter-web</artifactId>
2928
</dependency>
3029
<dependency>
31-
<groupId>com.azure.spring</groupId>
32-
<artifactId>spring-cloud-azure-starter</artifactId>
33-
</dependency>
34-
<dependency>
35-
<groupId>com.azure.spring</groupId>
36-
<artifactId>spring-messaging-azure-servicebus</artifactId>
37-
</dependency>
38-
<dependency>
39-
<groupId>com.azure</groupId>
40-
<artifactId>azure-storage-blob</artifactId>
41-
<version>12.29.0</version>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-amqp</artifactId>
4232
</dependency>
4333
<dependency>
44-
<groupId>com.azure</groupId>
45-
<artifactId>azure-identity</artifactId>
46-
<version>1.14.2</version>
34+
<groupId>software.amazon.awssdk</groupId>
35+
<artifactId>s3</artifactId>
4736
</dependency>
4837
<dependency>
4938
<groupId>org.springframework.boot</groupId>
@@ -80,17 +69,10 @@
8069
<dependencyManagement>
8170
<dependencies>
8271
<dependency>
83-
<groupId>com.azure</groupId>
84-
<artifactId>azure-storage-blob-batch</artifactId>
85-
<version>12.25.0</version>
72+
<groupId>software.amazon.awssdk</groupId>
73+
<artifactId>s3</artifactId>
74+
<version>${aws-sdk.version}</version>
8675
</dependency>
87-
<dependency>
88-
<groupId>com.azure.spring</groupId>
89-
<artifactId>spring-cloud-azure-dependencies</artifactId>
90-
<version>${version.spring.cloud.azure}</version>
91-
<type>pom</type>
92-
<scope>import</scope>
93-
</dependency>
9476
</dependencies>
9577
</dependencyManagement>
9678

asset-manager/web/src/main/java/com/microsoft/migration/assets/AssetsManagerApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.microsoft.migration.assets;
22

3-
import com.azure.spring.messaging.implementation.annotation.EnableAzureMessaging;
3+
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
44
import org.springframework.boot.SpringApplication;
55
import org.springframework.boot.autoconfigure.SpringBootApplication;
66
import org.springframework.boot.context.ApplicationPidFileWriter;
77

88
@SpringBootApplication
9-
@EnableAzureMessaging
9+
@EnableRabbit
1010
public class AssetsManagerApplication {
1111
public static void main(String[] args) {
1212
SpringApplication application = new SpringApplication(AssetsManagerApplication.class);
Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
package com.microsoft.migration.assets.config;
22

3-
import com.azure.identity.DefaultAzureCredentialBuilder;
4-
import com.azure.storage.blob.BlobServiceClient;
5-
import com.azure.storage.blob.BlobServiceClientBuilder;
63
import org.springframework.beans.factory.annotation.Value;
7-
import org.springframework.context.annotation.Bean;
84
import org.springframework.context.annotation.Configuration;
95

6+
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
7+
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
8+
import software.amazon.awssdk.regions.Region;
9+
import software.amazon.awssdk.services.s3.S3Client;
10+
1011
@Configuration
1112
public class AwsS3Config {
1213

13-
@Value("${azure.storage.account-name}")
14-
private String accountName;
14+
@Value("${aws.accessKey}")
15+
private String accessKey;
16+
17+
@Value("${aws.secretKey}")
18+
private String secretKey;
19+
20+
@Value("${aws.region}")
21+
private String region;
1522

16-
@Bean
17-
public BlobServiceClient blobServiceClient() {
18-
return new BlobServiceClientBuilder()
19-
.endpoint("https://" + accountName + ".blob.core.windows.net")
20-
.credential(new DefaultAzureCredentialBuilder().build())
21-
.buildClient();
23+
public S3Client s3Client() {
24+
AwsBasicCredentials awsCredentials = AwsBasicCredentials.create(accessKey, secretKey);
25+
26+
return S3Client.builder()
27+
.region(Region.of(region))
28+
.credentialsProvider(StaticCredentialsProvider.create(awsCredentials))
29+
.build();
2230
}
2331
}

asset-manager/web/src/main/java/com/microsoft/migration/assets/config/RabbitConfig.java

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import org.springframework.context.annotation.Bean;
1212
import org.springframework.context.annotation.Configuration;
1313

14-
import java.time.Duration;
15-
1614
@Configuration
1715
public class RabbitConfig {
1816
public static final String IMAGE_PROCESSING_QUEUE = "image-processing";
@@ -24,30 +22,8 @@ public Queue imageProcessingQueue() {
2422
}
2523

2624
@Bean
27-
public QueueProperties imageProcessingQueue(ServiceBusAdministrationClient adminClient, QueueProperties retryQueue) {
28-
QueueProperties queue;
29-
try {
30-
queue = adminClient.getQueue(IMAGE_PROCESSING_QUEUE);
31-
} catch (ResourceNotFoundException e) {
32-
try {
33-
CreateQueueOptions options = new CreateQueueOptions()
34-
.setForwardDeadLetteredMessagesTo(RETRY_QUEUE);
35-
queue = adminClient.createQueue(IMAGE_PROCESSING_QUEUE, options);
36-
} catch (ResourceExistsException ex) {
37-
// Queue was created by another instance in the meantime
38-
queue = adminClient.getQueue(IMAGE_PROCESSING_QUEUE);
39-
}
40-
}
41-
42-
// Configure retry queue's DLQ forwarding now that image processing queue exists
43-
try {
44-
retryQueue.setForwardDeadLetteredMessagesTo(IMAGE_PROCESSING_QUEUE);
45-
adminClient.updateQueue(retryQueue);
46-
} catch (Exception ex) {
47-
// Ignore update errors since basic functionality will still work
48-
}
49-
50-
return queue;
25+
public MessageConverter jsonMessageConverter() {
26+
return new Jackson2JsonMessageConverter();
5127
}
5228

5329
@Bean
@@ -60,4 +36,5 @@ public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
6036
factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
6137
return factory;
6238
}
63-
}
39+
40+
}

asset-manager/web/src/main/java/com/microsoft/migration/assets/service/AwsS3Service.java

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
package com.microsoft.migration.assets.service;
22

3-
import com.azure.identity.DefaultAzureCredentialBuilder;
4-
import com.azure.storage.blob.BlobServiceClient;
5-
import com.azure.storage.blob.BlobServiceClientBuilder;
6-
import com.azure.storage.blob.models.BlobHttpHeaders;
7-
import com.azure.storage.blob.models.BlobItem;
8-
import com.azure.storage.blob.options.BlobParallelUploadOptions;
93
import com.microsoft.migration.assets.model.ImageMetadata;
104
import com.microsoft.migration.assets.model.ImageProcessingMessage;
115
import com.microsoft.migration.assets.model.S3StorageItem;
126
import com.microsoft.migration.assets.repository.ImageMetadataRepository;
137
import lombok.RequiredArgsConstructor;
14-
import com.azure.spring.messaging.servicebus.core.ServiceBusTemplate;
15-
import org.springframework.messaging.support.MessageBuilder;
8+
import org.springframework.amqp.rabbit.core.RabbitTemplate;
169
import org.springframework.beans.factory.annotation.Value;
1710
import org.springframework.context.annotation.Profile;
1811
import org.springframework.stereotype.Service;
1912
import org.springframework.web.multipart.MultipartFile;
13+
import software.amazon.awssdk.core.sync.RequestBody;
14+
import software.amazon.awssdk.services.s3.S3Client;
15+
import software.amazon.awssdk.services.s3.model.*;
2016

2117
import java.io.IOException;
2218
import java.io.InputStream;
@@ -32,31 +28,37 @@
3228
@Profile("!dev") // Active when not in dev profile
3329
public class AwsS3Service implements StorageService {
3430

35-
private final BlobServiceClient blobServiceClient;
36-
private final ServiceBusTemplate serviceBusTemplate;
31+
private final S3Client s3Client;
32+
private final RabbitTemplate rabbitTemplate;
3733
private final ImageMetadataRepository imageMetadataRepository;
3834

39-
@Value("${azure.storage.blob.container-name}")
40-
private String containerName;
35+
@Value("${aws.s3.bucket}")
36+
private String bucketName;
4137

4238
@Override
4339
public List<S3StorageItem> listObjects() {
44-
return blobServiceClient.getBlobContainerClient(containerName).listBlobs().stream()
45-
.map(blobItem -> {
40+
ListObjectsV2Request request = ListObjectsV2Request.builder()
41+
.bucket(bucketName)
42+
.build();
43+
44+
ListObjectsV2Response response = s3Client.listObjectsV2(request);
45+
46+
return response.contents().stream()
47+
.map(s3Object -> {
4648
// Try to get metadata for upload time
4749
Instant uploadedAt = imageMetadataRepository.findAll().stream()
48-
.filter(metadata -> metadata.getS3Key().equals(blobItem.getName()))
50+
.filter(metadata -> metadata.getS3Key().equals(s3Object.key()))
4951
.map(metadata -> metadata.getUploadedAt().atZone(java.time.ZoneId.systemDefault()).toInstant())
5052
.findFirst()
51-
.orElse(blobItem.getProperties().getLastModified().toInstant()); // fallback to lastModified if metadata not found
53+
.orElse(s3Object.lastModified()); // fallback to lastModified if metadata not found
5254

5355
return new S3StorageItem(
54-
blobItem.getName(),
55-
extractFilename(blobItem.getName()),
56-
blobItem.getProperties().getContentLength(),
57-
blobItem.getProperties().getLastModified().toInstant(),
56+
s3Object.key(),
57+
extractFilename(s3Object.key()),
58+
s3Object.size(),
59+
s3Object.lastModified(),
5860
uploadedAt,
59-
generateUrl(blobItem.getName())
61+
generateUrl(s3Object.key())
6062
);
6163
})
6264
.collect(Collectors.toList());
@@ -65,11 +67,13 @@ public List<S3StorageItem> listObjects() {
6567
@Override
6668
public void uploadObject(MultipartFile file) throws IOException {
6769
String key = generateKey(file.getOriginalFilename());
68-
69-
var blobClient = blobServiceClient.getBlobContainerClient(containerName).getBlobClient(key);
70-
BlobHttpHeaders headers = new BlobHttpHeaders().setContentType(file.getContentType());
71-
BlobParallelUploadOptions options = new BlobParallelUploadOptions(file.getInputStream()).setHeaders(headers);
72-
blobClient.uploadWithResponse(options, null, null);
70+
PutObjectRequest request = PutObjectRequest.builder()
71+
.bucket(bucketName)
72+
.key(key)
73+
.contentType(file.getContentType())
74+
.build();
75+
76+
s3Client.putObject(request, RequestBody.fromInputStream(file.getInputStream(), file.getSize()));
7377

7478
// Send message to queue for thumbnail generation
7579
ImageProcessingMessage message = new ImageProcessingMessage(
@@ -78,7 +82,7 @@ public void uploadObject(MultipartFile file) throws IOException {
7882
getStorageType(),
7983
file.getSize()
8084
);
81-
serviceBusTemplate.send(IMAGE_PROCESSING_QUEUE, MessageBuilder.withPayload(message).build());
85+
rabbitTemplate.convertAndSend(IMAGE_PROCESSING_QUEUE, message);
8286

8387
// Create and save metadata to database
8488
ImageMetadata metadata = new ImageMetadata();
@@ -94,23 +98,31 @@ public void uploadObject(MultipartFile file) throws IOException {
9498

9599
@Override
96100
public InputStream getObject(String key) throws IOException {
97-
return blobServiceClient.getBlobContainerClient(containerName)
98-
.getBlobClient(key)
99-
.openInputStream();
101+
GetObjectRequest request = GetObjectRequest.builder()
102+
.bucket(bucketName)
103+
.key(key)
104+
.build();
105+
106+
return s3Client.getObject(request);
100107
}
101108

102109
@Override
103110
public void deleteObject(String key) throws IOException {
104111
// Delete both original and thumbnail if it exists
105-
blobServiceClient.getBlobContainerClient(containerName)
106-
.getBlobClient(key)
107-
.delete();
112+
DeleteObjectRequest request = DeleteObjectRequest.builder()
113+
.bucket(bucketName)
114+
.key(key)
115+
.build();
116+
117+
s3Client.deleteObject(request);
108118

109119
try {
110120
// Try to delete thumbnail if it exists
111-
blobServiceClient.getBlobContainerClient(containerName)
112-
.getBlobClient(getThumbnailKey(key))
113-
.delete();
121+
DeleteObjectRequest thumbnailRequest = DeleteObjectRequest.builder()
122+
.bucket(bucketName)
123+
.key(getThumbnailKey(key))
124+
.build();
125+
s3Client.deleteObject(thumbnailRequest);
114126
} catch (Exception e) {
115127
// Ignore if thumbnail doesn't exist
116128
}
@@ -124,7 +136,7 @@ public void deleteObject(String key) throws IOException {
124136

125137
@Override
126138
public String getStorageType() {
127-
return "azure";
139+
return "s3";
128140
}
129141

130142
private String extractFilename(String key) {
@@ -134,9 +146,11 @@ private String extractFilename(String key) {
134146
}
135147

136148
private String generateUrl(String key) {
137-
return blobServiceClient.getBlobContainerClient(containerName)
138-
.getBlobClient(key)
139-
.getBlobUrl();
149+
GetUrlRequest request = GetUrlRequest.builder()
150+
.bucket(bucketName)
151+
.key(key)
152+
.build();
153+
return s3Client.utilities().getUrl(request).toString();
140154
}
141155

142156
private String generateKey(String filename) {

asset-manager/web/src/main/java/com/microsoft/migration/assets/service/BackupMessageProcessor.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package com.microsoft.migration.assets.service;
22

33
import com.microsoft.migration.assets.model.ImageProcessingMessage;
4-
import com.azure.spring.messaging.servicebus.implementation.core.annotation.ServiceBusListener;
5-
import com.azure.messaging.servicebus.ServiceBusReceivedMessageContext;
6-
import com.azure.spring.messaging.servicebus.support.ServiceBusMessageHeaders;
4+
import com.rabbitmq.client.Channel;
75
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.amqp.rabbit.annotation.RabbitListener;
7+
import org.springframework.amqp.support.AmqpHeaders;
88
import org.springframework.messaging.handler.annotation.Header;
99
import org.springframework.context.annotation.Profile;
1010
import org.springframework.stereotype.Component;
1111

1212
import static com.microsoft.migration.assets.config.RabbitConfig.IMAGE_PROCESSING_QUEUE;
1313

14+
import java.io.IOException;
1415

1516
/**
1617
* A backup message processor that serves as a monitoring and logging service.
@@ -24,28 +25,28 @@ public class BackupMessageProcessor {
2425

2526
/**
2627
* Processes image messages from a backup queue for monitoring and resilience purposes.
27-
* Uses the same Azure Service Bus API pattern as the worker module.
28+
* Uses the same RabbitMQ API pattern as the worker module.
2829
*/
29-
@ServiceBusListener(destination = IMAGE_PROCESSING_QUEUE)
30+
@RabbitListener(queues = IMAGE_PROCESSING_QUEUE)
3031
public void processBackupMessage(final ImageProcessingMessage message,
31-
@Header(ServiceBusMessageHeaders.RECEIVED_MESSAGE_CONTEXT) ServiceBusReceivedMessageContext context) {
32-
32+
Channel channel,
33+
@Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
3334
try {
3435
log.info("[BACKUP] Monitoring message: {}", message.getKey());
3536
log.info("[BACKUP] Content type: {}, Storage: {}, Size: {}",
3637
message.getContentType(), message.getStorageType(), message.getSize());
3738

3839
// Acknowledge the message
39-
context.complete();
40+
channel.basicAck(deliveryTag, false);
4041
log.info("[BACKUP] Successfully processed message: {}", message.getKey());
4142
} catch (Exception e) {
4243
log.error("[BACKUP] Failed to process message: " + message.getKey(), e);
4344

4445
try {
45-
// Reject the message and dead letter it
46-
context.deadLetter();
46+
// Reject the message and requeue it
47+
channel.basicNack(deliveryTag, false, true);
4748
log.warn("[BACKUP] Message requeued: {}", message.getKey());
48-
} catch (Exception ackEx) {
49+
} catch (IOException ackEx) {
4950
log.error("[BACKUP] Error handling RabbitMQ acknowledgment: {}", message.getKey(), ackEx);
5051
}
5152
}

0 commit comments

Comments
 (0)