FileIoOSS.java 7.2 KB
Newer Older
苗卫卫 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
package com.boco.nbd.wios.downloadfile.fileio;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;
import com.boco.nbd.wios.downloadfile.mapper.def.CptDataInfoMapper;
import com.boco.nbd.wios.downloadfile.model.CptDataInfo;
import com.boco.nbd.wios.downloadfile.model.CptDataStore;
import com.boco.nbd.wios.downloadfile.service.DataStoreService;
import com.ihidea.core.support.exception.ServiceException;
import com.ihidea.core.util.JSONUtilsEx;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 阿里云存储对象OSS
 * @author wenhao
 */
@Component
@Lazy
public class FileIoOSS implements IFileIo {

	private static Map<String, Map<String, String>> bucketMap = new HashMap<>(8);
	
	private static OSS ossClient = null;
	
	@Autowired
	private DataStoreService dataStoreService;

	@Autowired
	private CptDataInfoMapper dataInfoDao;

	@SuppressWarnings("unchecked")
	private synchronized Map<String, String> initBucketInfo(String storeName) {

		CptDataStore cptDataStore = dataStoreService.getInfoByName(storeName);
		Map<String, String> bucketInfo = JSONUtilsEx.deserialize(cptDataStore.getPath(), Map.class);
		bucketMap.put(storeName, bucketInfo);

		return bucketInfo;
	}
	
	private synchronized void initOssClient(Map<String, String> bucketInfo) {
//		ossClient = new OSSClient(bucketInfo.get("endpoint"), bucketInfo.get("accessKeyId"), bucketInfo.get("accessKeySecret"));
		ossClient = new OSSClientBuilder().build(bucketInfo.get("endpoint"), bucketInfo.get("accessKeyId"), bucketInfo.get("accessKeySecret"));
	}

	public Map<String, String> getBucketInfo(String storeName){
		Map<String, String> bucketInfo = bucketMap.get(storeName);

		// 根据storeName得到参数
		if (bucketInfo == null) {
			bucketInfo = initBucketInfo(storeName);
		}
		
		if(ossClient == null){
			initOssClient(bucketInfo);
		}
		
		return bucketInfo;
	}
	
	/**
	 * 保存到OSS
	 */
	@Override
	public void save(FileIoEntity entity) {
		String id= StringUtils.isNotBlank(entity.getDataInfo().getPrefix()) ? entity.getDataInfo().getPrefix()+"/"+entity.getDataInfo().getId() : entity.getDataInfo().getId();
		saveFile(id, entity.getDataInfo().getFileName(), entity.getContent(), entity.getDataInfo().getStoreName());
	}

	/**
	 * 保存到备份存储OSS
	 */
	@Override
	public void saveBak(FileIoEntity entity) {
		throw new ServiceException("未实现");
	}


	private void saveFile(String id, String name, byte[] content, String storeName) {

		try{
			Map<String, String> bucketInfo = getBucketInfo(storeName);
			
			ossClient.putObject(bucketInfo.get("bucketName"), bucketInfo.get("key") + id, new ByteArrayInputStream(content));
		}catch (Exception e) {
			throw new ServiceException("阿里云存储对象OSS上传出现异常:" + e.getMessage(), e);
		}
		
	}

	@Override
	public boolean remove(FileIoEntity entity) {
		throw new ServiceException("未实现");
	}

	@Override
    public byte[] get(String id) {
		
		byte[] data = null;
		
		CptDataInfo dataInfo = dataInfoDao.selectByPrimaryKey(id);
		if(dataInfo != null){
			Map<String, String> bucketInfo = getBucketInfo(dataInfo.getStoreName());
			
			OSSObject ossObject = ossClient.getObject(bucketInfo.get("bucketName"),  bucketInfo.get("key") + id);
			
	        try {
	        	InputStream inputStream = ossObject.getObjectContent();
				data=toByteArray(inputStream);
				inputStream.close();
			} catch (IOException e) {
				throw new ServiceException("阿里云存储对象OSS获得对象出现异常:" + e.getMessage());
			}
		}
		return data;
	}

	@Override
	public void execute(FileIoEntity fileIoEntity, IFileInputStream fileInputStreamImpl) throws Exception {
		throw new ServiceException("未实现");
	}

	@Override
	public void updateContent(String id, byte[] content) {
		throw new ServiceException("未实现");
	}
	
	private static byte[] toByteArray(InputStream in) throws IOException {
        ByteArrayOutputStream out=new ByteArrayOutputStream();
        byte[] buffer=new byte[1024*4];
        int n=0;
        while ( (n=in.read(buffer)) !=-1) {
            out.write(buffer,0,n);
        }
        return out.toByteArray();
    }
	
	public void deleteFile(String id, String storeName, String prefix) {
		try{
			Map<String, String> bucketInfo = getBucketInfo(storeName);
			
			ossClient.deleteObject(bucketInfo.get("bucketName"), bucketInfo.get("key") +(StringUtils.isNotBlank(prefix) ? prefix+"/"+id : id));
		}catch (Exception e) {
			throw new ServiceException("阿里云OSS批量删除文件出现异常:" + e.getMessage(), e);
		}
	}
	
	public void deleteFileBatch(List<String> idList, String storeName, String prefix) {
		try{
			Map<String, String> bucketInfo = getBucketInfo(storeName);
			
			List<String> keys=new ArrayList<>();
			for (String id : idList) {
				keys.add(bucketInfo.get("key") + (StringUtils.isNotBlank(prefix) ? prefix+"/"+id : id));
			}
			
			DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketInfo.get("bucketName")).withKeys(keys));
			List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
			System.out.println("成功删除:"+deletedObjects.toString());
			
		}catch (Exception e) {
			throw new ServiceException("阿里云OSS批量删除文件出现异常:" + e.getMessage(), e);
		}
	}
	
	public void copyFile(String sourceFileName, String destFileName, String storeName) {
		try{
			Map<String, String> bucketInfo = getBucketInfo(storeName);
			
			CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucketInfo.get("bucketName"), bucketInfo.get("key") + sourceFileName, bucketInfo.get("bucketName"), bucketInfo.get("key") +destFileName);

			ossClient.copyObject(copyObjectRequest);
		}catch (Exception e) {
			throw new ServiceException("阿里云OSS复制文件出现异常:" + e.getMessage(), e);
		}
	}
	
	public boolean exist(String id, String storeName, String prefix) {
		try{
			Map<String, String> bucketInfo = getBucketInfo(storeName);
			

			return ossClient.doesObjectExist(bucketInfo.get("bucketName"), bucketInfo.get("key") + (StringUtils.isNotBlank(prefix) ? prefix+"/"+id : id));
		}catch (Exception e) {
			throw new ServiceException("阿里云检查文件是否存在出现异常:" + e.getMessage(), e);
		}
	}
	
	/**
	 * 获取文件列表,最多100个
	 * @param prefix
	 * @param storeName
	 * @return
	 */
	public List<String> list(String prefix, String storeName) {
		try{
			Map<String, String> bucketInfo = getBucketInfo(storeName);
			
			ObjectListing objectListing = ossClient.listObjects(bucketInfo.get("bucketName"), bucketInfo.get("key")+prefix);
			
			List<String> fileIdList=new ArrayList<>();
			List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
			for (OSSObjectSummary s : sums) {
				fileIdList.add(s.getKey());
			}
			return fileIdList;
		}catch (Exception e) {
			throw new ServiceException("阿里云列举文件出现异常:" + e.getMessage(), e);
		}
	}
	

	
}