栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

阿里云oss文件存储

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

阿里云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);
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/692184.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号