JAVA可以通过接口使用阿里云的对象存储业务(OSS)。
首先,需要导入相关的依赖:
<!--aliyunOSS-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
</dependency>
<!-- 日期工具栏依赖 -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<!--让自定义的配置在application.yaml进行自动提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
随后,可以通过application.yaml对阿里云的OSS相关信息进行配置:
server:
port: 8130 # 服务端口
spring:
profiles:
active: dev # 环境设置
application:
name: service-oss # 服务名
# cloud:
# nacos:
# discovery:
# server-addr: localhost:8848 # nacos服务地址
aliyun:
oss:
endpoint: oss-cn-beijing.aliyuncs.com
keyId: 自己的keyid
keySecret: 自己的keySecret
bucketName: rongzibao-file
然后,可以通过一个工具类获取keyId、keySecret、bucketName信息:
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class OssProperties implements InitializingBean {
private String endpoint;
private String keyId;
private String keySecret;
private String bucketName;
public static String ENDPOINT;
public static String KEY_ID;
public static String KEY_SECRET;
public static String BUCKET_NAME;
@Override
public void afterPropertiesSet() {
ENDPOINT = endpoint;
KEY_ID = keyId;
KEY_SECRET = keySecret;
BUCKET_NAME = bucketName;
}
}