package cn.stylefeng.guns.utils; import cn.hutool.core.lang.UUID; import com.obs.services.ObsClient; import com.obs.services.model.PutObjectResult; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; /** * @author guohongjin * @create 2023/5/8 17:37 */ @Slf4j @Data @Component @ConfigurationProperties(prefix = "obs") public class ObsUtil { //endpoint private String endPoint; private String ak; private String sk; //桶名 private String bucketName; private static ObsClient obsClient; //生成obsclient private ObsClient createObsClient(){ if (obsClient == null){ synchronized (ObsClient.class){ if (obsClient == null){ // 创建ObsClient实例 obsClient = new ObsClient(ak, sk, endPoint); } } } return obsClient; } /** * 文件上传 * @param filePath 文件地址 * @param fileName 文件名称 */ public String upload(String filePath,String fileName){ // 待上传的本地文件路径,需要指定到具体的文件名 FileInputStream fis = null; try { fis = new FileInputStream(new File(filePath)); String obsName = fileName.substring(fileName.lastIndexOf("/")+1,fileName.length()); String obsFilePath = UUID.randomUUID().toString().replace("-","")+"/"+fileName.substring(0,fileName.lastIndexOf("/")+1); // createObsClient().putObject(this.bucketName, obsFilePath, new ByteArrayInputStream(new byte[0])); PutObjectResult putObjectResult = createObsClient().putObject(this.bucketName, "//teest//tet//"+obsName, fis); return putObjectResult.getObjectUrl(); } catch (FileNotFoundException e) { e.printStackTrace(); } return null; } /** * 文件上传 * @param file 文件对象 * @param fileName 文件名称 */ public String upload(File file,String fileName){ // 待上传的本地文件路径,需要指定到具体的文件名 FileInputStream fis = null; try { fis = new FileInputStream(file); PutObjectResult putObjectResult = createObsClient().putObject(this.bucketName, UUID.randomUUID().toString().replace("-","")+"/"+fileName, fis); return putObjectResult.getObjectUrl(); } catch (FileNotFoundException e) { e.printStackTrace(); } return null; } /** * 文件上传 * @param file 文件对象 */ public String upload(MultipartFile file) { // 待上传的本地文件路径,需要指定到具体的文件名 try { PutObjectResult putObjectResult = createObsClient().putObject(this.bucketName, UUID.randomUUID().toString().replace("-", "") + "/" + file.getOriginalFilename(), file.getInputStream()); return putObjectResult.getObjectUrl(); } catch (FileNotFoundException e) { e.printStackTrace(); log.error("上传文件失败!FileNotFoundException:", e); } catch (IOException e) { e.printStackTrace(); log.error("上传文件失败!IOException:", e); } catch (Exception ex) { ex.printStackTrace(); log.error("上传文件失败!", ex); } return null; } // public static void main(String[] args) { // // Endpoint以北京四为例,其他地区请按实际情况填写。 // String endPoint = "https://obs.cn-east-3.myhuaweicloud.com"; // String ak = "AYQCHVTCQUTHVDVLQZMJ"; // String sk = "QSC0iTbxmugIzYoTH13lptrOkix6e15DGK1LOoky"; // // 创建ObsClient实例 // ObsClient obsClient = new ObsClient(ak, sk, endPoint); // // // 待上传的本地文件路径,需要指定到具体的文件名 // FileInputStream fis = null; // try { // fis = new FileInputStream(new File("C:\\Users\\hyb\\Desktop\\CI.xlsx")); // PutObjectResult putObjectResult = obsClient.putObject("fbwl", "CI.xlsx", fis); // System.out.println(JSONObject.toJSON(putObjectResult)); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } // // //// // 待上传的本地文件路径,需要指定到具体的文件名 //// FileInputStream fis2 = new FileInputStream(new File("localfile2")); //// PutObjectRequest request = new PutObjectRequest(); //// request.setBucketName("bucketname"); //// request.setObjectKey("objectname2"); //// request.setInput(fis2); //// obsClient.putObject(request); // } // public static String uploadFile(String fileUrl){ // // } }