1.公共服务导入依赖
<dependency> <!--不加版本号会有问题-->
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alicloud-oss</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
</dependency>
2.需要文件上传的服务配置
spring:
cloud:
alicloud:
access-key: xx
secret-key: xx
oss:
endpoint: oss-cn-beijing.aliyuncs.com
3.测试类测试
@Autowired
OSSClient ossClient;
@Test
void testUpload(){
InputStream inputStream = null;
try {
inputStream = new FileInputStream("C:\\Users\\28417\\Desktop\\上传.jpg");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 依次填写Bucket名称(例如examplebucket)和Object完整路径(例如exampledir/exampleobject.txt)。Object完整路径中不能包含Bucket名称。
ossClient.putObject("wlun-bucket", "上传1.jpg", inputStream);
// 关闭OSSClient。
ossClient.shutdown();
System.out.println("上传完成!!!");
}
错误:
com.aliyun.oss.OSSException: The difference between the request time and the current time is too large.
双系统,所以开机之后时间存在问题,跟oss服务器时间间隔超过15分钟,更新系统时间解决问题