Commit 79aea4cd by gdj

修改oss配置

parent 04c142d5
......@@ -26,6 +26,39 @@ public class IPUtils {
private static final String LOCALHOST_IP = "0:0:0:0:0:0:0:1";
private static final String LOCALHOST_IP1 = "127.0.0.1";
public static boolean isLanIp() {
String ipAddr = getIpAddr();
logger.info("isLanIp getIpAddr(): {} ", ipAddr);
return isLanIp(ipAddr);
}
public static boolean isLanIp(String ip) {
InetAddress inet = null;
try {
inet = InetAddress.getByName(ip);
} catch (UnknownHostException e) {
// e.printStackTrace();
return false;
}
byte[] addr = inet.getAddress();
int first = addr[0] & 0xFF;
int second = addr[1] & 0xFF;
if (first == 10) {
return true; // 10.0.0.0/8
}
if (first == 172 && (second >= 16 && second <= 31)) {
return true; // 172.16.0.0 - 172.31.255.255
}
if (first == 192 && second == 168) {
return true; // 192.168.0.0/16
}
if (first == 127) {
return true; // 本地环回
}
return false;
}
public static String getIpAddr() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
......
......@@ -28,6 +28,8 @@ public class OssConfiguration {
*/
public static String endpoint;
public static String localEndpoint;
public static String accessKey;
public static String secretKey;
......@@ -56,6 +58,10 @@ public class OssConfiguration {
OssConfiguration.endpoint = endpoint;
}
public void setLocalEndpoint(String localEndpoint) {
OssConfiguration.localEndpoint = localEndpoint;
}
public void setAccessKey(String accessKey) {
OssConfiguration.accessKey = accessKey;
}
......
......@@ -30,6 +30,20 @@ public interface IOssService {
URL getObjectUrl(String bucket, String objectKey);
/**
* Get the local address of the object based on the bucket name and the object name.
* @param bucket bucket name
* @param objectKey object name
* @return download link
*/
URL getObjectLocalUrl(String bucket, String objectKey);
/**
* Get the net address of the object based on the bucket name and the object name.
* @param bucket bucket name
* @param objectKey object name
* @return download link
*/
URL getObjectNetUrl(String bucket, String objectKey);
/**
* Deletes the object in the storage bucket.
* @param bucket
* @param objectKey
......
......@@ -75,6 +75,30 @@ public class AliyunOssServiceImpl implements IOssService {
new Date(System.currentTimeMillis() + OssConfiguration.expire * 1000));
}
/**
* Get the local address of the object based on the bucket name and the object name.
*
* @param bucket bucket name
* @param objectKey object name
* @return download link
*/
@Override
public URL getObjectLocalUrl(String bucket, String objectKey) {
return null;
}
/**
* Get the net address of the object based on the bucket name and the object name.
*
* @param bucket bucket name
* @param objectKey object name
* @return download link
*/
@Override
public URL getObjectNetUrl(String bucket, String objectKey) {
return null;
}
@Override
public Boolean deleteObject(String bucket, String objectKey) {
if (!ossClient.doesObjectExist(bucket, objectKey)) {
......
......@@ -66,6 +66,30 @@ public class AmazonS3ServiceImpl implements IOssService {
new Date(System.currentTimeMillis() + OssConfiguration.expire * 1000), HttpMethod.GET);
}
/**
* Get the local address of the object based on the bucket name and the object name.
*
* @param bucket bucket name
* @param objectKey object name
* @return download link
*/
@Override
public URL getObjectLocalUrl(String bucket, String objectKey) {
return null;
}
/**
* Get the net address of the object based on the bucket name and the object name.
*
* @param bucket bucket name
* @param objectKey object name
* @return download link
*/
@Override
public URL getObjectNetUrl(String bucket, String objectKey) {
return null;
}
@Override
public Boolean deleteObject(String bucket, String objectKey) {
if (!client.doesObjectExist(bucket, objectKey)) {
......
......@@ -20,6 +20,8 @@ import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;
import static com.dji.sample.common.util.IPUtils.isLanIp;
/**
* @author sean
* @version 0.3
......@@ -30,7 +32,9 @@ import java.util.Objects;
public class MinIOServiceImpl implements IOssService {
private MinioClient client;
private MinioClient localClient;
@Override
public OssTypeEnum getOssType() {
return OssTypeEnum.MINIO;
......@@ -38,8 +42,16 @@ public class MinIOServiceImpl implements IOssService {
@Override
public CredentialsToken getCredentials() {
// 根据ip 是否局域网
boolean isLanIp = isLanIp();
String endpoint;
if (isLanIp) {
endpoint = OssConfiguration.endpoint;
} else {
endpoint = OssConfiguration.localEndpoint;
}
try {
AssumeRoleProvider provider = new AssumeRoleProvider(OssConfiguration.endpoint, OssConfiguration.accessKey,
AssumeRoleProvider provider = new AssumeRoleProvider(endpoint, OssConfiguration.accessKey,
OssConfiguration.secretKey, Math.toIntExact(OssConfiguration.expire),
null, OssConfiguration.region, null, null, null, null);
Credentials credential = provider.fetch();
......@@ -53,15 +65,37 @@ public class MinIOServiceImpl implements IOssService {
@Override
public URL getObjectUrl(String bucket, String objectKey) {
// 根据ip 是否局域网
boolean isLanIp = isLanIp();
MinioClient client;
if (isLanIp) {
client = this.localClient;
} else {
client = this.client;
}
return getObjectUrl(client, bucket, objectKey);
}
@Override
public URL getObjectLocalUrl(String bucket, String objectKey) {
return getObjectUrl(this.localClient, bucket, objectKey);
}
@Override
public URL getObjectNetUrl(String bucket, String objectKey) {
return getObjectUrl(this.client, bucket, objectKey);
}
public URL getObjectUrl(MinioClient client, String bucket, String objectKey) {
try {
return new URL(
client.getPresignedObjectUrl(
GetPresignedObjectUrlArgs.builder()
.method(Method.GET)
.bucket(bucket)
.object(objectKey)
.expiry(Math.toIntExact(OssConfiguration.expire))
.build()));
GetPresignedObjectUrlArgs.builder()
.method(Method.GET)
.bucket(bucket)
.object(objectKey)
.expiry(Math.toIntExact(OssConfiguration.expire))
.build()));
} catch (ErrorResponseException | InsufficientDataException | InternalException |
InvalidKeyException | InvalidResponseException | IOException |
NoSuchAlgorithmException | XmlParserException | ServerException e) {
......@@ -71,6 +105,14 @@ public class MinIOServiceImpl implements IOssService {
@Override
public Boolean deleteObject(String bucket, String objectKey) {
// 根据ip 是否局域网
boolean isLanIp = isLanIp();
MinioClient client;
if (isLanIp) {
client = this.localClient;
} else {
client = this.client;
}
try {
client.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(objectKey).build());
} catch (MinioException | NoSuchAlgorithmException | IOException | InvalidKeyException e) {
......@@ -83,6 +125,14 @@ public class MinIOServiceImpl implements IOssService {
@Override
public InputStream getObject(String bucket, String objectKey) {
// 根据ip 是否局域网
boolean isLanIp = isLanIp();
MinioClient client;
if (isLanIp) {
client = this.localClient;
} else {
client = this.client;
}
try {
GetObjectResponse object = client.getObject(GetObjectArgs.builder().bucket(bucket).object(objectKey).build());
return new ByteArrayInputStream(object.readAllBytes());
......@@ -94,6 +144,14 @@ public class MinIOServiceImpl implements IOssService {
@Override
public void putObject(String bucket, String objectKey, InputStream input) {
// 根据ip 是否局域网
boolean isLanIp = isLanIp();
MinioClient client;
if (isLanIp) {
client = this.localClient;
} else {
client = this.client;
}
try {
client.statObject(StatObjectArgs.builder().bucket(bucket).object(objectKey).build());
throw new RuntimeException("The filename already exists.");
......@@ -112,12 +170,26 @@ public class MinIOServiceImpl implements IOssService {
public void createClient() {
if (Objects.nonNull(this.client)) {
return;
} else {
this.client = MinioClient.builder()
.endpoint(OssConfiguration.endpoint)
.credentials(OssConfiguration.accessKey, OssConfiguration.secretKey)
.region(OssConfiguration.region)
.build();
}
this.client = MinioClient.builder()
.endpoint(OssConfiguration.endpoint)
.credentials(OssConfiguration.accessKey, OssConfiguration.secretKey)
.region(OssConfiguration.region)
.build();
// 增加 本地连接
try {
if (Objects.nonNull(this.localClient)) {
} else {
this.localClient = MinioClient.builder()
.endpoint(OssConfiguration.localEndpoint)
.credentials(OssConfiguration.accessKey, OssConfiguration.secretKey)
.region(OssConfiguration.region)
.build();
}
} catch (Exception e) {
log.error("createClient Exception: ", e);
}
}
}
......@@ -49,6 +49,18 @@ public class OssServiceContext {
}
return this.ossService.getObjectUrl(bucket, objectKey);
}
public URL getObjectLocalUrl(String bucket, String objectKey) {
if (!StringUtils.hasText(bucket) || !StringUtils.hasText(objectKey)) {
throw new IllegalArgumentException();
}
return this.ossService.getObjectLocalUrl(bucket, objectKey);
}
public URL getObjectNetUrl(String bucket, String objectKey) {
if (!StringUtils.hasText(bucket) || !StringUtils.hasText(objectKey)) {
throw new IllegalArgumentException();
}
return this.ossService.getObjectNetUrl(bucket, objectKey);
}
public Boolean deleteObject(String bucket, String objectKey) {
return this.ossService.deleteObject(bucket, objectKey);
......
......@@ -305,7 +305,7 @@ public class FlightAreaServiceImpl extends AbstractFlightAreaService implements
.setName(file.getName())
.setSize(file.getSize())
.setChecksum(file.getSign())
.setUrl(ossServiceContext.getObjectUrl(OssConfiguration.bucket, file.getObjectKey()).toString())
.setUrl(ossServiceContext.getObjectNetUrl(OssConfiguration.bucket, file.getObjectKey()).toString())
))));
}
......
......@@ -47,6 +47,21 @@ public interface IWaylineFileService extends IService<WaylineFileEntity> {
URL getObjectUrl(String workspaceId, String waylineId) throws SQLException;
/**
* Get the download address of the file object.
* @param workspaceId
* @param waylineId
* @return
*/
URL getObjectLocalUrl(String workspaceId, String waylineId) throws SQLException;
/**
* Get the download address of the file object.
* @param workspaceId
* @param waylineId
* @return
*/
URL getObjectNetUrl(String workspaceId, String waylineId) throws SQLException;
/**
* Save the basic information of the wayline file.
* @param workspaceId
* @param metadata
......
......@@ -325,7 +325,7 @@ public class FlightTaskServiceImpl extends AbstractWaylineService implements IFl
}
// get file url
URL url = waylineFileService.getObjectUrl(waylineJob.getWorkspaceId(), waylineFile.get().getId());
URL url = waylineFileService.getObjectNetUrl(waylineJob.getWorkspaceId(), waylineFile.get().getId());
FlighttaskPrepareRequest flightTask = new FlighttaskPrepareRequest()
.setFlightId(waylineJob.getJobId())
......
......@@ -145,7 +145,7 @@ public class SDKWaylineService extends AbstractWaylineService {
}
// get file url
try {
URL url = waylineFileService.getObjectUrl(waylineJob.getWorkspaceId(), waylineFile.get().getId());
URL url = waylineFileService.getObjectNetUrl(waylineJob.getWorkspaceId(), waylineFile.get().getId());
return new TopicRequestsResponse<MqttReply<FlighttaskResourceGetResponse>>().setData(
MqttReply.success(new FlighttaskResourceGetResponse()
.setFile(new FlighttaskFile()
......
......@@ -144,6 +144,24 @@ public class WaylineFileServiceImpl extends ServiceImpl<IWaylineFileMapper, Wayl
}
@Override
public URL getObjectLocalUrl(String workspaceId, String waylineId) throws SQLException {
Optional<GetWaylineListResponse> waylineOpt = this.getWaylineByWaylineId(workspaceId, waylineId);
if (waylineOpt.isEmpty()) {
throw new SQLException(waylineId + " does not exist.");
}
return ossService.getObjectLocalUrl(OssConfiguration.bucket, waylineOpt.get().getObjectKey());
}
@Override
public URL getObjectNetUrl(String workspaceId, String waylineId) throws SQLException {
Optional<GetWaylineListResponse> waylineOpt = this.getWaylineByWaylineId(workspaceId, waylineId);
if (waylineOpt.isEmpty()) {
throw new SQLException(waylineId + " does not exist.");
}
return ossService.getObjectNetUrl(OssConfiguration.bucket, waylineOpt.get().getObjectKey());
}
@Override
public Integer saveWaylineFile(String workspaceId, WaylineFileDTO metadata) {
// 航线文件 一定是文件
metadata.setIsDir(WaylineFileTypeEnum.FILE.getVal());
......
......@@ -168,6 +168,7 @@ oss:
# endpoint: https://gt7-oss.geotwin.cc
# 深圳
endpoint: https://gt-oss-dev.geotwin.cn
local-endpoint: http://10.0.8.72:9000
access-key: minioadmin
secret-key: minioadmin
bucket: gtfly
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment