OSS基本概念
oss是一种用于分布式存储的服务,其特点是具有稳定、可靠、安全、低成本的特点。提供数据的可靠性,提供与平台无关的RESTful API接口 。
基础不在概述:
阿里云官方文档参考传送门:https://help.aliyun.com/product/31815.html?spm=a2c4g.11186623.6.540.be1b583eeWM3AV
简单介绍:
存储类型(Storage Class)
OSS 提供标准、低频访问、归档三种存储类型。
存储空间(Bucket)
存储空间是您用于存储对象(Object)的容器,所有的对象都必须隶属于某个存储空间。
对象/文件(Object)
对象是 OSS 存储数据的基本单元,也被称为 OSS 的文件。
地域(Region)
地域表示 OSS 的数据中心所在物理位置。(注:一个地域有创建bucket上限为30个)
访问域名(Endpoint)
Endpoint 表示 OSS 对外服务的访问域名。
访问密钥(AccessKey)
AccessKey(简称 AK)指的是访问身份验证中用到的 AccessKeyId 和 AccessKeySecret。
spring boot 集成oss:
maven引入Java ossSDK:
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.5.0</version>
</dependency>
创建yml文件:
aliyun:
oss:
access-key-secret: your accessKeySecret
access-key-id: your accessKeyId
endpoint: http: your endpoint
创建config文件用于配置:
package com.springboot_oss_01.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/***
* 读取AliyunOss.yml的配置信息
*
* **/
@Configuration
@ConfigurationProperties("aliyun.oss")
@Data
public class AliyunOssProperties {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
@Override
public String toString() {
return "AliyunOssProperties{" +
"endpoint='" + endpoint + '\'' +
", accessKeyId='" + accessKeyId + '\'' +
", accessKeySecret='" + accessKeySecret + '\'' +
'}';
}
public String getEndpoint() {
return endpoint;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public String getAccessKeyId() {
return accessKeyId;
}
public void setAccessKeyId(String accessKeyId) {
this.accessKeyId = accessKeyId;
}
public String getAccessKeySecret() {
return accessKeySecret;
}
public void setAccessKeySecret(String accessKeySecret) {
this.accessKeySecret = accessKeySecret;
}
public AliyunOssProperties() {
}
}
上述配置完成后,就可以写上传下载类了
ccreate class FileUpload.java
这里使用的是简单的文件上传,上传的方式官方有几种,根据需求进行选择
// 自动注入
@Autowired
AliyunOssProperties aliyunOssProperties;
public static boolean FileUpload(File file,String BucketName){
String endpoint=aliyunOssProperties.getEndpoint();
String accessKeyId =aliyunOssProperties.getAccessKeyId();
String accessKeySecret =aliyunOssProperties.getAccessKeySecret();
// 创建OSSClient实例。
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
String objectName = "";
if(file!=null){
try {
objectName=file.getName();
//判断是否有特殊字符”+“ 号 官方建议过滤这些带有特殊字符的文件,否则会出现签名不匹配的问题,具体请看问题总结
if(objectName.contains("+")){
objectName=objectName.replaceAll("\\+","%2b");
}
// String objectNameTest ="";
// objectNameTest= URLEncoder.encode(objectName,"utf-8").replaceAll("%2B","%2b");
// objectNameTest= URLDecoder.decode(objectName,"utf-8");
// System.out.println(objectNameTest);
// 上传文件。<yourLocalFile>由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt。
ossClient.putObject(BucketName, objectName,file);//("D:\\TestFile\\OSS_Flie\\RocketMQ.txt"));
// 关闭OSSClient。
ossClient.shutdown();
}catch (Exception e){
e.printStackTrace();
return false;
}
}
//调用方法查询文件是否存在
return FileExites(objectName,BucketName);
}
/**判断文件是否存在*/
public static boolean FileExites(String objectName,String bucketName){
String endpoint=aliyunOssProperties.getEndpoint();
String accessKeyId =aliyunOssProperties.getAccessKeyId();
String accessKeySecret =aliyunOssProperties.getAccessKeySecret();
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// 判断文件是否存在。doesObjectExist还有一个参数isOnlyInOSS,如果为true则忽略302重定向或镜像;如果
//为false,则考虑302重定向或镜像。
boolean found = ossClient.doesObjectExist(bucketName, objectName);
if(found){
System.out.println("上传成功!");
}else{
System.out.println("上传失败!");
}
System.out.println(found);
return found;
}
文件下载:
这里使用的是下载方式是流式下载: 可以直接放入response.getOutputStream();返回给客户端
create class FileDownload.java
public class DownloadFiles {
@Autowired
AliyunOssProperties aliyunOssProperties;
public void downloadFile(String objectName ,String filePath, String bucketName){
String endpoint=aliyunOssProperties.getEndpoint();
String accessKeyId =aliyunOssProperties.getAccessKeyId();
String accessKeySecret =aliyunOssProperties.getAccessKeySecret();
OSSClient ossClient= new OSSClient(endpoint, accessKeyId, accessKeySecret);
// ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
OSSObject ossObject = ossClient.getObject(bucketName, objectName);
BufferedInputStream in=null;
try {
// 读取文件内容。
System.out.println("Object content:");
in=new BufferedInputStream(ossObject.getObjectContent());
byte[] buffer = new byte[1024];
OutputStream out =new FileOutputStream("C:\\Users\\1\\Desktop\\OssTestFiles\\4.jpg");// response.getOutputStream();
int len = 0;
int i=0;
while ((len = in.read(buffer)) > 0) {
i=i+len;
out.write(buffer, 0, len);
}
System.out.println(i);
// 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
in.close();
out.close();
// 关闭OSSClient。
ossClient.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
总结问题:
1、上传文件的时候出现签名不匹配?
答:如果上传的文件带有一些特殊字符如”+“ ,会出现上传失败,签名不匹配的Code,出现的原因就是这些特殊字符在计算签名的时候被转换成了空字符串。提交工单,官方建议过滤一些带有特殊字符的文件,尽量避免带有特殊字符的文件上传。
2、如何分页查询文件?
答:官方文档有写,但是不好用,如果要分页查询,还是用表来控制比较好。
最重要的一点:看文档!一定要看文档需要什么功能就去看文档,实在不行的就百度,如果百度也没有,就提交工单和阿里技术支持进行沟通,能有效解决问题