storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/mint/build/aws-sdk-java/src/S3TestUtils.java (about) 1 /* 2 * Mint, (C) 2018 Minio, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package io.minio.awssdk.tests; 19 20 import java.io.*; 21 import java.util.*; 22 import java.nio.channels.Channels; 23 24 import com.amazonaws.services.s3.model.GetObjectMetadataRequest; 25 import com.amazonaws.services.s3.model.GetObjectRequest; 26 import com.amazonaws.services.s3.model.ObjectMetadata; 27 import com.amazonaws.services.s3.model.PutObjectRequest; 28 import com.amazonaws.services.s3.model.CopyObjectRequest; 29 import com.amazonaws.services.s3.model.S3Object; 30 import com.amazonaws.services.s3.model.S3ObjectInputStream; 31 import com.amazonaws.services.s3.model.SSECustomerKey; 32 33 import com.amazonaws.services.s3.model.CompleteMultipartUploadRequest; 34 import com.amazonaws.services.s3.model.InitiateMultipartUploadRequest; 35 import com.amazonaws.services.s3.model.InitiateMultipartUploadResult; 36 import com.amazonaws.services.s3.model.PartETag; 37 import com.amazonaws.services.s3.model.UploadPartRequest; 38 39 import com.amazonaws.services.s3.model.MetadataDirective; 40 41 import com.amazonaws.services.s3.AmazonS3; 42 43 class S3TestUtils { 44 45 private AmazonS3 s3Client; 46 47 S3TestUtils(AmazonS3 s3Client) { 48 this.s3Client = s3Client; 49 } 50 51 void uploadMultipartObject(String bucketName, String keyName, 52 String filePath, SSECustomerKey sseKey) throws IOException { 53 54 File file = new File(filePath); 55 56 List<PartETag> partETags = new ArrayList<PartETag>(); 57 58 // Step 1: Initialize. 59 InitiateMultipartUploadRequest initRequest = new 60 InitiateMultipartUploadRequest(bucketName, keyName); 61 62 if (sseKey != null) { 63 initRequest.setSSECustomerKey(sseKey); 64 } 65 66 InitiateMultipartUploadResult initResponse = 67 s3Client.initiateMultipartUpload(initRequest); 68 69 long contentLength = file.length(); 70 long partSize = 5242880; // Set part size to 5 MB. 71 72 // Step 2: Upload parts. 73 long filePosition = 0; 74 for (int i = 1; filePosition < contentLength; i++) { 75 // Last part can be less than 5 MB. Adjust part size. 76 partSize = Math.min(partSize, (contentLength - filePosition)); 77 78 // Create request to upload a part. 79 UploadPartRequest uploadRequest = new UploadPartRequest() 80 .withBucketName(bucketName).withKey(keyName) 81 .withUploadId(initResponse.getUploadId()).withPartNumber(i) 82 .withFileOffset(filePosition) 83 .withFile(file) 84 .withPartSize(partSize); 85 86 if (sseKey != null) { 87 uploadRequest.withSSECustomerKey(sseKey); 88 } 89 90 // Upload part and add response to our list. 91 partETags.add(s3Client.uploadPart(uploadRequest).getPartETag()); 92 93 filePosition += partSize; 94 } 95 96 // Step 3: Complete. 97 CompleteMultipartUploadRequest compRequest = new 98 CompleteMultipartUploadRequest( 99 bucketName, 100 keyName, 101 initResponse.getUploadId(), 102 partETags); 103 104 s3Client.completeMultipartUpload(compRequest); 105 } 106 107 void uploadObject(String bucketName, String keyName, 108 String filePath, SSECustomerKey sseKey) throws IOException { 109 110 File f = new File(filePath); 111 PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, f); 112 if (sseKey != null) { 113 putObjectRequest.withSSECustomerKey(sseKey); 114 } 115 s3Client.putObject(putObjectRequest); 116 } 117 118 void downloadObject(String bucketName, String keyName, SSECustomerKey sseKey) 119 throws Exception, IOException { 120 downloadObject(bucketName, keyName, sseKey, "", -1, -1); 121 } 122 123 void downloadObject(String bucketName, String keyName, SSECustomerKey sseKey, 124 String expectedMD5) 125 throws Exception, IOException { 126 downloadObject(bucketName, keyName, sseKey, expectedMD5, -1, -1); 127 } 128 129 void downloadObject(String bucketName, String keyName, SSECustomerKey sseKey, 130 String expectedMD5, int start, int length) throws Exception, IOException { 131 GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, keyName) 132 .withSSECustomerKey(sseKey); 133 134 if (start >= 0 && length >= 0) { 135 getObjectRequest.setRange(start, start+length-1); 136 } 137 138 S3Object s3Object = s3Client.getObject(getObjectRequest); 139 140 int size = 0; 141 int c; 142 143 S3ObjectInputStream input = s3Object.getObjectContent(); 144 145 ByteArrayOutputStream output = new ByteArrayOutputStream(); 146 String data = ""; 147 while ((c = input.read()) != -1) { 148 output.write((byte) c); 149 size++; 150 } 151 152 if (length >= 0 && size != length) { 153 throw new Exception("downloaded object has unexpected size, expected: " + length + ", received: " + size); 154 } 155 156 String calculatedMD5 = Utils.getBufferMD5(output.toByteArray()); 157 158 if (!expectedMD5.equals("") && !calculatedMD5.equals(expectedMD5)) { 159 throw new Exception("downloaded object has unexpected md5sum, expected: " + expectedMD5 + ", found: " + calculatedMD5); 160 161 } 162 } 163 164 void copyObject(String bucketName, String keyName, SSECustomerKey sseKey, 165 String targetBucketName, String targetKeyName, SSECustomerKey newSseKey, 166 boolean replace) { 167 CopyObjectRequest copyRequest = new CopyObjectRequest(bucketName, keyName, targetBucketName, targetKeyName); 168 if (sseKey != null) { 169 copyRequest.withSourceSSECustomerKey(sseKey); 170 } 171 if (newSseKey != null) { 172 copyRequest.withDestinationSSECustomerKey(newSseKey); 173 } 174 if (replace) { 175 copyRequest.withMetadataDirective(MetadataDirective.COPY); 176 } 177 s3Client.copyObject(copyRequest); 178 } 179 180 long retrieveObjectMetadata(String bucketName, String keyName, SSECustomerKey sseKey) { 181 GetObjectMetadataRequest getMetadataRequest = new GetObjectMetadataRequest(bucketName, keyName) 182 .withSSECustomerKey(sseKey); 183 ObjectMetadata objectMetadata = s3Client.getObjectMetadata(getMetadataRequest); 184 return objectMetadata.getContentLength(); 185 } 186 187 }