Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure #99

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Resource> 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<Resource> 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();
}
}
6 changes: 5 additions & 1 deletion initial/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
<version>12.20.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions initial/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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