1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| @Component public class OSSClientUtil { Log log = LogFactory.getLog(OSSClientUtil.class);
protected static String endpoint = "http://oss-cn-shanghai.aliyuncs.com"; protected static String accessKeyId = ""; protected static String accessKeySecret = ""; protected static String bucketName = "xxx";
private static String filename = "";
private volatile static OSS ossClient = null;
public String uploadFile(MultipartFile file) throws Exception { initOSS(); String[] fileWords = file.getContentType().split("/"); String time = TimeUtil.dateToString(LocalDateTime.now()); String filename = time + "/" + System.currentTimeMillis() + "-" + UUID.randomUUID().toString().substring(0, 18) + "." + fileWords[fileWords.length - 1]; System.out.println(filename); try { ossClient.putObject(new PutObjectRequest(bucketName, filename , file.getInputStream())); } catch (OSSException oe) { System.out.println(TimeUtil.preTime() + "Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println(TimeUtil.preTime() + "Error Message: " + oe.getErrorMessage()); System.out.println(TimeUtil.preTime() + "Error Code: " + oe.getErrorCode()); System.out.println(TimeUtil.preTime() + "Request ID: " + oe.getRequestId()); System.out.println(TimeUtil.preTime() + "Host ID: " + oe.getHostId()); throw oe; } catch (ClientException ce) { System.out.println(TimeUtil.preTime() + "Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println(TimeUtil.preTime() + "Error Message: " + ce.getMessage()); throw ce; } return "https://" + bucketName + "." + endpoint.split("//")[1] + "/" + filename; }
public boolean delete(String url) throws Exception{ initOSS(); try { ossClient.deleteObject(bucketName,url); } catch (OSSException oe) { System.out.println(TimeUtil.preTime() + "Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println(TimeUtil.preTime() + "Error Message: " + oe.getErrorMessage()); System.out.println(TimeUtil.preTime() + "Error Code: " + oe.getErrorCode()); System.out.println(TimeUtil.preTime() + "Request ID: " + oe.getRequestId()); System.out.println(TimeUtil.preTime() + "Host ID: " + oe.getHostId()); throw oe; } catch (ClientException ce) { System.out.println(TimeUtil.preTime() + "Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println(TimeUtil.preTime() + "Error Message: " + ce.getMessage()); throw ce; } return true; }
private static OSS initOSS() throws Exception { if(ossClient == null) { synchronized (OSSClientUtil.class) { if(ossClient == null) { try{ FileReader fileReader = new FileReader("./pass/pass.txt"); BufferedReader bufferedReader = new BufferedReader(fileReader); String[] str = bufferedReader.readLine().split(" "); accessKeyId = str[0]; accessKeySecret = str[1]; }catch (Exception e){ e.printStackTrace(); throw e; } ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); } } } return ossClient; }
}
|