Skip to content

Instantly share code, notes, and snippets.

@backwind1233
Last active December 23, 2021 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save backwind1233/89a3becc02feb98bb2a9db3d0230ce5a to your computer and use it in GitHub Desktop.
Save backwind1233/89a3becc02feb98bb2a9db3d0230ce5a to your computer and use it in GitHub Desktop.
Gist about how to use ResourceLoader and ResourcePatternResolver
package com.azure.spring.sample.storage.resource;
import com.azure.spring.core.resource.AzureStorageBlobProtocolResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.WritableResource;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
@RestController
public class ResourceController {
@Autowired
private ResourceLoader resourceLoader;
@Autowired
private AzureStorageBlobProtocolResolver;
@PostMapping("uploadtoBlobWithResourceLoader")
public String uploadtoBlobWithResourceLoader(@RequestParam("file") MultipartFile file) throws IOException {
String name = file.getOriginalFilename();
Resource resource = resourceLoader.getResource("azure-blob://blobcontainer/" + name);
try (OutputStream os = ((WritableResource) resource).getOutputStream()) {
os.write(file.getBytes());
}
return "success";
}
@GetMapping("getResourcesWithResolver")
public String getResourcesWithResolver() throws IOException {
Resource[] resources = resolver.getResources("azure-blob://[your container]/*.txt");
System.out.println("resources = " + Arrays.toString(resources));
return "success";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment