当前位置: 首页 > news >正文

苍穹外卖-创建阿里云oss工具包

添加配置信息:

sky:alioss:endpoint: ***access-key-id: ***access-key-secret: ***bucket-name: ***

把配置的内容转换成对象:

@Component
@ConfigurationProperties(prefix = "sky.alioss")
@Data
public class AliOssProperties {private String endpoint;private String accessKeyId;private String accessKeySecret;private String bucketName;
}

创建工具包对象:

@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();}
}

把工具包对象交给spring的ioc容器管理:

@Configuration
@Slf4j
public class OssConfiguration {@Bean@ConditionalOnMissingBean   //这个注解的意思是:如果没有这中bean的时候spring在创建bean对象public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties){log.info("开始创建阿里云文件上传工具类对象:{}", aliOssProperties);return new AliOssUtil(aliOssProperties.getEndpoint(),aliOssProperties.getAccessKeyId(),aliOssProperties.getAccessKeySecret(),aliOssProperties.getBucketName());}
}
http://www.dtcms.com/a/184106.html

相关文章:

  • 缓存(4):常见缓存 概念、问题、现象 及 预防问题
  • 关于物联网的基础知识(一)
  • 串口通讯
  • 【东枫科技】使用LabVIEW进行NVIDIA CUDA GPU 开发
  • 数据库分库分表实战指南:从原理到落地
  • ARMV8 RK3399 u-boot TPL启动流程分析 --start.S
  • JVM-类加载子系统
  • 机器学习极简入门:从基础概念到行业应用
  • 锚定基础与拥抱融合:C 语言在编程教育与技术社区的破圈之路
  • 全模态具身智能:从 VLM 到 MLLM
  • Java大师成长计划之第18天:Java Memory Model与Volatile关键字
  • 单片机-STM32部分:13、PWM
  • STM32智能窗帘系统:从零到一的开发实战
  • 开源字体设计工具字玩 FontPlayer
  • “爱生活”小项目问题总结
  • 【大模型】解决最新的Dify1.3.1版本 无法基于Ollama成功添加模型
  • LabVIEW的PID参数自适应控制
  • Nacos 从入门到实践:服务注册与配置中心完整指南
  • 兔子队列?RabbitMQ详解(1)
  • Springboot中如何自定义配置类
  • 【Tools】git使用详解以及遇到问题汇总
  • 【iOS】Tagged Pointer
  • 从0开始学习大模型--Day06--大模型的相关网络架构
  • 【Bluedroid】HID DEVICE 连接的源码分析
  • LWIP的ICMP协议
  • C++模板梳理
  • 网络编程(一)网络编程入门
  • go-gin
  • Redis--常见数据类型List列表
  • LOJ 6346 线段树:关于时间 Solution