栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

阿里云OSS对象存储服务在Cloud中的使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

阿里云OSS对象存储服务在Cloud中的使用

目录

一、使用方式

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

我alibaba-cloud版本选择的 2.2.5,过高不提供oss,需要手动添加版本


	com.alibaba.cloud
	spring-cloud-starter-alicloud-oss
	2.2.0.RELEASE

3.2 配置文件
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("上传成功");
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/854595.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号