前言
阿里云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上传 有压缩包处理 水印处理自带