|
@@ -9,6 +9,11 @@ import com.aliyun.oss.OSS;
|
|
import com.aliyun.oss.OSSClientBuilder;
|
|
import com.aliyun.oss.OSSClientBuilder;
|
|
import com.aliyun.oss.model.GetObjectRequest;
|
|
import com.aliyun.oss.model.GetObjectRequest;
|
|
import com.aliyun.oss.model.OSSObject;
|
|
import com.aliyun.oss.model.OSSObject;
|
|
|
|
+import com.aliyuncs.DefaultAcsClient;
|
|
|
|
+import com.aliyuncs.IAcsClient;
|
|
|
|
+import com.aliyuncs.cdn.model.v20180510.RefreshObjectCachesRequest;
|
|
|
|
+import com.aliyuncs.cdn.model.v20180510.RefreshObjectCachesResponse;
|
|
|
|
+import com.aliyuncs.profile.DefaultProfile;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -34,27 +39,26 @@ public class AliyunOssService implements FssService {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public FileInfo writeFile(String filePath, File file, String md5) {
|
|
public FileInfo writeFile(String filePath, File file, String md5) {
|
|
- if (StringUtils.isEmpty(md5)) {
|
|
|
|
- throw new StatusException("文件MD5值不能为空!");
|
|
|
|
- }
|
|
|
|
|
|
+ if (StringUtils.isNotEmpty(md5)) {
|
|
|
|
+ String realMd5 = FssHelper.getFileMD5(file);
|
|
|
|
|
|
- String realMd5 = FssHelper.getFileMD5(file);
|
|
|
|
- if (!realMd5.equals(md5)) {
|
|
|
|
- log.warn("filePath:{} realMD5:{} MD5:{}", filePath, realMd5, md5);
|
|
|
|
- throw new StatusException("文件MD5值校验不通过!");
|
|
|
|
|
|
+ if (!md5.equals(realMd5)) {
|
|
|
|
+ log.warn("filePath:{} realMD5:{} MD5:{}", filePath, realMd5, md5);
|
|
|
|
+ throw new StatusException("文件MD5值校验不通过!");
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
filePath = this.fixOssFilePath(filePath);
|
|
filePath = this.fixOssFilePath(filePath);
|
|
- OSS ossClient = this.getClient();
|
|
|
|
|
|
+ OSS client = this.getClient();
|
|
|
|
|
|
try {
|
|
try {
|
|
- ossClient.putObject(FssProperty.FSS_BUCKET, filePath, file);
|
|
|
|
|
|
+ client.putObject(FssProperty.FSS_BUCKET, filePath, file);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("文件上传失败!filePath:{} err:{}", filePath, e.getMessage());
|
|
log.error("文件上传失败!filePath:{} err:{}", filePath, e.getMessage());
|
|
throw new StatusException("文件上传失败!");
|
|
throw new StatusException("文件上传失败!");
|
|
} finally {
|
|
} finally {
|
|
try {
|
|
try {
|
|
- ossClient.shutdown();
|
|
|
|
|
|
+ client.shutdown();
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
// ignore
|
|
// ignore
|
|
}
|
|
}
|
|
@@ -70,27 +74,26 @@ public class AliyunOssService implements FssService {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public FileInfo writeFile(String filePath, byte[] bytes, String md5) {
|
|
public FileInfo writeFile(String filePath, byte[] bytes, String md5) {
|
|
- if (StringUtils.isEmpty(md5)) {
|
|
|
|
- throw new StatusException("文件MD5值不能为空!");
|
|
|
|
- }
|
|
|
|
|
|
+ if (StringUtils.isNotEmpty(md5)) {
|
|
|
|
+ String realMd5 = FssHelper.getFileMD5(bytes);
|
|
|
|
|
|
- String realMd5 = FssHelper.getFileMD5(bytes);
|
|
|
|
- if (!realMd5.equals(md5)) {
|
|
|
|
- log.warn("filePath:{} realMD5:{} MD5:{}", filePath, realMd5, md5);
|
|
|
|
- throw new StatusException("文件MD5值校验不通过!");
|
|
|
|
|
|
+ if (!md5.equals(realMd5)) {
|
|
|
|
+ log.warn("filePath:{} realMD5:{} MD5:{}", filePath, realMd5, md5);
|
|
|
|
+ throw new StatusException("文件MD5值校验不通过!");
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
filePath = this.fixOssFilePath(filePath);
|
|
filePath = this.fixOssFilePath(filePath);
|
|
- OSS ossClient = this.getClient();
|
|
|
|
|
|
+ OSS client = this.getClient();
|
|
|
|
|
|
try (ByteArrayInputStream is = new ByteArrayInputStream(bytes);) {
|
|
try (ByteArrayInputStream is = new ByteArrayInputStream(bytes);) {
|
|
- ossClient.putObject(FssProperty.FSS_BUCKET, filePath, is);
|
|
|
|
|
|
+ client.putObject(FssProperty.FSS_BUCKET, filePath, is);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("文件上传失败!filePath:{} err:{}", filePath, e.getMessage());
|
|
log.error("文件上传失败!filePath:{} err:{}", filePath, e.getMessage());
|
|
throw new StatusException("文件上传失败!");
|
|
throw new StatusException("文件上传失败!");
|
|
} finally {
|
|
} finally {
|
|
try {
|
|
try {
|
|
- ossClient.shutdown();
|
|
|
|
|
|
+ client.shutdown();
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
// ignore
|
|
// ignore
|
|
}
|
|
}
|
|
@@ -107,19 +110,19 @@ public class AliyunOssService implements FssService {
|
|
@Override
|
|
@Override
|
|
public void readFile(String filePath, File toFile) {
|
|
public void readFile(String filePath, File toFile) {
|
|
filePath = this.fixOssFilePath(filePath);
|
|
filePath = this.fixOssFilePath(filePath);
|
|
- OSS ossClient = this.getClient();
|
|
|
|
|
|
+ OSS client = this.getClient();
|
|
|
|
|
|
try {
|
|
try {
|
|
GetObjectRequest request = new GetObjectRequest(FssProperty.FSS_BUCKET, filePath);
|
|
GetObjectRequest request = new GetObjectRequest(FssProperty.FSS_BUCKET, filePath);
|
|
|
|
|
|
FssHelper.makeDirs(toFile.getParent());
|
|
FssHelper.makeDirs(toFile.getParent());
|
|
- ossClient.getObject(request, toFile);
|
|
|
|
|
|
+ client.getObject(request, toFile);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("文件下载失败!filePath:{} err:{}", filePath, e.getMessage());
|
|
log.error("文件下载失败!filePath:{} err:{}", filePath, e.getMessage());
|
|
throw new StatusException("文件下载失败!");
|
|
throw new StatusException("文件下载失败!");
|
|
} finally {
|
|
} finally {
|
|
try {
|
|
try {
|
|
- ossClient.shutdown();
|
|
|
|
|
|
+ client.shutdown();
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
// ignore
|
|
// ignore
|
|
}
|
|
}
|
|
@@ -129,12 +132,12 @@ public class AliyunOssService implements FssService {
|
|
@Override
|
|
@Override
|
|
public byte[] readFile(String filePath) {
|
|
public byte[] readFile(String filePath) {
|
|
filePath = this.fixOssFilePath(filePath);
|
|
filePath = this.fixOssFilePath(filePath);
|
|
- OSS ossClient = this.getClient();
|
|
|
|
|
|
+ OSS client = this.getClient();
|
|
|
|
|
|
InputStream is = null;
|
|
InputStream is = null;
|
|
ByteArrayOutputStream bos = null;
|
|
ByteArrayOutputStream bos = null;
|
|
try {
|
|
try {
|
|
- OSSObject ossObject = ossClient.getObject(FssProperty.FSS_BUCKET, filePath);
|
|
|
|
|
|
+ OSSObject ossObject = client.getObject(FssProperty.FSS_BUCKET, filePath);
|
|
is = ossObject.getObjectContent();
|
|
is = ossObject.getObjectContent();
|
|
bos = new ByteArrayOutputStream();
|
|
bos = new ByteArrayOutputStream();
|
|
|
|
|
|
@@ -159,38 +162,87 @@ public class AliyunOssService implements FssService {
|
|
bos.close();
|
|
bos.close();
|
|
}
|
|
}
|
|
|
|
|
|
- ossClient.shutdown();
|
|
|
|
|
|
+ client.shutdown();
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
// ignore
|
|
// ignore
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public boolean deleteFile(String filePath) {
|
|
|
|
+ try {
|
|
|
|
+ filePath = this.fixOssFilePath(filePath);
|
|
|
|
+ this.getClient().deleteObject(FssProperty.FSS_BUCKET, filePath);
|
|
|
|
+ return true;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean existFile(String filePath) {
|
|
|
|
+ filePath = this.fixOssFilePath(filePath);
|
|
|
|
+ return this.getClient().doesObjectExist(FssProperty.FSS_BUCKET, filePath);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 刷新文件的CDN缓存
|
|
|
|
+ * 1、同一个账号 每天最多可提交2000条URL刷新和100个目录刷新
|
|
|
|
+ * 2、每次请求最多只能提交1000条URL刷新,多个URL之间需要用换行符\n分割
|
|
|
|
+ * 3、每秒最多50次请求
|
|
|
|
+ *
|
|
|
|
+ * @param fileUrls 完整访问路径
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public void refreshFile(String fileUrls) {
|
|
|
|
+ try {
|
|
|
|
+ DefaultProfile profile = DefaultProfile.getProfile(FssProperty.FSS_REGION_ID, FssProperty.FSS_ACCESS_KEY_ID,
|
|
|
|
+ FssProperty.FSS_ACCESS_KEY_SECRET);
|
|
|
|
+ IAcsClient client = new DefaultAcsClient(profile);
|
|
|
|
+
|
|
|
|
+ RefreshObjectCachesRequest request = new RefreshObjectCachesRequest();
|
|
|
|
+ request.setObjectPath(fileUrls);
|
|
|
|
+ request.setObjectType("file");
|
|
|
|
+
|
|
|
|
+ RefreshObjectCachesResponse resp = client.getAcsResponse(request);
|
|
|
|
+
|
|
|
|
+ log.info("refreshFile:{} taskId:{}", fileUrls, resp != null ? resp.getRefreshTaskId() : "none");
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("refreshFile:{} err:{}", fileUrls, e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
private OSS getClient() {
|
|
private OSS getClient() {
|
|
try {
|
|
try {
|
|
// ClientBuilderConfiguration configuration = new ClientBuilderConfiguration();
|
|
// ClientBuilderConfiguration configuration = new ClientBuilderConfiguration();
|
|
- OSS ossClient = new OSSClientBuilder().build(
|
|
|
|
- internal ? FssProperty.FSS_INTERNAL_ENDPOINT : FssProperty.FSS_ENDPOINT,
|
|
|
|
|
|
+
|
|
|
|
+ final String endpoint = "https://" + FssProperty.FSS_REGION_ID + ".aliyuncs.com";
|
|
|
|
+ final String internalEndpoint = "https://" + FssProperty.FSS_REGION_ID + "-internal.aliyuncs.com";
|
|
|
|
+
|
|
|
|
+ OSS client = new OSSClientBuilder().build(this.internal ? internalEndpoint : endpoint,
|
|
FssProperty.FSS_ACCESS_KEY_ID, FssProperty.FSS_ACCESS_KEY_SECRET);
|
|
FssProperty.FSS_ACCESS_KEY_ID, FssProperty.FSS_ACCESS_KEY_SECRET);
|
|
- // ossClient.setBucketTransferAcceleration(FssProperty.FSS_BUCKET, internal);
|
|
|
|
- return ossClient;
|
|
|
|
|
|
+ // client.setBucketTransferAcceleration(FssProperty.FSS_BUCKET, true);
|
|
|
|
+
|
|
|
|
+ return client;
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error(e.getMessage(), e);
|
|
log.error(e.getMessage(), e);
|
|
throw new StatusException("OSS客户端初始化失败!");
|
|
throw new StatusException("OSS客户端初始化失败!");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private String fixOssFilePath(String ossFilePath) {
|
|
|
|
- if (StringUtils.isEmpty(ossFilePath)) {
|
|
|
|
|
|
+ private String fixOssFilePath(String filePath) {
|
|
|
|
+ if (StringUtils.isEmpty(filePath)) {
|
|
throw new StatusException("文件存储路径不能为空!");
|
|
throw new StatusException("文件存储路径不能为空!");
|
|
}
|
|
}
|
|
|
|
|
|
// OSS存储路径不允许以 / 开头,需要去掉 /
|
|
// OSS存储路径不允许以 / 开头,需要去掉 /
|
|
- if (ossFilePath.startsWith("/")) {
|
|
|
|
- return ossFilePath.substring(1);
|
|
|
|
|
|
+ if (filePath.startsWith("/")) {
|
|
|
|
+ return filePath.substring(1);
|
|
}
|
|
}
|
|
|
|
|
|
- return ossFilePath;
|
|
|
|
|
|
+ return filePath;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|