public class OSSClientUtil {
private static final Logger logger = LoggerFactory.getLogger(OSSClientUtil.class);
private static final String ENDPOINT = "http://oss-cn-beijing.aliyuncs.com"; private static final String ACCESS_KEY_ID = "xxxxxxxxx"; private static final String ACCESS_KEY_SECRET = "xxxxxxxxxxxx"; private static final String BUCKET_NAME = "xxx";
public static final OSSClient OSSClient = new OSSClient(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
public static final boolean createBucket(OSSClient client, String bucketName) { Bucket bucket = client.createBucket(bucketName); return bucketName.equals(bucket.getName()); }
public static final String uploadObject2OSS(OSSClient client, File file, String bucketName, String diskName) { String resultStr = null; try { InputStream is = new FileInputStream(file); String fileName = file.getName(); Long fileSize = file.length(); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(is.available());
metadata.setContentEncoding("utf-8"); metadata.setContentType(getContentType(fileName));
PutObjectResult putResult = client.putObject(bucketName, diskName + fileName, is, metadata); resultStr = putResult.getETag(); } catch (Exception e) { e.printStackTrace(); } return resultStr; }
public static InputStream getOSS2InputStream(OSSClient client, String bucketName, String diskName, String key) { OSSObject ossObj = client.getObject(bucketName, diskName + key); return ossObj.getObjectContent(); }
public static InputStream getObjectInputStream(String key) { OSSObject ossObj = OSSClient.getObject(BUCKET_NAME, key); return ossObj.getObjectContent(); }
public static void deleteFile(String diskName) { OSSClient.deleteObject(BUCKET_NAME, diskName); logger.info("删除" + BUCKET_NAME + "下的文件" + diskName + "成功"); }
public static final String getContentType(String fileName) { String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1); if ("png".equalsIgnoreCase(fileExtension)) { return "image/png"; } if ("bmp".equalsIgnoreCase(fileExtension)) { return "image/bmp"; } if ("gif".equalsIgnoreCase(fileExtension)) { return "image/gif"; } if ("jpeg".equalsIgnoreCase(fileExtension) || "jpg".equalsIgnoreCase(fileExtension)) { return "image/jpeg"; } 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"; } if ("zip".equalsIgnoreCase(fileExtension)) { return "application/zip"; } return "text/html"; }
public static String scpFileToOss(String filePath, String diskName) { File file = new File(filePath); String md5key = uploadObject2OSS(OSSClient, file, BUCKET_NAME, diskName); logger.info("上传cdn成功,MD5数字唯一签名:" + md5key); String[] split = filePath.split("/"); return split[split.length - 1]; }
public static String scpFileToOss(File file, String diskName) { String md5key = uploadObject2OSS(OSSClient, file, BUCKET_NAME, diskName); logger.info("上传cdn成功,MD5数字唯一签名:" + md5key); return md5key; }
public static void scpFolderToOss(String folderPath, String diskName) { File dirFile = new File(folderPath); if (!dirFile.exists()) { System.out.println("文件夹不存在"); return; } if (!dirFile.isDirectory()) { System.out.println("不是一个文件夹路径"); return; } File[] fileList = dirFile.listFiles(); Arrays.stream(fileList).forEach(file -> scpFileToOss(file.getPath(), diskName)); }
public static void deleteFolder(String diskName) { ListObjectsRequest listObjectsRequest = new ListObjectsRequest(BUCKET_NAME); System.out.println(diskName); listObjectsRequest.setPrefix(diskName); ObjectListing listing = OSSClient.listObjects(listObjectsRequest); System.out.println("Objects:"); for (OSSObjectSummary objectSummary : listing.getObjectSummaries()) { System.out.println(objectSummary.getKey()); deleteFile(objectSummary.getKey()); } }
}
|