Last active
December 23, 2021 01:38
-
-
Save backwind1233/89a3becc02feb98bb2a9db3d0230ce5a to your computer and use it in GitHub Desktop.
Gist about how to use ResourceLoader and ResourcePatternResolver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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