spring boot ali OSS
时间: 2023-07-27 16:08:30 浏览: 122
Spring Boot中使用阿里云OSS的配置可以参考以下代码:
首先,创建一个OSSConfig类,使用@Configuration注解将其标记为配置类。在该类中,使用@Bean注解将OSS对象放入IOC容器中,并通过参数传递OSSProperties对象进行配置。
```java
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class OSSConfig {
@Bean
public OSS client(OSSProperties prop){
return new OSSClientBuilder()
.build(prop.getEndpoint(), prop.getAccessKeyId(), prop.getAccessKeySecret());
}
}
```
接下来,创建一个OSSProperties类,使用@ConfigurationProperties注解读取配置文件中的OSS配置信息。
```java
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "alicloud.oss")
public class OSSProperties {
private String accessKeyId;
private String accessKeySecret;
private String host;
private String endpoint;
private String dir;
private Long expireTime;
private Long maxFileSize;
}
```
在配置文件中,可以按照以下格式配置OSS相关信息:
```yaml
alicloud:
oss:
accessKeyId: your-access-key-id
accessKeySecret: your-access-key-secret
host: your-oss-host
endpoint: your-oss-endpoint
dir: your-oss-dir
expireTime: your-oss-expire-time
maxFileSize: your-oss-max-file-size
```
以上是使用Spring Boot配置阿里云OSS的方法。希望对你有所帮助。
#### 引用[.reference_title]
- *1* *2* [Spring Boot 整合 AliCloud OSS(对象存储服务)](https://blog.csdn.net/qq_18947093/article/details/127742961)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [Spring Boot项目OSS整合](https://blog.csdn.net/chire_jr/article/details/82733828)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文