阿里云对象存储-图片存储

1.依赖jar

<!--阿里云对象存储-->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>2.8.3</version>
        </dependency>

2.实例

   @PatchMapping("/pic/alimg")
    @ApiOperation(value = "阿里图片上传")
    @ResponseBody
    public String picOSS(MultipartFile uploadFile) throws Exception {
     String endpoint = "http://oss-cn-shanghai.aliyuncs.com";
        // 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录
        // https://ram.console.aliyun.com 创建
        String accessKeyId = "";
        String accessKeySecret = "";
        // 创建OSSClient实例
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        // 上传
        String filename = FtpUtil.genImageName() + uploadFile.getOriginalFilename().substring(uploadFile.getOriginalFilename().lastIndexOf("."));
        ossClient.putObject(bucktName, filename, new ByteArrayInputStream(uploadFile.getBytes()));
        // 关闭client
        ossClient.shutdown();
        Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
        String url = ossClient.generatePresignedUrl(bucktName, filename, expiration).toString();
        System.out.println(url);
        return url;
    }

3.结果

返回图片的url地址(可直接打开)
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41988504/article/details/88743184
今日推荐