阿里云OSS工具类
程序员文章站
2022-12-04 13:52:24
阿里云OSS对象存储java工具package me.zhengjie.utils;import com.aliyun.oss.OSSClient;import com.aliyun.oss.model.Bucket;import com.aliyun.oss.model.OSSObject;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annot...
package me.zhengjie.utils;
import cn.hutool.core.util.RandomUtil;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.OSSObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
/**
* 阿里云客户端
* @Auther: xushengyu
* @Date: 2020/11/11
*/
public class AliOSSClientUtil {
/** 日志 */
private static Logger logger = LoggerFactory.getLogger(AliOSSClientUtil.class);
/** 阿里云API的密钥Access Key ID */
private static String accessKeyId = "****";
/** 阿里云API的密钥Access Key Secret */
private static String accessKeySecret = "****";
/** 阿里云API的内或外网域名 */
private static String endpoint = "****";
/** 阿里云短信签名名称 */
private static String signName = "*****";
/** 阿里云短信模板代码 */
private static String templateCode = "*****";
@Value("{ali.folder}")
private String folder;
/**
* 获取阿里云OSS客户端对象
*
* @return ossClient
*/
public static OSSClient getOSSClient() {
return new OSSClient(endpoint, accessKeyId, accessKeySecret);
}
/**
* 创建存储空间
*
* @param ossClient OSS连接
* @param bucketName 存储空间
* @return
*/
public static String createBucketName(OSSClient ossClient, String bucketName) {
// 存储空间
final String bucketNames = bucketName;
if (!ossClient.doesBucketExist(bucketName)) {
// 创建存储空间
Bucket bucket = ossClient.createBucket(bucketName);
logger.info("创建存储空间成功");
return bucket.getName();
}
return bucketNames;
}
/**
* 删除存储空间buckName
*
* @param ossClient oss对象
* @param bucketName 存储空间
*/
public static void deleteBucket(OSSClient ossClient, String bucketName) {
ossClient.deleteBucket(bucketName);
logger.info("删除" + bucketName + "Bucket成功");
}
/**
* 根据key删除OSS服务器上的文件
*
* @param ossClient oss连接
* @param bucketName 存储空间
* @param folder 模拟文件夹名
* @param key Bucket下的文件的路径名+文件名 如:"upload/cake.jpg"
*/
public static void deleteFile(OSSClient ossClient, String bucketName, String folder, String key) {
ossClient.deleteObject(bucketName, folder + key);
logger.info("删除" + bucketName + "下的文件" + folder + key + "成功");
}
/**
* 创建模拟文件夹
*
* @param ossClient oss连接
* @param bucketName 存储空间
* @param folder 模拟文件夹名如
* @return 文件夹名
*/
public static String createFolder(OSSClient ossClient, String bucketName, String folder) {
// 文件夹名
final String keySuffixWithSlash = folder;
// 判断文件夹是否存在,不存在则创建
if (!ossClient.doesObjectExist(bucketName, keySuffixWithSlash)) {
// 创建文件夹
ossClient.putObject(bucketName, keySuffixWithSlash, new ByteArrayInputStream(new byte[0]));
logger.info("创建文件夹成功");
// 得到文件夹名
OSSObject object = ossClient.getObject(bucketName, keySuffixWithSlash);
String fileDir = object.getKey();
return fileDir;
}
return keySuffixWithSlash;
}
/**
* 上传图片至OSS
*
* @param ossClient oss连接
* @param file 上传文件(文件全路径如:D:\\image\\cake.jpg)
* @param bucketName 存储空间
* @param folder 模拟文件夹名
* @return String 返回文件url
*/
public static String upload(OSSClient ossClient, File file, String bucketName, String folder) {
String resultStr = null;
try {
InputStream is = new FileInputStream(file);
String fileName = file.getName();
ossClient.putObject(bucketName, folder + fileName, is);
resultStr = "http://"+endpoint+"/"+folder+fileName;
} catch (Exception e) {
e.printStackTrace();
logger.error("上传阿里云OSS服务器异常." + e.getMessage(), e);
}
return resultStr;
}
/**
* 通过文件名判断并获取OSS服务文件上传时文件的contentType
*
* @param fileName 文件名
* @return 文件的contentType
*/
public static String getContentType(String fileName) {
// 文件的后缀名
String fileExtension = fileName.substring(fileName.lastIndexOf("."));
if (".bmp".equalsIgnoreCase(fileExtension)) {
return "image/bmp";
}
if (".gif".equalsIgnoreCase(fileExtension)) {
return "image/gif";
}
if (".jpeg".equalsIgnoreCase(fileExtension) || ".jpg".equalsIgnoreCase(fileExtension)
|| ".png".equalsIgnoreCase(fileExtension)) {
return "image/jpeg";
}
if (".png".equalsIgnoreCase(fileExtension)) {
return "image/png";
}
if (".html".equalsIgnoreCase(fileExtension)) {
return "text/html";
}
if (".txt".equalsIgnoreCase(fileExtension)) {
return "text/plain";
}
if (".vsd".equalsIgnoreCase(fileExtension)) {
return "application/vnd.visio";
}
if (".ppt".equalsIgnoreCase(fileExtension) || "pptx".equalsIgnoreCase(fileExtension)) {
return "application/vnd.ms-powerpoint";
}
if (".doc".equalsIgnoreCase(fileExtension) || "docx".equalsIgnoreCase(fileExtension)) {
return "application/msword";
}
if (".xml".equalsIgnoreCase(fileExtension)) {
return "text/xml";
}
// 默认返回类型
return "";
}
/**
* 阿里云短信验证码
* @return
*/
public static String smsCode(String phone){
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
String smsCode = RandomUtil.randomNumbers(6);
request.setSysMethod(MethodType.POST);
request.setSysVersion("2017-05-25");
request.setSysDomain("dysmsapi.aliyuncs.com");
request.setSysAction("SendSms");
request.putQueryParameter("RegionId", "cn-hangzhou");
request.putQueryParameter("PhoneNumbers", phone);
request.putQueryParameter("SignName", signName);
request.putQueryParameter("TemplateCode", templateCode);
request.putQueryParameter("TemplateParam","{code:"+smsCode+"}");
try {
CommonResponse response = client.getCommonResponse(request);
String data = response.getData();
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return smsCode;
}
}
本文地址:https://blog.csdn.net/qq_41917773/article/details/109643323
上一篇: Web服务器软件