准備工作
maven
pom.xml添加七牛雲的sdk依賴
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.2.27</version>
</dependency>
配置項
七牛雲上傳必要的配置有:accessKey、secretKey、bucket
其中accessKey、secretKey在該網址可查看
bucket為你的存儲空間名,如下:
實現
application.yml配置
upload:
qiniu:
domain: 填你的域名
access-key: 你的accesskey
secret-key: 你的secretKey
bucket: 你的存儲空間名,我這里為colablog
可以看到我的七牛雲上傳配置中有domain
這項配置,這個配置是七牛雲buket的存儲域名,在內容管理下,主要用於上傳文件成功后把文件訪問路徑返還回去。
但是這個域名是限時30天使用的,所以你最好綁定一個新的域名。
上傳配置類
使用SpringBoot的@ConfigurationProperties
和@Component
注解實現上傳的配置類UploadProperties
,
因為上傳配置可能會有本地上傳和雲上傳或者其他上傳的,所以該配置類我留了擴展點。因為受到了rabbitmq的配置類啟發,而且上傳的配置不會很多,所以用內部類來分割這種配置類。上傳配置類如下:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author Johnson
* @date 2019/12/16/ 09:35:36
*/
@Component
@ConfigurationProperties(prefix = "upload")
public class UploadProperties {
private Local local = new Local();
public Local getLocal() {
return local;
}
/**
* @author: Johnson
* @Date: 2019/12/16
* 本地上傳配置
*/
public class Local {
...
}
private Qiniu qiniu = new Qiniu();
public Qiniu getQiniu() {
return qiniu;
}
/**
* @author: Johnson
* @Date: 2019/12/16
* 七牛雲上傳配置
*/
public class Qiniu {
/**
* 域名
*/
private String domain;
/**
* 從下面這個地址中獲取accessKey和secretKey
* https://portal.qiniu.com/user/key
*/
private String accessKey;
private String secretKey;
/**
* 存儲空間名
*/
private String bucket;
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getAccessKey() {
return accessKey;
}
public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public String getBucket() {
return bucket;
}
public void setBucket(String bucket) {
this.bucket = bucket;
}
}
}
七牛雲上傳接口和類
上傳接口如下:
public interface UploadFile {
String uploadFile(MultipartFile file);
}
上傳類
import cn.colablog.blogserver.utils.properties.UploadProperties;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.UUID;
/**
* @author Johnson
* @date 2019/12/14/ 17:20:16
* 上傳文件到七牛雲
*/
public class UploadFileQiniu implements UploadFile {
private UploadProperties.Qiniu properties;
//構造一個帶指定Region對象的配置類
private Configuration cfg = new Configuration(Region.region2());
private UploadManager uploadManager= new UploadManager(cfg);
public UploadFileQiniu(UploadProperties.Qiniu properties) {
this.properties = properties;
}
/**
* @author: Johnson
*/
@Override
public String uploadFile(MultipartFile file) {
Auth auth = Auth.create(properties.getAccessKey(), properties.getSecretKey());
String token = auth.uploadToken(properties.getBucket());
try {
String originalFilename = file.getOriginalFilename();
// 文件后綴
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
String fileKey = UUID.randomUUID().toString() + suffix;
Response response = uploadManager.put(file.getInputStream(), fileKey, token, null, null);
return properties.getDomain() + fileKey;
} catch (IOException e) {
e.printStackTrace();
}
return "error";
}
}
Region
配置,這里代表空間的存取區域,因為我的存儲空間的區域為華南。所以為Region.region2()
,查看自己的存儲區域可在空間概覽的最下方查看到,這里就不截圖了,圖片占位太大。
Region
對應的設置:
好了,准備工作已完成,現在就到調用了,調用類如下:
@RestController
@RequestMapping("/upload")
public class UploadFileController {
@Autowired
UploadProperties uploadProperties;
@PostMapping("/img")
public String uploadFileYun(MultipartFile file) {
// 上傳到七牛雲
UploadFile uploadFile = new UploadFileQiniu(uploadProperties.getQiniu());
return uploadFile.uploadFile(file);
}
}
是不是很簡單呢?屁啊!簡單個毛線,其實這個我是已經簡化了,實際上在我的項目的結構是比這個復雜的。
總結
一:我的類名都是以Upload
開頭的,類名已經寫死了只有上傳功能,就限制了這個類的可擴展性了,假如添加文件刪除功能,就不應該添加到這個類中。現在已經在重構文件操作(非文件上傳了)的功能模塊了。
二:一開始我覺得文件上傳功能可能使用的比較少,所以使用到的時候才去實例化文件上傳類,但是這需要根據開發場景來決定,因為我的項目是一個博客后台管理系統,會經常上傳圖片,所以上傳文件類可以注入到Spring容器中,這樣也可以減少實例化的開銷(雖然很小)。注入的話就是用@Component
類注解。
三:配置文件我為什么會想到使用內部類來分割配置項呢?其實大家在編寫一些類似功能的時候,都可以去參照一下別人的源碼,當然,這里指的是大神的源碼。因為我在寫配置項的時候就想看看大神的配置項是怎么寫的,就點進了rabbitmq的配置項。所以啊,看到了大神的代碼是真的有長進的。
如果你需要查看更詳細的官方文檔,請點擊下方鏈接:
最后:感謝大家的閱讀,Thanks♪(・ω・)ノ
個人博客網址: https://colablog.cn/
如果我的文章幫助到您,可以關注我的微信公眾號,第一時間分享文章給您