增加获取文件元数据接口
This commit is contained in:
parent
cc4393b936
commit
045a561c07
@ -2,7 +2,11 @@ package cn.axzo.oss.client.controller;
|
||||
|
||||
import cn.axzo.core.utils.converter.BeanConverter;
|
||||
import cn.axzo.framework.auth.domain.ContextInfo;
|
||||
import cn.axzo.oss.common.enums.ChannelTypeEnum;
|
||||
import cn.axzo.oss.common.enums.CodeEnum;
|
||||
import cn.axzo.oss.common.exception.BizException;
|
||||
import cn.axzo.oss.common.utils.BeanConvertUtil;
|
||||
import cn.axzo.oss.common.utils.UrlUtil;
|
||||
import cn.axzo.oss.http.api.ServerFileServiceApi;
|
||||
import cn.axzo.oss.http.model.ApiSignUrlDownloadRequest;
|
||||
import cn.axzo.oss.http.model.ApiSignUrlDownloadResponse;
|
||||
@ -13,6 +17,8 @@ import cn.axzo.oss.http.model.FindFileKeyRequest;
|
||||
import cn.axzo.oss.http.model.FindFileKeyResponse;
|
||||
import cn.axzo.oss.http.model.FindFileUrlRequest;
|
||||
import cn.axzo.oss.http.model.FindFileUrlResponse;
|
||||
import cn.axzo.oss.http.model.GetObjectMetaRequest;
|
||||
import cn.axzo.oss.http.model.GetObjectMetaResponse;
|
||||
import cn.axzo.oss.http.model.ServerFileDeleteRequest;
|
||||
import cn.axzo.oss.http.model.ServerFileUploadRequest;
|
||||
import cn.axzo.oss.http.model.ServerFileUploadResponse;
|
||||
@ -23,10 +29,12 @@ import cn.axzo.oss.manager.api.dto.request.ServerFileDeleteDto;
|
||||
import cn.axzo.oss.manager.api.dto.request.ServerFileUploadDto;
|
||||
import cn.axzo.oss.manager.api.dto.request.SignUrlDownloadDto;
|
||||
import cn.axzo.oss.manager.api.dto.request.SignUrlUploadDto;
|
||||
import cn.axzo.oss.manager.api.dto.response.ManaGetObjectMetaResponse;
|
||||
import cn.axzo.oss.service.api.FileService;
|
||||
import cn.azxo.framework.common.model.CommonResponse;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@ -127,4 +135,24 @@ public class ServerFileController implements ServerFileServiceApi {
|
||||
ContextInfo.LiteSaasContext liteSaasContext = JSONUtil.toBean(contextInfoLiteJsonStr, ContextInfo.LiteSaasContext.class);
|
||||
return CommonResponse.success(BeanConverter.convert(fileService.signUrlUpload(dto, liteSaasContext), ApiSignUrlUploadResponse.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件元数据信息
|
||||
*/
|
||||
@Override
|
||||
public CommonResponse<GetObjectMetaResponse> getObjectMeta(@RequestBody GetObjectMetaRequest request) {
|
||||
if (!StringUtils.hasText(request.getUrl()) && !StringUtils.hasText(request.getBucketName()) && !StringUtils.hasText(request.getKey())) {
|
||||
throw new BizException(CodeEnum.URL_BUCKET_NAME_KEY_ALL_EMPTY);
|
||||
}
|
||||
|
||||
|
||||
String bucketName = UrlUtil.fetchBucketName(request.getBucketName(), request.getUrl());
|
||||
String bucketKey = UrlUtil.fetchBucketKey(request.getKey(), request.getUrl());
|
||||
ManaGetObjectMetaResponse response = this.fileService.getObjectMeta(bucketName
|
||||
, bucketKey
|
||||
, ChannelTypeEnum.OSS.getChannelCode());
|
||||
return CommonResponse.success(BeanConverter.convert(response, GetObjectMetaResponse.class));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -60,7 +60,9 @@ public enum CodeEnum implements EnumBase<Integer> {
|
||||
UPLOAD_SIGN_URL_FAIL(503,"上传临时授权获取SIGNURL失败"),
|
||||
DOWNLOAD_SIGN_URL_FAIL(504,"下载临时授权获取SIGNURL失败"),
|
||||
FILE_APP_IS_EMPTY(505, "app is empty"),
|
||||
BUCKET_TYPE_IS_EMPTY(506, "bucketType is empty")
|
||||
BUCKET_TYPE_IS_EMPTY(506, "bucketType is empty"),
|
||||
GET_OBJECT_META_FAIL(507, "获取元文件失败"),
|
||||
URL_BUCKET_NAME_KEY_ALL_EMPTY(508, "url-bucketName-key同时为空")
|
||||
;
|
||||
|
||||
private final Integer code;
|
||||
|
||||
@ -26,12 +26,42 @@ public class UrlUtil {
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过url或者bucketName,获取桶名称
|
||||
*/
|
||||
public static String fetchBucketName(String bucketName, String url) {
|
||||
if (org.springframework.util.StringUtils.hasText(bucketName)) {
|
||||
return bucketName;
|
||||
}
|
||||
if (org.springframework.util.StringUtils.hasText(url)) {
|
||||
if (url.contains(".") && url.contains("//")) {
|
||||
return url.substring(url.indexOf("//") + 2,url.indexOf("."));
|
||||
}
|
||||
}
|
||||
return url;
|
||||
}
|
||||
/**
|
||||
* 通过url或者bucketKey,获取桶文件Key
|
||||
*/
|
||||
public static String fetchBucketKey(String bucketKey, String url) {
|
||||
if (org.springframework.util.StringUtils.hasText(bucketKey)) {
|
||||
return bucketKey;
|
||||
}
|
||||
if (org.springframework.util.StringUtils.hasText(url)) {
|
||||
if (url.contains(".") && url.contains("//")) {
|
||||
String urlPathNoHttp = url.split("//")[1];
|
||||
return urlPathNoHttp.substring(urlPathNoHttp.indexOf("/") + 1);
|
||||
}
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
String str = "http://axzo-test.oss-cn-chengdu.aliyuncs.com/reportCenter/reportCenter_test";
|
||||
String str1 = "https://baidu.com";
|
||||
System.out.println(httpToHttps(str));
|
||||
System.out.println(httpToHttps(str1));
|
||||
String str = "https://axzo-public.oss-cn-chengdu.aliyuncs.com/cms/cms/ecefcd11d1744bd19770ccac2b7724e2.jpg";
|
||||
System.out.println(fetchBucketName("", str));
|
||||
System.out.println(fetchBucketKey("", str));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -9,6 +9,8 @@ import cn.axzo.oss.http.model.FindFileKeyRequest;
|
||||
import cn.axzo.oss.http.model.FindFileKeyResponse;
|
||||
import cn.axzo.oss.http.model.FindFileUrlRequest;
|
||||
import cn.axzo.oss.http.model.FindFileUrlResponse;
|
||||
import cn.axzo.oss.http.model.GetObjectMetaRequest;
|
||||
import cn.axzo.oss.http.model.GetObjectMetaResponse;
|
||||
import cn.axzo.oss.http.model.ServerFileDeleteRequest;
|
||||
import cn.axzo.oss.http.model.ServerFileUploadRequest;
|
||||
import cn.axzo.oss.http.model.ServerFileUploadResponse;
|
||||
@ -82,4 +84,10 @@ public interface ServerFileServiceApi {
|
||||
@RequestMapping(value = "api/signUrl/fetchUpload", method = RequestMethod.POST)
|
||||
CommonResponse<ApiSignUrlUploadResponse> signUrlFetchUpload(ApiSignUrlUploadRequest request);
|
||||
|
||||
/**
|
||||
* 文件元数据信息
|
||||
*/
|
||||
@RequestMapping(value = "api/server/getObjectMeta", method = RequestMethod.POST)
|
||||
CommonResponse<GetObjectMetaResponse> getObjectMeta(GetObjectMetaRequest request);
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,30 @@
|
||||
package cn.axzo.oss.http.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author: xudawei
|
||||
* @date: 2024-03-12
|
||||
* @description: 授权给第三方下载
|
||||
**/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class GetObjectMetaRequest {
|
||||
/**
|
||||
* 桶名称
|
||||
*/
|
||||
private String bucketName;
|
||||
/**
|
||||
* 文件key
|
||||
*/
|
||||
private String key;
|
||||
/**
|
||||
* 文件url
|
||||
*/
|
||||
private String url;
|
||||
}
|
||||
@ -0,0 +1,29 @@
|
||||
package cn.axzo.oss.http.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 元数据
|
||||
*
|
||||
* @author xudawei
|
||||
* @since 2024-05-20
|
||||
*/
|
||||
@Data
|
||||
public class GetObjectMetaResponse {
|
||||
/**
|
||||
* 桶名称
|
||||
*/
|
||||
private String bucketName;
|
||||
/**
|
||||
* 文件key
|
||||
*/
|
||||
private String key;
|
||||
/**
|
||||
* 文件url
|
||||
*/
|
||||
private String url;
|
||||
/**
|
||||
* 文件大小
|
||||
*/
|
||||
private long size;
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
package cn.axzo.oss.integration.s3;
|
||||
|
||||
import cn.axzo.oss.integration.s3.base.BaseS3Service;
|
||||
import com.aliyun.oss.model.SimplifiedObjectMeta;
|
||||
|
||||
/**
|
||||
* @program: oss
|
||||
@ -29,4 +30,9 @@ public interface AliOssService extends BaseS3Service {
|
||||
* 获取url
|
||||
*/
|
||||
String getUrl(String bucketName, String tgtFileKey);
|
||||
|
||||
/**
|
||||
* 元数据
|
||||
*/
|
||||
SimplifiedObjectMeta getObjectMeta(String bucketName, String key);
|
||||
}
|
||||
|
||||
@ -19,6 +19,7 @@ import com.aliyun.oss.model.OSSObject;
|
||||
import com.aliyun.oss.model.ObjectMetadata;
|
||||
import com.aliyun.oss.model.PartETag;
|
||||
import com.aliyun.oss.model.ResponseHeaderOverrides;
|
||||
import com.aliyun.oss.model.SimplifiedObjectMeta;
|
||||
import com.aliyun.oss.model.UploadPartRequest;
|
||||
import com.aliyun.oss.model.UploadPartResult;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -359,4 +360,18 @@ public class AliOssServiceImpl implements AliOssService {
|
||||
throw new BizException(CodeEnum.DELETE_FILE_FAIL);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 元数据
|
||||
*/
|
||||
@Override
|
||||
public SimplifiedObjectMeta getObjectMeta(String bucketName, String key) {
|
||||
try {
|
||||
log.info("aliyun getObjectMeta params, bucketName:{}, key:{}", bucketName, key);
|
||||
return aliOssClient.getClient().getSimplifiedObjectMeta(bucketName, key);
|
||||
} catch (Exception e) {
|
||||
log.error("aliyun getObjectMeta exception, bucketName:{}, key:{}", bucketName, key, e);
|
||||
throw new BizException(CodeEnum.GET_OBJECT_META_FAIL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ package cn.axzo.oss.manager.api;
|
||||
|
||||
import cn.axzo.oss.manager.api.dto.PartETag;
|
||||
import cn.axzo.oss.manager.api.dto.request.MultipartUploadDto;
|
||||
import cn.axzo.oss.manager.api.dto.response.ManaGetObjectMetaResponse;
|
||||
import cn.axzo.oss.manager.api.vo.SignUrlUploadVo;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@ -75,4 +76,9 @@ public interface FileManager {
|
||||
* 根据华为云/阿里云,授权给第三方下载
|
||||
*/
|
||||
String fetchDownloadUrl(String bucketName, String key, String channelCode);
|
||||
|
||||
/**
|
||||
* 元数据
|
||||
*/
|
||||
ManaGetObjectMetaResponse getObjectMeta(String bucketName, String key, String channelCode);
|
||||
}
|
||||
|
||||
@ -0,0 +1,35 @@
|
||||
package cn.axzo.oss.manager.api.dto.response;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 元数据
|
||||
*
|
||||
* @author xudawei
|
||||
* @since 2024-05-20
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ManaGetObjectMetaResponse {
|
||||
/**
|
||||
* 桶名称
|
||||
*/
|
||||
private String bucketName;
|
||||
/**
|
||||
* 文件key
|
||||
*/
|
||||
private String key;
|
||||
/**
|
||||
* 文件url
|
||||
*/
|
||||
private String url;
|
||||
/**
|
||||
* 文件大小
|
||||
*/
|
||||
private long size;
|
||||
}
|
||||
@ -13,7 +13,9 @@ import cn.axzo.oss.integration.s3.HuaWeiCloudService;
|
||||
import cn.axzo.oss.manager.api.FileManager;
|
||||
import cn.axzo.oss.manager.api.dto.PartETag;
|
||||
import cn.axzo.oss.manager.api.dto.request.MultipartUploadDto;
|
||||
import cn.axzo.oss.manager.api.dto.response.ManaGetObjectMetaResponse;
|
||||
import cn.axzo.oss.manager.api.vo.SignUrlUploadVo;
|
||||
import com.aliyun.oss.model.SimplifiedObjectMeta;
|
||||
import com.obs.services.model.TemporarySignatureResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
@ -252,4 +254,22 @@ public class FileManagerImpl implements FileManager {
|
||||
}
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
|
||||
/**
|
||||
* 元数据
|
||||
*/
|
||||
@Override
|
||||
public ManaGetObjectMetaResponse getObjectMeta(String bucketName, String key, String channelCode) {
|
||||
ChannelTypeEnum typeEnum = ChannelTypeEnum.getChannelTypeByChannelCode(channelCode);
|
||||
switch (typeEnum) {
|
||||
case OBS:// 华为云
|
||||
return ManaGetObjectMetaResponse.builder().build();
|
||||
case OSS:// 阿里云
|
||||
SimplifiedObjectMeta simplifiedObjectMeta = aliOssService.getObjectMeta(bucketName, key);
|
||||
return ManaGetObjectMetaResponse.builder().bucketName(bucketName).key(key).size(simplifiedObjectMeta.getSize()).build();
|
||||
default:
|
||||
BizException.error(CodeEnum.CHANNEL_TYPE_NOT_EXIST);
|
||||
}
|
||||
return ManaGetObjectMetaResponse.builder().build();
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,6 +16,7 @@ import cn.axzo.oss.manager.api.dto.request.SignUrlUploadDto;
|
||||
import cn.axzo.oss.manager.api.dto.response.FileInformationResponse;
|
||||
import cn.axzo.oss.manager.api.dto.response.FindFileKeyResponse;
|
||||
import cn.axzo.oss.manager.api.dto.response.FindFileUrlResponse;
|
||||
import cn.axzo.oss.manager.api.dto.response.ManaGetObjectMetaResponse;
|
||||
import cn.axzo.oss.manager.api.dto.response.MultipartUploadInitResponse;
|
||||
import cn.axzo.oss.manager.api.dto.response.MultipartUploadResponse;
|
||||
import cn.axzo.oss.manager.api.dto.response.ServerFileDownloadResponse;
|
||||
@ -86,4 +87,9 @@ public interface FileService {
|
||||
* @param dto
|
||||
*/
|
||||
void deleteFile(DeleteFileDto dto);
|
||||
|
||||
/**
|
||||
* 元数据
|
||||
*/
|
||||
ManaGetObjectMetaResponse getObjectMeta(String bucketName, String key, String channelCode);
|
||||
}
|
||||
|
||||
@ -45,6 +45,7 @@ import cn.axzo.oss.manager.api.dto.request.SignUrlUploadDto;
|
||||
import cn.axzo.oss.manager.api.dto.response.FileInformationResponse;
|
||||
import cn.axzo.oss.manager.api.dto.response.FindFileKeyResponse;
|
||||
import cn.axzo.oss.manager.api.dto.response.FindFileUrlResponse;
|
||||
import cn.axzo.oss.manager.api.dto.response.ManaGetObjectMetaResponse;
|
||||
import cn.axzo.oss.manager.api.dto.response.MultipartUploadInitResponse;
|
||||
import cn.axzo.oss.manager.api.dto.response.MultipartUploadResponse;
|
||||
import cn.axzo.oss.manager.api.dto.response.ServerFileDownloadResponse;
|
||||
@ -1128,6 +1129,16 @@ public class FileServiceImpl implements FileService {
|
||||
fileManager.deleteFile(file.getBucketName(), tgtFileKey,file.getChannelCode());
|
||||
fileDao.deleteFile(dto.getFileKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* 元数据
|
||||
*/
|
||||
@Override
|
||||
public ManaGetObjectMetaResponse getObjectMeta(String bucketName, String key, String channelCode) {
|
||||
log.info("getObjectMeta bucketName:{},key:{},channelCode:{}", bucketName, key, channelCode);
|
||||
Long start = System.currentTimeMillis();
|
||||
ManaGetObjectMetaResponse response = fileManager.getObjectMeta(bucketName, key, channelCode);
|
||||
log.info("getObjectMeta response:{}, times:{}", JsonUtil.obj2Str(response), System.currentTimeMillis() - start);
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user