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

Recommender systems: overview of the characteristics of architecture: user/item engineering -- -- -- -- -- -- -- -- > recall layer > sort layer - > test/evaluation 】 【 cold start problems, real-time 】

M3SDA: Moment matching for multi-source domain adaptation

我是一名阿里在职9年软件测试工程师,我的经历也许能帮到处于迷茫期的你

Interviewer Ali: Describe to me the phenomenon of cache breakdown, and talk about your solution?

推荐系统:开源项目/工具【谷歌:TensorFlow Recommenders】【Facebook:TorchRec】【百度:Graph4Rec】【阿里:DeepRec和EasyRec】

ERROR 1045 (28000) Access denied for user 'root'@'localhost'Solution

多线程的互斥锁应用RAII机制

How to install and use PostgreSQL 14.4

并发与并行的区别

360杜跃进:太空安全风险加剧,需打造一体化防御体系
随机推荐
如何优化OpenSumi终端性能?
多线程的互斥锁应用RAII机制
Redisson 的分布式锁找不到?
MySQL数据库 ---MySQL表的增删改查(进阶)
移动web开发01
历史上的今天:Win10 七周年;微软和雅虎的搜索协议;微软发行 NT 4.0
ImportError: attempted relative import with no known parent package
Centos7 install mysql8
Apple Silicon配置二进制环境(一)
centos7安装mysql8
[Ask] SQL statement to calculate the sum of column 2 by deduplicating column 1?
英文字母间隔突然增大(全角与半角转换)
线性结构:栈和队列
FFmpeg —— 裁剪视频(含音视频),不需编解码(附完整源码)
MySQL八股文背诵版
湖仓一体电商项目(四):项目数据种类与采集
【请教】SQL语句按列1去重来计算列2之和?
Can't find the distributed lock of Redisson?
MySQL kills 10 questions, how many questions can you stick to?
推荐系统:评估指标【离线评估指标:RMSE(均方根误差)、AUC、准确率、召回率、F1】【在线评估:A/B测试】【一般要求响应时间<0.5s】