AliyunOSS整合Spring的一些简单操作。

AliyunOSS整合Spring的一些简单操作。
2016-03-20 分类:插件工具 阅读(2190) 评论(0)
阿里云出品的对象存储服务——OSS

对象存储(Object Storage Service,简称OSS),是阿里云提供的海量、安全和高可靠的云存储服务。存储容量和处理能力的弹性扩展,按量付费真正使您专注于核心业务。您还可以方便的同其他云产品搭配使用,广泛的应用于海量数据存储与备份,数据加工与处理,内容加速分发,业务数据挖掘分析等多种业务场景

OSS工具博客已经更新,现在不需要整合到Spring内就可以很轻松的使用。

推荐大家请访问:阿里云 OSS 工具

aliyun_java_sdk_20160301.zip

OSSToolsJava

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;
    }
   /**
     * 签名url
     *
     * @author kira
     * @param bucketName
     * @param key
     * @param expiration
     *            有效期
     * @return
     */
    public static URL generatePresignedUrl(String bucketName, String key, Date expiration) {
        logger.info(ENDPOINTINFO + "获取URL,bucketName=" + bucketName + ",key=" + key);
        return ossClient.generatePresignedUrl(bucketName, key, expiration);
    }

    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;
    }

}

AppliactionContext.xmlXHTML

<!--配置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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值