码迷,mamicode.com
首页 > 其他好文 > 详细

JEESZ分布式架构集成阿里云OSS存储

时间:2018-07-20 16:49:17      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:删除   bucket   constant   throw   value   delete   secret   time   oid   

1. 服务接口定义

@RequestMapping(value = "/upload/base64Code", method = RequestMethod.POST)

public ResponseVO fileuploadBase64(@RequestBody JSONObject json, HttpServletRequest request, HttpServletResponse response) throws Exception {

String uid = json.optString("uid");

String userType = json.optString("userType");

String base64Code = json.optString("base64Code");

String oldName = json.optString("oldName");

String suffix = json.optString("suffix");

//获取当前登陆用户

if(StringUtils.isEmpty(uid)){

return CloudResponseCode.buildEnumResponseVO(CloudResponseCode.USER_ID_NOT_NULL, null);

}

//上传文件新名字

String newName = String.valueOf(new Date().getTime());

String fileKey =  CloudConstant.VITAL_USER_INFO_PATH + uid + "/" + newName + "." + suffix;

AliyunUtils.getInstance().uploadByte(base64Code.getBytes(), fileKey);

......

.......

return CloudResponseCode.buildEnumResponseVO(CloudResponseCode.FILEUPLOAD_SUCCESS, obj);

}

2. oss utils封装

 

public void uploadByte(byte[] fileByte, String fileKey){

// 创建OSSClient实例

OSSClient ossClient = new OSSClient(CloudConstant.ENDPOINT, CloudConstant.ACCESSKEYID, CloudConstant.ACCESSKEYSECRET);

// 上传byte数组

ossClient.putObject(CloudConstant.BUCKET, fileKey, new ByteArrayInputStream(fileByte));

// 关闭client

ossClient.shutdown();

}

 

public void uploadInputStream(InputStream inputStream, String fileKey){

// 创建OSSClient实例

OSSClient ossClient = new OSSClient(CloudConstant.ENDPOINT, CloudConstant.ACCESSKEYID, CloudConstant.ACCESSKEYSECRET);

// 上传文件流

ossClient.putObject(CloudConstant.BUCKET, fileKey, inputStream);

// 关闭client

ossClient.shutdown();

}

 

public void deleteFile(String fileKey){

// 创建OSSClient实例

OSSClient ossClient = new OSSClient(CloudConstant.ENDPOINT, CloudConstant.ACCESSKEYID, CloudConstant.ACCESSKEYSECRET);

// 删除文件

ossClient.deleteObject(CloudConstant.BUCKET, fileKey);

// 关闭client

ossClient.shutdown();

}

JEESZ分布式架构集成阿里云OSS存储

标签:删除   bucket   constant   throw   value   delete   secret   time   oid   

原文地址:https://www.cnblogs.com/JavaUI/p/9341955.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!