From 9a1b6d284a4ec715ff181f8a1544fde8dcb2d04d Mon Sep 17 00:00:00 2001
From: tejesavi <125518577+tejesavi@users.noreply.github.com>
Date: Sun, 13 Oct 2024 10:01:19 -0400
Subject: [PATCH 1/4] Update pom.xml
---
initial/pom.xml | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/initial/pom.xml b/initial/pom.xml
index bcebf09..76df415 100644
--- a/initial/pom.xml
+++ b/initial/pom.xml
@@ -27,7 +27,11 @@
org.springframework.boot
spring-boot-starter-web
-
+
+ com.azure
+ azure-storage-blob
+ 12.20.0
+
org.springframework.boot
spring-boot-starter-test
From 591ac54d61a70876d07a93c34025464c8504e11f Mon Sep 17 00:00:00 2001
From: tejesavi <125518577+tejesavi@users.noreply.github.com>
Date: Sun, 13 Oct 2024 10:03:06 -0400
Subject: [PATCH 2/4] Update application.properties
---
initial/src/main/resources/application.properties | 2 ++
1 file changed, 2 insertions(+)
diff --git a/initial/src/main/resources/application.properties b/initial/src/main/resources/application.properties
index e69de29..a95c5e9 100644
--- a/initial/src/main/resources/application.properties
+++ b/initial/src/main/resources/application.properties
@@ -0,0 +1,2 @@
+azure.storage.connection-string=DefaultEndpointsProtocol=https;AccountName=demotejstorage;AccountKey=FAkUyhOdvDyUksGkQ6E++ixBJdYsu4Grel0q3/CfOrL0uPqfAFh7StSHrJjYump74KN4bZ7KvbrA+AStnfuF1Q==;EndpointSuffix=core.windows.net
+azure.storage.blob-container=uploads
From 51002b279bdb6ca4c2ec925c92ccbceab5015ad3 Mon Sep 17 00:00:00 2001
From: tejesavi <125518577+tejesavi@users.noreply.github.com>
Date: Sun, 13 Oct 2024 10:09:10 -0400
Subject: [PATCH 3/4] Create AzureBlobStorageService.java
---
.../AzureBlobStorageService.java | 26 +++++++++++++++++++
1 file changed, 26 insertions(+)
create mode 100644 initial/src/main/java/com/example/uploadingfiles/AzureBlobStorageService.java
diff --git a/initial/src/main/java/com/example/uploadingfiles/AzureBlobStorageService.java b/initial/src/main/java/com/example/uploadingfiles/AzureBlobStorageService.java
new file mode 100644
index 0000000..ed42d3c
--- /dev/null
+++ b/initial/src/main/java/com/example/uploadingfiles/AzureBlobStorageService.java
@@ -0,0 +1,26 @@
+package com.example.uploadingfiles; // Ensure the package matches
+
+import com.azure.storage.blob.BlobClientBuilder;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+@Service
+public class AzureBlobStorageService {
+
+ @Value("${azure.storage.connection-string}")
+ private String connectionString;
+
+ @Value("${azure.storage.blob-container}")
+ private String containerName;
+
+ public void uploadFile(MultipartFile file) {
+ var blobClient = new BlobClientBuilder()
+ .connectionString(connectionString)
+ .containerName(containerName)
+ .blobName(file.getOriginalFilename())
+ .buildClient();
+
+ blobClient.upload(file.getInputStream(), file.getSize(), true);
+ }
+}
From 1d367727dd9ffbb5bc346da6821b385d229f5230 Mon Sep 17 00:00:00 2001
From: tejesavi <125518577+tejesavi@users.noreply.github.com>
Date: Sun, 13 Oct 2024 10:20:49 -0400
Subject: [PATCH 4/4] Update FileUploadController.java
---
.../uploadingfiles/FileUploadController.java | 99 ++++++++++---------
1 file changed, 50 insertions(+), 49 deletions(-)
diff --git a/complete/src/main/java/com/example/uploadingfiles/FileUploadController.java b/complete/src/main/java/com/example/uploadingfiles/FileUploadController.java
index a529b69..7ac4eca 100644
--- a/complete/src/main/java/com/example/uploadingfiles/FileUploadController.java
+++ b/complete/src/main/java/com/example/uploadingfiles/FileUploadController.java
@@ -3,6 +3,8 @@
import java.io.IOException;
import java.util.stream.Collectors;
+import com.example.upload.AzureBlobStorageService; // Ensure this path is correct
+
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
@@ -15,61 +17,60 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
-import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.example.uploadingfiles.storage.StorageFileNotFoundException;
-import com.example.uploadingfiles.storage.StorageService;
@Controller
public class FileUploadController {
- private final StorageService storageService;
-
- @Autowired
- public FileUploadController(StorageService storageService) {
- this.storageService = storageService;
- }
-
- @GetMapping("/")
- public String listUploadedFiles(Model model) throws IOException {
-
- model.addAttribute("files", storageService.loadAll().map(
- path -> MvcUriComponentsBuilder.fromMethodName(FileUploadController.class,
- "serveFile", path.getFileName().toString()).build().toUri().toString())
- .collect(Collectors.toList()));
-
- return "uploadForm";
- }
-
- @GetMapping("/files/{filename:.+}")
- @ResponseBody
- public ResponseEntity serveFile(@PathVariable String filename) {
-
- Resource file = storageService.loadAsResource(filename);
-
- if (file == null)
- return ResponseEntity.notFound().build();
-
- return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
- "attachment; filename=\"" + file.getFilename() + "\"").body(file);
- }
-
- @PostMapping("/")
- public String handleFileUpload(@RequestParam("file") MultipartFile file,
- RedirectAttributes redirectAttributes) {
-
- storageService.store(file);
- redirectAttributes.addFlashAttribute("message",
- "You successfully uploaded " + file.getOriginalFilename() + "!");
-
- return "redirect:/";
- }
-
- @ExceptionHandler(StorageFileNotFoundException.class)
- public ResponseEntity> handleStorageFileNotFound(StorageFileNotFoundException exc) {
- return ResponseEntity.notFound().build();
- }
-
+ // Removed StorageService and added AzureBlobStorageService
+ private final AzureBlobStorageService azureBlobStorageService;
+
+ @Autowired
+ public FileUploadController(AzureBlobStorageService azureBlobStorageService) {
+ this.azureBlobStorageService = azureBlobStorageService;
+ }
+
+ @GetMapping("/")
+ public String listUploadedFiles(Model model) throws IOException {
+ // Note: You may want to modify this method if you want to list files from Azure Blob Storage
+ // For now, we can keep it unchanged, but remember Azure Blob Storage requires different handling.
+ model.addAttribute("files",
+ azureBlobStorageService.getAllBlobs().stream() // Implement this method in your service
+ .map(blobName -> MvcUriComponentsBuilder.fromMethodName(FileUploadController.class,
+ "serveFile", blobName).build().toUri().toString())
+ .collect(Collectors.toList()));
+
+ return "uploadForm";
+ }
+
+ @GetMapping("/files/{filename:.+}")
+ @ResponseBody
+ public ResponseEntity serveFile(@PathVariable String filename) {
+ Resource file = azureBlobStorageService.loadAsResource(filename); // Implement this method in your service
+
+ if (file == null)
+ return ResponseEntity.notFound().build();
+
+ return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
+ "attachment; filename=\"" + file.getFilename() + "\"").body(file);
+ }
+
+ @PostMapping("/")
+ public String handleFileUpload(@RequestParam("file") MultipartFile file,
+ RedirectAttributes redirectAttributes) {
+
+ azureBlobStorageService.uploadFile(file); // Use Azure service for upload
+ redirectAttributes.addFlashAttribute("message",
+ "You successfully uploaded " + file.getOriginalFilename() + "!");
+
+ return "redirect:/";
+ }
+
+ @ExceptionHandler(StorageFileNotFoundException.class)
+ public ResponseEntity> handleStorageFileNotFound(StorageFileNotFoundException exc) {
+ return ResponseEntity.notFound().build();
+ }
}