当前位置:网站首页>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;}}}边栏推荐
猜你喜欢

win2003下FTP服务器如何搭建

centos7安装mysql8

Centos7 install mysql8

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

360杜跃进:太空安全风险加剧,需打造一体化防御体系

The technology is very powerful, do you still need to "manage up"?

To the operation of the int variable assignment is atom?

Cesium加载离线地图和离线地形

How to install and use PostgreSQL 14.4

ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
随机推荐
TensorFlow2:概述
【请教】SQL语句按列1去重来计算列2之和?
Zabbix 5.0 Monitoring Tutorial (1)
对int变量赋值的操作是原子的吗?
明解C语言第七章习题
多线程的互斥锁应用RAII机制
After watching "Second Uncle", I was even more internalized
055 c# print
These services can't ali interview?Then don't go to, the basic notification, etc
数据库索引:索引并不是万能药
Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案
MySQL大批量造数据
centos7安装mysql8
Linux download and install mysql5.7 version tutorial the most complete and detailed explanation
Linux下安装Mysql5.7,超详细完整教程,以及云mysql连接
使用MULTISET来比较数据集的实例介绍
win2003下FTP服务器如何搭建
el-input can only input integers (including positive numbers, negative numbers, 0) or only integers (including positive numbers, negative numbers, 0) and decimals
网络层协议------IP协议
Linux下载安装mysql5.7版本教程最全详解