当前位置:网站首页>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;
}
}
}
边栏推荐
- MindSpore:【语音识别】DFCNN网络训练loss不收敛
- Another company interview
- MindSpore:mindspore有没有类似tf.GradientTape()用来求解梯度的?
- [hbuilder] cannot run some projects, open the terminal and cannot enter commands
- linux下mysql8安装
- LeetCode 0952. Calculate Maximum Component Size by Common Factor: Mapping / Union Search
- Centos7 install mysql8
- Mac安装PHP开发环境
- 在jOOQ中获取数据的多种不同方式
- MySQL夺命10问,你能坚持到第几问?
猜你喜欢

MySql密码
Typora设置标题自动标号

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

推荐系统:冷启动问题【用户冷启动、物品冷启动、系统冷启动】

推荐系统:实时性【特征实时性:客户端实时特征(秒级,实时)、流处理平台(分钟级,近实时)、分布式批处理平台(小时/天级,非实时)】【模型实时性:在线学习、增量更新、全量更新】

【MindSpore】用coco2017训练Model_zoo上的 yolov4,迭代了两千多batch_size之后报错,大佬们帮忙看看。

从离线到实时对客,湖仓一体释放全量数据价值

Download Win11 how to change the default path?Download Win11 change the default path method

推荐系统:评估指标【离线评估指标:RMSE(均方根误差)、AUC、准确率、召回率、F1】【在线评估:A/B测试】【一般要求响应时间<0.5s】

VBA batch import Excel data into Access database
随机推荐
推荐系统:实时性【特征实时性:客户端实时特征(秒级,实时)、流处理平台(分钟级,近实时)、分布式批处理平台(小时/天级,非实时)】【模型实时性:在线学习、增量更新、全量更新】
MySQL six-pulse sword, SQL customs clearance summary
Frog jumping steps (recursive and non-recursive) ------- Xiaolele walks the steps
时间复杂度与空间复杂度
Linux下安装MySQL教程
一文2500字手把手教你配置Jenkins自动化邮件通知
牛客网——华为题库(100~108)
刷题记录----字符串
VBA connects Access database and Excel
The 17th "Revitalization Cup" National Youth Vocational Skills Competition - Computer Programmers (Cloud Computing Platform and Operation and Maintenance) Participation Review and Summary
MySQL八股文背诵版
el-input can only input integers (including positive numbers, negative numbers, 0) or only integers (including positive numbers, negative numbers, 0) and decimals
M3SDA: Moment matching for multi-source domain adaptation
[Private Series] All kinds of strange problems encountered in daily PHP
MySQL slow query optimization
试写C语言三子棋
What is the difference between a cloud database and an on-premises database?
推荐系统:AB测试(AB Test)
Correct pose of Vulkan open feature
Zabbix 5.0 Monitoring Tutorial (1)