阿里云出品的对象存储服务——OSS
对象存储(Object Storage Service,简称OSS),是阿里云提供的海量、安全和高可靠的云存储服务。存储容量和处理能力的弹性扩展,按量付费真正使您专注于核心业务。您还可以方便的同其他云产品搭配使用,广泛的应用于海量数据存储与备份,数据加工与处理,内容加速分发,业务数据挖掘分析等多种业务场景
OSS工具博客已经更新,现在不需要整合到Spring内就可以很轻松的使用。
推荐大家请访问:阿里云 OSS 工具
package util; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import com.aliyun.oss.ClientConfiguration; import com.aliyun.oss.OSSClient; import com.aliyun.oss.common.utils.IOUtils; import com.aliyun.oss.model.OSSObject; import com.aliyun.oss.model.OSSObjectSummary; import com.aliyun.oss.model.ObjectListing; /** * Aliyun OSS * 2016-1-20 15:10:21 * @author Jecced */ public class OSSTools { private String accessKeyId; private String accessKeySecret; private String bucketName; private String ossEndpoint; private int timeout; private OSSClient ossClient; public OSSTools() {} /** * OSS初始化 */ public void init() { ClientConfiguration conf = new ClientConfiguration(); conf.setConnectionTimeout(timeout); conf.setMaxErrorRetry(10); ossClient = new OSSClient("http://" + ossEndpoint, accessKeyId, accessKeySecret, conf); System.out.println("OSS初始化"); } public void destroy(){ ossClient.shutdown(); } /** * 指定的key是否存在 */ public boolean isExist(String key){ key = genKey(key); return ossClient.doesObjectExist(bucketName, key); } /** * 从OSS中获取文件输入流 */ public InputStream getObjeInputStream(String key){ key = genKey(key); OSSObject obj = ossClient.getObject(bucketName, key); return obj.getObjectContent(); } /** * 将输入流下载存到指定的File文件中 */ public void saveIsToFile(InputStream is ,File file){ FileOutputStream fos = null; try { fos = new FileOutputStream(file); byte[] buffer = new byte[1024 * 10]; int len = -1; while ((len = is.read(buffer)) != -1) { fos.write(buffer, 0, len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.safeClose(fos); } } /** * 文件下载,以流的形式 */ public void downObj(String key,File file){ key = genKey(key); InputStream is = getObjeInputStream(key); saveIsToFile(is, file); } /** * 简单上传OSS文件 * @param name 文件名 * @param file File对象 * @param path 存储路径 * @param contentType 手动设置文件类型:image/png * @return OSS文件Key的路径 */ public String uploadObj(String path, String name, File file,String contentType) { String key = path + "/" + name; key = genKey(key); ObjectMetadata meta = null; if(contentType != null){ meta = new ObjectMetadata(); meta.setContentType(contentType); } ossClient.putObject(bucketName, key, file, meta); return "http://" + bucketName + "." + ossEndpoint + "/" + key; } public String uploadObj(String path, String name, File file) { return uploadObj(path,name,file,null); } /** * 删除指定key */ public void delObj(String key){ ossClient.deleteObject(bucketName, key); } /** * 处理key开头是/,返回开头没有/的key */ private String genKey(String key){ if (key != null) { key = key.replaceAll("\\\\", "/"); } while (key != null && key.startsWith("/")) { key = key.substring(1, key.length()); } return key; } public OSSClient getOSSClient() { return ossClient; } public void setAccessKeyId(String accessKeyId) { this.accessKeyId = accessKeyId; } public void setAccessKeySecret(String accessKeySecret) { this.accessKeySecret = accessKeySecret; } public void setOssBucket(String ossBucket) { this.bucketName = ossBucket; } public void setOssEndpoint(String ossEndpoint) { this.ossEndpoint = ossEndpoint; } public void setTimeout(int timeout) { this.timeout = timeout; } }
<!--配置OSS--> <bean id="aliyun" class="Util.Aliyun" lazy-init="false" init-method="init" destroy-method="destroy"> <property name="accessKeyId" value="****AccessKey****"/> <property name="accessKeySecret" value="****AccessKeySecret****"/> <property name="bucketName" value="****BucketName****"/> <property name="timeout" value="5000"/> <property name="ossEndpoint" value="oss-cn-hangzhou.aliyuncs.com"/> </bean>
最新评论