阿里云OSS对象存储

官方定义:

阿里云对象存储OSS(Object Storage Service)是一款海量、安全、低成本、高可靠的云存储服务,可提供99.9999999999%(12个9)的数据持久层,99.995%的数据可用性。多种存储类型供选择,群面优化存储成本。

导入相关依赖:

<dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
    <version>3.10.2</version>
</dependency>

配置类:

@Data
@ConfigurationProperties(prefix="tanhua.oss")
public class OssProperties{
    private String accessKey;
    private String secret;
    private String bucketName;
    private String url; //路径
    private String endpoint;
}

模板类:

package com.tanhua.autoconfig.template;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.tanhua.autoconfig.properties.OssProperties;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @Author zzw2000
 * @Date 2022年04月13日 22:56
 * @Description OSS模板类
 */
 public class OssTemplate{
     private OssProperties properties;
     public OssTemplate(OssProperties properties){
         this.properties=properties;     
     } 
     
    /**
     * 文件上传到服务器
     *
     * @param filename 文件名
     * @param inputStream 文件输入流
     * @return 文件路径
     */
     public String upload(String filename, InputStream inputStream){
        //拼写图片路径
        filename = new SimpleDateFormat("yyyy-MM-dd").format(new Date())
                + "/" + UUID.randomUUID()
                + filename.substring(filename.lastIndexOf("."));
        // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
        String endpoint = properties.getEndpoint();
        // 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
        String accessKeyId = properties.getAccessKey();
        String accessKeySecret = properties.getSecret();
        
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        
        ossClient.putObject(properties.getBucketName(), filename, inputStream);
        //关闭ossClient
        ossClient.shutdown();
        return properties.getUrl() + "/" + filename;   
     }
 }

注册到Spring IoC容器中:

package com.tanhua.autoconfig;

import com.tanhua.autoconfig.properties.OssProperties;
import com.tanhua.autoconfig.template.OssTemplate;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * @Author zzw2000
 * @Date 2022年04月10日 23:12
 * @Description 自动装配类
 */
@EnableConfigurationProperties({classes=OssTemplate.class})
public class TanhuaAutoConfiguration{
    @Bean
    public OssTemplate ossTemplate(OssProperties properties){
        return new OssProperties(properties);    
    }
}

在application.yml中添加OSS配置:

oss:
  accessKey: LTAI4GKgob9vZ53k2SZdyAC7
  secret: LHLBvXmILRoyw0niRSBuXBZewQ30la
  endpoint: oss-cn-beijing.aliyuncs.com
  bucketName: tanhua001
  url: https://tanhua001.oss-cn-beijing.aliyuncs.com/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lucky赵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值