aliyunOSS 封装(java)

package com.itcast.common.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;


public class OSSUtils {
    private static String endpoint  = "oss-cn-xxxxxxx.aliyuncs.com";
    private static String accessKeyId  = "xxxxxx";
    private static String accessKeySecret  = "xxxxxx";
    private static String bucketName = "xxxxxx";
    private static OSS ossClient;


    /**
     *@Title:uploadByInputStream
     *@Description 流上传
     *@Param ossPath OSS上的路径
     */
    public static void upload(String fileName, InputStream inputStream) {
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            ossClient.putObject(bucketName, fileName,inputStream);
        }catch (OSSException oe) {
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
        } catch (ClientException ce) {
            System.out.println("Error Message:" + ce.getMessage());
        }finally{
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    /**
     *@Title:uploadByInputStream
     *@Description 上传Byte数组
     *@Param fileName 文件名
     */
    public static void upload(String fileName,byte[] bytes) {
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            ossClient.putObject(bucketName, fileName,new ByteArrayInputStream(bytes));
        }catch (OSSException oe) {
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
        } catch (ClientException ce) {
            System.out.println("Error Message:" + ce.getMessage());
        }finally{
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    /**
     *@Title:download
     *@Description 流式下载
     *@param filePath 本地路径
     *@Param ossPath OSS上的路径
     */
    public static void download(String ossPath,String filePath) throws Exception{
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            ossClient.getObject(new GetObjectRequest(bucketName, ossPath), new File(filePath));
        } catch (OSSException oe) {
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());

        } catch (ClientException ce) {
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    /**
     * @Title: delete
     * @param ossPath OSS上的路径
     * @Description:
     */
    public static void delete(String ossPath){
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            ossClient.deleteObject(bucketName, ossPath);
        } catch (OSSException oe) {
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
        } catch (ClientException ce) {
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值