cn.minsin.aliyun.oss.AliyunOssDownloadFunctions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mutils-aliyun-oss Show documentation
Show all versions of mutils-aliyun-oss Show documentation
mutils组件中提供阿里云对象储存(OSS)等操作,该组件不可单独引用至maven依赖。详情请查看AliyunOssUploadFunctions、AliyunOssDownloadFunctions、AliyunOssManageFunctions
官方文档:https://help.aliyun.com/document_detail/32008.html
package cn.minsin.aliyun.oss;
import cn.minsin.aliyun.oss.config.MutilsAliyunOssMultiProperties;
import cn.minsin.core.exception.MutilsErrorException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import java.io.File;
import java.io.InputStream;
/**
* 阿里云OSS 文件下载
* 官方文档 https://helpcdn.aliyun.com/document_detail/32014.html
*
* @author mintonzhang
* @date 2019年1月29日
* @since 0.2.9
*/
public class AliyunOssDownloadFunctions extends AliyunOssBaseFunctions {
protected AliyunOssDownloadFunctions(MutilsAliyunOssMultiProperties config) {
super(config);
}
/**
* 初始化配置文件
*
* @param prefix
* @throws MutilsErrorException
*/
public static AliyunOssDownloadFunctions init(String prefix) throws MutilsErrorException {
return new AliyunOssDownloadFunctions(loadConfig(prefix));
}
/**
* 下载文件
*
* @param fileName 文件名 调用{@link AliyunOssUploadFunctions} 获取此fileName
* 文件流
*/
public InputStream downloadFromOss(String fileName) {
OSS initClient = initClient();
try {
OSSObject object = initClient.getObject(childConfig.getBucketName(), fileName);
return object.getObjectContent();
} finally {
initClient.shutdown();
}
}
/**
* 下载文件
*
* @param fileName 文件名 调用{@link AliyunOssUploadFunctions} 获取此fileName
* @param file 存放oss下载出的文件
*/
public void downloadFromOss(String fileName, File file) {
OSS initClient = initClient();
try {
initClient.getObject(new GetObjectRequest(childConfig.getBucketName(), fileName), file);
} finally {
initClient.shutdown();
}
}
}