图片在公有桶中获取

pull/1121/head
梁宇奇 2025-07-11 19:23:02 +08:00
parent bd0c2b09e9
commit 9bded7ff2b
5 changed files with 42 additions and 43 deletions

View File

@ -33,6 +33,7 @@ public class OSSAutoConfiguration {
configuration.setAccessKeyId(properties.getAccessKeyId());
configuration.setAccessKeySecret(properties.getAccessKeySecret());
configuration.setBucketName(properties.getBucketName());
configuration.setPublicBucketName(properties.getPublicBucketName());
configuration.setRegionId(properties.getRegionId());
configuration.setRoleArn(properties.getRoleArn());
configuration.setHttps(properties.isHttps());

View File

@ -70,9 +70,9 @@ public class OSSClientWrapper {
* @param key key
* @param inputStream
*/
public void upload(String key, InputStream inputStream)
public void upload(String bucketName, String key, InputStream inputStream)
throws Exception {
upload(key, inputStream, null);
upload(bucketName, key, inputStream, null);
}
/**
@ -82,20 +82,21 @@ public class OSSClientWrapper {
* @param inputStream
* @param contentType
*/
public void upload(String key, InputStream inputStream, String contentType)
public void upload(String bucketName, String key, InputStream inputStream, String contentType)
throws Exception {
upload(key, inputStream, contentType, null);
upload(bucketName, key, inputStream, contentType, null);
}
/**
*
*
* @param bucketName bucketName
* @param key key
* @param inputStream
* @param contentType
* @param contentDisposition
*/
public void upload(String key, InputStream inputStream, String contentType, String contentDisposition) throws Exception {
public void upload(String bucketName, String key, InputStream inputStream, String contentType, String contentDisposition) throws Exception {
ObjectMetadata objectMeta = new ObjectMetadata();
objectMeta.setContentLength(inputStream.available());
// 可以在metadata中标记文件类型
@ -105,8 +106,8 @@ public class OSSClientWrapper {
objectMeta.setContentType(contentType);
// 设置下载名
objectMeta.setContentDisposition(contentDisposition);
client.putObject(configuration.getBucketName(), key, inputStream, objectMeta);
if (!client.doesObjectExist(configuration.getBucketName(), key)) {
client.putObject(bucketName, key, inputStream, objectMeta);
if (!client.doesObjectExist(bucketName, key)) {
throw new IllegalStateException("文件上传失败");
}
}
@ -117,15 +118,15 @@ public class OSSClientWrapper {
* @param sourceKey KEY
* @param targetKey KEY
*/
public void move(String sourceKey, String targetKey) {
if (!client.doesObjectExist(configuration.getBucketName(), sourceKey)) {
public void move(String bucketName, String sourceKey, String targetKey) {
if (!client.doesObjectExist(bucketName, sourceKey)) {
throw new IllegalStateException("源文件信息不存在");
}
client.copyObject(configuration.getBucketName(), sourceKey, configuration.getBucketName(), targetKey);
if (!client.doesObjectExist(configuration.getBucketName(), targetKey)) {
client.copyObject(bucketName, sourceKey, bucketName, targetKey);
if (!client.doesObjectExist(bucketName, targetKey)) {
throw new IllegalStateException("移动文件失败[1]");
}
if (!this.delete(sourceKey)) {
if (!this.delete(bucketName, sourceKey)) {
throw new IllegalStateException("移动文件失败[2]");
}
}
@ -133,25 +134,16 @@ public class OSSClientWrapper {
/**
* key访
*
* @param key key
* @param expireTime
*/
public URL generateUrl(String key, Long expireTime) throws Exception {
return generateUrl(key, expireTime, false);
}
/**
* key访
*
* @param bucketName bucketName
* @param key key
* @param expireTime
* @param down url
*/
public URL generateUrl(String key, Long expireTime, boolean down) throws Exception {
public URL generateUrl(String bucketName, String key, Long expireTime, boolean down) throws Exception {
Assert.notNull(expireTime, "过期时间为空");
GeneratePresignedUrlRequest request;
Date expiration = new Date(System.currentTimeMillis() + expireTime);
request = new GeneratePresignedUrlRequest(this.configuration.getBucketName(), key);
request = new GeneratePresignedUrlRequest(bucketName, key);
request.setExpiration(expiration);
request.setMethod(HttpMethod.GET);
ResponseHeaderOverrides header = new ResponseHeaderOverrides();
@ -164,12 +156,12 @@ public class OSSClientWrapper {
return this.client.generatePresignedUrl(request);
}
public String generateBase64(String key) {
public String generateBase64(String bucketName, String key) {
if (key == null) {
return null;
}
try {
OSSObject ossObject = this.client.getObject(this.configuration.getBucketName(), key);
OSSObject ossObject = this.client.getObject(bucketName, key);
if (ossObject == null) {
return null;
}
@ -242,17 +234,17 @@ public class OSSClientWrapper {
return "image/jpeg";
}
public InputStream getObject(String key) {
OSSObject ossObject = client.getObject(configuration.getBucketName(), key);
public InputStream getObject(String bucketName, String key) {
OSSObject ossObject = client.getObject(bucketName, key);
return ossObject.getObjectContent();
}
/**
*
*/
public void download(String key, String filename)
public void download(String bucketName, String key, String filename)
throws OSSException, ClientException {
client.getObject(new GetObjectRequest(configuration.getBucketName(), key), new File(filename));
client.getObject(new GetObjectRequest(bucketName, key), new File(filename));
}
/**
@ -260,11 +252,11 @@ public class OSSClientWrapper {
*
* @return true false
*/
public boolean delete(String key) {
public boolean delete(String bucketName, String key) {
//检查是否是有效文件
boolean exists = client.doesObjectExist(configuration.getBucketName(), key);
boolean exists = client.doesObjectExist(bucketName, key);
if (exists) {
client.deleteObject(configuration.getBucketName(), key);
client.deleteObject(bucketName, key);
}
return exists;
}

View File

@ -12,6 +12,7 @@ public class OSSConfiguration {
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
private String publicBucketName;
private String regionId;
private String roleArn;
private boolean https;
@ -24,6 +25,7 @@ public class OSSConfiguration {
configuration.setAccessKeyId(this.accessKeyId);
configuration.setAccessKeySecret(this.getAccessKeySecret());
configuration.setBucketName(this.bucketName);
configuration.setPublicBucketName(this.publicBucketName);
configuration.setRegionId(this.regionId);
configuration.setRoleArn(this.roleArn);
configuration.setHttps(this.https);

View File

@ -14,7 +14,7 @@ import java.util.List;
public interface IPictureService {
/**
*
*
*
* @param key
* @return
@ -22,7 +22,7 @@ public interface IPictureService {
PicZipDTO processPicZip(String key);
/**
*
*
*
* @param productPicSyncDTO
* @return
@ -30,7 +30,7 @@ public interface IPictureService {
ProductPicSyncResultDTO sync2ImgSearchServer(ProductPicSyncDTO productPicSyncDTO);
/**
*
*
*
* @param picKey
* @param num

View File

@ -50,9 +50,10 @@ public class PictureServiceImpl implements IPictureService {
@Override
public PicZipDTO processPicZip(String key) {
String bucketName = ossClient.getConfiguration().getBucketName();
checkProcessAccess(key);
//下载至系统临时文件夹
DownloadResultDTO downloadResult = downloadFile2TempDir(key);
DownloadResultDTO downloadResult = downloadFile2TempDir(bucketName, key);
String tempDirPath = downloadResult.getTempDirPath();
try {
String midDirName = "unzip_" + downloadResult.getTempDirName();
@ -106,10 +107,10 @@ public class PictureServiceImpl implements IPictureService {
String keyOf450 = StrUtil.replaceLast(key, downloadResult.getFileName(), nameOf450);
String keyOf750 = StrUtil.replaceLast(key, downloadResult.getFileName(), nameOf750);
try (InputStream is = new FileInputStream(pathOf450)) {
ossClient.upload(keyOf450, is);
ossClient.upload(bucketName, keyOf450, is);
}
try (InputStream is = new FileInputStream(pathOf750)) {
ossClient.upload(keyOf750, is);
ossClient.upload(bucketName, keyOf750, is);
}
return new PicZipDTO(key, keyOf450, keyOf750);
} catch (Exception e) {
@ -124,6 +125,7 @@ public class PictureServiceImpl implements IPictureService {
@Override
public ProductPicSyncResultDTO sync2ImgSearchServer(ProductPicSyncDTO productPicSyncDTO) {
Assert.notNull(productPicSyncDTO);
String bucketName = ossClient.getConfiguration().getPublicBucketName();
String productId = productPicSyncDTO.getStoreProductId().toString();
//删除商品图片
boolean allSuccess = imgSearchClient.deleteImg(productId);
@ -135,7 +137,7 @@ public class PictureServiceImpl implements IPictureService {
imgAdd.setPicName(FileUtil.getName(picKey));
imgAdd.setCategoryId(Constants.IMG_SEARCH_CATEGORY_ID);
try {
imgAdd.setPicInputStream(ossClient.getObject(picKey));
imgAdd.setPicInputStream(ossClient.getObject(bucketName, picKey));
} catch (Exception e) {
log.error("获取图片流异常: " + picKey, e);
allSuccess = false;
@ -155,6 +157,7 @@ public class PictureServiceImpl implements IPictureService {
@Override
public List<ProductMatchDTO> searchProductByPicKey(String picKey, Integer num) {
Assert.notEmpty(picKey);
String bucketName = ossClient.getConfiguration().getPublicBucketName();
if (num == null || num < 0) {
//默认返回30条数据
num = Constants.IMG_SEARCH_DEFAULT_REQUEST_NUM;
@ -168,7 +171,7 @@ public class PictureServiceImpl implements IPictureService {
imgSearchReq.setStart(i);
imgSearchReq.setDistinctProductId(true);
try {
imgSearchReq.setPicInputStream(ossClient.getObject(picKey));
imgSearchReq.setPicInputStream(ossClient.getObject(bucketName, picKey));
} catch (Exception e) {
log.error("获取图片流异常: " + picKey, e);
return rtnList;
@ -216,10 +219,11 @@ public class PictureServiceImpl implements IPictureService {
/**
* OSS
*
* @param bucketName
* @param ossKey
* @return
*/
private DownloadResultDTO downloadFile2TempDir(String ossKey) {
private DownloadResultDTO downloadFile2TempDir(String bucketName, String ossKey) {
Assert.notEmpty("ossKey不能为空");
try {
String baseTempDir = ossProperties.getTempDir();
@ -229,7 +233,7 @@ public class PictureServiceImpl implements IPictureService {
FileUtil.mkdir(tempDirPath);
String fileName = FileNameUtil.getName(ossKey);
String filePath = tempDirPath + fileName;
ossClient.download(ossKey, filePath);
ossClient.download(bucketName, ossKey, filePath);
return DownloadResultDTO
.builder()
.fileName(fileName)