记录开发内容demo-java阿里云OOS上传


前言

阿里云OOS上传


一、所需sdk

    <dependency>
        <groupId>com.aliyun.oss</groupId>
        <artifactId>aliyun-sdk-oss</artifactId>
        <version>3.10.2</version>
    </dependency>

二、tuil类

1.demo

代码如下(示例):

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.pcwl.util.servlet.BaseController;
import com.pcwl.util.servlet.R;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;

//阿里云文件控制器
@RestController
@RequestMapping("/aLiYunOSS")
@RequiresAuthentication
public class ALiYunOSSController extends BaseController {


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

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        //获取文件名称
        String name = file.getOriginalFilename();

//        name = name.substring(name.indexOf("."));
        String objectname = "file" + System.currentTimeMillis()+":"+name;

        String url = "前缀";
        //调用oss实现上传第一个参数bucket名称  第二个参数文件名称  第三个参数输入流
        ossClient.putObject("bucketName", objectname, file.getInputStream());
        // 关闭OSSClient。
        ossClient.shutdown();
        //返回组成的文件url
        String photoUrl = url + objectname ;

        return data(photoUrl);
    }

}


总结

阿里云OOS上传 有压缩包处理 水印处理自带

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值