shoothzj commented on issue #4756: URL: https://github.com/apache/opendal/issues/4756#issuecomment-2275280567
@Xuanwo I put some sample codes in https://gitcode.com/shoothzj/opendal-demo/overview. And here is some docs, would you please take a look? ## Usage ### Configuration First, configure the `application.yml` file with the necessary OpenDAL settings: ```yaml spring: opendal: schema: "fs" conf: root: "/tmp" ``` ### Code Integration #### Features - **OpenDAL/OpenDALReactive Bean:** Easily operate on data using OpenDAL/OpenDALReactive. - **Auto Serialize/Deserialize Data:** Leverage Jackson for automatic serialization and deserialization. I plan to implement **Auto Serialize/Deserialize Data** in the next. #### Implementation ##### Spring WebMVC To use OpenDAL with Spring WebMVC: 1. Configure the `application.yml` file as shown above. 2. Autowire the `OpenDAL` bean in your controller. ```java import org.apache.opendal.Entry; import org.apache.opendal.Metadata; import org.apache.opendal.PresignedRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.time.Duration; import java.util.List; @RestController @RequestMapping("/api/v1/opendal") public class OpenDALController { @Autowired private OpenDAL openDAL; @PostMapping("/write") public ResponseEntity<Void> write(@RequestParam String path, @RequestBody String content) { openDAL.write(path, content); return ResponseEntity.ok().build(); } @PostMapping("/writeBytes") public ResponseEntity<Void> writeBytes(@RequestParam String path, @RequestBody byte[] content) { openDAL.write(path, content); return ResponseEntity.ok().build(); } @PostMapping("/append") public ResponseEntity<Void> append(@RequestParam String path, @RequestBody String content) { openDAL.append(path, content); return ResponseEntity.ok().build(); } @PostMapping("/appendBytes") public ResponseEntity<Void> appendBytes(@RequestParam String path, @RequestBody byte[] content) { openDAL.append(path, content); return ResponseEntity.ok().build(); } @GetMapping("/stat") public ResponseEntity<Metadata> stat(@RequestParam String path) { Metadata metadata = openDAL.stat(path); return ResponseEntity.ok(metadata); } @GetMapping("/read") public ResponseEntity<byte[]> read(@RequestParam String path) { byte[] content = openDAL.read(path); return ResponseEntity.ok(content); } @GetMapping("/presignRead") public ResponseEntity<PresignedRequest> presignRead(@RequestParam String path, @RequestParam long duration) { PresignedRequest request = openDAL.presignRead(path, Duration.ofSeconds(duration)); return ResponseEntity.ok(request); } @GetMapping("/presignWrite") public ResponseEntity<PresignedRequest> presignWrite(@RequestParam String path, @RequestParam long duration) { PresignedRequest request = openDAL.presignWrite(path, Duration.ofSeconds(duration)); return ResponseEntity.ok(request); } @GetMapping("/presignStat") public ResponseEntity<PresignedRequest> presignStat(@RequestParam String path, @RequestParam long duration) { PresignedRequest request = openDAL.presignStat(path, Duration.ofSeconds(duration)); return ResponseEntity.ok(request); } @DeleteMapping("/delete") public ResponseEntity<Void> delete(@RequestParam String path) { openDAL.delete(path); return ResponseEntity.ok().build(); } @PostMapping("/createDir") public ResponseEntity<Void> createDir(@RequestParam String path) { openDAL.createDir(path); return ResponseEntity.ok().build(); } @PostMapping("/copy") public ResponseEntity<Void> copy(@RequestParam String sourcePath, @RequestParam String targetPath) { openDAL.copy(sourcePath, targetPath); return ResponseEntity.ok().build(); } @PostMapping("/rename") public ResponseEntity<Void> rename(@RequestParam String sourcePath, @RequestParam String targetPath) { openDAL.rename(sourcePath, targetPath); return ResponseEntity.ok().build(); } @DeleteMapping("/removeAll") public ResponseEntity<Void> removeAll(@RequestParam String path) { openDAL.removeAll(path); return ResponseEntity.ok().build(); } @GetMapping("/list") public ResponseEntity<List<Entry>> list(@RequestParam String path) { List<Entry> entries = openDAL.list(path); return ResponseEntity.ok(entries); } } ``` ##### Spring WebFlux To use OpenDALReactive with Spring WebFlux: 1. Configure the `application.yml` file as shown above. 2. Autowire the `OpenDALReactive` bean in your controller. ```java import com.shoothzj.opendal.spring.OpenDALReactive; import org.apache.opendal.Entry; import org.apache.opendal.Metadata; import org.apache.opendal.PresignedRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.time.Duration; @RestController @RequestMapping("/api/v1/opendal/reactive") public class OpenDALReactiveController { @Autowired private OpenDALReactive<byte[]> openDALReactive; @PostMapping("/write") public Mono<ResponseEntity<Void>> write(@RequestParam("path") String path, @RequestBody String content) { return openDALReactive.write(path, content).then(Mono.just(ResponseEntity.ok().<Void>build())); } @PostMapping("/writeBytes") public Mono<ResponseEntity<Void>> writeBytes(@RequestParam("path") String path, @RequestBody byte[] content) { return openDALReactive.write(path, content).then(Mono.just(ResponseEntity.ok().<Void>build())); } @PostMapping("/append") public Mono<ResponseEntity<Void>> append(@RequestParam("path") String path, @RequestBody String content) { return openDALReactive.append(path, content).then(Mono.just(ResponseEntity.ok().<Void>build())); } @PostMapping("/appendBytes") public Mono<ResponseEntity<Void>> appendBytes(@RequestParam("path") String path, @RequestBody byte[] content) { return openDALReactive.append(path, content).then(Mono.just(ResponseEntity.ok().<Void>build())); } @GetMapping("/stat") public Mono<ResponseEntity<Metadata>> stat(@RequestParam("path") String path) { return openDALReactive.stat(path).map(ResponseEntity::ok); } @GetMapping("/read") public Mono<ResponseEntity<byte[]>> read(@RequestParam("path") String path) { return openDALReactive.read(path).map(ResponseEntity::ok); } @GetMapping("/presignRead") public Mono<ResponseEntity<PresignedRequest>> presignRead(@RequestParam("path") String path, @RequestParam("duration") long duration) { return openDALReactive.presignRead(path, Duration.ofSeconds(duration)).map(ResponseEntity::ok); } @GetMapping("/presignWrite") public Mono<ResponseEntity<PresignedRequest>> presignWrite(@RequestParam("path") String path, @RequestParam("duration") long duration) { return openDALReactive.presignWrite(path, Duration.ofSeconds(duration)).map(ResponseEntity::ok); } @GetMapping("/presignStat") public Mono<ResponseEntity<PresignedRequest>> presignStat(@RequestParam("path") String path, @RequestParam("duration") long duration) { return openDALReactive.presignStat(path, Duration.ofSeconds(duration)).map(ResponseEntity::ok); } @DeleteMapping("/delete") public Mono<ResponseEntity<Void>> delete(@RequestParam("path") String path) { return openDALReactive.delete(path).then(Mono.just(ResponseEntity.ok().<Void>build())); } @PostMapping("/createDir") public Mono<ResponseEntity<Void>> createDir(@RequestParam("path") String path) { return openDALReactive.createDir(path).then(Mono.just(ResponseEntity.ok().<Void>build())); } @PostMapping("/copy") public Mono<ResponseEntity<Void>> copy(@RequestParam("sourcePath") String sourcePath, @RequestParam("targetPath") String targetPath) { return openDALReactive.copy(sourcePath, targetPath).then(Mono.just(ResponseEntity.ok().<Void>build())); } @PostMapping("/rename") public Mono<ResponseEntity<Void>> rename(@RequestParam("sourcePath") String sourcePath, @RequestParam("targetPath") String targetPath) { return openDALReactive.rename(sourcePath, targetPath).then(Mono.just(ResponseEntity.ok().<Void>build())); } @DeleteMapping("/removeAll") public Mono<ResponseEntity<Void>> removeAll(@RequestParam("path") String path) { return openDALReactive.removeAll(path).then(Mono.just(ResponseEntity.ok().<Void>build())); } @GetMapping("/list") public Flux<Entry> list(@RequestParam("path") String path) { return openDALReactive.list(path); } } ``` ### Testing with `curl` Use the following `curl` commands to test the endpoints of your Spring WebMVC and WebFlux controllers: 1. **Write String Content** ```sh curl -X POST "http://localhost:8080/api/v1/opendal/write?path=/example/path" -H "Content-Type: application/json" -d "This is a test content" ``` 2. **Write Byte Content** ```sh curl -X POST "http://localhost:8080/api/v1/opendal/writeBytes?path=/example/path 3. **Append String Content** ```sh curl -X POST "http://localhost:8080/api/v1/opendal/append?path=/example/path" -H "Content-Type: application/json" -d "This is additional content" ``` 4. **Append Byte Content** ```sh curl -X POST "http://localhost:8080/api/v1/opendal/appendBytes?path=/example/path" -H "Content-Type: application/octet-stream" --data-binary @/path/to/your/file ``` 5. **Stat** ```sh curl -X GET "http://localhost:8080/api/v1/opendal/stat?path=/example/path" ``` 6. **Read** ```sh curl -X GET "http://localhost:8080/api/v1/opendal/read?path=/example/path" ``` 7. **Presign Read** ```sh curl -X GET "http://localhost:8080/api/v1/opendal/presignRead?path=/example/path&duration=3600" ``` 8. **Presign Write** ```sh curl -X GET "http://localhost:8080/api/v1/opendal/presignWrite?path=/example/path&duration=3600" ``` 9. **Presign Stat** ```sh curl -X GET "http://localhost:8080/api/v1/opendal/presignStat?path=/example/path&duration=3600" ``` 10. **Delete** ```sh curl -X DELETE "http://localhost:8080/api/v1/opendal/delete?path=/example/path" ``` 11. **Create Directory** ```sh curl -X POST "http://localhost:8080/api/v1/opendal/createDir?path=/example/path" ``` 12. **Copy** ```sh curl -X POST "http://localhost:8080/api/v1/opendal/copy?sourcePath=/example/source&targetPath=/example/target" ``` 13. **Rename** ```sh curl -X POST "http://localhost:8080/api/v1/opendal/rename?sourcePath=/example/source&targetPath=/example/target" ``` 14. **Remove All** ```sh curl -X DELETE "http://localhost:8080/api/v1/opendal/removeAll?path=/example/path" ``` 15. **List** ```sh curl -X GET "http://localhost:8080/api/v1/opendal/list?path=/example/path" ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
