阿里云oss文件服务器

阿里云oss文件服务器

1、开始
a、创建bucket,并设置为公共读权限
b、创建用户,访问方式为编程访问
c、为用户创建accesskey,并开启用户的oss权限
d、application.yml
aliyun:
  # 要带上 https:// ,上传时要使用的
  endpoint: https://oss-cn-shanghai.aliyuncs.com
  accessKeyId: xxxxxxxx # 根据自己的帐号配置
  accessKeySecret: xxxxxxxx # 根据自己的帐号配置
  bucketName: xxxx # 存储空间名称
  # Bucket域名,访问文件时作为URL前缀,注意要加上 https:// 和 结尾带上 /
  bucketDomain: https://xxxx.oss-cn-shanghai.aliyuncs.com/
2、pom.xml
<dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
</dependency>
3、AliyunUtil
/**
 * 阿里云工具类
 */
public final class AliyunUtil {

    /**
     * 上传图片文件
     * @param platformEnum 类型:文章,用户
     * @param file  MultipartFile文件对象
     * @param aliyun AliyunProperties 阿里云配置
     * @return
     */
    public static Result uploadFileToOss(PlatformEnum platformEnum, MultipartFile file, AliyunProperties aliyun ) {
        // 上传
        // 上传文件所在目录名,当天上传的文件放到当天日期的目录下。article/19990101/123123.png
        String folderName = platformEnum.name().toLowerCase() + "/" + DateFormatUtils.format(new Date(), "yyyyMMdd");

        // 保存到 OSS 中的文件名,采用 UUID 命名。
        String fileName = UUID.randomUUID().toString().replace("-", "");

        // 从原始文件名中,获取文件扩展名
        String fileExtensionName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

        // 文件在 OSS 中存储的完整路径
        String filePath = folderName + "/" + fileName + fileExtensionName;

        OSS ossClient = null;
        try {
            // 获取 OSS 客户端实例
            ossClient = new OSSClientBuilder().build(aliyun.getEndpoint(), aliyun.getAccessKeyId(), aliyun.getAccessKeySecret());

            // 上传文件到OSS 并响应结果
            PutObjectResult putObjectResult = ossClient.putObject(aliyun.getBucketName(), filePath, file.getInputStream());

            ResponseMessage response = putObjectResult.getResponse();
            if(response == null) {
                // 上传成功

                // 返回上传文件的访问完整路径
                return Result.ok( aliyun.getBucketDomain() + filePath );
            }else {
                // 上传失败,OOS服务端会响应状态码和错误信息
                String errorMsg = "响应的错误状态码是【" + response.getStatusCode() +"】," +
                        "错误信息【"+response.getErrorResponseAsString()+"】";
                return Result.error(errorMsg);
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        } finally {
            if (ossClient != null) {
                // 关闭OSSClient。
                ossClient.shutdown();
            }
        }
    }

    /**
     * 根据文件url删除
     * @param fileUrl
     */
    public static Result delete(String fileUrl, AliyunProperties aliyun) {
        // 去除文件 url 中的 Bucket域名 article/20200729/9d83082760e84c15a685d6e61338174a.png
        String filePath = fileUrl.replace(aliyun.getBucketDomain(), "");

        OSS ossClient = null;
        try {
            ossClient = new OSSClientBuilder().build(aliyun.getEndpoint(), aliyun.getAccessKeyId(), aliyun.getAccessKeySecret());
            // 删除
            ossClient.deleteObject(aliyun.getBucketName(), filePath);
            return Result.ok();
        } catch (Exception e) {
            return Result.error("删除失败:"+e.getMessage());
        }finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

}
4、FileController
/**
 * 文件控制器
 */
@RequestMapping("/file")
@RestController
public class FileController {

    @Autowired
    private BlogProperties blogProperties;

    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file ) {
        // 获取阿里云 OSS 相关配置信息
        AliyunProperties aliyun = blogProperties.getAliyun();

        return AliyunUtil.uploadFileToOss(PlatformEnum.ARTICEL, file, aliyun);
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestParam(value = "fileUrl", required = true)
                                     String fileUrl) {
        return AliyunUtil.delete(fileUrl, blogProperties.getAliyun());
    }
    
}
原文地址:https://www.cnblogs.com/linding/p/14863672.html