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>
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