Skip to content

Commit

Permalink
集成OSS对象存储
Browse files Browse the repository at this point in the history
  • Loading branch information
x201206030 committed May 26, 2020
1 parent 9975fae commit c62acc2
Show file tree
Hide file tree
Showing 16 changed files with 211 additions and 15 deletions.
5 changes: 3 additions & 2 deletions novel-common/src/main/resources/application-common-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/novel_plus?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password:
password: test123456
driver-class-name: com.mysql.cj.jdbc.Driver
#Redis服务器IP
redis:
Expand All @@ -30,7 +30,8 @@ spring:

pic:
save:
type: 2 #图片保存方式, 1不保存使用网络图片2本地保存
type: 2 #图片保存方式, 1不保存使用爬取的网络图片2保存在自己的存储介质
storage: local #存储介质local本地OSS阿里云对象存储fastDfs分布式文件系统
path: /var/pic #图片保存路径


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void contextInitialized(ServletContextEvent sce) {
Book book = CrawlParser.parseBook(ruleBean, needUpdateBook.getCrawlBookId());
//这里只做老书更新
book.setId(needUpdateBook.getId());
if(needUpdateBook.getPicUrl()!=null && needUpdateBook.getPicUrl().startsWith(Constants.LOCAL_PIC_PREFIX)) {
if(needUpdateBook.getPicUrl()!=null && needUpdateBook.getPicUrl().contains(Constants.LOCAL_PIC_PREFIX)) {
//本地图片则不更新
book.setPicUrl(null);
}
Expand Down
12 changes: 12 additions & 0 deletions novel-front/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@
<version>${jest.version}</version>
</dependency>

<!--aliyunOSS-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.java2nb.novel.core.config;

import lombok.Data;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
* @author 11797
*/
@Data
@Component
@ConfigurationProperties(prefix="novel.file")
public class OssProperties{

private String endpoint;

private String keyId;

private String keySecret;

private String fileHost;

private String bucketName;

private String webUrl;


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.java2nb.novel.core.schedule;

import com.java2nb.novel.core.utils.Constants;
import com.java2nb.novel.entity.Book;
import com.java2nb.novel.service.BookService;
import lombok.RequiredArgsConstructor;
Expand All @@ -13,15 +14,15 @@
import java.util.List;

/**
* 网络图片转存本地任务
* 将爬取的网络图片转存为自己的存储介质(本地、OSS、fastDfs)任务
*
* @author Administrator
*/
@ConditionalOnProperty(prefix = "pic.save", name = "type", havingValue = "2")
@Service
@RequiredArgsConstructor
@Slf4j
public class Network2LocalPicSchedule {
public class CrawlPicTransSchedule {

private final BookService bookService;

Expand All @@ -41,7 +42,7 @@ public void trans() {
log.info("Network2LocalPicSchedule。。。。。。。。。。。。");


List<Book> networkPicBooks = bookService.queryNetworkPicBooks(100);
List<Book> networkPicBooks = bookService.queryNetworkPicBooks(Constants.LOCAL_PIC_PREFIX,100);
for (Book book : networkPicBooks) {
bookService.updateBookPicToLocal(book.getPicUrl(), book.getId());
//3秒钟转化一张图片,10分钟转化200张
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Constants {
public static final String TEMPLATE_PATH_PREFIX_KEY = "templatePathPrefixKey";

/**
* 本地图片保存前缀
* 保存图片到自己的存储介质路径前缀
* */
public static final String LOCAL_PIC_PREFIX = "/localPic/";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public interface FrontBookMapper extends BookMapper {

void addCommentCount(@Param("bookId") Long bookId);

List<Book> queryNetworkPicBooks(@Param("limit") Integer limit);
List<Book> queryNetworkPicBooks(@Param("localPicPrefix") String localPicPrefix, @Param("limit") Integer limit);

/**
* 按评分随机查询小说集合
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,17 @@ public interface BookService {

/**
* 查询网络图片的小说
*
* @param localPicPrefix
* @param limit 查询条数
* @return 返回小说集合
* */
List<Book> queryNetworkPicBooks(Integer limit);
List<Book> queryNetworkPicBooks(String localPicPrefix, Integer limit);


/**
* 更新小说网络图片到本地
* @param picUrl 网络图片路径
* 更新爬取的小说网络图片到自己的存储介质(本地、OSS、fastDfs)
* @param picUrl 爬取的网络图片路径
* @param bookId 小说ID
*/
void updateBookPicToLocal(String picUrl, Long bookId);
Expand All @@ -198,6 +200,7 @@ public interface BookService {
* @param userId 用户ID
* @param page 页码
* @param pageSize 分页大小
* @return 小说集合
* */
List<Book> listBookPageByUserId(Long userId, int page, int pageSize);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.java2nb.novel.service;


/**
* @author 11797
*/
public interface FileService {

/**
* 将爬取的网络图片转存为自己的存储介质(本地、OSS、fastDfs)
* @param picSrc 爬取的网络图片路径
* @param picSavePath 保存路径
* @return 新图片地址
* */
String transFile(String picSrc, String picSavePath);

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.java2nb.novel.search.BookSP;
import com.java2nb.novel.service.AuthorService;
import com.java2nb.novel.service.BookService;
import com.java2nb.novel.service.FileService;
import com.java2nb.novel.service.SearchService;
import com.java2nb.novel.vo.BookCommentVO;
import com.java2nb.novel.vo.BookSettingVO;
Expand Down Expand Up @@ -82,6 +83,8 @@ public class BookServiceImpl implements BookService {

private final SearchService searchService;

private final FileService fileService;


@SneakyThrows
@Override
Expand Down Expand Up @@ -460,14 +463,14 @@ public List<Integer> queryIndexNumByBookId(Long bookId) {
}

@Override
public List<Book> queryNetworkPicBooks(Integer limit) {
return bookMapper.queryNetworkPicBooks(limit);
public List<Book> queryNetworkPicBooks(String localPicPrefix, Integer limit) {
return bookMapper.queryNetworkPicBooks(localPicPrefix,limit);
}

@Override
public void updateBookPicToLocal(String picUrl, Long bookId) {

picUrl = FileUtil.network2Local(picUrl, picSavePath, Constants.LOCAL_PIC_PREFIX);
picUrl = fileService.transFile(picUrl, picSavePath);

bookMapper.update(update(book)
.set(BookDynamicSqlSupport.picUrl)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.java2nb.novel.service.impl;

import com.java2nb.novel.service.FileService;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

/**
* @author 11797
*/
@Service
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "pic.save", name = "storage", havingValue = "fastDfs")
public class FastDfsFileServiceImpl implements FileService {

@Override
public String transFile(String picSrc, String picSavePath) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.java2nb.novel.service.impl;

import com.java2nb.novel.core.utils.Constants;
import com.java2nb.novel.core.utils.FileUtil;
import com.java2nb.novel.service.FileService;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

/**
* @author 11797
*/
@Service
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "pic.save", name = "storage", havingValue = "local")
public class LocalFileServiceImpl implements FileService {

@Override
public String transFile(String picSrc, String picSavePath){

return FileUtil.network2Local(picSrc, picSavePath, Constants.LOCAL_PIC_PREFIX);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.java2nb.novel.service.impl;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.java2nb.novel.core.config.OssProperties;
import com.java2nb.novel.core.utils.Constants;
import com.java2nb.novel.core.utils.FileUtil;
import com.java2nb.novel.service.FileService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import java.io.File;

/**
* @author 11797
*/
@Service
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "pic.save", name = "storage", havingValue = "OSS")
@Slf4j
public class OssFileServiceImpl implements FileService {

private final OssProperties ossProperties;

@Override
public String transFile(String picSrc, String picSavePath) {

File file;
String filePath = FileUtil.network2Local(picSrc, picSavePath, Constants.LOCAL_PIC_PREFIX);
if (filePath.contains(Constants.LOCAL_PIC_PREFIX)) {
file = new File(picSavePath+filePath);
} else {
//默认图片不存储
return filePath;
}

filePath = filePath.replaceFirst(picSavePath,"");

filePath = filePath.startsWith("/") ? filePath.replaceFirst("/","") : filePath;


OSSClient ossClient = new OSSClient(ossProperties.getEndpoint(), ossProperties.getKeyId(), ossProperties.getKeySecret());
try {
//容器不存在,就创建
if (!ossClient.doesBucketExist(ossProperties.getBucketName())) {
ossClient.createBucket(ossProperties.getBucketName());
CreateBucketRequest createBucketRequest = new CreateBucketRequest(ossProperties.getBucketName());
createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
ossClient.createBucket(createBucketRequest);
}
//上传文件
PutObjectResult result = ossClient.putObject(new PutObjectRequest(ossProperties.getBucketName(), filePath, file));
//设置权限 这里是公开读
ossClient.setBucketAcl(ossProperties.getBucketName(), CannedAccessControlList.PublicRead);

if(result != null) {
return ossProperties.getWebUrl() + "/" + filePath;
}
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
//关闭
ossClient.shutdown();
file.delete();
}

return "/images/default.gif";
}


}
9 changes: 9 additions & 0 deletions novel-front/src/main/resources/application-oss.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#OSS
novel:
file:
endpoint: oss-cn-shenzhen.aliyuncs.com #不同的服务器地址不同
key-id: dhjjrue6767778878 #去OSS控制台获取
key-secret: dssdkkrkelrkellk44554 #去OSS控制台获取
bucket-name: novel #这个自己创建bucket时的命名控制台创建也行代码创建也行
file-host: pic #文件路径
web-url: https://xxyimg.oss-cn-hangzhou.aliyuncs.com #OSS文件的web访问地址
2 changes: 1 addition & 1 deletion novel-front/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ server:
spring:
profiles:
active: dev
include: alipay
include: alipay,oss


rabbitmq:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
select
id,pic_url from book
where pic_url like 'http%'
and pic_url not like concat('%',#{localPicPrefix},'%')
limit #{limit}
</select>

Expand Down

0 comments on commit c62acc2

Please sign in to comment.