- 在pom.xml中添加OSS依赖
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.9.1</version>
</dependency>
- 编写在application.yml中编写oss配置信息
aliyun:
oss:
endpoint: oss-cn-shanghai.aliyuncs.com
accessKeyId: LTA
secret: EyYAjI
bucket: yygh-oos
- 编写oss接口文件 OssService.java
package cn.sh.ideal.service;
import org.springframework.web.multipart.MultipartFile;
public interface OssService {
String uploadFile(MultipartFile multipartFile);
}
- 编写oss实现类OssServiceImpl.java
package cn.sh.ideal.service.impl;
import java.io.InputStream;
import java.util.UUID;
import cn.sh.ideal.config.OSSProperties;
import cn.sh.ideal.service.OssService;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.InitiateMultipartUploadRequest;
import com.aliyun.oss.model.InitiateMultipartUploadResult;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@RequiredArgsConstructor
@Service
@Slf4j
public class OssServiceImpl implements OssService {
private final OSSProperties ossProperties;
@SneakyThrows
@Override
public String uploadFile(MultipartFile multipartFile) {
OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getSecret());
String fileName = multipartFile.getOriginalFilename();
InputStream inputStream = multipartFile.getInputStream();
String uuid= UUID.randomUUID().toString().replace("-","");
fileName=uuid+fileName;
String timeUrl=new DateTime().toString("yyyy/MM/dd");
fileName=timeUrl+"/"+fileName;
ossClient.putObject(ossProperties.getBucket(),fileName, inputStream);
ossClient.shutdown();
String url = "https://" +
ossProperties.getBucket()+ "."+
ossProperties.getEndpoint() + "/"+fileName;
return url;
}
public void upload(MultipartFile file){
OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getSecret());
String uuid= UUID.randomUUID().toString().replace("-","");
String fileName = file.getOriginalFilename();
fileName=uuid+fileName;
String timeUrl=new DateTime().toString("yyyy/MM/dd");
fileName=timeUrl+"/"+fileName;
InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(ossProperties.getBucket(), fileName);
InitiateMultipartUploadResult initiateMultipartUploadResult = ossClient.initiateMultipartUpload(request);
}
}
- 编写oss控制类OssController.java
package cn.sh.ideal.controller;
import cn.sh.ideal.result.ServerResponseEntity;
import cn.sh.ideal.service.OssService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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;
@Slf4j
@RestController
@RequestMapping("/oss")
@RequiredArgsConstructor
public class OssController {
private final OssService ossService;
@PostMapping("/upload")
public ServerResponseEntity<String> uploadFile(MultipartFile file){
String url = ossService.uploadFile(file);
return ServerResponseEntity.success(url);
}
}