oss拷贝文件官方地址:
https://help.aliyun.com/zh/oss/developer-reference/java-copy-objects?spm=a2c4g.11186623.0.0.16f76083xr3lKM
<!-- OSS -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.12.0</version>
</dependency>
package com.hcl.demo.configure.oss;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class OSSProperties {
private String fileUrlPrefix;
private String bucketName;
private String diskName;
private String regionId;
private String endpoint;
private String accessKey;
private String accessSecret;
private String oss_path;
@Bean
public OSS ossClient(){
CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKey, accessSecret);
return new OSSClientBuilder().build(endpoint, accessKey, accessSecret);
}
}
public String copyFile(String fileUrl) {
String reUrl = null;
// 原文件路径加文件名称:例如:xxx/test/testfile.xlsx
String sourceObjectName = null;
try {
// 处理是图片还是文件,我们系统区分了文件和图片
String url1 = "https://file.xxx.com/";
String url2 = "https://pic.xxx.com/";
if (fileUrl.contains(url1)) {
sourceObjectName = fileUrl.substring(url1.length());
}
if (fileUrl.contains(url2)) {
sourceObjectName = fileUrl.substring(url2.length());
}
int doIndex = fileUrl.lastIndexOf("/");
// 文件名称带后缀 例如:testfile.xlsx
String fileName = fileUrl.substring(doIndex + 1);
reUrl = this.ossCopyFileToNewPath(sourceObjectName, fileName);
} catch (Exception e) {
e.printStackTrace();
}
return reUrl;
}
private String ossCopyFileToNewPath(String sourceObjectName, String fileName) {
String sourceBucketName = ossProperties.getBucketName();
// String sourceObjectName = "xxx/test/testfile.xlsx";
String destinationBucketName = ossProperties.getBucketName();
String destinationObjectName = "xxxa/copy/" + fileName;
// 创建OSS连接
OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKey(), ossProperties.getAccessSecret());
// 判断bucket(存储空间)是否存在
boolean exist = ossClient.doesBucketExist(ossProperties.getBucketName());
JSONObject json = new JSONObject();
if (!exist) {
json.put("result", "存储空间不存在");
}
try {
ossClient.copyObject(sourceBucketName, sourceObjectName, destinationBucketName, destinationObjectName);
// 如果是移动文件就可以执行下面步骤
// ossClient.deleteObject(sourceBucketName, sourceObjectName);//比复制文件多了一步删除
} catch (Exception e) {
log.info("报错信息:" + e.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
return ossProperties.getFileUrlPrefix() + destinationObjectName;
}
参数说明:
断点查看
补充:
移动文件操作,就是在上面拷贝文件后删除原路径的文件即可。
ossClient.copyObject(sourceBucketName, sourceObjectName, destinationBucketName, destinationObjectName);
// 如果是移动文件就可以执行下面步骤
ossClient.deleteObject(sourceBucketName, sourceObjectName);//比复制文件多了一步删除
注意:
有些依赖没有的可以自行导入,例如lombok等
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>