赞
踩
参考项目: RuoYi-Vue-Plus
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.12.215</version>
</dependency>
oss:
# 对应云厂商的站点域名
endpoint: XXX.XXX.XXX
# 对应云厂商桶的绑定域名
domain: XXX.XXX.XXX
accessKey: XXXXXXX
secretKey: XXXXXXX
# 存储空间名/桶名
bucketName: LionLi
# 存储区域
region: XXX
# 是否https(Y=是,N=否)
isHttps: N
OssProperties
/**
* OSS对象存储 配置属性
*
* @author Lion Li
*/
@Data
@Component
@ConfigurationProperties(prefix = "oss")
public class OssProperties {
/**
* 访问站点
*/
private String endpoint;
/**
* 自定义域名
*/
private String domain;
/**
* ACCESS_KEY
*/
private String accessKey;
/**
* SECRET_KEY
*/
private String secretKey;
/**
* 存储空间名
*/
private String bucketName;
/**
* 存储区域
*/
private String region;
/**
* 是否https(Y=是,N=否)
*/
private String isHttps;
}
OssConfig
/**
* S3 存储协议 所有兼容S3协议的云厂商均支持
* 阿里云 腾讯云 七牛云 minio
*
* @author Lion Li
*/
@Configuration
public class OssConfig {
@Bean
public AmazonS3 createOssClient(OssProperties properties) {
AwsClientBuilder.EndpointConfiguration endpointConfig =
new AwsClientBuilder.EndpointConfiguration(properties.getEndpoint(), properties.getRegion());
AWSCredentials credentials = new BasicAWSCredentials(properties.getAccessKey(), properties.getSecretKey());
AWSCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(credentials);
ClientConfiguration clientConfig = new ClientConfiguration();
if (OssConstant.IS_HTTPS.equals(properties.getIsHttps())) {
clientConfig.setProtocol(Protocol.HTTPS);
} else {
clientConfig.setProtocol(Protocol.HTTP);
}
return AmazonS3Client.builder()
.withEndpointConfiguration(endpointConfig)
.withClientConfiguration(clientConfig)
.withCredentials(credentialsProvider)
.disableChunkedEncoding()
.build();
}
}
@Autowired
private OssProperties properties;
@Autowired
private AmazonS3 client;
String bucketName = properties.getBucketName();
if (client.doesBucketExistV2(bucketName)) {
return;
}
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
createBucketRequest.setCannedAcl(CannedAccessControlList.PublicRead);
client.createBucket(createBucketRequest);
try {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(contentType);
metadata.setContentLength(inputStream.available());
client.putObject(new PutObjectRequest(properties.getBucketName(), "文件路径", inputStream, metadata));
} catch (Exception e) {
e.printStackTrace();
}
client.deleteObject(properties.getBucketName(), "文件路径");
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。