目录
一、使用方式
1.1 进入管理控制台,没有注册点击申请开通就行
1.2 主界面
1.3 创建自己的存储仓库
二、快速入门
2.1 文件流上传
2.2 创建子用户获取AK
三、AlibabaOSS服务
3.1 添加依赖
3.2 配置文件
3.3 上传成功代码
一、使用方式 1.1 进入管理控制台,没有注册点击申请开通就行 1.2 主界面
关于开发可以点新手入门后的Java SDK
1.3 创建自己的存储仓库 二、快速入门 2.1 文件流上传添加依赖:
com.aliyun.oss
aliyun-sdk-oss
3.10.2
查看官网Java SDK上传图片(使用文件流上传)
@Test
void testOSS() throws FileNotFoundException {
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
String endpoint = "oss-cn-beijing.aliyuncs.com";
// 这里是创建的子用户获得的AK
String accessKeyId = "LTAI5tSntGLJY4woGUMvjaTm1";
String accessKeySecret = "zRRDgkjvHh7FIE6hhpDUdKgEKIcO3Y1";
// 填写Bucket名称,例如examplebucket。
String bucketName = "righteye-img";
// 存储后文件的名称
String objectName = "111.jpg";
// 填写本地文件的完整路径,例如D:\localpath\examplefile.txt。
// 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
String filePath= "C:\Users\DELL\Pictures\立华奏\2021121420492421280.jpg";
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
InputStream inputStream = new FileInputStream(filePath);
// 创建PutObject请求。
ossClient.putObject(bucketName, objectName, inputStream);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
System.out.println("上传成功");
}
2.2 创建子用户获取AK
然后可以继续使用了
三、AlibabaOSS服务 3.1 添加依赖使用官网提供的方式比价繁琐,对于身份信息可以单独抽离;这里使用SpringCloudAlibaba OSS服务来简化开发,添加对应的依赖
com.alibaba.cloud
spring-cloud-starter-alicloud-oss
3.2 配置文件我alibaba-cloud版本选择的 2.2.5,过高不提供oss,需要手动添加版本
com.alibaba.cloud spring-cloud-starter-alicloud-oss2.2.0.RELEASE
spring:
cloud:
alicloud:
access-key: LTAI5tSntGLJY4woGUM1jaTm
secret-key: zRRDgkjvHh7FIE6hhp1UdKgEKIcO3Y
oss:
endpoint: oss-cn-beijing.aliyuncs.com
3.3 上传成功代码
@Autowired
private OSSClient ossClient;
@Test
void testOSSClient() throws FileNotFoundException {
// 填写Bucket名称,例如examplebucket。
String bucketName = "righteye-img";
// 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
String objectName = "222.jpg";
// 填写本地文件的完整路径,例如D:\localpath\examplefile.txt。
// 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
String filePath= "C:\Users\DELL\Pictures\立华奏\2021121420492421280.jpg";
try {
InputStream inputStream = new FileInputStream(filePath);
// 创建PutObject请求。
ossClient.putObject(bucketName, objectName, inputStream);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
System.out.println("上传成功");
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)