开发前的准备:
自己在阿里云上申请免费的Oss对象存储,新建Bucket---读写权限选择公共读,我开始这里忘了导致后来踩了小坑
接下来是代码中的一些配置:我用的是springboot
1》添加pom依赖:
<!--OSS上传-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.8.3</version>
</dependency>
2》yml配置:
oss:
endpoint: http://oss-cn-hangzhou.aliyuncs.com
accessKeyId: 你自己的---阿里云右上角头像点击选择可以获取
accessKeySecret: 你自己的---阿里云右上角头像点击选择可以获取
bucketName: tao11140------你自己创建的bucket节点
key: group1/pic-----------目录前缀
webserver: https://tao11140.oss-cn-hangzhou.aliyuncs.com
3》添加配置类:
OssProperties:
@Data
@ConfigurationProperties(prefix = "oss")
public class OssProperties {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
private String key;
private String webserver;
}
OssConfig:
Configuration
@EnableConfigurationProperties(OssProperties.class)
public class OssConfig {
@Autowired
private OssProperties ossProperties;
@Bean
public OSSClient getOSSClient(){
ClientConfiguration conf=new ClientConfiguration();
// Set the maximum number of allowed open HTTP connections
conf.setMaxConnections(1000);
conf.setConnectionTimeout(600000);
conf.setSocketTimeout(600000);
OSSClient client = new OSSClient(ossProperties.getEndpoint(),ossProperties.getAccessKeyId(),ossProperties.getAccessKeySecret(),conf);
return client;
}
}
OssClientUtils:
@Component
public class OssClientUtils {
@Autowired