初学对象存储OSS---学习笔记
文章目录
前言
初学OSS,白嫖了三个月,分享一下学习心得!
一、OSS是什么?
下面以一个小故事介绍OSS的作用:
从前,有一个小村庄,这个小村庄的人们生活朴素,但是却非常团结。他们的主要收入来源是从周边山林中收集各种野果和草药,然后卖给城市里的商贩。 有一天,村庄里的年轻人听说了互联网和电子商务,他们想利用这个机会来将自己的野果和草药推销到全国各地。然而,他们发现自己没有能力和资源来开发和维护自己的在线商店。
幸运的是,他们听说了阿里云的对象存储(OSS)服务,这个服务可以为他们提供可靠的存储和访问服务,同时还可以自动扩展存储容量,以满足他们日益增长的业务需求。
于是,他们决定使用OSS服务来构建自己的在线商店。他们将自己的产品照片和介绍上传到OSS中,并使用OSS的API来构建自己的网站,然后使用阿里云的负载均衡服务来管理流量,确保他们的网站可以快速响应用户请求。
通过OSS和阿里云的帮助,这个小村庄的年轻人成功地将自己的野果和草药卖到了全国各地,他们的生意日益繁荣。最终,他们甚至能够扩大自己的生产规模,为更多人提供优质的野果和草药,也让更多的人了解和认识这个美丽的小村庄。
故事的结局告诉我们,OSS的作用是提供可靠的存储和访问服务。
二、怎么使用OSS
1.进入 -----> 阿里云官网
2.点击进入OSS控制台
默认你已经买了,就可以直接进入控制台了:
3.获取accessKeyId 和 accessKeySecret
关于在OSS控制太的可视化操作这里就不说了,下面是关于怎么使用java代码对oss进行操作
创建Bucket 然后点击你的Bucket列表(桶)或者 在搜索栏搜索RAM 访问控制 进入RAM 控制台,获取accessKeyId 和 accessKeySecret
点击AccessKey:
这样就可以拿到accessKeyId 和 accessKeySecret了
4.拿到了accessKeyId 和 accessKeySecret ,就可以直接上代码操作了
下面列举几个简单的示例:
打开idea,在pom.xml导入依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.15.1</version>
</dependency>
新建Demo类:
4.1.新建桶代码
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;public class Demo {// 新建桶public static void main(String[] args) throws Exception {// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。String accessKeyId = "你的accessKeyId ";String accessKeySecret = "你的accessKeySecret ";// 填写Bucket名称,例如jljlm。String bucketName = "jljlm";// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);try {// 创建存储空间。ossClient.createBucket(bucketName);} 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();}}}
}
运行后刷新oss控制台,结果如下:
成功!!!
4.2.写入txt文件并上传到oss代码
// 通过流式上传的方式将文件上传到OSS。public static void main(String[] args) throws Exception {// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。String accessKeyId = "你的accessKeyId ";String accessKeySecret = "你的accessKeySecret ";// 填写Bucket名称,例如jljlm。String bucketName = "jljlm";// 填写Object完整路径,例如txt文件/hello.txt。Object完整路径中不能包含Bucket名称。// 这是oss上的路径String objectName = "txt文件/hello.txt";// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);try {// 文件内容String content = "Hello OSS";ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));} 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();}}}
运行后刷新oss控制台,结果如下:
成功!!!
4.3.文件下载读取输出到控制台代码
// 通过流式下载方式从OSS下载读取文件。
public static void main(String[] args) throws Exception {// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。String accessKeyId = "你的accessKeyId ";String accessKeySecret = "你的accessKeySecret ";// 填写Bucket名称,例如jljlm。String bucketName = "jljlm";// 填写Object完整路径,例如txt文件/hello.txt。Object完整路径中不能包含Bucket名称。String objectName = "txt文件/hello.txt";// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);try {// 调用ossClient.getObject返回一个OSSObject实例,该实例包含文件内容及文件元信息。OSSObject ossObject = ossClient.getObject(bucketName, objectName);// 调用ossObject.getObjectContent获取文件输入流,可读取此输入流获取其内容。InputStream content = ossObject.getObjectContent();if (content != null) {BufferedReader reader = new BufferedReader(new InputStreamReader(content));while (true) {String line = reader.readLine();if (line == null) break;System.out.println("\\n" + line);}// 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。content.close();}} 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();}}}
读取结果输出控制台
成功!!!
4.4.列举文件
// 列举文件:默认列举100个文件。public static void main(String[] args) throws Exception {// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";String accessKeyId = "你的accessKeyId ";String accessKeySecret = "你的accessKeySecret ";// 填写Bucket名称,例如jljlm。String bucketName = "jljlm";// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);try {// ossClient.listObjects返回ObjectListing实例,包含此次listObject请求的返回结果。ObjectListing objectListing = ossClient.listObjects(bucketName);// objectListing.getObjectSummaries获取所有文件的描述信息。for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {System.out.println(" - " + objectSummary.getKey() + " " +"(size = " + objectSummary.getSize() + ")");}} 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();}}}
读取结果输出控制台:
成功!!!
4.5.删除指定文件
// 删除指定文件public static void main(String[] args) throws Exception {// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。String accessKeyId = "你的accessKeyId ";String accessKeySecret = "你的accessKeySecret ";// 填写Bucket名称,例如examplebucket。String bucketName = "jljlm";// 填写Object完整路径,例如txt文件/hello.txt。Object完整路径中不能包含Bucket名称。// 这是oss上的路径String objectName = "txt文件/hello.txt";// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);try {// 删除文件。ossClient.deleteObject(bucketName, objectName);} 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();}}}
运行后刷新OSS控制台:
成功!!!
三、总结
OSS提供可靠的存储和访问服务,OSS可用于图片、音视频、日志等海量文件的存储,方便的存储各种文件。
@作者:加辣椒了吗?
简介:憨批大学生一枚,喜欢在博客上记录自己的学习心得,也希望能够帮助到你们!