1、OSS介绍
阿里云对象存储OSS(Object Storage Service)是一款云存储服务,通常用于图片、音视频、日志等海量文件的存储,并且数据以对象(Object)的形式存储在OSS的存储空间(Bucket )中。详细介绍和说明可以看阿里云的产品文档:产品概述_对象存储(OSS)-阿里云帮助中心。本文是记录使用Java实现OSS的一些基础操作,包括创建存储空间(Bucket)、上传文件、下载文件、查询文件和删除文件。
2、Java代码操作OSS
1. 引入pom依赖
<dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.15.1</version>
</dependency>
2. 代码实现基础操作
- 概念介绍
Bucket是指一块存储空间,类似于电脑的C盘,D盘。 Object(对象)是指一个文档、图片或者音频等。
endpoint类似OSS的访问路径,可以从下面图片的位置获取。
accessKeyId和accessKeySecret是登录阿里云的凭证信息,在右上角个人信息的accessKey管理中获取。
- 代码逻辑
创建存储空间:调用ossClient.createBucket()方法
上传文件:调用ossClient.putObject()方法
下载文件:调用ossClient.getObject()方法
查询文件:调用ossClient.listObjects()方法
删除文件:调用ossClient.deleteObject()方法
方法的详细介绍和说明可以看官方文档:基础操作_对象存储(OSS)-阿里云帮助中心
- 示例代码
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.PutObjectResult;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;public class AliyunOssUtils {public static String endpoint = "oss-cn-shenzhen.aliyuncs.com";public static String accessKeyId = "your accessKeyId";public static String accessKeySecret = "your accessKeySecret";// 创建阿里云登录凭证public static CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);// 创建OSSClient实例。public static OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);/*** 创建bucket*/public static void createBucket(){String bucketName = "java-hello-world";try {// 创建存储空间。ossClient.createBucket(bucketName);} catch (Exception e) {System.out.println("Error Message:" + e.getMessage());} finally {if (ossClient != null) {ossClient.shutdown();}}}/*** 上传文件*/public static void uploadFile(){// 填写Bucket名称,如果Bucket不存在, 可以先创建一个String bucketName = "java-hello-world";// 填写Object完整路径,需要包含文件名,但不用包含Bucket名称String objectName = "test/test.txt";try {String content = "Hello OSS";// 采用字节数组方式上传PutObjectResult putObjectResult = ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));System.out.println("putObjectResult = " + putObjectResult.toString());} catch (Exception e) {System.out.println("Error Message:" + e.getMessage());} finally {if (ossClient != null) {ossClient.shutdown();}}}/*** 下载文件*/public static void downloadFile(){// 填写Bucket名称String bucketName = "java-hello-world";// 填写Object完整路径,需要包含文件名,但不用包含Bucket名称String objectName = "test/test.txt";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 (Exception e) {System.out.println("Error Message:" + e.getMessage());} finally {if (ossClient != null) {ossClient.shutdown();}}}/*** 查询某个bucket的Object列表*/public static void getObjectList(){// 填写Bucket名称String bucketName = "java-hello-world";try {// ossClient.listObjects返回ObjectListing实例,包含此次listObject请求的返回结果。ObjectListing objectListing = ossClient.listObjects(bucketName);int size = objectListing.getObjectSummaries().size();System.out.println("=========查询到的对象数量是:"+size);// objectListing.getObjectSummaries获取所有文件的描述信息。for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {System.out.println(" ======================== " + objectSummary.getKey() + " " +"(size = " + objectSummary.getSize() + ")");}} catch (Exception e) {System.out.println("Error Message:" + e.getMessage());} finally {if (ossClient != null) {ossClient.shutdown();}}}/*** 删除某个对象*/public static void deleteObject(){// 填写Bucket名称String bucketName = "java-hello-world";// 填写Object完整路径,需要包含文件名,但不用包含Bucket名称String objectName = "test/test.txt";try {// 删除文件ossClient.deleteObject(bucketName, objectName);} catch (OSSException e) {System.out.println("Error Message:" + e.getMessage());} finally {if (ossClient != null) {ossClient.shutdown();}}}public static void main(String[] args) throws Exception {
// uploadFile();
// downloadFile();
// getObjectList();
// deleteObject();}}