当前位置:网站首页>46: Chapter 4: develop file services: 7: integrate Alibaba OSS in [files] file services;
46: Chapter 4: develop file services: 7: integrate Alibaba OSS in [files] file services;
2022-07-22 01:19:00 【Small withered forest】
explain :
(1) The content of this blog : In the project , Integrated use of Alibaba OSS;
Catalog
One : stay 【imooc-news-dev-common】 In general engineering , Integrating Ali OSS;
1. stay 【imooc-news-dev-common】 In general engineering , Introducing Ali OSS rely on ;
One : stay 【imooc-news-dev-common】 In general engineering , Integrating Ali OSS;
and 【19: The third chapter : Developing a pass service :2: In the program , Get through Alibaba cloud SMS service ; Preliminary development 【 Send a text message , Interface 】;】 equally , For the content of the tool nature of this third-party service , We define it in 【common】 In general engineering ;
PS: To use OSS;JDK The version needs to be 1.7 And above ;
1. stay 【imooc-news-dev-common】 In general engineering , Introducing Ali OSS rely on ;
<dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.10.2</version> </dependency>
(1) For our specific business scenarios ; The way we upload , choice 【 Stream upload , The way 】, Instead of 【 Upload files , The way 】; then , I will choose 【 Streaming upload , How to upload network stream 】;
(2) namely , In our business here , When users upload pictures on the page , You'll get one MultipartFile; obtain MultipartFile after , We can go through MultipartFile Get one InputStream; then , You can use 【 Streaming upload , How to upload network stream 】 Go upload ;
2. stay 【files】 In the document subproject UploaderService Interface : Define a 【 Based on Ali OSS, How to upload files 】;
/** * How to upload files ( Based on Ali OSS) * @param file: Files from the front end ; then ,Controller When receiving documents , It's using MultipartFile To receive ; * @param userId: user id; * @param fileExtName: Extension of the file ; * @return * @throws Exception */ public String uploadOSS(MultipartFile file, String userId, String fileExtName) throws Exception;
explain :
(1) Method definition description ;
additional 1. stay 【files】 Resource files for file services file-dev.properties and file-prod.properties in : Definition 【 self-created bucket Of Endpoint】、【 self-created bucket The name of 】、【 The directory where you are going to store user avatars 】、【 Can point to this bucket Of url】;
# Ali OSS in , self-created 【imooc-news-dev-wgy】 This bucket Of endpoint file.endpoint=oss-cn-shanghai.aliyuncs.com # The use of bucket Name :imooc-news-dev-wgy file.bucketName=imooc-news-dev-wgy # When uploading files , The path of resources ;( Quite so in bucket in , Create different directory structures ; such as , We created image/face This directory , It is used to store user avatars ) file.objectName=image/face # Own Ali OSS in :【 The one I created Bucket Of , name 】+【Endpoint】 The path of ; Through this path , The only Alibaba that can determine itself in the whole network OSS Of Bucket; file.ossHost=https://imooc-news-dev-wgy.oss-cn-shanghai.aliyuncs.com/
explain :
(1)Endpoint: self-created bucket Of Endpoint;
● A little like this : Ali OSS There are servers in many regions ; I'm creating Bucket When , I chose Shanghai , therefore Bucket Of Endpoint Namely 【oss-cn-shanghai.aliyuncs.com】; If you choose Hangzhou ,Bucket Of Endpoint Namely 【oss-cn-hangzhou.aliyuncs.com】;
● therefore , This Endpoint, There are different Alibaba OSS The role of ;
(2)bucketName: self-created bucket Name ;
● I'm creating Bucket When , Its Bucket There is a warning of duplicate names ; therefore , In giving Bucket When you name it , Be careful not to have the same name as others ;
● therefore , Guess blindly ; In the same area ( such as , I chose Shanghai ), Its Bucket You can't duplicate names ;
● then ,【Endpoint】+【Bucket name 】 A combination of the two , can ( In Ali OSS in ) The only one to be sure of Bucket;
● PS: Ali OSS Medium Bucket, It's the equivalent of Redis One of the Libraries ; Or say , Like the logical space in the database ;
(3)objectName; The reason , To define a objectName, The logic is as follows :
● We created one Bucket; then , When we upload files later , Will upload to this Bucket in ;
● The files we will upload later include : The avatars , Article cover, etc ;
● In order to better distinguish these files , Make the classification of these documents clearer ; We want to put different documents , Stored in different folders ;
● therefore , Here we create 【file.objectName=image/face】; namely , The user avatar we uploaded , Will be stored in a 【 Belong to oss-cn-shanghai.aliyuncs.com This Endpoint Of 】、【imooc-news-dev-wgy This Bucket Medium 】、【/image/face】 Under the table of contents ;
(4)ossHost; The reason , To define a ossHost, The logic is as follows :
● We upload the file to Alibaba OSS after ; This image needs to be accessible elsewhere ; natural , You need the full path of this file ;
● We define here 【file.ossHost=https://imooc-news-dev-wgy.oss-cn-shanghai.aliyuncs.com/】; It is the first half of the full path of the future file ; You can see in this path , In fact, that is 【https】+【Bucket name 】+【Endpoint】;
additional 2. stay 【files】 The development of file service “ Get resource file information ” Configuration class ,FileResource Class : from file-dev.properties or file-prod.properties in , Get attribute value ;
3. stay 【files】 In the document subproject UploaderServiceImpl In the implementation class : To achieve 【 Based on Ali OSS, How to upload files 】;
/** * How to upload files ( Based on Ali OSS) * @param file: Files from the front end ; then ,Controller When receiving documents , It's using MultipartFile To receive ; * @param userId: user id; * @param fileExtName: Extension of the file ; * @return * @throws Exception */ @Override public String uploadOSS(MultipartFile file, String userId, String fileExtName) throws Exception { /** * 1. structure ossClient; The three parameters in turn are :【 Ali OSS, Created bucket Of Endpoint】、【 Personal Alibaba account Access Key Secret key id】、 * 【 Personal Alibaba account Access Key The secret key value of the secret key 】; */ OSS ossClient = new OSSClientBuilder() .build(fileResource.getEndpoint(), aliyunResource.getAccessKeyId(), aliyunResource.getAccessKeySecret()); //2. according to Controller From here MultipartFile( natural , This MultipartFile, It's from the front end to Controller Layer of ), obtain InputStream flow ; InputStream inputStream = file.getInputStream(); //3. utilize 【 front , We generate users id Of , Snowflake algorithm tool class 】, To generate a random string ; As the file name ( Part of ); String fileName = sid.nextShort(); /** * 4. Pieced together resource file path ; * What we put together here is 【image/face/ The user of the currently logged in user id/ File name generated based on snowflake Algorithm . File extension 】; * This means : We're going back OSS When uploading files , Want to upload the file to Bucket Of image/face/ The user of the currently logged in user id/ Under the table of contents ; */ String myObjectName = fileResource.getObjectName() + "/" + userId + "/" + fileName + "." + fileExtName; /** * 5. Upload files ; * Upload stream ; The three parameters in turn are :【 Ali OSS, Created bucket Name 】、【 Path to resource file 】、【 File stream 】; */ ossClient.putObject(fileResource.getBucketName(), myObjectName, inputStream); // 6. After uploading , close ossClient; ossClient.shutdown(); // 7. Put our spliced resource file path , Return to Controller;Controller When you get it , Will continue to splice this path , Get the full path of the file , And then it goes back to the front end ; return myObjectName; }
explain :
(1) First , To be in UploaderServiceImpl In the implementation class , Inject three objects :FileResource、AliyunResource、Sid;
(2) Read notes ;
4. stay 【files】 Document subproject FileUploaderController Class , To modify 【 Upload files , Interface 】;
5. effect ;
(1) First, the whole situation install Look at the whole project ;
(2) then , start-up 【user】 Customer service 、【files】 File service ;
(3) Remember to use SwitchHost Open the virtual domain name mapping 、 start-up Tomcat Run the front-end project ;;
……………………………………………………
(4) then , visit "http://writer.imoocnews.com:8080/imooc-news/writer/accountInfo.html", User information improvement page ; Upload a file with a size no larger than 500kb User's Avatar of ;
● On the front-end browser interface OK Of ;
then , Click on 【 determine 】 after , It also loads new pictures ;
● Back end console , We also printed out the expected log information ;
● then , Look at the front end , Its file path , It's also in line with our expectations ;
● then , Look at Ali OSS; namely , In the future, if there are multiple users , Will be in /image/face/ Under the table of contents , According to different users id, Create different folders , To store the avatar files of different users ;
边栏推荐
猜你喜欢
MQTT5.0新特性(比对3.1.1)
IP address segment classification
第一讲 概述
Check the version number of IAR project
Malloc et configurateur d'espace
Analyze the Enlightenment of children's programming thinking
44: Chapter 4: develop file service: 5: integrate fastdfs in [files] file service to realize the logic of [upload avatar]; (including: integrating fastdfs in the project; cross domain issues; creating
网络 IO 模型的演化过程
uniapp 使用 u-view 框架小程序的样式问题集合
人工神经网络的分类包括,人工神经网络的分类有
随机推荐
可视化:这十个数据可视化工具软件平台你必须知道
Cloud foundry 4: application lifecycle
Strategies for implementing steam school-based curriculum system
Websocket transfer file
uniapp 使用 u-view 框架小程序的样式问题集合
Cloud Foundry 4:应用程序的生命周期
1306_两个开源printf的资源使用对比测试
初识c语言之循环语句与分支语句
Mysql08(事务)
Diffusion Model
Centos7 online MySQL installation
(pytorch进阶之路七)Attention Based Seq2Seq
OpenGL (1)顶点缓冲
Digitalization opens the second growth curve. Huawei summarizes three scenarios of cloud transformation of operators
传输层协议解析——UDP和TCP
Qt:qstring and qstringlist
逍遙管理器安裝
Explore the interest in robot maker Education
malloc 和 空间配置器
Chrome实现自动化测试:录制回放网页动作