阿里云oss文件存储

阿里云oss文件存储,第1张

阿里云oss文件存储

首先添加依赖


    com.aliyun.oss
    aliyun-sdk-oss

然后再yml文件中自定义oss配置

server:
  port: 8083
spring:
  application:
    name: serviceOSS # 服务名称
  cloud:
    nacos:
      server-addr: 192.168.92.101:8848 # nacos地址
aliyun:
  oss:
    end-point: your endpoint
    accessKey-id: your accessKeyId
    secret: your secret
    bucket-name: your bucketName

创建OssProperties,从配置文件中读取oss相关配置

@Setter
@Component
@ConfigurationProperties(prefix = "zhenzi.sms")
public class SmsProperties  implements InitializingBean {

    private String apiUrl;
    private String appId;
    private String appSecret;
    private String templateId;
    //公有静态变量
    public static String API_URL;
    public static String APP_ID;
    public static String APP_SECRET;
    public static String TEMPLATE_ID;

     //当私有属性被赋值后会自动调用afterPropertiesSet方法
    @Override
    public void afterPropertiesSet() throws Exception {
        API_URL=apiUrl;
        APP_ID=appId;
        APP_SECRET=appSecret;
        TEMPLATE_ID=templateId;
    }
}

fileServiceImpl实现

@Service
public class OssServiceImpl implements OssService {

    
    @Override
    public String upload(MultipartFile file) {
        OSS ossClient = null;
        try {

// yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
            String endpoint = OssProperties.END_POINT;
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
            String accessKeyId = OssProperties.ACCESSKEY_ID;
            String accessKeySecret = OssProperties.SECRET;

// 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

           // 从上传的文件中得到流
            InputStream inputStream = file.getInputStream();
            String oldName = file.getOriginalFilename();
              //获取新的文件名
            String fileName= UUID.randomUUID().toString().replaceAll("-","")+oldName.substring(oldName.lastIndexOf("."));
// 依次填写Bucket名称(例如examplebucket)和Object完整路径(例如exampledir/exampleobject.txt)。Object完整路径中不能包含Bucket名称。
            //要保存文件的目录结构,就是类似于2022/01/02的三级目录
            String dir = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
            fileName=dir+"/"+fileName;
            ossClient.putObject( OssProperties.BUCKET_NAME , fileName, inputStream);
          //  https:// bucketName.endPoint/文件的路径(目录+文件名),%s是字符串的占位符
            String url=String.format("https://%s.%s/%s", OssProperties.BUCKET_NAME,endpoint,fileName);
            return url;
        } catch (IOException e) {
            throw new RuntimeException("上传文件失败");
        } finally {
            //关闭OSSClient。
            ossClient.shutdown();
        }

    }
}

最后创建controller

@PostMapping("/upload")
@ApiOperation("上传文件到OSS服务器")
public Result  upload(MultipartFile file){
    String url = ossService.upload(file);
    return Result.ok().data("url",url);
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5692669.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存