当前位置:网站首页>OSS simply upload pictures
OSS simply upload pictures
2022-07-30 20:03: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;@Servicepublic class OssServiceImpl implements OssService {//Upload avatar to [email protected] String uploadFileAvatar(MultipartFile file) {// The constant value obtained by the tool class can also be configured to the configuration file @Value(${xxx}) to obtainString endpoint = ConstantPropertiesUtils.END_POIND;String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;String bucketName = ConstantPropertiesUtils.BUCKET_NAME;try {// Create an OSS instance.OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);//Get the upload file input streamInputStream inputStream = file.getInputStream();//get file nameString fileName = file.getOriginalFilename();String uuid = UUID.randomUUID().toString().replaceAll("-", "");fileName = uuid + fileName;String datePath = new DateTime().toString("yyyy/MM/dd");fileName = datePath + "/" + fileName;/*** Call the oss method to upload* The first parameter Bucket name* The second parameter uploads to the oss file path and file name* The third parameter upload file input stream*/ossClient.putObject(bucketName, fileName, inputStream);// Close OSSClient.ossClient.shutdown();//Need to manually splice the oss path uploaded to Alibaba Cloudreturn "https://" + bucketName + "." + endpoint + "/" + fileName;} catch (Exception e) {e.printStackTrace();return null;}}}
边栏推荐
猜你喜欢
MySQL six-pulse sword, SQL customs clearance summary
推荐系统:冷启动问题【用户冷启动、物品冷启动、系统冷启动】
Database Tuning - Database Tuning
Weak Banks to data conversion ability?Matt software help solve bank dilemma
Linux下载安装mysql5.7版本教程最全详解
移动web开发01
ERROR 1045 (28000) Access denied for user 'root'@'localhost'Solution
[Node implements data encryption]
4年测试经验去面试10分钟就被赶出来了,面试官说我还不如应届生?都这么卷吗...
PHP低代码开发平台 V5.0.7新版发布
随机推荐
KEIL问题:【keil Error: failed to execute ‘C:\Keil\ARM\ARMCC‘】
YOLO V3详解
centos7安装mysql8
【视频】极值理论EVT与R语言应用:GPD模型火灾损失分布分析
MySQL分组后取最大一条数据【最优解】
The JDBC programming of the MySQL database
Recommender systems: overview of the characteristics of architecture: user/item engineering -- -- -- -- -- -- -- -- > recall layer > sort layer - > test/evaluation 】 【 cold start problems, real-time 】
To the operation of the int variable assignment is atom?
Download Win11 how to change the default path?Download Win11 change the default path method
推荐系统-排序层:排序层架构【用户、物品特征处理步骤】
Linux下最新版MySQL 8.0的下载与安装(详细步骤)
阿里面试官:给我描述一下缓存击穿的现象,并说说你的解决思路?
数据库索引:索引并不是万能药
Multi-threaded mutex application RAII mechanism
【PM专用】快速统计团队还有谁没有登记上报信息,快速筛选出属于自己项目组的成员,未完成XXX工作事项的名单
【PyTorchVideo教程01】快速实现视频动作识别
[PyTorchVideo Tutorial 01] Quickly implement video action recognition
PHP低代码开发平台 V5.0.7新版发布
Mac安装PHP开发环境
Swift简介