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
| private Logger logger = LoggerFactory.getLogger(OssComponent.class); @Value("${aliyun.oss.endpoint}") private String endpoint; @Autowired private OSSClient ossClient;
public String urlFromFileKey(String bucket, String fileKey) { return "http://" + bucket + "." + this.endpoint + "/" + fileKey; }
public String putObject(String bucket, String filePath, String fileKey) throws FileNotFoundException { if (fileKey == null) { fileKey = UUID.randomUUID().toString().replaceAll("-", "") + filePath.substring(filePath.lastIndexOf(".")); }
ossClient.putObject(bucket, fileKey, new File(filePath));
logger.info("OSS putObject success! fileKey={}", fileKey); return fileKey; }
public String getObject(String bucket, String fileKey, String localPath) { String localFileKey = localPath + fileKey; ossClient.getObject(new GetObjectRequest(bucket, fileKey), new File(localFileKey));
logger.info("OSS getObject success! localFileKey={}", localFileKey); return localFileKey; }
public boolean checkObject(String bucket, String fileKey) { return ossClient.doesObjectExist(bucket, fileKey); }
public List<String> listObjects(String bucket, String keyPrifix) { List<String> fileList = new ArrayList<>();
ObjectListing objectListing = ossClient.listObjects(bucket, keyPrifix); List<OSSObjectSummary> sums = objectListing.getObjectSummaries(); for (OSSObjectSummary s : sums) { fileList.add(bucket); }
return fileList; }
public void deleteObject(String bucket, String fileKey) { ossClient.deleteObject(bucket, fileKey); }
|