From 51df6e262c2d625e700a8194cbe0ec1e40b80843 Mon Sep 17 00:00:00 2001
From: xin <1099200748@qq.com>
Date: Tue, 03 Jun 2025 19:23:31 +0800
Subject: [PATCH] 存储桶

---
 oying-tools/src/main/java/com/oying/utils/ObsUtils.java |  127 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 127 insertions(+), 0 deletions(-)

diff --git a/oying-tools/src/main/java/com/oying/utils/ObsUtils.java b/oying-tools/src/main/java/com/oying/utils/ObsUtils.java
new file mode 100644
index 0000000..23c45fe
--- /dev/null
+++ b/oying-tools/src/main/java/com/oying/utils/ObsUtils.java
@@ -0,0 +1,127 @@
+package com.oying.utils;
+
+import com.obs.services.ObsClient;
+import com.obs.services.model.AccessControlList;
+import com.obs.services.model.ObsObject;
+import com.obs.services.model.PutObjectRequest;
+import com.obs.services.model.PutObjectResult;
+import com.oying.exception.BadRequestException;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+
+public class ObsUtils {
+
+
+
+    /**
+     * 创建ObsClient实例
+     *
+     * @param properties obs配置
+     * @return ObsClient实例
+     */
+    public static ObsClient getObsClient(ObsProperties properties) {
+        // 创建ObsClient实例
+        return new ObsClient(properties.getAccessKeyId(), properties.getAccessKeySecret(), properties.getEndpoint());
+    }
+
+    /**
+     * 上传对象权限
+     *
+     * @param properties    obs配置
+     * @param file      文件
+     * @param objectKey 对象名
+     * @param acl       对象权限
+     * @return PutObjectResult
+     */
+    public static PutObjectResult putObject(ObsProperties properties, File file, String objectKey, AccessControlList acl) {
+        try {
+            // 创建ObsClient实例
+            ObsClient obsClient = getObsClient(properties);
+            PutObjectRequest request = new PutObjectRequest();
+            request.setBucketName(properties.getBucket());
+            request.setObjectKey(objectKey);
+            request.setFile(file);
+            // 对象权限
+            request.setAcl(acl);
+            // 为待上传的本地文件路径,需要指定到具体的文件名
+            return obsClient.putObject(request);
+        } catch (Exception e) {
+            throw new BadRequestException("上传对象失败" + e.getMessage());
+        } finally {
+            FileUtil.del(file);
+        }
+    }
+
+    /**
+     * 上传对象
+     *
+     * @param properties    obs配置
+     * @param file      文件
+     * @param objectKey 对象名
+     * @return PutObjectResult
+     */
+    public static PutObjectResult putObject(ObsProperties properties, File file, String objectKey) {
+        try {
+            // 创建ObsClient实例
+            ObsClient obsClient = getObsClient(properties);
+            // 为待上传的本地文件路径,需要指定到具体的文件名
+            return obsClient.putObject(properties.getBucket(), objectKey, file);
+        } catch (Exception e) {
+            throw new BadRequestException("上传对象失败" + e.getMessage());
+        } finally {
+            FileUtil.del(file);
+        }
+    }
+
+    /**
+     * 下载对象
+     *
+     * @param properties    obs配置
+     * @param objectKey 对象名
+     * @param path      保存路径
+     */
+    public static String getObject(ObsProperties properties, String objectKey, String path) {
+        // 创建ObsClient实例
+        ObsClient obsClient = getObsClient(properties);
+        ObsObject obsObject = obsClient.getObject(properties.getBucket(), objectKey);
+        InputStream in = obsObject.getObjectContent();
+        if (in != null) {
+            FileOutputStream out = null;
+            try {
+                // getCanonicalFile 可解析正确各种路径
+                File dest = new File(path).getCanonicalFile();
+                // 检测是否存在目录
+                if (!dest.getParentFile().exists()) {
+                    dest.getParentFile().mkdirs();
+                }
+                out = new FileOutputStream(path);
+                byte[] b = new byte[1024];
+                int length;
+                while ((length = in.read(b)) > 0) {
+                    out.write(b, 0, length);
+                }
+            } catch (Exception e) {
+                throw new BadRequestException("流输出异常");
+            } finally {
+                CloseUtil.close(in);
+                CloseUtil.close(out);
+            }
+        }
+        return path;
+    }
+
+    /**
+     * 删除对象
+     *
+     * @param properties    obs配置
+     * @param objectKey 对象名
+     */
+    public static void deleteObject(ObsProperties properties, String objectKey) {
+        // 创建ObsClient实例
+        ObsClient obsClient = getObsClient(properties);
+        // 删除指定的对象
+        obsClient.deleteObject(properties.getBucket(), objectKey);
+    }
+}

--
Gitblit v1.9.3