OssClientTest.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import cn.reghao.oss.api.constant.ObjectAction;
  2. import cn.reghao.oss.api.dto.ServerInfo;
  3. import cn.reghao.oss.api.dto.rest.UploadFileRet;
  4. import cn.reghao.oss.sdk.OssClient;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.nio.file.*;
  8. import java.nio.file.attribute.BasicFileAttributes;
  9. /**
  10. * @author reghao
  11. * @date 2023-06-02 16:19:04
  12. */
  13. public class OssClientTest {
  14. public void startWalk(String baseDir) throws IOException {
  15. Path path = Paths.get(baseDir);
  16. walkDir(path);
  17. }
  18. void walkDir(Path path) throws IOException {
  19. Files.walkFileTree(path, new FileVisitor<>() {
  20. @Override
  21. public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
  22. File dir1 = dir.toFile();
  23. return FileVisitResult.CONTINUE;
  24. }
  25. @Override
  26. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  27. processFile("", "", file.toFile());
  28. return FileVisitResult.CONTINUE;
  29. }
  30. @Override
  31. public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
  32. return FileVisitResult.CONTINUE;
  33. }
  34. @Override
  35. public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
  36. return FileVisitResult.CONTINUE;
  37. }
  38. });
  39. }
  40. void processFile(String uploadUrl, String uploadToken, File file) {
  41. String filePath = file.getAbsolutePath();
  42. try {
  43. String filename = file.getName();
  44. } catch (Exception e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. public static void main(String[] args) throws Exception {
  49. String endpoint = "http://localhost:8010";
  50. String ak = "PmvonhHI";
  51. String sk = "28aQEK7wJ6U5m7E1u7";
  52. OssClient ossClient = new OssClient(endpoint, ak, sk);
  53. ServerInfo serverInfo = ossClient.getServerInfo(101);
  54. String uploadUrl = serverInfo.getOssUrl();
  55. String uploadToken = serverInfo.getToken();
  56. String baseDir = "/home/reghao/Downloads/1";
  57. //client.startWalk(baseDir);
  58. /*String filePath = "/home/reghao/Downloads/abc.mp4";
  59. File file = new File(filePath);
  60. if (!file.exists()) {
  61. System.out.printf("%s not exist\n", filePath);
  62. return;
  63. }
  64. UploadFileRet uploadFileRet = ossClient.uploadFilePart(uploadUrl, uploadToken, Path.of(filePath));
  65. if (uploadFileRet != null) {
  66. System.out.printf("uploadId -> %s\n", uploadFileRet.getUploadId());
  67. }*/
  68. File file = new File("");
  69. UploadFileRet uploadFileRet = ossClient.postObject(serverInfo, file);
  70. String objectId = "653ee234e1964cfda3578efbe1e7d0b0";
  71. String signedUrl = ossClient.getSignedUrl(objectId, ObjectAction.access.getName(), null);
  72. System.out.printf("signed url -> %s\n", signedUrl);
  73. }
  74. }