1. package com.example.cephs3;
    2. import com.amazonaws.ClientConfiguration;
    3. import com.amazonaws.HttpMethod;
    4. import com.amazonaws.Protocol;
    5. import com.amazonaws.auth.AWSCredentials;
    6. import com.amazonaws.auth.BasicAWSCredentials;
    7. import com.amazonaws.regions.Region;
    8. import com.amazonaws.services.s3.AmazonS3;
    9. import com.amazonaws.services.s3.AmazonS3Client;
    10. import com.amazonaws.services.s3.model.*;
    11. import com.amazonaws.util.StringUtils;
    12. import org.junit.Test;
    13. import org.springframework.beans.factory.annotation.Value;
    14. import java.io.*;
    15. import java.net.URL;
    16. import java.util.ArrayList;
    17. import java.util.Date;
    18. import java.util.List;
    19. /**
    20. * description
    21. *
    22. * @author ${user}
    23. * @Time 2019-04-26
    24. */
    25. public class TestS3UploadFile {
    26. @Value("${s3.accessKey}")
    27. // private String accessKey = "NB8SR8JU85V16ROBD4B3";
    28. private String accessKey = "KE0AA98865C726GUWP6B";
    29. // private String accessKey = "DQ2P7AIUZB956AMAE94Z";
    30. @Value("${s3.secretKey}")
    31. // private String secretKey = "gkIxvICALUahng0MTlSXhNafdN2FVpwM0qDBTtRB";
    32. private String secretKey = "mbxbD5vZTrIZdMINhPGNtJocU2YjqFK66FLi0sV9";
    33. // private String secretKey = "4CacXZRCKxjp3fssyIIX51Ai4xeQrQW7EpWwIuIs";
    34. @Value("${s3.endPoint}")
    35. // private String endPoint = "192.168.1.34:7480";
    36. private String endPoint = "192.168.1.252:7480";
    37. AmazonS3 connhandler = null;
    38. private void init() {
    39. AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
    40. ClientConfiguration clientConfiguration = new ClientConfiguration();
    41. clientConfiguration.setProtocol(Protocol.HTTP);
    42. connhandler = new AmazonS3Client(credentials, clientConfiguration);
    43. connhandler.setEndpoint(endPoint);
    44. }
    45. @Test
    46. public void TestlistBuckets() {
    47. init();
    48. List<Bucket> buckets = connhandler.listBuckets();
    49. for( Bucket bucket: buckets) {
    50. System.out.println( bucket.getName() + "\t" +
    51. StringUtils.fromDate(bucket.getCreationDate()));
    52. }
    53. }
    54. @Test
    55. public void createBuckets() {
    56. init();
    57. if ( connhandler.doesBucketExist("bucket01") ) {
    58. System.out.println("exits");
    59. } else {
    60. System.out.println("not exits");
    61. }
    62. //
    63. // AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
    64. // AmazonS3 conn = new AmazonS3Client(credentials);
    65. //// com.amazonaws.services.s3.model.Region.US_Standard;
    66. // conn.setEndpoint(endPoint);
    67. // Bucket bucket = conn.createBucket("createbyapi");
    68. Bucket bucket = connhandler.createBucket(new CreateBucketRequest("bucket01"));
    69. // Bucket bucket = connhandler.createBucket("createbyapi");
    70. // String bucketLocation = connhandler.getBucketLocation(new GetBucketLocationRequest("createbyapi"));
    71. // System.out.println("bucket_name:" + bucket.getName());
    72. // System.out.println("localtion:" + bucketLocation);
    73. }
    74. @Test
    75. public void listObject() {
    76. init();
    77. //first_bucket
    78. ObjectListing objectListing = connhandler.listObjects("bucket");
    79. do {
    80. for(S3ObjectSummary objectSummary: objectListing.getObjectSummaries()) {
    81. System.out.println(objectSummary.getKey() + "\t" +
    82. objectSummary.getSize() + "\t" +
    83. StringUtils.fromDate(objectSummary.getLastModified()));
    84. }
    85. } while ( objectListing.isTruncated());
    86. }
    87. @Test
    88. public void uploadFile() {
    89. init();
    90. // UploadPartRequest uploadPartRequest = new UploadPartRequest();
    91. // conn.upload_file_multipart('/root/中国.mp4',u'videos/中国.mp4',bucket)
    92. // uploadPartRequest.setBucketName("first_bucket");
    93. //
    94. // connhandler.uploadPart(uploadPartRequest);
    95. // PutObjectResult putObjectResult = connhandler.putObject("first_bucket", accessKey, new File("F:\\20190101\\abc.txt"));
    96. PutObjectResult putObjectResult =
    97. connhandler.putObject("bucket1",
    98. "20170301/58434_128M.txt",
    99. new File("F:\\tmpdata\\20170301\\Z_SURF_I_58434_20170301000000_O_AWS_DAY.txt"));
    100. // connhandler.put
    101. System.out.println(putObjectResult.getContentMd5() + "\t" + putObjectResult.getETag());
    102. }
    103. @Test
    104. public void getObject() throws IOException {
    105. init();
    106. S3Object s3Object = connhandler.getObject(new GetObjectRequest("first_bucket", "20190101/abc.txt"));
    107. System.out.println("content-type: " + s3Object.getObjectMetadata().getContentType());
    108. System.out.println("Contect: ");
    109. InputStream inputStream = s3Object.getObjectContent();
    110. BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    111. String line = null;
    112. while ( (line = reader.readLine()) != null) {
    113. System.out.println(line);
    114. }
    115. System.out.println();
    116. }
    117. @Test
    118. public void DeleteBucket() {
    119. init();
    120. connhandler.deleteBucket("createbucketbyapi");
    121. }
    122. @Test
    123. public void TestACL() {
    124. init();
    125. AccessControlList accessControlList = connhandler.getObjectAcl("first_bucket", "20190101/abc.txt");
    126. System.out.println(accessControlList.getOwner().getDisplayName());
    127. System.out.println(accessControlList.getOwner().getId());
    128. }
    129. @Test
    130. public void downloadObject() {
    131. init();
    132. connhandler.getObject(new GetObjectRequest("first_bucket", "20190101/abc.txt"),
    133. new File("F:\\20190101\\download.txt"));
    134. }
    135. @Test
    136. public void deleteObject() {
    137. init();
    138. connhandler.deleteObject("first_bucket", "NB8SR8JU85V16ROBD4B3");
    139. }
    140. @Test
    141. public void TestURLRequest() {
    142. // 类似于生成的url是在有限时间(比如一个小时内)内生效的,超过时间范围就失效了
    143. init();
    144. GeneratePresignedUrlRequest generatePresignedUrlRequest =
    145. new GeneratePresignedUrlRequest("bucket1", "20170111/Z_SURF_I_53983_20170111000000_O_AWS_DAY.txt");
    146. System.out.println(connhandler.generatePresignedUrl(generatePresignedUrlRequest));
    147. }
    148. @Test
    149. public void TestUrl12Hour() {
    150. init();
    151. Date expiration = new Date();
    152. long expirationMS = expiration.getTime();
    153. expirationMS += 1000 * 60 * 60 * 12;
    154. expiration.setTime(expirationMS);
    155. GeneratePresignedUrlRequest request =
    156. new GeneratePresignedUrlRequest("first_bucket", "20190101/abc.txt");
    157. request.withMethod(HttpMethod.PUT);
    158. request.withExpiration(expiration);
    159. URL url = connhandler.generatePresignedUrl(request);
    160. System.out.println(url);
    161. }
    162. @Test
    163. public void TestDeleteObjects() {
    164. String[] object_keys = new String[]{"", ""};
    165. List<String> objectListKeys = null;
    166. objectListKeys = new ArrayList<String>();
    167. init();
    168. DeleteObjectsRequest dor = new DeleteObjectsRequest("first_bucket");
    169. dor.withKeys();
    170. connhandler.deleteObjects(dor);
    171. }
    172. @Test
    173. public void CopyObject() {
    174. init();
    175. connhandler.copyObject("first_bucket", "20190101/abc.txt",
    176. "bucket", "20190101/abc1.txt" );
    177. }
    178. @Test
    179. public void TestList() {
    180. String[] strlst = new String[]{"abc", "aef"};
    181. String[] strlst1 = new String[]{""};
    182. boolean flag = org.springframework.util.StringUtils.isEmpty(strlst1);
    183. System.out.println(flag);
    184. }
    185. // 验证一个用户可以创建多少bucket
    186. @Test
    187. public void TestCreateBuckets() {
    188. }
    189. // 每个bucket可以创建多少object
    190. @Test
    191. public void TestCreateObjects() {
    192. }
    193. }