上传文件
private final AliyunOssProperties aliyunOssProperties;
private final ClientBuilderConfiguration config;
public static OSS getOssClient(AliyunOssProperties aliyunOssProperties,ClientBuilderConfiguration config){
return new OSSClientBuilder()
.build(aliyunOssProperties.getEndpoint(), aliyunOssProperties.getAccessKeyId(), aliyunOssProperties.getAccessKeySecret(),config);
}
public void postFileToOSS(OSS ossClient,String bucketName,String path,File file) throws IOException{
boolean st = aliyunOssProperties.getSt();
if(st){
ossClient.putObject(bucketName,path,new FileInputStream(file));
}
}
获取文件
public File getOSSObjectToFile(String objectName,String filePath){
OSS ossClient = getOssClient(aliyunOssProperties,config);
File file = new File(filePath);
try{
if(!file.getParentFile().exists()){
Files.createDirectories(file.getParentFile().toPath());
}
if(!file.exists()){
Files.createFile(file.toPath());
}
if(aliyunOssProperties.getSt()){
ossClient.getObject(new GetObjectRequest(aliyunOssProperties.getBucketName(), objectName), file);
}
}finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
return file;
}