mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
sync & commit in edgware
This commit is contained in:
@@ -18,13 +18,13 @@ package com.alibaba.alicloud.oss;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.event.ContextClosedEvent;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
|
||||
/**
|
||||
* Shutdown All OSS Clients when {@code ApplicationContext} gets closed
|
||||
* {@link ApplicationListener}
|
||||
|
@@ -16,16 +16,16 @@
|
||||
|
||||
package com.alibaba.alicloud.oss;
|
||||
|
||||
import com.alibaba.alicloud.oss.resource.OssStorageProtocolResolver;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.alibaba.alicloud.oss.resource.OssStorageProtocolResolver;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
|
||||
/**
|
||||
* OSS Auto {@link Configuration}
|
||||
*
|
||||
|
@@ -17,13 +17,29 @@
|
||||
package com.alibaba.alicloud.oss;
|
||||
|
||||
/**
|
||||
* OSS constants
|
||||
* OSS constants.
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public interface OssConstants {
|
||||
public final class OssConstants {
|
||||
|
||||
String PREFIX = "spring.cloud.alibaba.oss";
|
||||
String ENABLED = PREFIX + ".enabled";
|
||||
/**
|
||||
* Prefix of OSSConfigurationProperties.
|
||||
*/
|
||||
public static final String PREFIX = "spring.cloud.alibaba.oss";
|
||||
|
||||
/**
|
||||
* Enable OSS.
|
||||
*/
|
||||
public static final String ENABLED = PREFIX + ".enabled";
|
||||
|
||||
/**
|
||||
* OSS ThreadPool bean name.
|
||||
*/
|
||||
public static final String OSS_TASK_EXECUTOR_BEAN_NAME = "ossTaskExecutor";
|
||||
|
||||
private OssConstants() {
|
||||
throw new AssertionError("Must not instantiate constant utility class");
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -21,12 +21,12 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.aliyun.oss.OSSClient;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import com.aliyun.oss.OSSClient;
|
||||
|
||||
/**
|
||||
* Actuator Endpoint to expose OSS Meta Data
|
||||
*
|
||||
|
@@ -16,6 +16,8 @@
|
||||
|
||||
package com.alibaba.alicloud.oss.endpoint;
|
||||
|
||||
import com.alibaba.alicloud.context.oss.OssProperties;
|
||||
|
||||
import org.springframework.boot.actuate.condition.ConditionalOnEnabledEndpoint;
|
||||
import org.springframework.boot.actuate.endpoint.Endpoint;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
@@ -24,8 +26,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.alibaba.alicloud.context.oss.OssProperties;
|
||||
|
||||
/**
|
||||
* OSS {@link Endpoint} Auto-{@link Configuration}
|
||||
*
|
||||
|
@@ -16,8 +16,10 @@
|
||||
|
||||
package com.alibaba.alicloud.oss.resource;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
@@ -27,8 +29,6 @@ import org.springframework.core.io.ProtocolResolver;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
|
||||
/**
|
||||
* A {@link ProtocolResolver} implementation for the {@code oss://} protocol.
|
||||
*
|
||||
@@ -37,6 +37,9 @@ import com.aliyun.oss.OSS;
|
||||
public class OssStorageProtocolResolver
|
||||
implements ProtocolResolver, BeanFactoryPostProcessor, ResourceLoaderAware {
|
||||
|
||||
/**
|
||||
* protocol of oss resource.
|
||||
*/
|
||||
public static final String PROTOCOL = "oss://";
|
||||
|
||||
private static final Logger log = LoggerFactory
|
||||
@@ -63,7 +66,7 @@ public class OssStorageProtocolResolver
|
||||
if (!location.startsWith(PROTOCOL)) {
|
||||
return null;
|
||||
}
|
||||
return new OssStorageResource(getOSS(), location);
|
||||
return new OssStorageResource(getOSS(), location, beanFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -82,4 +85,5 @@ public class OssStorageProtocolResolver
|
||||
throws BeansException {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -20,19 +20,29 @@ import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PipedInputStream;
|
||||
import java.io.PipedOutputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
||||
import com.aliyun.oss.ClientException;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSException;
|
||||
import com.aliyun.oss.model.Bucket;
|
||||
import com.aliyun.oss.model.OSSObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.WritableResource;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import static com.alibaba.alicloud.oss.OssConstants.OSS_TASK_EXECUTOR_BEAN_NAME;
|
||||
|
||||
/**
|
||||
* Implements {@link Resource} for reading and writing objects in Aliyun Object Storage
|
||||
@@ -44,18 +54,40 @@ import com.aliyun.oss.model.OSSObject;
|
||||
* @see Bucket
|
||||
* @see OSSObject
|
||||
*/
|
||||
public class OssStorageResource implements Resource {
|
||||
public class OssStorageResource implements WritableResource {
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(OssStorageResource.class);
|
||||
|
||||
private static final String MESSAGE_KEY_NOT_EXIST = "The specified key does not exist.";
|
||||
|
||||
private final OSS oss;
|
||||
|
||||
private final String bucketName;
|
||||
|
||||
private final String objectKey;
|
||||
|
||||
private final URI location;
|
||||
|
||||
public OssStorageResource(OSS oss, String location) {
|
||||
private final boolean autoCreateFiles;
|
||||
|
||||
private final ExecutorService ossTaskExecutor;
|
||||
|
||||
private final ConfigurableListableBeanFactory beanFactory;
|
||||
|
||||
public OssStorageResource(OSS oss, String location,
|
||||
ConfigurableListableBeanFactory beanFactory) {
|
||||
this(oss, location, beanFactory, false);
|
||||
}
|
||||
|
||||
public OssStorageResource(OSS oss, String location,
|
||||
ConfigurableListableBeanFactory beanFactory, boolean autoCreateFiles) {
|
||||
Assert.notNull(oss, "Object Storage Service can not be null");
|
||||
Assert.isTrue(location.startsWith(OssStorageProtocolResolver.PROTOCOL),
|
||||
"Location must start with " + OssStorageProtocolResolver.PROTOCOL);
|
||||
this.oss = oss;
|
||||
this.autoCreateFiles = autoCreateFiles;
|
||||
this.beanFactory = beanFactory;
|
||||
try {
|
||||
URI locationUri = new URI(location);
|
||||
this.bucketName = locationUri.getAuthority();
|
||||
@@ -71,6 +103,13 @@ public class OssStorageResource implements Resource {
|
||||
catch (URISyntaxException e) {
|
||||
throw new IllegalArgumentException("Invalid location: " + location, e);
|
||||
}
|
||||
|
||||
this.ossTaskExecutor = this.beanFactory.getBean(OSS_TASK_EXECUTOR_BEAN_NAME,
|
||||
ExecutorService.class);
|
||||
}
|
||||
|
||||
public boolean isAutoCreateFiles() {
|
||||
return this.autoCreateFiles;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -136,7 +175,7 @@ public class OssStorageResource implements Resource {
|
||||
@Override
|
||||
public Resource createRelative(String relativePath) throws IOException {
|
||||
return new OssStorageResource(this.oss,
|
||||
this.location.resolve(relativePath).toString());
|
||||
this.location.resolve(relativePath).toString(), this.beanFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -208,4 +247,73 @@ public class OssStorageResource implements Resource {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* create a bucket.
|
||||
* @return OSS Bucket
|
||||
*/
|
||||
public Bucket createBucket() {
|
||||
return this.oss.createBucket(this.bucketName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWritable() {
|
||||
return !isBucket() && (this.autoCreateFiles || exists());
|
||||
}
|
||||
|
||||
/**
|
||||
* acquire an OutputStream for write. Note: please close the stream after writing is
|
||||
* done
|
||||
* @return OutputStream of OSS resource
|
||||
* @throws IOException throw by oss operation
|
||||
*/
|
||||
@Override
|
||||
public OutputStream getOutputStream() throws IOException {
|
||||
if (isBucket()) {
|
||||
throw new IllegalStateException(
|
||||
"Cannot open an output stream to a bucket: '" + getURI() + "'");
|
||||
}
|
||||
else {
|
||||
OSSObject ossObject;
|
||||
|
||||
try {
|
||||
ossObject = this.getOSSObject();
|
||||
}
|
||||
catch (OSSException ex) {
|
||||
if (ex.getMessage() != null
|
||||
&& ex.getMessage().startsWith(MESSAGE_KEY_NOT_EXIST)) {
|
||||
ossObject = null;
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
if (ossObject == null) {
|
||||
if (!this.autoCreateFiles) {
|
||||
throw new FileNotFoundException(
|
||||
"The object was not found: " + getURI());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
final PipedInputStream in = new PipedInputStream();
|
||||
final PipedOutputStream out = new PipedOutputStream(in);
|
||||
|
||||
ossTaskExecutor.submit(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
OssStorageResource.this.oss.putObject(bucketName, objectKey, in);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
logger.error("Failed to put object", ex);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user