springboot集成阿里OSS上传文件

开发前的准备:

自己在阿里云上申请免费的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
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值