Commit 79aea4cd by gdj

修改oss配置

parent 04c142d5
...@@ -26,6 +26,39 @@ public class IPUtils { ...@@ -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_IP = "0:0:0:0:0:0:0:1";
private static final String LOCALHOST_IP1 = "127.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() { public static String getIpAddr() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
......
...@@ -28,6 +28,8 @@ public class OssConfiguration { ...@@ -28,6 +28,8 @@ public class OssConfiguration {
*/ */
public static String endpoint; public static String endpoint;
public static String localEndpoint;
public static String accessKey; public static String accessKey;
public static String secretKey; public static String secretKey;
...@@ -56,6 +58,10 @@ public class OssConfiguration { ...@@ -56,6 +58,10 @@ public class OssConfiguration {
OssConfiguration.endpoint = endpoint; OssConfiguration.endpoint = endpoint;
} }
public void setLocalEndpoint(String localEndpoint) {
OssConfiguration.localEndpoint = localEndpoint;
}
public void setAccessKey(String accessKey) { public void setAccessKey(String accessKey) {
OssConfiguration.accessKey = accessKey; OssConfiguration.accessKey = accessKey;
} }
......
...@@ -30,6 +30,20 @@ public interface IOssService { ...@@ -30,6 +30,20 @@ public interface IOssService {
URL getObjectUrl(String bucket, String objectKey); 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. * Deletes the object in the storage bucket.
* @param bucket * @param bucket
* @param objectKey * @param objectKey
......
...@@ -75,6 +75,30 @@ public class AliyunOssServiceImpl implements IOssService { ...@@ -75,6 +75,30 @@ public class AliyunOssServiceImpl implements IOssService {
new Date(System.currentTimeMillis() + OssConfiguration.expire * 1000)); 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 @Override
public Boolean deleteObject(String bucket, String objectKey) { public Boolean deleteObject(String bucket, String objectKey) {
if (!ossClient.doesObjectExist(bucket, objectKey)) { if (!ossClient.doesObjectExist(bucket, objectKey)) {
......
...@@ -66,6 +66,30 @@ public class AmazonS3ServiceImpl implements IOssService { ...@@ -66,6 +66,30 @@ public class AmazonS3ServiceImpl implements IOssService {
new Date(System.currentTimeMillis() + OssConfiguration.expire * 1000), HttpMethod.GET); 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 @Override
public Boolean deleteObject(String bucket, String objectKey) { public Boolean deleteObject(String bucket, String objectKey) {
if (!client.doesObjectExist(bucket, objectKey)) { if (!client.doesObjectExist(bucket, objectKey)) {
......
...@@ -20,6 +20,8 @@ import java.security.InvalidKeyException; ...@@ -20,6 +20,8 @@ import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Objects; import java.util.Objects;
import static com.dji.sample.common.util.IPUtils.isLanIp;
/** /**
* @author sean * @author sean
* @version 0.3 * @version 0.3
...@@ -31,6 +33,8 @@ public class MinIOServiceImpl implements IOssService { ...@@ -31,6 +33,8 @@ public class MinIOServiceImpl implements IOssService {
private MinioClient client; private MinioClient client;
private MinioClient localClient;
@Override @Override
public OssTypeEnum getOssType() { public OssTypeEnum getOssType() {
return OssTypeEnum.MINIO; return OssTypeEnum.MINIO;
...@@ -38,8 +42,16 @@ public class MinIOServiceImpl implements IOssService { ...@@ -38,8 +42,16 @@ public class MinIOServiceImpl implements IOssService {
@Override @Override
public CredentialsToken getCredentials() { public CredentialsToken getCredentials() {
// 根据ip 是否局域网
boolean isLanIp = isLanIp();
String endpoint;
if (isLanIp) {
endpoint = OssConfiguration.endpoint;
} else {
endpoint = OssConfiguration.localEndpoint;
}
try { try {
AssumeRoleProvider provider = new AssumeRoleProvider(OssConfiguration.endpoint, OssConfiguration.accessKey, AssumeRoleProvider provider = new AssumeRoleProvider(endpoint, OssConfiguration.accessKey,
OssConfiguration.secretKey, Math.toIntExact(OssConfiguration.expire), OssConfiguration.secretKey, Math.toIntExact(OssConfiguration.expire),
null, OssConfiguration.region, null, null, null, null); null, OssConfiguration.region, null, null, null, null);
Credentials credential = provider.fetch(); Credentials credential = provider.fetch();
...@@ -53,6 +65,28 @@ public class MinIOServiceImpl implements IOssService { ...@@ -53,6 +65,28 @@ public class MinIOServiceImpl implements IOssService {
@Override @Override
public URL getObjectUrl(String bucket, String objectKey) { 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 { try {
return new URL( return new URL(
client.getPresignedObjectUrl( client.getPresignedObjectUrl(
...@@ -71,6 +105,14 @@ public class MinIOServiceImpl implements IOssService { ...@@ -71,6 +105,14 @@ public class MinIOServiceImpl implements IOssService {
@Override @Override
public Boolean deleteObject(String bucket, String objectKey) { public Boolean deleteObject(String bucket, String objectKey) {
// 根据ip 是否局域网
boolean isLanIp = isLanIp();
MinioClient client;
if (isLanIp) {
client = this.localClient;
} else {
client = this.client;
}
try { try {
client.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(objectKey).build()); client.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(objectKey).build());
} catch (MinioException | NoSuchAlgorithmException | IOException | InvalidKeyException e) { } catch (MinioException | NoSuchAlgorithmException | IOException | InvalidKeyException e) {
...@@ -83,6 +125,14 @@ public class MinIOServiceImpl implements IOssService { ...@@ -83,6 +125,14 @@ public class MinIOServiceImpl implements IOssService {
@Override @Override
public InputStream getObject(String bucket, String objectKey) { public InputStream getObject(String bucket, String objectKey) {
// 根据ip 是否局域网
boolean isLanIp = isLanIp();
MinioClient client;
if (isLanIp) {
client = this.localClient;
} else {
client = this.client;
}
try { try {
GetObjectResponse object = client.getObject(GetObjectArgs.builder().bucket(bucket).object(objectKey).build()); GetObjectResponse object = client.getObject(GetObjectArgs.builder().bucket(bucket).object(objectKey).build());
return new ByteArrayInputStream(object.readAllBytes()); return new ByteArrayInputStream(object.readAllBytes());
...@@ -94,6 +144,14 @@ public class MinIOServiceImpl implements IOssService { ...@@ -94,6 +144,14 @@ public class MinIOServiceImpl implements IOssService {
@Override @Override
public void putObject(String bucket, String objectKey, InputStream input) { 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 { try {
client.statObject(StatObjectArgs.builder().bucket(bucket).object(objectKey).build()); client.statObject(StatObjectArgs.builder().bucket(bucket).object(objectKey).build());
throw new RuntimeException("The filename already exists."); throw new RuntimeException("The filename already exists.");
...@@ -112,12 +170,26 @@ public class MinIOServiceImpl implements IOssService { ...@@ -112,12 +170,26 @@ public class MinIOServiceImpl implements IOssService {
public void createClient() { public void createClient() {
if (Objects.nonNull(this.client)) { if (Objects.nonNull(this.client)) {
return; } else {
}
this.client = MinioClient.builder() this.client = MinioClient.builder()
.endpoint(OssConfiguration.endpoint) .endpoint(OssConfiguration.endpoint)
.credentials(OssConfiguration.accessKey, OssConfiguration.secretKey) .credentials(OssConfiguration.accessKey, OssConfiguration.secretKey)
.region(OssConfiguration.region) .region(OssConfiguration.region)
.build(); .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 { ...@@ -49,6 +49,18 @@ public class OssServiceContext {
} }
return this.ossService.getObjectUrl(bucket, objectKey); 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) { public Boolean deleteObject(String bucket, String objectKey) {
return this.ossService.deleteObject(bucket, objectKey); return this.ossService.deleteObject(bucket, objectKey);
......
...@@ -305,7 +305,7 @@ public class FlightAreaServiceImpl extends AbstractFlightAreaService implements ...@@ -305,7 +305,7 @@ public class FlightAreaServiceImpl extends AbstractFlightAreaService implements
.setName(file.getName()) .setName(file.getName())
.setSize(file.getSize()) .setSize(file.getSize())
.setChecksum(file.getSign()) .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> { ...@@ -47,6 +47,21 @@ public interface IWaylineFileService extends IService<WaylineFileEntity> {
URL getObjectUrl(String workspaceId, String waylineId) throws SQLException; 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. * Save the basic information of the wayline file.
* @param workspaceId * @param workspaceId
* @param metadata * @param metadata
......
...@@ -325,7 +325,7 @@ public class FlightTaskServiceImpl extends AbstractWaylineService implements IFl ...@@ -325,7 +325,7 @@ public class FlightTaskServiceImpl extends AbstractWaylineService implements IFl
} }
// get file url // 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() FlighttaskPrepareRequest flightTask = new FlighttaskPrepareRequest()
.setFlightId(waylineJob.getJobId()) .setFlightId(waylineJob.getJobId())
......
...@@ -145,7 +145,7 @@ public class SDKWaylineService extends AbstractWaylineService { ...@@ -145,7 +145,7 @@ public class SDKWaylineService extends AbstractWaylineService {
} }
// get file url // get file url
try { 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( return new TopicRequestsResponse<MqttReply<FlighttaskResourceGetResponse>>().setData(
MqttReply.success(new FlighttaskResourceGetResponse() MqttReply.success(new FlighttaskResourceGetResponse()
.setFile(new FlighttaskFile() .setFile(new FlighttaskFile()
......
...@@ -144,6 +144,24 @@ public class WaylineFileServiceImpl extends ServiceImpl<IWaylineFileMapper, Wayl ...@@ -144,6 +144,24 @@ public class WaylineFileServiceImpl extends ServiceImpl<IWaylineFileMapper, Wayl
} }
@Override @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) { public Integer saveWaylineFile(String workspaceId, WaylineFileDTO metadata) {
// 航线文件 一定是文件 // 航线文件 一定是文件
metadata.setIsDir(WaylineFileTypeEnum.FILE.getVal()); metadata.setIsDir(WaylineFileTypeEnum.FILE.getVal());
......
...@@ -168,6 +168,7 @@ oss: ...@@ -168,6 +168,7 @@ oss:
# endpoint: https://gt7-oss.geotwin.cc # endpoint: https://gt7-oss.geotwin.cc
# 深圳 # 深圳
endpoint: https://gt-oss-dev.geotwin.cn endpoint: https://gt-oss-dev.geotwin.cn
local-endpoint: http://10.0.8.72:9000
access-key: minioadmin access-key: minioadmin
secret-key: minioadmin secret-key: minioadmin
bucket: gtfly 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