华为云obs;
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package com.linke.product.infrastructure.obs;
|
package com.linke.product.infrastructure.obs;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
public interface FileStorageService {
|
public interface FileStorageService {
|
||||||
|
|||||||
+52
-2
@@ -9,6 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@@ -16,8 +17,8 @@ import java.util.UUID;
|
|||||||
@Service
|
@Service
|
||||||
public class HuaweiObsService implements FileStorageService {
|
public class HuaweiObsService implements FileStorageService {
|
||||||
|
|
||||||
@Autowired
|
/*@Autowired
|
||||||
private ObsClient obsClient;
|
private ObsClient obsClient;*/
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ObsProperties obsProperties;
|
private ObsProperties obsProperties;
|
||||||
@@ -41,7 +42,9 @@ public class HuaweiObsService implements FileStorageService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String uploadFile(String fileKey, MultipartFile file) {
|
public String uploadFile(String fileKey, MultipartFile file) {
|
||||||
|
ObsClient obsClient = null;
|
||||||
try {
|
try {
|
||||||
|
obsClient = new ObsClient(obsProperties.getAccessKey(), obsProperties.getSecretKey(), obsProperties.getEndpoint());
|
||||||
// 上传文件到OBS
|
// 上传文件到OBS
|
||||||
obsClient.putObject(bucketName, fileKey, file.getInputStream());
|
obsClient.putObject(bucketName, fileKey, file.getInputStream());
|
||||||
|
|
||||||
@@ -50,6 +53,14 @@ public class HuaweiObsService implements FileStorageService {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("文件上传失败: {}", fileKey, e);
|
log.error("文件上传失败: {}", fileKey, e);
|
||||||
throw new RuntimeException("文件上传失败", 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
|
* 生成指定有效期的临时URL
|
||||||
*/
|
*/
|
||||||
public String generateTemporaryUrl(String objectKey, long expireSeconds) {
|
public String generateTemporaryUrl(String objectKey, long expireSeconds) {
|
||||||
|
ObsClient obsClient = null;
|
||||||
try {
|
try {
|
||||||
|
obsClient = new ObsClient(obsProperties.getAccessKey(), obsProperties.getSecretKey(), obsProperties.getEndpoint());
|
||||||
TemporarySignatureRequest request = new TemporarySignatureRequest(
|
TemporarySignatureRequest request = new TemporarySignatureRequest(
|
||||||
HttpMethodEnum.GET,
|
HttpMethodEnum.GET,
|
||||||
expireSeconds
|
expireSeconds
|
||||||
@@ -72,39 +85,76 @@ public class HuaweiObsService implements FileStorageService {
|
|||||||
|
|
||||||
return obsClient.createTemporarySignature(request).getSignedUrl();
|
return obsClient.createTemporarySignature(request).getSignedUrl();
|
||||||
} finally {
|
} finally {
|
||||||
|
if (obsClient != null) {
|
||||||
|
try {
|
||||||
|
obsClient.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InputStream downloadFile(String fileKey) {
|
public InputStream downloadFile(String fileKey) {
|
||||||
|
ObsClient obsClient = null;
|
||||||
try {
|
try {
|
||||||
|
obsClient = new ObsClient(obsProperties.getAccessKey(), obsProperties.getSecretKey(), obsProperties.getEndpoint());
|
||||||
ObsObject obsObject = obsClient.getObject(bucketName, fileKey);
|
ObsObject obsObject = obsClient.getObject(bucketName, fileKey);
|
||||||
return obsObject.getObjectContent();
|
return obsObject.getObjectContent();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("文件下载失败: {}", fileKey, e);
|
log.error("文件下载失败: {}", fileKey, e);
|
||||||
throw new RuntimeException("文件下载失败", e);
|
throw new RuntimeException("文件下载失败", e);
|
||||||
|
} finally {
|
||||||
|
if (obsClient != null) {
|
||||||
|
try {
|
||||||
|
obsClient.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean deleteFile(String fileKey) {
|
public boolean deleteFile(String fileKey) {
|
||||||
|
ObsClient obsClient = null;
|
||||||
try {
|
try {
|
||||||
|
obsClient = new ObsClient(obsProperties.getAccessKey(), obsProperties.getSecretKey(), obsProperties.getEndpoint());
|
||||||
obsClient.deleteObject(bucketName, fileKey);
|
obsClient.deleteObject(bucketName, fileKey);
|
||||||
log.info("文件删除成功: {}", fileKey);
|
log.info("文件删除成功: {}", fileKey);
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("文件删除失败: {}", fileKey, e);
|
log.error("文件删除失败: {}", fileKey, e);
|
||||||
return false;
|
return false;
|
||||||
|
} finally {
|
||||||
|
if (obsClient != null) {
|
||||||
|
try {
|
||||||
|
obsClient.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean fileExists(String fileKey) {
|
public boolean fileExists(String fileKey) {
|
||||||
|
ObsClient obsClient = null;
|
||||||
try {
|
try {
|
||||||
|
obsClient = new ObsClient(obsProperties.getAccessKey(), obsProperties.getSecretKey(), obsProperties.getEndpoint());
|
||||||
return obsClient.doesObjectExist(bucketName, fileKey);
|
return obsClient.doesObjectExist(bucketName, fileKey);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("检查文件存在失败: {}", fileKey, e);
|
log.error("检查文件存在失败: {}", fileKey, e);
|
||||||
return false;
|
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();
|
outputStream.flush();
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (obsClient != null) {
|
if (obsClient1 != null) {
|
||||||
obsClient.close();
|
obsClient1.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user