华为云obs;

This commit is contained in:
王奎兴
2025-12-26 15:35:12 +08:00
parent 3ea4bb8170
commit be97110b3c
3 changed files with 57 additions and 5 deletions
@@ -1,5 +1,7 @@
package com.linke.product.infrastructure.obs;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
public interface FileStorageService {
@@ -9,6 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
@@ -16,8 +17,8 @@ import java.util.UUID;
@Service
public class HuaweiObsService implements FileStorageService {
@Autowired
private ObsClient obsClient;
/*@Autowired
private ObsClient obsClient;*/
@Autowired
private ObsProperties obsProperties;
@@ -41,7 +42,9 @@ public class HuaweiObsService implements FileStorageService {
@Override
public String uploadFile(String fileKey, MultipartFile file) {
ObsClient obsClient = null;
try {
obsClient = new ObsClient(obsProperties.getAccessKey(), obsProperties.getSecretKey(), obsProperties.getEndpoint());
// 上传文件到OBS
obsClient.putObject(bucketName, fileKey, file.getInputStream());
@@ -50,6 +53,14 @@ public class HuaweiObsService implements FileStorageService {
} catch (Exception e) {
log.error("文件上传失败: {}", fileKey, e);
throw new RuntimeException("文件上传失败", e);
} finally {
if (obsClient != null) {
try {
obsClient.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
@@ -62,7 +73,9 @@ public class HuaweiObsService implements FileStorageService {
* 生成指定有效期的临时URL
*/
public String generateTemporaryUrl(String objectKey, long expireSeconds) {
ObsClient obsClient = null;
try {
obsClient = new ObsClient(obsProperties.getAccessKey(), obsProperties.getSecretKey(), obsProperties.getEndpoint());
TemporarySignatureRequest request = new TemporarySignatureRequest(
HttpMethodEnum.GET,
expireSeconds
@@ -71,40 +84,77 @@ public class HuaweiObsService implements FileStorageService {
request.setObjectKey(objectKey);
return obsClient.createTemporarySignature(request).getSignedUrl();
} finally {
} finally {
if (obsClient != null) {
try {
obsClient.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
@Override
public InputStream downloadFile(String fileKey) {
ObsClient obsClient = null;
try {
obsClient = new ObsClient(obsProperties.getAccessKey(), obsProperties.getSecretKey(), obsProperties.getEndpoint());
ObsObject obsObject = obsClient.getObject(bucketName, fileKey);
return obsObject.getObjectContent();
} catch (Exception e) {
log.error("文件下载失败: {}", fileKey, e);
throw new RuntimeException("文件下载失败", e);
} finally {
if (obsClient != null) {
try {
obsClient.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
@Override
public boolean deleteFile(String fileKey) {
ObsClient obsClient = null;
try {
obsClient = new ObsClient(obsProperties.getAccessKey(), obsProperties.getSecretKey(), obsProperties.getEndpoint());
obsClient.deleteObject(bucketName, fileKey);
log.info("文件删除成功: {}", fileKey);
return true;
} catch (Exception e) {
log.error("文件删除失败: {}", fileKey, e);
return false;
} finally {
if (obsClient != null) {
try {
obsClient.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
@Override
public boolean fileExists(String fileKey) {
ObsClient obsClient = null;
try {
obsClient = new ObsClient(obsProperties.getAccessKey(), obsProperties.getSecretKey(), obsProperties.getEndpoint());
return obsClient.doesObjectExist(bucketName, fileKey);
} catch (Exception e) {
log.error("检查文件存在失败: {}", fileKey, e);
return false;
} finally {
if (obsClient != null) {
try {
obsClient.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
@@ -357,8 +357,8 @@ public class MenuApi extends BaseController {
outputStream.flush();
}
} finally {
if (obsClient != null) {
obsClient.close();
if (obsClient1 != null) {
obsClient1.close();
}
}
});