1 import com.amazonaws.AmazonServiceException; 2 import com.amazonaws.SdkClientException; 3 import com.amazonaws.auth.AWSStaticCredentialsProvider; 4 import com.amazonaws.auth.BasicAWSCredentials; 5 import com.amazonaws.client.builder.AwsClientBuilder; 6 import com.amazonaws.services.s3.AmazonS3; 7 import com.amazonaws.services.s3.AmazonS3ClientBuilder; 8 import com.amazonaws.services.s3.model.*; 9 10 import java.io.File; 11 12 /** 13 * 工具类:S3服务器中文件上传、删除操作(用来存储jmeter脚本中参数化文件) 14 */ 15 public class AwsS3Utils { 16 private static org.apache.logging.log4j.Logger logger; 17 private static final String FILE_TYPE = "fileType"; 18 19 //隐私信息已模糊处理(keyID、key、endpoint、bucket) 20 /** 21 * keyID 22 */ 23 private static String accessKeyID = "*************"; 24 25 /** 26 * key 27 */ 28 private static String secretKey = "************************"; 29 30 /** 31 * endpoint 32 */ 33 private static String endpoint = "https://xxx.b.xxxxx.cn:443/"; 34 35 /** 36 * default bucket 37 */ 38 private static String defaultBucket = "xxxxxxxx"; 39 /** 40 * 向 AWS 客户端明确提供凭证 41 */ 42 private static final BasicAWSCredentials AWS_CREDENTIALS = new BasicAWSCredentials(accessKeyID, secretKey); 43 44 /** 45 * s3 客户端 46 */ 47 private static final AmazonS3 S3 = AmazonS3ClientBuilder.standard() 48 .withCredentials(new AWSStaticCredentialsProvider(AWS_CREDENTIALS)) 49 .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, "")) 50 .build(); 51 52 /** 53 * 存放文件至s3 54 * 55 * @param bucketName 桶名称 AmazonS3BucketConstant 56 * @param key key名称 57 * @param file 文件 58 * @return PutObjectResult 59 */ 60 public static PutObjectResult putFile(String bucketName, String key, File file) { 61 long startMillis = System.currentTimeMillis(); 62 long endMillis; 63 PutObjectResult result = null; 64 try { 65 PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file); 66 String name = file.getName(); 67 //新增文件目录 performanceFile 68 // 记录文件类型 69 ObjectMetadata metadata = new ObjectMetadata(); 70 metadata.addUserMetadata(FILE_TYPE, name.substring(name.lastIndexOf(".") + 1)); 71 putObjectRequest.setMetadata(metadata); 72 result = S3.putObject(putObjectRequest); 73 endMillis = System.currentTimeMillis(); 74 } catch (AmazonServiceException e) { 75 endMillis = System.currentTimeMillis(); 76 } catch (SdkClientException e) { 77 // Amazon S3 couldn't be contacted for a response, or the client 78 // couldn't parse the response from Amazon S3. 79 80 } 81 return result; 82 } 83 84 /** 85 * 根据文件名称进行删除 86 * @param bucketName 87 * @param key 88 * @return 89 */ 90 public static void delFile(String bucketName, String key){ 91 if(S3.doesBucketExistV2(bucketName)==false){ 92 logger.info(bucketName+"不存在"); 93 return ; 94 } 95 96 //根据桶名及文件夹名获取该桶该文件夹操作对象 97 ListObjectsRequest lor = new ListObjectsRequest().withBucketName(defaultBucket).withPrefix("perfFile/"); 98 ObjectListing objectListing = S3.listObjects(lor); 99 100 String str_key = null; 101 102 //根据操作对象列出所有文件对象,单个对象使用objectSummary.getKey()即可获取此文件完整路径,配合桶名可以用于操作 103 for(S3ObjectSummary objectSummary : objectListing.getObjectSummaries()){ 104 str_key = objectSummary.getKey(); 105 //文件名是否匹配 106 if(str_key.matches(key)){ 107 //根据桶名及key信息进行删除 108 S3.deleteObject(bucketName,key); 109 break; 110 } 111 } 112 } 113 114 public static void delFiles(String key){ 115 delFile(defaultBucket,key); 116 } 117 118 public static PutObjectResult putFile(String key, File file) { 119 return putFile(defaultBucket, key, file); 120 } 121 122 /** 123 * 读取文件 124 * @param bucketName 桶名称 AmazonS3BucketConstant 125 * @param key key名称 126 * @return S3Object: s3Object.getBucketName(); s3Object.getKey(); InputStream inputStream = s3Object.getObjectContent()... 127 * 务必关闭 S3Object: s3Object.close(); 128 */ 129 public static S3Object getFile(String bucketName, String key) { 130 long startMillis = System.currentTimeMillis(); 131 long endMillis; 132 S3Object s3Object = null; 133 try { 134 s3Object = S3.getObject(bucketName, key); 135 endMillis = System.currentTimeMillis(); 136 } catch (AmazonServiceException e) { 137 endMillis = System.currentTimeMillis(); 138 } catch (SdkClientException e) { 139 // Amazon S3 couldn't be contacted for a response, or the client 140 // couldn't parse the response from Amazon S3. 141 } 142 return s3Object; 143 } 144 public static S3Object getFile(String key) { 145 return getFile(defaultBucket, key); 146 } 147 }
标签:存储,AmazonS3,S3,static,bucketName,key,增删,import,String From: https://www.cnblogs.com/lychee-wang/p/16963823.html