oss简单上传

package com.zyt.back.utils;

//import com.aliyun.oss.OSSClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Created by sumeng on 2018/7/25.
 */
public class AliOssUtil {
    private static final Logger logger = LoggerFactory.getLogger(AliOssUtil.class);

    public static String accessKeyId = "LTAIcSNsNoJqQvUr";
    public static String accessKeySecret = "zceKh1ZYYwN10RPjPe6WiLKEEr9ipd";
    public static OSSClient ossClient = null;
    public static String bucketName = "cube-yunze";
    public static String endpoint = "http://oss-cn-beijing.aliyuncs.com";
    public static String key = "<downloadKey>";

    //初始化
//     {
//
//        // 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建
//
//        // 创建ClientConfiguration实例
//        ClientConfiguration conf = new ClientConfiguration();
//        // 设置OSSClient使用的最大连接数,默认1024
//        conf.setMaxConnections(200);
//        // 设置请求超时时间,默认50秒
//        conf.setSocketTimeout(10000);
//        // 设置失败请求重试次数,默认3次
//        conf.setMaxErrorRetry(3);
//
//        // 创建OSSClient实例
//        ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
//
//    }

    public static OSSClient getOssClient() {
        return new OSSClient(endpoint, accessKeyId, accessKeySecret);
    }

    public static String getUniqueFilePre() {
        LocalDateTime localDateTime = LocalDateTime.now();
        String time = localDateTime.format(DateTimeFormatter.ofPattern("yyMMddHHmmssSSS"));
        return time;
    }

    public static boolean upload(MultipartFile file, String path) {
        System.out.println("-----------upload start time = " + System.currentTimeMillis());
        OSSClient ossClient = null;
        try{
            // 创建OSSClient实例
            ossClient = AliOssUtil.getOssClient();
            // 上传文件流
//            ossClient.putObject(AliOssUtil.bucketName, path, file.getInputStream());

            String fileName = file.getOriginalFilename();
            if(fileName.trim() != "") {
                File newFile = new File(fileName);
                FileOutputStream outStream = new FileOutputStream(newFile);
                outStream.write(file.getBytes());
                outStream.close();
                file.transferTo(newFile);
                InputStream inputStream = new FileInputStream(newFile);
                ossClient.putObject(AliOssUtil.bucketName, path, inputStream);
                newFile.delete();
            }
            System.out.println("-----------upload end time = " + System.currentTimeMillis());
            return true;
        }catch (Exception e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            logger.info("[oss-upload] error:" + sw.toString());
        }finally {
            // 关闭client
            ossClient.shutdown();
        }
        return false;
    }

    public static boolean delete(String path) {
        OSSClient ossClient = null;
        try{
            // 创建OSSClient实例
            ossClient = AliOssUtil.getOssClient();
            ossClient.deleteObject(AliOssUtil.bucketName, path);
            return true;
        }catch (Exception e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            logger.info("[oss-delete] error:" + sw.toString());
        }finally {
            // 关闭client
            ossClient.shutdown();
        }
        return false;
    }

    public static void main(String[] args) {

        try{
            //上传
            // 创建OSSClient实例
            OSSClient ossClient = AliOssUtil.getOssClient();
//            // 上传文件流
            File file = new File("F:/1.jpg");
            InputStream inputStream = new FileInputStream(file);
            ossClient.putObject(AliOssUtil.bucketName, "goods01"+"/" + getUniqueFilePre()+"-"+file.getName(), inputStream);

//            //删除
//            ossClient.deleteObject(AliOssUtil.bucketName, "commodity/171025161438789-file");

            // 关闭client
            ossClient.shutdown();

            // Object是否存在
//            boolean found = ossClient.doesObjectExist("<bucketName>", "<key>");
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值