Aws对象存储工具类




import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.S3ClientOptions;
import com.amazonaws.services.s3.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

//import com.amazonaws.SdkClientException;
//import com.amazonaws.services.clouddirectory.model.DeleteObjectRequest;

/**
 * aws3对象存储工具类 
 * @author 
 *
 */
@SuppressWarnings("deprecation")
public class AwsUtil {
	private static final Logger logger = LoggerFactory.getLogger(AwsUtil.class);
	private static AmazonS3  s3Client = null;
	private static String bucketName = null;
	private static BasicAWSCredentials credentials =null;
	private static  ClientConfiguration clientConfig =null;

	public AwsUtil(){};

	static{
		Properties awsProp = new Properties();
		try {
			awsProp.load(AwsUtil.class.getResourceAsStream("/com/landicorp/config/aws.properties"));//获取对象存储配置文件
			String akey = awsProp.getProperty("AWSAccessKeyId");
			String skey = awsProp.getProperty("AWSSecretKey");
			String endpoint = awsProp.getProperty("endpoint");
			String bucketName = awsProp.getProperty("bucketName");
			logger.info("akey为 " + akey);
			logger.info("skey为 " + skey);
			logger.info("endpoint为 " + endpoint);
			logger.info("bucketName为 " + bucketName);
		    credentials  = new BasicAWSCredentials(akey, skey);
		    clientConfig = new ClientConfiguration();
			clientConfig.setProtocol(Protocol.HTTP);
			
			s3Client = new AmazonS3Client(credentials,clientConfig);
			s3Client.setEndpoint(endpoint);
			//Added by zuoyongwei 20190311 begin
			s3Client.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build());
			//Added by zuoyongwei 20190311 end
			//Region reigon=Region.getRegion(Regions.US_EAST_1);
			//endpoint = reigon.getServiceEndpoint(ServiceAbbreviations.S3);
			//s3Client.setEndpoint(endpoint);
			logger.info("aws初始化成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("aws初始化失败",e);
		}
		
	}
	/**
	 * 文件上传
	 * @param file  MultipartFile
	 * @param bucketName 存储桶名称
	 * @param keyName 要存储的文件对象名称
	 */
	public static void uploadFile(MultipartFile file, String bucketName, String keyName){
		ObjectMetadata metadata = new ObjectMetadata();//元数据
		metadata.setContentType(file.getContentType());
		metadata.setContentLength(file.getSize());
		try {//上传文件
			s3Client.putObject(bucketName, keyName, file.getInputStream(), metadata);
		} /*catch (SdkClientException | IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	*/
	catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		
	}
	/**
	 * 文件上传   
	 * 桶名称配置在配置文件中
	 * @param file MultipartFile 待上传的文件
	 * @param keyName 上传后被存储的名称
	 */
	public static void uploadFile(MultipartFile file, String keyName){
		ObjectMetadata metadata = new ObjectMetadata();//元数据
		metadata.setContentType(file.getContentType());
		metadata.setContentLength(file.getSize());
		try {//上传文件
			s3Client.putObject(bucketName, keyName, file.getInputStream(), metadata);
		} /*catch (SdkClientException | IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	*/
	catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		
	}
	/**
	 * 文件上传
	 * @param file  待上传的File
	 * @param bucketName 存储桶名称
	 * @param keyName 上传后被存储的名称
	 */
	public static void uploadFile(File file,String bucketName,String keyName){
		s3Client.putObject(bucketName, keyName, file);
	}
	/**
	 * 文件上传
	 * 桶名称配置在配置文件中
	 * @param file 待上传的File
	 * @param keyName 上传后被存储的名称
	 */
	public static void uploadFile(File file,String keyName){
		s3Client.putObject(bucketName, keyName, file);
	}
	
	
	/**
	 * 文件上传   
	 * @param filepath 待上传文件路径
	 * @param bucketName 存储桶名称
	 * @param keyName 上传后被存储的名称
	 */
	public static void uploadFile(String filepath,String bucketName,String keyName){
		File file = new File(filepath);
		uploadFile(file, bucketName, keyName);
	}
	/**
	 * 文件上传
	 * 桶名称配置在配置文件中
	 * @param filepath 待上传文件路径
	 * @param keyName 上传后被存储名称
	 */
	public static void uploadFile(String filepath,String keyName){
		File file = new File(filepath);
		uploadFile(file, bucketName, keyName);
	}
	/**
	 * 以流的形式上传对象
	 *  @param input 待上传流
	 *  @param keyName 上传后被存储名称
	 */
	public static void putObjectStream( InputStream input,String keyName) throws IOException {
		//创建用户自定义元数据
		ObjectMetadata metadata = new ObjectMetadata();
		Properties awsProp = new Properties();
		awsProp.load(AwsUtil.class.getResourceAsStream("/com/landicorp/config/aws.properties"));
		bucketName=awsProp.getProperty("bucketName");
		s3Client.putObject(bucketName,keyName,input,metadata);
	}
	
