springboot接入阿里云oss
时间: 2023-11-11 20:59:40 浏览: 107
springboot整合阿里云oss进行文件上传
5星 · 资源好评率100%
要在Spring Boot中接入阿里云OSS,可以使用阿里云官方提供的Java SDK。具体步骤如下:
1. 在pom.xml中添加依赖:
```
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
```
2. 在application.properties中配置OSS相关信息:
```
aliyun.oss.endpoint=yourEndpoint
aliyun.oss.accessKeyId=yourAccessKeyId
aliyun.oss.accessKeySecret=yourAccessKeySecret
aliyun.oss.bucketName=yourBucketName
```
3. 创建OSSClient对象:
```
@Configuration
public class OSSConfig {
@Value("${aliyun.oss.endpoint}")
private String endpoint;
@Value("${aliyun.oss.accessKeyId}")
private String accessKeyId;
@Value("${aliyun.oss.accessKeySecret}")
private String accessKeySecret;
@Bean
public OSSClient ossClient() {
return new OSSClient(endpoint, accessKeyId, accessKeySecret);
}
}
```
4. 使用OSSClient对象上传文件:
```
@Autowired
private OSSClient ossClient;
public void uploadFile(String key, File file) {
ossClient.putObject(bucketName, key, file);
}
```
阅读全文