|
@@ -29,29 +29,25 @@ public class OssStore implements FileStore {
|
|
|
|
|
|
private static final Pattern CONFIG_PATTERN = Pattern.compile("^oss://([\\w]+):([\\w]+)@([\\w-]+).([\\w-.]+)$");
|
|
private static final Pattern CONFIG_PATTERN = Pattern.compile("^oss://([\\w]+):([\\w]+)@([\\w-]+).([\\w-.]+)$");
|
|
|
|
|
|
- private String endpoint;
|
|
|
|
|
|
+ private OSS ossClient;
|
|
|
|
|
|
private String bucket;
|
|
private String bucket;
|
|
|
|
|
|
- private String accessKey;
|
|
|
|
-
|
|
|
|
- private String accessSecret;
|
|
|
|
-
|
|
|
|
public OssStore(String config) {
|
|
public OssStore(String config) {
|
|
String message = "[file.store]" + config + ": pattern error";
|
|
String message = "[file.store]" + config + ": pattern error";
|
|
Matcher m = CONFIG_PATTERN.matcher(config);
|
|
Matcher m = CONFIG_PATTERN.matcher(config);
|
|
if (m.find()) {
|
|
if (m.find()) {
|
|
- accessKey = StringUtils.trimToNull(m.group(1));
|
|
|
|
- accessSecret = StringUtils.trimToNull(m.group(2));
|
|
|
|
|
|
+ String accessKey = StringUtils.trimToNull(m.group(1));
|
|
|
|
+ String accessSecret = StringUtils.trimToNull(m.group(2));
|
|
bucket = StringUtils.trimToNull(m.group(3));
|
|
bucket = StringUtils.trimToNull(m.group(3));
|
|
- endpoint = StringUtils.trimToNull(m.group(4));
|
|
|
|
|
|
+ String endpoint = StringUtils.trimToNull(m.group(4));
|
|
|
|
|
|
Assert.notNull(accessKey, message);
|
|
Assert.notNull(accessKey, message);
|
|
Assert.notNull(accessSecret, message);
|
|
Assert.notNull(accessSecret, message);
|
|
Assert.notNull(bucket, message);
|
|
Assert.notNull(bucket, message);
|
|
Assert.notNull(endpoint, message);
|
|
Assert.notNull(endpoint, message);
|
|
|
|
|
|
- endpoint = ENDPOINT_PREFIX.concat(endpoint);
|
|
|
|
|
|
+ ossClient = new OSSClientBuilder().build(ENDPOINT_PREFIX.concat(endpoint), accessKey, accessSecret);
|
|
} else {
|
|
} else {
|
|
throw new IllegalArgumentException(message);
|
|
throw new IllegalArgumentException(message);
|
|
}
|
|
}
|
|
@@ -59,32 +55,33 @@ public class OssStore implements FileStore {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void write(String path, InputStream ins, String md5) throws Exception {
|
|
public void write(String path, InputStream ins, String md5) throws Exception {
|
|
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKey, accessSecret);
|
|
|
|
ObjectMetadata metadata = new ObjectMetadata();
|
|
ObjectMetadata metadata = new ObjectMetadata();
|
|
metadata.setContentMD5(BinaryUtil.toBase64String(Encodes.decodeHex(md5)));
|
|
metadata.setContentMD5(BinaryUtil.toBase64String(Encodes.decodeHex(md5)));
|
|
ossClient.putObject(bucket, path, ins, metadata);
|
|
ossClient.putObject(bucket, path, ins, metadata);
|
|
- ossClient.shutdown();
|
|
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public byte[] read(String path) throws Exception {
|
|
public byte[] read(String path) throws Exception {
|
|
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKey, accessSecret);
|
|
|
|
OSSObject ossObject = ossClient.getObject(bucket, path);
|
|
OSSObject ossObject = ossClient.getObject(bucket, path);
|
|
ByteArrayOutputStream ous = new ByteArrayOutputStream();
|
|
ByteArrayOutputStream ous = new ByteArrayOutputStream();
|
|
IOUtils.copy(ossObject.getObjectContent(), ous);
|
|
IOUtils.copy(ossObject.getObjectContent(), ous);
|
|
- ossClient.shutdown();
|
|
|
|
|
|
+ ossObject.close();
|
|
return ous.toByteArray();
|
|
return ous.toByteArray();
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public boolean exist(String path) {
|
|
public boolean exist(String path) {
|
|
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKey, accessSecret);
|
|
|
|
try {
|
|
try {
|
|
return ossClient.doesObjectExist(bucket, path);
|
|
return ossClient.doesObjectExist(bucket, path);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("oss exist operate error", e);
|
|
log.error("oss exist operate error", e);
|
|
return false;
|
|
return false;
|
|
- } finally {
|
|
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void close() {
|
|
|
|
+ if (ossClient != null) {
|
|
ossClient.shutdown();
|
|
ossClient.shutdown();
|
|
}
|
|
}
|
|
}
|
|
}
|