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

aliyunoss上传图片

依赖

        <dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.8.1</version></dependency>

配置文件

config:alioss:endpoint: oss-cn-shanghai.aliyuncs.com(节点名 我是上传了一张图片 然后根据图片地址上的节点信息截取下来的)access-key-id: LTAI4FdeNAAAAAAYGbQhpaccess-key-secret: 1myBBBBBBBBhrtxqSZbucket-name: bucketName(你的桶名字)

代码:

读取配置

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

注入bean配置类

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

上传工具类

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

controller

@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {@Autowiredprivate AliOssUtil aliOssUtil;/*** 文件上传* @param file* @return*/@PostMapping("/upload")@ApiOperation("文件上传")public Result<String> upload(MultipartFile file){log.info("文件上传:{}",file);try {//原始文件名String originalFilename = file.getOriginalFilename();//截取原始文件名的后缀   dfdfdf.pngString extension = originalFilename.substring(originalFilename.lastIndexOf("."));//构造新文件名称String objectName = UUID.randomUUID().toString() + extension;//文件的请求路径String filePath = aliOssUtil.upload(file.getBytes(), objectName);return Result.success(filePath);} catch (IOException e) {log.error("文件上传失败:{}", e);}return Result.error(MessageConstant.UPLOAD_FAILED);}
}
http://www.lryc.cn/news/172730.html

相关文章:

  • 动手吧,vue数字动画
  • Android12之仿Codec2.0实现传递编解码器组件本质(四十六)
  • MongoDB【部署 04】Windows系统实现MongoDB多磁盘存储
  • ruoyi框架使用自定义用户表登录
  • 计算机视觉与深度学习-卷积神经网络-卷积图像去噪边缘提取-卷积-[北邮鲁鹏]
  • JS手动实现发布者-订阅者模式
  • 【含面试题】MySQL死锁日志分析与解决的Java代码实现
  • 解决方案:TSINGSEE青犀+智能分析网关助力智慧仓储智能化监管
  • 进程间通信
  • Ubuntu 22.04.3 LTS安装
  • 记一次manjaro-i3系统sogoupinying候选词无法正常显示中文(变方框了)问题解决方案
  • Lua学习笔记:词法分析
  • flask服务鉴权
  • 【2023华为杯B题】DFT类矩阵的整数分解逼近(思路及代码下载)
  • 基于微信小程序的校园生活管理系统设计与实现(源码+lw+部署文档+讲解等)
  • SQL server 创建存储过程
  • 一文了解亚马逊云科技适用于 Amazon Lightsail 的托管数据库
  • 【antd Col】奇怪的TypeError: Cannot read properties of undefined (reading ‘then‘)
  • requests处理 multipart/form-data 请求以及 boundary值问题
  • FBX文件结构解读【文本格式】
  • JS基础语法
  • 【Zabbix监控一】zabbix的原理与安装
  • 图的十字链表存储结构
  • 精华回顾:Web3 前沿创新者在 DESTINATION MOON 共话未来
  • 【RPC】gRPC 安装及使用
  • Pygame中Sprite类的使用3
  • 23年下考前须知-软考中级信息安全工程师
  • 关于表单快速开发低代码技术平台的内容介绍
  • 比特币 ZK 赏金系列:第 1 部分——支付解密密钥
  • 【Python深度学习】深度学习中框架和模型的区别