From c0e9ac907ee658802f68eb4e770942e34e87cfd0 Mon Sep 17 00:00:00 2001 From: liujie <1793218484@qq.com> Date: 星期五, 25 七月 2025 16:43:50 +0800 Subject: [PATCH] Merge branches 'master' and 'master' of http://120.76.84.145:10101/gitblit/r/java/QianYunTong --- DriverQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/qianyuntong/NCOSSUtil.java | 483 +++++++++++++++++++++++++++-------------------------- 1 files changed, 243 insertions(+), 240 deletions(-) diff --git a/DriverQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/qianyuntong/NCOSSUtil.java b/DriverQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/qianyuntong/NCOSSUtil.java index 0d30226..e65e84f 100644 --- a/DriverQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/qianyuntong/NCOSSUtil.java +++ b/DriverQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/qianyuntong/NCOSSUtil.java @@ -1,240 +1,243 @@ -//package com.stylefeng.guns.modular.system.util.qianyuntong; -// -//import com.heredata.hos.HOS; -//import com.heredata.hos.HOSClientBuilder; -//import com.heredata.hos.model.CreateBucketRequest; -//import com.heredata.hos.model.HOSObject; -//import com.heredata.hos.model.PutObjectRequest; -//import com.heredata.hos.model.PutObjectResult; -//import com.heredata.hos.model.bucket.Bucket; -//import com.heredata.model.VoidResult; -//import lombok.extern.slf4j.Slf4j; -// -//import java.io.InputStream; -// -///** -// * OSS 工具类 -// * @author zhibing.pu -// * @Date 2025/6/23 17:50 -// */ -//@Slf4j -//public class NCOSSUtil { -// -// /** -// * 创建桶 -// * @param bucketName -// * @return -// */ -// public static Boolean createBucket(String bucketName) { -// /** -// * endPoint:HOS的基础路径(公共前缀) -// * account:账户的ID -// * accessKey:向UAAS服务请求到的access_key -// * secretKey:向UAAS服务请求到的secret_key -// */ -// HOS hos = new HOSClientBuilder().build(QianYunTongProperties.endPoint, QianYunTongProperties.account, QianYunTongProperties.accessKey, QianYunTongProperties.secretKey); -// try { -// // 创建请求对象,并且设置创建桶名为"example"的桶 -// CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName); -// VoidResult result = hos.createBucket(createBucketRequest); -// if (result.getResponse().isSuccessful()) { -// log.info("创建桶成功:" + bucketName); -// return true; -// } -// } catch (Exception e){ -// log.info("创建桶失败:" + bucketName); -// e.printStackTrace(); -// } -// return false; -// } -// -// -// /** -// * 查询桶详情 -// * @param bucketName -// * @return -// */ -// public static Bucket getBucketInfo(String bucketName) { -// /** -// * endPoint:HOS的基础路径(公共前缀) -// * account:账户的ID -// * accessKey:向UAAS服务请求到的access_key -// * secretKey:向UAAS服务请求到的secret_key -// */ -// HOS hos = new HOSClientBuilder().build(QianYunTongProperties.endPoint, QianYunTongProperties.account, QianYunTongProperties.accessKey, QianYunTongProperties.secretKey); -// try { -// // 查询桶名为"example"的详情 -// Bucket bucket = hos.getBucketInfo(bucketName); -// return bucket; -// } catch (Exception e){ -// e.printStackTrace(); -// } -// return null; -// } -// -// -// /** -// * 删除桶 -// * @param bucketName -// * @return -// */ -// public static Boolean deleteBucket(String bucketName) { -// /** -// * endPoint:HOS的基础路径(公共前缀) -// * account:账户的ID -// * accessKey:向UAAS服务请求到的access_key -// * secretKey:向UAAS服务请求到的secret_key -// */ -// HOS hos = new HOSClientBuilder().build(QianYunTongProperties.endPoint, QianYunTongProperties.account, QianYunTongProperties.accessKey, QianYunTongProperties.secretKey); -// try { -// VoidResult result = hos.deleteBucket(bucketName); -// if (result.getResponse().isSuccessful()) { -// log.info("删除桶成功:" + bucketName); -// return true; -// } -// } catch (Exception e){ -// log.info("删除桶成功:" + bucketName); -// e.printStackTrace(); -// } -// return false; -// } -// -// -// /** -// * 设置桶策略 -// * @param bucketName 桶名 -// * @param policyText 策略规则字符串 -// * { -// * // 策略配置的版本号,由用户定义 -// * "Version": "2012-10-17", -// * // 策略规则配置,可指定多条策略规则 -// * "Statement": [{ -// * // 策略规则ID,可用户指定,如不指定则服务自动生成一条 -// * "Sid": "sid", -// * // 策略规则指定的HOS API接口标识,可参照下表填写 -// * "Action": [ -// * "action" -// * ], -// * // 策略规则效果,具体表现为允许或拒绝,有效值为Allow(允许),Deny(拒绝); -// * "Effect": "Allow", -// * // 策略规则指定的资源参照下面resource配置 -// * "Resource": "resource", -// * // 策略规则指定的授权人 参照下面Principal配置 -// * "Principal": { -// * "HWS": [ -// * "account_id:root" -// * ] -// * } -// * } -// * ] -// * } -// * @return -// */ -// public static Boolean setBucketPolicy(String bucketName, String policyText) { -// /** -// * endPoint:HOS的基础路径(公共前缀) -// * account:账户的ID -// * accessKey:向UAAS服务请求到的access_key -// * secretKey:向UAAS服务请求到的secret_key -// */ -// HOS hos = new HOSClientBuilder().build(QianYunTongProperties.endPoint, QianYunTongProperties.account, QianYunTongProperties.accessKey, QianYunTongProperties.secretKey); -// // 桶策略字符串 -// try { -// VoidResult result = hos.setBucketPolicy(bucketName, policyText); -// if (result.getResponse().isSuccessful()) { -// log.info("设置桶策略成功:" + bucketName); -// return true; -// } -// } catch (Exception e) { -// log.info("设置桶策略失败:" + bucketName); -// e.printStackTrace(); -// } -// return false; -// } -// -// -// /** -// * 上传对象 -// * @param bucketName 桶名称 -// * @param key 存储文件名 -// * @param inputStream 上传文件流 -// * @return 对象唯一标识 -// */ -// public static String putObject(String bucketName, String key, InputStream inputStream) { -// /** -// * endPoint:HOS的基础路径(公共前缀) -// * account:账户的ID -// * accessKey:向UAAS服务请求到的access_key -// * secretKey:向UAAS服务请求到的secret_key -// */ -// HOS hos = new HOSClientBuilder().build(QianYunTongProperties.endPoint, QianYunTongProperties.account, QianYunTongProperties.accessKey, QianYunTongProperties.secretKey); -// try { -// PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key , inputStream); -// PutObjectResult example = hos.putObject(putObjectRequest); -// if (example.getResponse().isSuccessful()) { -// log.info("上传对象成功:" + bucketName + "--->" + key); -// return example.getETag(); -// } -// } catch (Exception e) { -// log.info("上传对象成功:" + bucketName); -// e.printStackTrace(); -// } -// return null; -// } -// -// -// /** -// * 查询对象 -// * @param bucketName 桶名称 -// * @param key 存储文件名 -// * @return -// */ -// public static HOSObject getObject(String bucketName, String key) { -// /** -// * endPoint:HOS的基础路径(公共前缀) -// * account:账户的ID -// * accessKey:向UAAS服务请求到的access_key -// * secretKey:向UAAS服务请求到的secret_key -// */ -// HOS hos = new HOSClientBuilder().build(QianYunTongProperties.endPoint, QianYunTongProperties.account, QianYunTongProperties.accessKey, QianYunTongProperties.secretKey); -// try { -// HOSObject example = hos.getObject(bucketName, key); -// if (example.getResponse().isSuccessful()) { -// log.info("查询对象成功:" + bucketName + "--->" + key); -// return example; -// } -// } catch (Exception e) { -// log.info("查询对象失败:" + bucketName); -// e.printStackTrace(); -// } -// return null; -// } -// -// -// /** -// * 删除对象 -// * @param bucketName 桶名称 -// * @param key 存储文件名 -// * @return -// */ -// public static Boolean deleteObject(String bucketName, String key) { -// /** -// * endPoint:HOS的基础路径(公共前缀) -// * account:账户的ID -// * accessKey:向UAAS服务请求到的access_key -// * secretKey:向UAAS服务请求到的secret_key -// */ -// HOS hos = new HOSClientBuilder().build(QianYunTongProperties.endPoint, QianYunTongProperties.account, QianYunTongProperties.accessKey, QianYunTongProperties.secretKey); -// try { -// VoidResult example = hos.deleteObject(bucketName, key); -// if (example.getResponse().isSuccessful()) { -// log.info("删除对象成功:" + bucketName + "--->" + key); -// return true; -// } -// } catch (Exception e) { -// log.info("删除对象失败:" + bucketName + "--->" + key); -// e.printStackTrace(); -// } -// return false; -// } -//} +package com.stylefeng.guns.modular.system.util.qianyuntong; + +import com.heredata.hos.HOS; +import com.heredata.hos.HOSClientBuilder; +import com.heredata.hos.model.CreateBucketRequest; +import com.heredata.hos.model.HOSObject; +import com.heredata.hos.model.PutObjectRequest; +import com.heredata.hos.model.PutObjectResult; +import com.heredata.hos.model.bucket.Bucket; +import com.heredata.model.VoidResult; +import com.stylefeng.guns.modular.system.util.SpringContextsUtil; +import lombok.extern.slf4j.Slf4j; + +import java.io.InputStream; + +/** + * OSS 工具类 + * @author zhibing.pu + * @Date 2025/6/23 17:50 + */ +@Slf4j +public class NCOSSUtil { + + private static QianYunTongConfig qianYunTongConfig = SpringContextsUtil.getBean(QianYunTongConfig.class).getQianYunTongConfig();; + + /** + * 创建桶 + * @param bucketName + * @return + */ + public static Boolean createBucket(String bucketName) { + /** + * endPoint:HOS的基础路径(公共前缀) + * account:账户的ID + * accessKey:向UAAS服务请求到的access_key + * secretKey:向UAAS服务请求到的secret_key + */ + HOS hos = new HOSClientBuilder().build(qianYunTongConfig.getEndPoint(), qianYunTongConfig.getAccount(), qianYunTongConfig.getAccessKey(), qianYunTongConfig.getSecretKey()); + try { + // 创建请求对象,并且设置创建桶名为"example"的桶 + CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName); + VoidResult result = hos.createBucket(createBucketRequest); + if (result.getResponse().isSuccessful()) { + log.info("创建桶成功:" + bucketName); + return true; + } + } catch (Exception e){ + log.info("创建桶失败:" + bucketName); + e.printStackTrace(); + } + return false; + } + + + /** + * 查询桶详情 + * @param bucketName + * @return + */ + public static Bucket getBucketInfo(String bucketName) { + /** + * endPoint:HOS的基础路径(公共前缀) + * account:账户的ID + * accessKey:向UAAS服务请求到的access_key + * secretKey:向UAAS服务请求到的secret_key + */ + HOS hos = new HOSClientBuilder().build(qianYunTongConfig.getEndPoint(), qianYunTongConfig.getAccount(), qianYunTongConfig.getAccessKey(), qianYunTongConfig.getSecretKey()); + try { + // 查询桶名为"example"的详情 + Bucket bucket = hos.getBucketInfo(bucketName); + return bucket; + } catch (Exception e){ + e.printStackTrace(); + } + return null; + } + + + /** + * 删除桶 + * @param bucketName + * @return + */ + public static Boolean deleteBucket(String bucketName) { + /** + * endPoint:HOS的基础路径(公共前缀) + * account:账户的ID + * accessKey:向UAAS服务请求到的access_key + * secretKey:向UAAS服务请求到的secret_key + */ + HOS hos = new HOSClientBuilder().build(qianYunTongConfig.getEndPoint(), qianYunTongConfig.getAccount(), qianYunTongConfig.getAccessKey(), qianYunTongConfig.getSecretKey()); + try { + VoidResult result = hos.deleteBucket(bucketName); + if (result.getResponse().isSuccessful()) { + log.info("删除桶成功:" + bucketName); + return true; + } + } catch (Exception e){ + log.info("删除桶成功:" + bucketName); + e.printStackTrace(); + } + return false; + } + + + /** + * 设置桶策略 + * @param bucketName 桶名 + * @param policyText 策略规则字符串 + * { + * // 策略配置的版本号,由用户定义 + * "Version": "2012-10-17", + * // 策略规则配置,可指定多条策略规则 + * "Statement": [{ + * // 策略规则ID,可用户指定,如不指定则服务自动生成一条 + * "Sid": "sid", + * // 策略规则指定的HOS API接口标识,可参照下表填写 + * "Action": [ + * "action" + * ], + * // 策略规则效果,具体表现为允许或拒绝,有效值为Allow(允许),Deny(拒绝); + * "Effect": "Allow", + * // 策略规则指定的资源参照下面resource配置 + * "Resource": "resource", + * // 策略规则指定的授权人 参照下面Principal配置 + * "Principal": { + * "HWS": [ + * "account_id:root" + * ] + * } + * } + * ] + * } + * @return + */ + public static Boolean setBucketPolicy(String bucketName, String policyText) { + /** + * endPoint:HOS的基础路径(公共前缀) + * account:账户的ID + * accessKey:向UAAS服务请求到的access_key + * secretKey:向UAAS服务请求到的secret_key + */ + HOS hos = new HOSClientBuilder().build(qianYunTongConfig.getEndPoint(), qianYunTongConfig.getAccount(), qianYunTongConfig.getAccessKey(), qianYunTongConfig.getSecretKey()); + // 桶策略字符串 + try { + VoidResult result = hos.setBucketPolicy(bucketName, policyText); + if (result.getResponse().isSuccessful()) { + log.info("设置桶策略成功:" + bucketName); + return true; + } + } catch (Exception e) { + log.info("设置桶策略失败:" + bucketName); + e.printStackTrace(); + } + return false; + } + + + /** + * 上传对象 + * @param bucketName 桶名称 + * @param key 存储文件名 + * @param inputStream 上传文件流 + * @return 对象唯一标识 + */ + public static String putObject(String bucketName, String key, InputStream inputStream) { + /** + * endPoint:HOS的基础路径(公共前缀) + * account:账户的ID + * accessKey:向UAAS服务请求到的access_key + * secretKey:向UAAS服务请求到的secret_key + */ + HOS hos = new HOSClientBuilder().build(qianYunTongConfig.getEndPoint(), qianYunTongConfig.getAccount(), qianYunTongConfig.getAccessKey(), qianYunTongConfig.getSecretKey()); + try { + PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key , inputStream); + PutObjectResult example = hos.putObject(putObjectRequest); + if (example.getResponse().isSuccessful()) { + log.info("上传对象成功:" + bucketName + "--->" + key); + return example.getETag(); + } + } catch (Exception e) { + log.info("上传对象成功:" + bucketName); + e.printStackTrace(); + } + return null; + } + + + /** + * 查询对象 + * @param bucketName 桶名称 + * @param key 存储文件名 + * @return + */ + public static HOSObject getObject(String bucketName, String key) { + /** + * endPoint:HOS的基础路径(公共前缀) + * account:账户的ID + * accessKey:向UAAS服务请求到的access_key + * secretKey:向UAAS服务请求到的secret_key + */ + HOS hos = new HOSClientBuilder().build(qianYunTongConfig.getEndPoint(), qianYunTongConfig.getAccount(), qianYunTongConfig.getAccessKey(), qianYunTongConfig.getSecretKey()); + try { + HOSObject example = hos.getObject(bucketName, key); + if (example.getResponse().isSuccessful()) { + log.info("查询对象成功:" + bucketName + "--->" + key); + return example; + } + } catch (Exception e) { + log.info("查询对象失败:" + bucketName); + e.printStackTrace(); + } + return null; + } + + + /** + * 删除对象 + * @param bucketName 桶名称 + * @param key 存储文件名 + * @return + */ + public static Boolean deleteObject(String bucketName, String key) { + /** + * endPoint:HOS的基础路径(公共前缀) + * account:账户的ID + * accessKey:向UAAS服务请求到的access_key + * secretKey:向UAAS服务请求到的secret_key + */ + HOS hos = new HOSClientBuilder().build(qianYunTongConfig.getEndPoint(), qianYunTongConfig.getAccount(), qianYunTongConfig.getAccessKey(), qianYunTongConfig.getSecretKey()); + try { + VoidResult example = hos.deleteObject(bucketName, key); + if (example.getResponse().isSuccessful()) { + log.info("删除对象成功:" + bucketName + "--->" + key); + return true; + } + } catch (Exception e) { + log.info("删除对象失败:" + bucketName + "--->" + key); + e.printStackTrace(); + } + return false; + } +} -- Gitblit v1.7.1