来吧,展示!SpringBoot OSS 整合全过程

前言

阿里云对象存储服务(Object Storage Service,简称 OSS),是阿里云提供的海量、安全、低成本、高可靠的云存储服务。其数据设计持久性不低于 99.9999999999%(12 个 9),服务设计可用性(或业务连续性)不低于 99.995%。

OSS 具有与平台无关的 RESTful API 接口,您可以在任何应用、任何时间、任何地点存储和访问任意类型的数据。

您可以使用阿里云提供的 API、SDK 接口或者 OSS 迁移工具轻松地将海量数据移入或移出阿里云 OSS。数据存储到阿里云 OSS 以后,您可以选择标准存储(Standard)作为移动应用、大型网站、图片分享或热点音视频的主要存储方式,也可以选择成本更低、存储期限更长的低频访问存储(Infrequent Access)和归档存储(Archive)作为不经常访问数据的存储方式。

登录阿里云,进入到控制台

来吧,展示!SpringBoot OSS 整合全过程

点击确定,就建好了。

  1. 接下来就开始附代码
    新建一个springboot项目
    导入依赖 pom.xml
<code> 

<

project

xmlns

=

"http://maven.apache.org/POM/4.0.0"

xmlns:xsi

=

"http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation

=

"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"

>

<

modelVersion

>

4.0.0

modelVersion

>

<

groupId

>

org.example

groupId

>

<

artifactId

>

SpringOOS

artifactId

>

<

version

>

1.0-SNAPSHOT

version

>

<

dependencies

>

<

dependency

>

<

groupId

>

org.springframework.boot

groupId

>

<

artifactId

>

spring-boot-starter-web

artifactId

>

<

version

>

2.3.0.RELEASE

version

>

dependency

>

<

dependency

>

<

groupId

>

org.springframework.boot

groupId

>

<

artifactId

>

spring-boot-starter-test

artifactId

>

<

scope

>

test

scope

>

<

version

>

2.3.0.RELEASE

version

>

dependency

>

<

dependency

>

<

groupId

>

com.aliyun.oss

groupId

>

<

artifactId

>

aliyun-sdk-oss

artifactId

>

<

version

>

3.4.2

version

>

dependency

>

<

dependency

>

<

groupId

>

org.projectlombok

groupId

>

<

artifactId

>

lombok

artifactId

>

<

version

>

1.18.4

version

>

<

scope

>

provided

scope

>

dependency

>

<

dependency

>

<

groupId

>

joda-time

groupId

>

<

artifactId

>

joda-time

artifactId

>

<

version

>

2.9.9

version

>

dependency

>

<

dependency

>

<

groupId

>

org.apache.commons

groupId

>

<

artifactId

>

commons-lang3

artifactId

>

<

version

>

3.8.1

version

>

dependency

>

dependencies

>

project

>

/<code>

application.yml

<code> 
 

oss:

endpoint:

oss-cn-shenzhen.aliyuncs.com

url:

https://oos-all.oss-cn-shenzhen.aliyuncs.com/

accessKeyId:

accessKeySecret:

bucketName:

/<code>
来吧,展示!SpringBoot OSS 整合全过程

来吧,展示!SpringBoot OSS 整合全过程

写一个config配置类

<code>

package

com.sykj.config;

import

lombok.Data;

import

org.springframework.beans.factory.

annotation

.Value;

import

org.springframework.context.

annotation

.Configuration;

import

java.io.Serializable;

public

class

OSSConfig

implements

Serializable

{

private

static

final

long serialVersionUID = -

119396871324982279L

;

private

String endpoint;

private

String url;

private

String accessKeyId;

private

String accessKeySecret;

private

String bucketName; } /<code>

工具类

<code>

package

com.sykj.util;

import

com.aliyun.oss.ClientConfiguration;

import

com.aliyun.oss.OSSClient;

import

com.aliyun.oss.common.auth.DefaultCredentialProvider;

import

com.aliyun.oss.model.ObjectMetadata;

import

com.sykj.config.OSSConfig;

import

org.springframework.web.multipart.MultipartFile;

import

java.io.IOException;

import

java.util.UUID;

public

class

OSSBootUtil

{

private

OSSBootUtil

()

{}

private

volatile

static

OSSClient ossClient =

null

;

public

static

String

upload

(OSSConfig ossConfig, MultipartFile file, String fileDir)

{ initOSS(ossConfig); StringBuilder fileUrl =

new

StringBuilder();

try

{ String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(

'.'

)); String fileName = System.currentTimeMillis() +

"-"

+ UUID.randomUUID().toString().substring(

0

,

18

) + suffix;

if

(!fileDir.endsWith(

"/"

)) { fileDir = fileDir.concat(

"/"

); } fileUrl = fileUrl.append(fileDir + fileName); System.out.println(fileUrl+

"-----------------"

); ObjectMetadata objectMetadata =

new

ObjectMetadata(); objectMetadata.setContentType(

"image/jpg"

); ossClient.putObject(ossConfig.getBucketName(), fileUrl.toString(), file.getInputStream(),objectMetadata); }

catch

(IOException e) { e.printStackTrace();

return

null

; } fileUrl = fileUrl.insert(

0

,ossConfig.getUrl());

return

fileUrl.toString(); }

private

static

OSSClient

initOSS

