七牛文件上传工具类
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import saas.app.config.SaasAppProperties;
import javax.annotation.PostConstruct;
import java.io.File;
/**
* 七牛的工具类。
*
* @author Terry
*/
@Component
public class QiniuApiUtils {
private static final Logger logger = LoggerFactory.getLogger(QiniuApiUtils.class);
/**
* saas-common配置
*/
private final SaasAppProperties saasAppProperties;
/**
* 验证对象。
*/
private Auth auth;
/**
* 上传管理器。
*/
private UploadManager uploadManager;
/**
* bucket管理器。
*/
private BucketManager bucketManager;
@Autowired
public QiniuApiUtils(SaasAppProperties saasAppProperties) {
this.saasAppProperties = saasAppProperties;
}
/**
* 初始化。
*/
@PostConstruct
public void init() {
SaasAppProperties.QiniuConfig qiniuConfig = saasAppProperties.getQiniuConfig();
auth = Auth.create(qiniuConfig.getAccessKey(), qiniuConfig.getSecretKey());
Configuration cfg = new Configuration(Region.region0());
uploadManager = new UploadManager(cfg);
bucketManager = new BucketManager(auth, cfg);
}
/**
* 获得bucketName.
*
* @return
*/
public String getBucketName() {
return saasAppProperties.getQiniuConfig().getBucketName();
}
/**
* 生成上传文件的URL
*
* @param key 文件的object key,自己定义
* @return URL
*/
public String getUploadToken(String bucket, String key, long expires) {
return auth.uploadToken(bucket, key, expires, null);
}
/**
* 生成上传文件的URL,此方法可以支持后续fops操作,比如截图,转码等。。。
*
* @param key 文件的object key,自己定义
* @return URL
*/
public String getUploadToken(String bucket, String key, long expires, String fops, String pipeline) {
return auth.uploadToken(bucket, key, expires, new StringMap().putNotEmpty("persistentOps", fops)
.putNotEmpty("persistentPipeline", pipeline).putNotEmpty("persistentNotifyUrl", ""), true);
}
/**
* 复制一个资源
*
* @param from_bucket
* @param from_key
* @param to_bucket
* @param to_key
* @return
*/
public boolean copy(String from_bucket, String from_key, String to_bucket, String to_key) {
try {
bucketManager.copy(from_bucket, from_key, to_bucket, to_key);
return true;
} catch (QiniuException e) {
logger.error(e.getMessage(), e);
}
return false;
}
/**
* 移动一个资源
*
* @param from_bucket
* @param from_key
* @param to_bucket
* @param to_key
* @return
*/
public boolean move(String from_bucket, String from_key, String to_bucket, String to_key) {
try {
bucketManager.move(from_bucket, from_key, to_bucket, to_key);
return true;
} catch (QiniuException e) {
logger.error(e.getMessage(), e);
}
return false;
}
/**
* 移动一个资源
*
* @param bucket
* @param oldname
* @param newname
* @return
*/
public boolean rename(String bucket, String oldname, String newname) {
try {
bucketManager.rename(bucket, oldname, newname);
return true;
} catch (QiniuException e) {
logger.error(e.getMessage(), e);
}
return false;
}
/**
* 上传文件
*
* @param file
* @param key
* @param upToken
*/
public boolean uploadFile(File file, String key, String upToken) {
try {
Response res = uploadManager.put(file, key, upToken);
if (res.isOK()) {
return true;
}
} catch (QiniuException ex) {
logger.error(ex.toString(), ex);
}
return false;
}
/**
* 上传数据
*
* @param data
* @param key
* @param upToken
*/
public boolean uploadData(byte[] data, String key, String upToken) {
try {
Response res = uploadManager.put(data, key, upToken);
if (res.isOK()) {
return true;
}
} catch (QiniuException ex) {
logger.error(ex.toString(), ex);
}
return false;
}
/**
* 删除一个文件。
*
* @throws QiniuException
*/
public boolean delete(String bucket, String key) {
try {
bucketManager.delete(bucket, key);
return true;
} catch (QiniuException ex) {
logger.error(ex.toString(), ex);
}
return false;
}
/**
* 批量删除指定前缀的文件。
*
* @throws QiniuException
*/
public boolean deleteByPrefix(String bucket, String prefix) {
if (prefix.length() > 1) {
BucketManager.FileListIterator it = bucketManager.createFileListIterator(bucket, prefix);
while (it.hasNext()) {
FileInfo[] items = it.next();
BucketManager.BatchOperations ops = new BucketManager.BatchOperations();
for (FileInfo fi : items) {
ops.addDeleteOp(bucket, fi.key);
}
try {
bucketManager.batch(ops);
} catch (QiniuException ex) {
logger.error(ex.toString(), ex);
}
}
return true;
} else {
return false;
}
}
}
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* @author Terry
*/
@Configuration
@ConfigurationProperties(prefix = "saas.app")
public class SaasAppProperties {
/**
* 七牛文件上传配置
*/
private QiniuConfig qiniuConfig = new QiniuConfig();
public QiniuConfig getQiniuConfig() {
return qiniuConfig;
}
public void setQiniuConfig(QiniuConfig qiniuConfig) {
this.qiniuConfig = qiniuConfig;
}
/**
* 七牛组件配置
*/
public static class QiniuConfig {
/**
* 七牛access_key
*/
private String accessKey = "JcAQ2bOUHYEImYLjyTmyhNVhhxwBzVzRHqkN-Mnc";
/**
* 七牛密钥
*/
private String secretKey = "MgA6DRtoYIlkCpTSwOtySKvAkfqySD3622OeU6jd";
/**
* 七牛bucket名
*/
private String bucketName = "common";
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 getBucketName() {
return bucketName;
}
public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}
}
}
配置文件中添加
saas:
app:
qiniu-config:
access-key: "test"
secret-key: "test"
bucket-name: "test"
评论区