获取元信息-增加批量接口
This commit is contained in:
parent
24ff5efcbb
commit
f3836bf9c0
@ -12,6 +12,8 @@ import cn.axzo.oss.http.model.ApiSignUrlDownloadRequest;
|
||||
import cn.axzo.oss.http.model.ApiSignUrlDownloadResponse;
|
||||
import cn.axzo.oss.http.model.ApiSignUrlUploadRequest;
|
||||
import cn.axzo.oss.http.model.ApiSignUrlUploadResponse;
|
||||
import cn.axzo.oss.http.model.BatchGetObjectMetaRequest;
|
||||
import cn.axzo.oss.http.model.BatchGetObjectMetaResponse;
|
||||
import cn.axzo.oss.http.model.FileInformationResponse;
|
||||
import cn.axzo.oss.http.model.FindFileKeyRequest;
|
||||
import cn.axzo.oss.http.model.FindFileKeyResponse;
|
||||
@ -34,13 +36,18 @@ 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.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 服务端文件 Controller
|
||||
@ -149,10 +156,27 @@ public class ServerFileController implements ServerFileServiceApi {
|
||||
String bucketName = UrlUtil.fetchBucketName(request.getBucketName(), request.getUrl());
|
||||
String bucketKey = UrlUtil.fetchBucketKey(request.getKey(), request.getUrl());
|
||||
ManaGetObjectMetaResponse response = this.fileService.getObjectMeta(bucketName
|
||||
, bucketKey
|
||||
, bucketKey, request.getUrl()
|
||||
, ChannelTypeEnum.OSS.getChannelCode());
|
||||
return CommonResponse.success(BeanConverter.convert(response, GetObjectMetaResponse.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件元数据信息-批量
|
||||
*/
|
||||
@Override
|
||||
public CommonResponse<BatchGetObjectMetaResponse> batchGetObjectMeta(@RequestBody BatchGetObjectMetaRequest request) {
|
||||
List<GetObjectMetaRequest> list = request.getList();
|
||||
if (Objects.isNull(request) || CollectionUtils.isEmpty(list)) {
|
||||
return CommonResponse.success(BatchGetObjectMetaResponse.builder().build());
|
||||
}
|
||||
List<GetObjectMetaResponse> responseList = request.getList().stream().map(item -> {
|
||||
GetObjectMetaResponse response = getObjectMeta(item).getData();
|
||||
response.setId(item.getId());
|
||||
return response;
|
||||
}).collect(Collectors.toList());
|
||||
return CommonResponse.success(BatchGetObjectMetaResponse.builder().list(responseList).build());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -4,6 +4,8 @@ import cn.axzo.oss.http.model.ApiSignUrlDownloadRequest;
|
||||
import cn.axzo.oss.http.model.ApiSignUrlDownloadResponse;
|
||||
import cn.axzo.oss.http.model.ApiSignUrlUploadRequest;
|
||||
import cn.axzo.oss.http.model.ApiSignUrlUploadResponse;
|
||||
import cn.axzo.oss.http.model.BatchGetObjectMetaRequest;
|
||||
import cn.axzo.oss.http.model.BatchGetObjectMetaResponse;
|
||||
import cn.axzo.oss.http.model.FileInformationResponse;
|
||||
import cn.axzo.oss.http.model.FindFileKeyRequest;
|
||||
import cn.axzo.oss.http.model.FindFileKeyResponse;
|
||||
@ -90,4 +92,10 @@ public interface ServerFileServiceApi {
|
||||
@RequestMapping(value = "api/server/getObjectMeta", method = RequestMethod.POST)
|
||||
CommonResponse<GetObjectMetaResponse> getObjectMeta(GetObjectMetaRequest request);
|
||||
|
||||
/**
|
||||
* 文件元数据信息
|
||||
*/
|
||||
@RequestMapping(value = "api/server/batchGetObjectMeta", method = RequestMethod.POST)
|
||||
CommonResponse<BatchGetObjectMetaResponse> batchGetObjectMeta(BatchGetObjectMetaRequest request);
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,26 @@
|
||||
package cn.axzo.oss.http.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author: xudawei
|
||||
* @date: 2024-03-12
|
||||
* @description: 授权给第三方下载
|
||||
**/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class BatchGetObjectMetaRequest {
|
||||
|
||||
@Max(value = 1000, message = "max size:1000")
|
||||
@NotEmpty(message = "list not empty")
|
||||
private List<GetObjectMetaRequest> list;
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
package cn.axzo.oss.http.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 元数据-批量
|
||||
*
|
||||
* @author xudawei
|
||||
* @since 2024-05-20
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class BatchGetObjectMetaResponse {
|
||||
|
||||
private List<GetObjectMetaResponse> list = new ArrayList<>();
|
||||
|
||||
}
|
||||
@ -27,4 +27,8 @@ public class GetObjectMetaRequest {
|
||||
* 文件url
|
||||
*/
|
||||
private String url;
|
||||
/**
|
||||
* 唯一标识
|
||||
*/
|
||||
private String id;
|
||||
}
|
||||
|
||||
@ -22,4 +22,13 @@ public class GetObjectMetaResponse {
|
||||
* 文件大小
|
||||
*/
|
||||
private long size;
|
||||
|
||||
/**
|
||||
* url
|
||||
*/
|
||||
private String url;
|
||||
/**
|
||||
* 唯一标识
|
||||
*/
|
||||
private String id;
|
||||
}
|
||||
|
||||
@ -14,7 +14,7 @@ public interface AliOssService extends BaseS3Service {
|
||||
/**
|
||||
* 授权给第三方下载
|
||||
*/
|
||||
String downloadSignUrl(String bucketName, String key, Long expireSecond, String fileName);
|
||||
String downloadSignUrl(String bucketName, String key, Long expireSecond, String fileName, boolean hasFileName);
|
||||
|
||||
/**
|
||||
* 授权给第三方-上传
|
||||
@ -31,6 +31,12 @@ public interface AliOssService extends BaseS3Service {
|
||||
*/
|
||||
String getUrl(String bucketName, String tgtFileKey);
|
||||
|
||||
/**
|
||||
* 通过url上传至OBS
|
||||
*/
|
||||
String uploadByUrl(String bucketName, String tgtFileKey, String fileName, String url);
|
||||
|
||||
|
||||
/**
|
||||
* 元数据
|
||||
*/
|
||||
|
||||
@ -289,7 +289,7 @@ public class AliOssServiceImpl implements AliOssService {
|
||||
* 授权给第三方-下载
|
||||
*/
|
||||
@Override
|
||||
public String downloadSignUrl(String bucketName, String key, Long expireSecond, String fileName) {
|
||||
public String downloadSignUrl(String bucketName, String key, Long expireSecond, String fileName, boolean hasFileName) {
|
||||
Date date = new Date();
|
||||
date.setTime(date.getTime() + expireSecond * 1000);
|
||||
try {
|
||||
@ -297,7 +297,9 @@ public class AliOssServiceImpl implements AliOssService {
|
||||
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.GET);
|
||||
request.setExpiration(date);
|
||||
ResponseHeaderOverrides responseHeaderOverrides = new ResponseHeaderOverrides();
|
||||
responseHeaderOverrides.setContentDisposition("attachment;filename=\"" + URLEncoder.encode(fileName, "utf-8") + "\"");
|
||||
if (hasFileName) {
|
||||
responseHeaderOverrides.setContentDisposition("attachment;filename=\"" + URLEncoder.encode(fileName, "utf-8") + "\"");
|
||||
}
|
||||
request.setResponseHeaders(responseHeaderOverrides);
|
||||
URL url = aliOssClient.getClient().generatePresignedUrl(request);
|
||||
log.info("aliyun downloadSignUrl result, bucketName:{}, key:{}, url:{}", bucketName, key, JsonUtil.obj2Str(url));
|
||||
@ -361,6 +363,31 @@ public class AliOssServiceImpl implements AliOssService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过url上传至OSS
|
||||
*/
|
||||
@Override
|
||||
public String uploadByUrl(String bucketName, String tgtFileKey, String fileName, String url) {
|
||||
OSS client = aliOssClient.getClient();
|
||||
try {
|
||||
InputStream srcStream = new URL(url).openStream();
|
||||
// 创建上传文件的元信息,通过文件云信息设置HTTP Header
|
||||
ObjectMetadata metadata = new ObjectMetadata();
|
||||
// 设置内容被下载时的名称
|
||||
metadata.setContentDisposition("attachment;filename=\"" + URLEncoder.encode(fileName, "utf-8") + "\"");
|
||||
metadata.setContentEncoding("utf-8");
|
||||
client.putObject(bucketName, tgtFileKey, srcStream, metadata);
|
||||
} catch (OSSException e) {
|
||||
LogUtil.error("uploadByStream OSSException", e);
|
||||
return "";
|
||||
} catch (Exception e) {
|
||||
LogUtil.error("uploadByStream ClientException", e);
|
||||
return "";
|
||||
}
|
||||
|
||||
return getUrl(bucketName, tgtFileKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 元数据
|
||||
*/
|
||||
|
||||
@ -28,4 +28,8 @@ public class ManaGetObjectMetaResponse {
|
||||
* 文件大小
|
||||
*/
|
||||
private long size;
|
||||
/**
|
||||
* url
|
||||
*/
|
||||
private String url;
|
||||
}
|
||||
|
||||
@ -91,5 +91,5 @@ public interface FileService {
|
||||
/**
|
||||
* 元数据
|
||||
*/
|
||||
ManaGetObjectMetaResponse getObjectMeta(String bucketName, String key, String channelCode);
|
||||
ManaGetObjectMetaResponse getObjectMeta(String bucketName, String key, String url,String channelCode);
|
||||
}
|
||||
|
||||
@ -1133,10 +1133,11 @@ public class FileServiceImpl implements FileService {
|
||||
* 元数据
|
||||
*/
|
||||
@Override
|
||||
public ManaGetObjectMetaResponse getObjectMeta(String bucketName, String key, String channelCode) {
|
||||
public ManaGetObjectMetaResponse getObjectMeta(String bucketName, String key, String url, String channelCode) {
|
||||
log.info("getObjectMeta bucketName:{},key:{},channelCode:{}", bucketName, key, channelCode);
|
||||
Long start = System.currentTimeMillis();
|
||||
ManaGetObjectMetaResponse response = fileManager.getObjectMeta(bucketName, key, channelCode);
|
||||
response.setUrl(url);
|
||||
log.info("getObjectMeta response:{}, times:{}", JsonUtil.obj2Str(response), System.currentTimeMillis() - start);
|
||||
return response;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user