	/**
	 * 文件下载    返回输入流
	 * @param bucketName 存储桶名称
	 * @param keyName 待下载的存储对象名称
	 * @return 
	 */
	public static InputStream downFile(String bucketName,String keyName){
		S3Object object = s3Client.getObject(new GetObjectRequest(bucketName, keyName));
		return object.getObjectContent();
	}
	/**
	 * 文件下载 返回输入流
	 * 桶名称配置在配置文件中
	 * @param keyName 待下载的存储对象名称
	 * @return
	 */
	public static InputStream downFile(String keyName){
		S3Object object = s3Client.getObject(new GetObjectRequest(bucketName, keyName));
		return object.getObjectContent();
	}
	
	/**
	 * 文件下载   
	 * @param bucketName 存储桶名称
	 * @param keyName 待下载的存储对象名称
	 * @param fileName 下载后文件显示名称
	 * @param response 响应对象
	 * @param request  请求对象
	 */
//	public static void downFile(String bucketName,String keyName,String fileName,HttpServletResponse response,HttpServletRequest request){
//		InputStream is = downFile(bucketName,keyName); //对象存储服务器获取输入流
//		response.setContentType("application/x-msdownload;charset=UTF-8");
//		OutputStream out = null;
//		try {
//			out = response.getOutputStream();
//			String downFileName = URLEncoder.encode(fileName,"UTF8" );
//			//判断浏览器类型,编码返回文件名称
//			if ("FF".equals(FileUtils.getBrowser(request))) {
//				downFileName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");
//			}
//			response.setHeader("Content-Disposition","attachment;filename="+downFileName);
//			//将字节流返回到浏览器 下载
//			byte[] b = new byte[1024];
//			int len = 0;
//			while ((len=is.read(b))!=-1) {
//				out.write(b, 0, len);
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		}finally{
//			//关闭资源
//			try {
//				if(is!=null){
//					is.close();
//				}
//				if(out!=null){
//					out.close();
//				}
//			} catch (IOException e) {
//			}
//		}
//	}
	/**
	 * 文件下载
	 * 桶名称配置在配置文件中
	 * @param keyName 待下载存储对象名称
	 * @param fileName 下载后文件显示名称
	 * @param response 响应
	 * @param request 请求
	 */
//	public static void downFile(String keyName,String fileName,HttpServletResponse response,HttpServletRequest request){
//		InputStream is = downFile(bucketName,keyName); //对象存储服务器获取输入流
//		response.setContentType("application/x-msdownload;charset=UTF-8");
//		OutputStream out = null;
//		try {
//			out = response.getOutputStream();
//			String downFileName = URLEncoder.encode(fileName,"UTF-8" );
//			//判断浏览器类型,编码返回文件名称
//			if ("FF".equals(FileUtils.getBrowser(request))) {
//				downFileName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");
//			}
//			response.setHeader("Content-Disposition","attachment;filename="+downFileName);
//			//将字节流返回到浏览器 下载
//			byte[] b = new byte[65000];
//			int len = 0;
//			while ((len=is.read(b))!=-1) {
//				out.write(b, 0, len);
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		}finally{
//			//关闭资源
//			try {
//				if(is!=null){
//					is.close();
//				}
//				if(out!=null){
//					out.close();
//				}
//			} catch (IOException e) {
//			}
//		}
//	}e
	
	/**
	 * 创建存储桶
	 * @param bucketName 存储桶名称
	 */
	public static void createBucket(String bucketName){
		s3Client.createBucket(bucketName);
	}
	
	/**
	 * 列出存储桶
	 * @return
	 */
	public static List<Bucket> listBuckets(){
		return s3Client.listBuckets();
	}
	
	/**
	 * 判断存储桶是否存在
	 * @param bucketName 存储桶名称
	 * @return
	 */
	public static boolean existBucket(String bucketName){
		List<Bucket> buckets = listBuckets();
		for (Bucket bucket : buckets) {
			String bn = bucket.getName();
			if (bn.equals(bucketName)) {
				return true;
			}
		}
		return false;
	}
	/**
	 * 列出存储桶内存储对象
	 * @param bucketName 存储桶名称
	 * @return
	 */
	public static List<String> listObjct(String bucketName){
		//Added by Zuoyoongwei on 20190307 begin
		/*
		ObjectListing objslist = s3Client.listObjects(bucketName);
		List<S3ObjectSummary> objs = objslist.getObjectSummaries();
		List<String> list = new ArrayList<String>();
		 for (S3ObjectSummary object:objs) {
         	list.add(object.getKey());
         }
		return list;
		*/
		//final AmazonS3 s3=AmazonS3ClientBuilder.defaultClient();
		ListObjectsV2Result objslist = s3Client.listObjectsV2(bucketName);
		List<S3ObjectSummary> objs = objslist.getObjectSummaries();
		List<String> list = new ArrayList<String>();
		 for (S3ObjectSummary object:objs) {
        	list.add(object.getKey());
        }
		return list;
		
		//Added by Zuoyoongwei on 20190307 end
	}
	/**
	 * 列出存储桶内存储对象
	 * 桶名称配置在配置文件中
	 * @return
	 */
	public static List<String> listObjct(){
		//Added by Zuoyongwei on 20190307 begin
		//ObjectListing objslist = s3Client.listObjects(bucketName);
		ListObjectsV2Result objslist = s3Client.listObjectsV2(bucketName);
		//Added by zuoyongwei on 20190307 end
		List<S3ObjectSummary> objs = objslist.getObjectSummaries();
		List<String> list = new ArrayList<String>();
		 for (S3ObjectSummary object:objs) {
         	list.add(object.getKey());
         }
		return list;
	}
	
	/**
	 * 判断对象是否在存储桶内
	 * @param bucketName 存储桶名称
	 * @param keyName 存储对象名称
	 * @return
	 */
	public static boolean existObjectForBucket(String bucketName,String keyName){
		List<String> objects = listObjct(bucketName);
		for (String obj : objects) {
			if (keyName.equals(obj)) {
				return true;
			}
		}
		return false;
	}
	/**
	 * 判断对象是否在存储桶内
	 * 桶名称配置在配置文件中
	 * @param keyName
	 * @return
	 */
	public static boolean existObjectForBucket(String keyName) throws IOException {
		Properties awsProp = new Properties();
		awsProp.load(AwsUtil.class.getResourceAsStream("/com/landicorp/config/aws.properties"));
		bucketName=awsProp.getProperty("bucketName");
		List<String> objects = listObjct(bucketName);
		for (String obj : objects) {
			if (keyName.equals(obj)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 删除存储桶内对象
	 * @param bucketName 桶名称
	 * @param keyName 待删除对象
	 */
	public static void deleteObject(String bucketName,String keyName){
		s3Client.deleteObject(bucketName, keyName);
	}
	/**
	 * 删除存储桶内对象
	 * 桶名称配置在配置文件中
	 * @param keyName 
	 */
	public static void deleteObject(String keyName){
		s3Client.deleteObject(bucketName, keyName);
	}
	
	public static void main(String[] args){
		
		/** 导入文件*//*
		File dir = new File("E:\\temp");
		File[] files = null;
		if (dir.isDirectory()) {
			files = dir.listFiles();
		}
		for (File file : files) {
			String fileName = file.getName();
			uploadFile(file, "template/"+fileName); //上传文件
			deleteObject(fileName);//删除文件
		}*/
		List<Bucket> listBuckets = listBuckets();
		//AwsUtil.uploadFile();
		for (Bucket bucket : listBuckets) {
			System.out.println("桶名称:"+bucket.getName());
		}
		List<String> listObjct = listObjct("fxqxt");
		for (String obj : listObjct) {
			System.out.println("桶内对象为"+obj);
		}
	}
	
	
}
posted @ 2020-11-09 23:30  Jump233  阅读(136)  评论(0编辑  收藏  举报