配置阿里云OSS对象存储服务

配置阿里云OSS对象存储服务

导入阿里云OSS依赖

           <dependency>
                <groupId>com.aliyun.ossgroupId>
                <artifactId>aliyun-sdk-ossartifactId>
                <version>2.8.3version>
            dependency>
                        
            <dependency>
                <groupId>joda-timegroupId>
                <artifactId>joda-timeartifactId>
                <version>2.10.1version>
            dependency>
            

配置文件application.yml

这个换成自己的阿里云OSS的配置即可

aliyun:
  oss:
    access-key-id: 
    access-key-secret: 
    bucket-domain: 
    bucket-name: 
    end-point: 

工具类

@Service
public class OssServiceImpl implements OssService {


    @Override
    public String uploadFileAvatar(MultipartFile file) {
        //获取阿里云存储相关常量
        String endPoint = ConstantPropertiesUtils.END_POINT;
        String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
        String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
        String bucketName = ConstantPropertiesUtils.BUCKET_NAME;
        String domain = ConstantPropertiesUtils.DOMAIN;
        String uploadUrl = null;
        OSS ossClient = null;
        try {
            //判断oss实例是否存在:如果不存在则创建,如果存在则获取
            ossClient = new OSSClientBuilder().build(endPoint, accessKeyId,
                    accessKeySecret);
            if (!ossClient.doesBucketExist(bucketName)) {
                //创建bucket
                ossClient.createBucket(bucketName);
                //设置oss实例的访问权限:公共读
                ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
            }
            //获取上传文件流
            InputStream inputStream = file.getInputStream();
            //构建日期路径: avatar/2019/02/26/文件名
            String filePath = new DateTime().toString("yyyy/MM/dd");
            //文件名: uuid.扩展名
            // 获取文件的原始名称
            String original = file.getOriginalFilename();
            // 使用UUID给文件重命名
            String fileName = UUID.randomUUID().toString().replace("-", "");
            // 获取原文件的拓展名
            String fileType = original.substring(original.lastIndexOf("."));
            String fileUrl = filePath + "/" + fileName + fileType;
            //文件上传至阿里云
            ossClient.putObject(bucketName, fileUrl, inputStream);

            //获取url地址
            uploadUrl = domain + "/" + fileUrl;
            return uploadUrl;

        } catch (IOException e) {
            throw new GuLiException(40000, "文件上传失败");
        } finally {
            if (ossClient != null) {
                // 关闭OSSClient。
                ossClient.shutdown();
            }
        }
    }
}


@Component
public class ConstantPropertiesUtils implements InitializingBean {

    // 读取配置文件中的值
    @Value("${aliyun.oss.end-point}")
    private String endPoint;

    @Value("${aliyun.oss.access-key-secret}")
    private String keySecret;

    @Value("${aliyun.oss.bucket-domain}")
    private String domain;

    @Value("${aliyun.oss.bucket-name}")
    private String bucketName;

    @Value("${aliyun.oss.access-key-id}")
    private String keyId;


    @Autowired
    private OSSProperties ossProperties;
    public static String END_POINT;
    public static String ACCESS_KEY_ID;
    public static String ACCESS_KEY_SECRET;
    public static String BUCKET_NAME;
    public static String FILE_HOST;
    public static String DOMAIN;
    // 这个方法会在上面的属性初始化之后进行执行

    @Override
    public void afterPropertiesSet() throws Exception {
        END_POINT = endPoint;
        ACCESS_KEY_ID = keyId;
        ACCESS_KEY_SECRET = keySecret;
        BUCKET_NAME = bucketName;
        DOMAIN = domain;
    }
}

测试
配置阿里云OSS对象存储服务_第1张图片

在这里插入图片描述
配置阿里云OSS对象存储服务_第2张图片

你可能感兴趣的:(SpringBoot,阿里云oss,文件上传)