(OSSConfig ossConfig)

{

if

(ossClient ==

null

) {

synchronized

(OSSBootUtil

.

class

)

{

if

(ossClient ==

null

) { ossClient =

new

OSSClient(ossConfig.getEndpoint(),

new

DefaultCredentialProvider(ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret()),

new

ClientConfiguration()); } } }

return

ossClient; }

public

static

ResponseResult

delete

(String objectName,OSSConfig ossConfig)

{ initOSS(ossConfig); String fileName = objectName.replace(

"https://oos-all.oss-cn-shenzhen.aliyuncs.com/"

,

""

); System.out.println(fileName+

"******************************"

); ossClient.deleteObject(ossConfig.getBucketName(), fileName);

return

ResponseResult.ok(

"删除成功"

,fileName); } } /<code>

数据返回工具类

<code>

package

com.sykj.util;

import

lombok.AllArgsConstructor;

import

lombok.Data;

import

lombok.NoArgsConstructor;

import

java.io.Serializable;

public

class

ResponseResult

implements

Serializable

{

private

Integer code;

private

String message;

private

Object object;

public

static

ResponseResult

ok

(String message)

{

return

new

ResponseResult(

200

,message,

null

); }

public

static

ResponseResult

ok

(String message, Object object)

{

return

new

ResponseResult(

200

,message,object); }

public

static

ResponseResult

error

(String message)

{

return

new

ResponseResult(

500

,message,

null

); }

public

static

ResponseResult

error

(String message, Object o)

{

return

new

ResponseResult(

500

,message,o); } } /<code>

Service类

<code>

package

com.sykj.service;

import

com.sykj.util.ResponseResult;

import

org.springframework.http.ResponseEntity;

import

org.springframework.web.multipart.MultipartFile;

public

interface

CommonService

{

ResponseResult

uploadOSS

(MultipartFile file)

throws

Exception

;

ResponseResult

delete

(String objectName)

; } /<code>

Service实现类

<code>

package

com.sykj.service.impl;

import

com.sykj.config.OSSConfig;

import

com.sykj.service.CommonService;

import

com.sykj.util.OSSBootUtil;

import

com.sykj.util.ResponseResult;

import

org.springframework.beans.factory.annotation.Autowired;

import

org.springframework.http.HttpHeaders;

import

org.springframework.http.HttpStatus;

import

org.springframework.http.MediaType;

import

org.springframework.http.ResponseEntity;

import

org.springframework.stereotype.Service;

import

org.springframework.web.multipart.MultipartFile;

import

java.text.SimpleDateFormat;

import

java.util.Date;

import

java.util.HashMap;

import

java.util.Map; (

"commonService"

)

public

class

CommonServiceImpl

implements

CommonService

{

private

OSSConfig ossConfig;

public

ResponseResult

uploadOSS

(MultipartFile file)

throws

Exception

{ SimpleDateFormat simpleDateFormat=

new

SimpleDateFormat(

"yyyy-MM-dd"

); String format = simpleDateFormat.format(

new

Date()); String ossFileUrlBoot =

null

; ossFileUrlBoot = OSSBootUtil.upload(ossConfig, file,

"jpc/"

+format); System.out.println(ossFileUrlBoot); Map resultMap =

new

HashMap<>(

16

); resultMap.put(

"ossFileUrlBoot"

, ossFileUrlBoot);

return

ResponseResult.ok(

"上传成功~~"

,ossFileUrlBoot); }

public

ResponseResult

delete

(String objectName)

{ ResponseResult delete = OSSBootUtil.delete(objectName, ossConfig);

return

delete; } } /<code>

Controller类

<code> 

package

com.sykj.comtroller;

import

com.sun.org.apache.regexp.

internal

.RE;

import

com.sykj.service.CommonService;

import

com.sykj.util.ResponseResult;

import

org.slf4j.Logger;

import

org.slf4j.LoggerFactory;

import

org.springframework.beans.factory.

annotation

.Autowired;

import

org.springframework.http.HttpHeaders;

import

org.springframework.http.HttpStatus;

import

org.springframework.http.MediaType;

import

org.springframework.http.ResponseEntity;

import

org.springframework.web.bind.

annotation

.RequestMapping;

import

org.springframework.web.bind.

annotation

.RequestMethod;

import

org.springframework.web.bind.

annotation

.RequestParam;

import

org.springframework.web.bind.

annotation

.RestController;

import

org.springframework.web.multipart.MultipartFile;

public

class

CommonController

{

private

static

final

Logger logger = LoggerFactory.getLogger(CommonController

.

class

);

private

CommonService commonService;

public

ResponseResult uploadOSS( MultipartFile file) throws Exception { System.

out

.println(file.getInputStream());

return

ResponseResult.ok(

"ok"

); }

public

ResponseResult deltetOss(String objectName){ System.

out

.println(objectName+

"-------------------------------"

); ResponseResult delete = commonService.delete(objectName);

return

delete; } } /<code>

注意postman选择file

来吧,展示!SpringBoot OSS 整合全过程


这样就完成了

最后

感谢你看到这里,看完有什么的不懂的可以在评论区问我,觉得文章对你有帮助的话记得给我点个赞,每天都会分享java相关技术文章或行业资讯,欢迎大家关注和转发文章!


分享到:


相關文章: