阿里云oos对象存储服务"/>
SpringBoot引入阿里云oos对象存储服务
自留,今天给自己的springboot项目配置了OOS,记录一下防止忘记
文章目录
- 前言
- 一、oos是什么?
- 二、使用步骤
- 1.引入库
- 2.创建工具类(AliOssUtil)
- 3、工具类参数补充
- 4、交给Spring去管理这个类
- 5、测试
- 6、运行结果
- 总结
前言
其实最后好的方式是去官网看开发文档
话不多说,上代码
一、oos是什么?
就是对象存储,存储文件的云服务
二、使用步骤
1.引入库
代码如下(示例):
<!-- 阿里云oos--><dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.15.1</version></dependency>
2.创建工具类(AliOssUtil)
代码如下(示例):
package com.wendeyu.util;import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;import java.io.ByteArrayInputStream;@Data
@AllArgsConstructor
@Slf4j
public class AliOssUtil {private String endpoint;private String accessKeyId;private String accessKeySecret;private String bucketName;/*** 文件上传** @param bytes* @param objectName* @return*/public String upload(byte[] bytes, String objectName) {// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);try {// 创建PutObject请求。ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));} 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();}}//文件访问路径规则 https://BucketName.Endpoint/ObjectNameStringBuilder stringBuilder = new StringBuilder("https://");stringBuilder.append(bucketName).append(".").append(endpoint).append("/").append(objectName);log.info("文件上传到:{}", stringBuilder.toString());return stringBuilder.toString();}
}
3、工具类参数补充
4个属性对应的oos需要的4个参数(存储区域地址、阿里云认证的AccessKeyId、对应的密钥AccessKeySecret、最后一个是你创建的Bucket名称)
我们创建一个类来存放这4个属性(便于后期管理)
package com.wendeyu.properties;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
//@ConfigurationProperties(prefix = "sky.alioss")
@Data
public class AliOssProperties {private String endpoint = "oss-cn-beijing.aliyuncs";private String accessKeyId = "******";private String accessKeySecret="******";private String bucketName="******";
}
4、交给Spring去管理这个类
这些完成以后我们需要给该工具类作一个配置,让它进入IOC容器里面
创建配置类:
package com.wendeyu.serserver.config;import com.wendeyu.properties.AliOssProperties;
import com.wendeyu.util.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @author Jason Wen* @version 1.0** 配置类,用于创建AliOssUtil对象*/
@Configuration
@Slf4j
public class OssConfiguration {@Bean@ConditionalOnMissingBeanpublic AliOssUtil aliOssUtil(AliOssProperties aliOssProperties){log.info("开始创建阿里云文件上传工具类对象:{}",aliOssProperties);return new AliOssUtil(aliOssProperties.getEndpoint(),aliOssProperties.getAccessKeyId(),aliOssProperties.getAccessKeySecret(),aliOssProperties.getBucketName());}
}
5、测试
在测试类里面运行该代码
@Autowiredprivate AliOssUtil aliOssUtil;@Testvoid contextLoads() {String content = "Hello OSS";aliOssUtil.upload(content.getBytes(),"1.txt");}
6、运行结果
可以看见已经成功上传
总结
SpringBoot项目引入阿里云OOS对象存储的示例代码
更多推荐
SpringBoot引入阿里云oos对象存储服务
发布评论