当前位置:网站首页>OSS简单上传图片
OSS简单上传图片
2022-07-30 19:50:00 【ldj2020】
package com.atguigu.oss.service.impl;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.atguigu.oss.service.OssService;
import com.atguigu.oss.utils.ConstantPropertiesUtils;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
@Service
public class OssServiceImpl implements OssService {
//上传头像到oss
@Override
public String uploadFileAvatar(MultipartFile file) {
// 工具类获取常量值也可以配置到配置文件@Value(${xxx})获取
String endpoint = ConstantPropertiesUtils.END_POIND;
String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
String bucketName = ConstantPropertiesUtils.BUCKET_NAME;
try {
// 创建OSS实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
//获取上传文件输入流
InputStream inputStream = file.getInputStream();
//获取文件名称
String fileName = file.getOriginalFilename();
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
fileName = uuid + fileName;
String datePath = new DateTime().toString("yyyy/MM/dd");
fileName = datePath + "/" + fileName;
/**
* 调用oss方法实现上传
* 第一个参数 Bucket名称
* 第二个参数 上传到oss文件路径和文件名称
* 第三个参数 上传文件输入流
*/
ossClient.putObject(bucketName, fileName, inputStream);
// 关闭OSSClient。
ossClient.shutdown();
//需要把上传到阿里云oss路径手动拼接出来
return "https://" + bucketName + "." + endpoint + "/" + fileName;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
边栏推荐
- 历史上的今天:Win10 七周年;微软和雅虎的搜索协议;微软发行 NT 4.0
- 推荐系统:实时性【特征实时性:客户端实时特征(秒级,实时)、流处理平台(分钟级,近实时)、分布式批处理平台(小时/天级,非实时)】【模型实时性:在线学习、增量更新、全量更新】
- Download and installation of the latest version of MySQL 8.0 under Linux (detailed steps)
- Trial writing C language sanbang
- MindSpore:mindspore有没有类似tf.GradientTape()用来求解梯度的?
- 推荐系统:冷启动问题【用户冷启动、物品冷启动、系统冷启动】
- MindSpore:ImageFolderDataset数据读取问题
- MySQL sub-database sub-table
- 阿里面试官:给我描述一下缓存击穿的现象,并说说你的解决思路?
- How do radio waves transmit information?
猜你喜欢

Snowflake vs. Redshift的2022战报:两个数据平台谁更适合你?

【无标题】多集嵌套集合使不再有MultipleBagFetchException

【视频】极值理论EVT与R语言应用:GPD模型火灾损失分布分析

MySQL Functions (Classic Collection)

These services can't ali interview?Then don't go to, the basic notification, etc
Database Tuning - Database Tuning

PostgreSQL 14.4如何安装使用

Frog jumping steps (recursive and non-recursive) ------- Xiaolele walks the steps

PHP低代码开发平台 V5.0.7新版发布

LeetCode 0952.按公因数计算最大组件大小:建图 / 并查集
随机推荐
MySQL函数(经典收藏)
SimpleOSS third-party library libcurl and engine libcurl error solution
mysql慢查询优化
青蛙跳台阶(递归和非递归)-------小乐乐走台阶
MindSpore:对image作normalize的目的是什么?
After MySQL grouping, take the largest piece of data [optimal solution]
Linux下最新版MySQL 8.0的下载与安装(详细步骤)
[PyTorchVideo Tutorial 01] Quickly implement video action recognition
如何优化OpenSumi终端性能?
Typora设置标题自动标号
MySQL slow query optimization
已删除
Day31 LeetCode
MindSpore:【JupyterLab】按照新手教程训练时报错
“数字化重构系统,搞定 CEO 是第一步”
MySQL夺命10问,你能坚持到第几问?
Linux下安装MySQL教程
MySQL六脉神剑,SQL通关大总结
Interviewer Ali: Describe to me the phenomenon of cache breakdown, and talk about your solution?
MySQl数据库————DQL数据查询语言