SpringBoot引入阿里云oos对象存储服务

编程入门 行业动态 更新时间:2024-10-08 02:18:38

SpringBoot引入<a href=https://www.elefans.com/category/jswz/34/1770131.html style=阿里云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对象存储服务

本文发布于:2024-02-11 21:19:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1683556.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阿里   对象   SpringBoot   oos

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!