1 /** 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * SPDX-License-Identifier: Apache-2.0. 4 */ 5 package software.amazon.awssdk.crt.s3; 6 7 import software.amazon.awssdk.crt.http.HttpRequest; 8 import software.amazon.awssdk.crt.auth.credentials.CredentialsProvider; 9 import software.amazon.awssdk.crt.auth.signing.AwsSigningConfig; 10 11 import java.net.URI; 12 import java.nio.file.Path; 13 import java.util.HashMap; 14 import java.util.Map; 15 16 public class S3MetaRequestOptions { 17 18 /** 19 * A Meta Request represents a group of generated requests that are being done on behalf of the 20 * original request. For example, one large GetObject request can be transformed into a series 21 * of ranged GetObject requests that are executed in parallel to improve throughput. 22 * 23 * The MetaRequestType is a hint of transformation to be applied. 24 */ 25 public enum MetaRequestType { 26 /** 27 * The Default meta request type sends any request to S3 as-is (with no transformation). For example, 28 * it can be used to pass a CreateBucket request. 29 */ 30 DEFAULT(0), 31 32 /** 33 * The GetObject request will be split into a series of ranged GetObject requests that are 34 * executed in parallel to improve throughput, when possible. 35 */ 36 GET_OBJECT(1), 37 38 /** 39 * The PutObject request will be split into MultiPart uploads that are executed in parallel 40 * to improve throughput, when possible. 41 */ 42 PUT_OBJECT(2), 43 44 /** 45 * The CopyObject meta request performs a multi-part copy using multiple S3 UploadPartCopy requests 46 * in parallel, or bypasses a CopyObject request to S3 if the object size is not large enough for 47 * a multipart upload. 48 */ 49 COPY_OBJECT(3); 50 MetaRequestType(int nativeValue)51 MetaRequestType(int nativeValue) { 52 this.nativeValue = nativeValue; 53 } 54 getNativeValue()55 public int getNativeValue() { 56 return nativeValue; 57 } 58 getEnumValueFromInteger(int value)59 public static MetaRequestType getEnumValueFromInteger(int value) { 60 MetaRequestType enumValue = enumMapping.get(value); 61 if (enumValue != null) { 62 return enumValue; 63 } 64 65 throw new RuntimeException("Invalid S3 Meta Request type"); 66 } 67 buildEnumMapping()68 private static Map<Integer, MetaRequestType> buildEnumMapping() { 69 Map<Integer, MetaRequestType> enumMapping = new HashMap<Integer, MetaRequestType>(); 70 enumMapping.put(DEFAULT.getNativeValue(), DEFAULT); 71 enumMapping.put(GET_OBJECT.getNativeValue(), GET_OBJECT); 72 enumMapping.put(PUT_OBJECT.getNativeValue(), PUT_OBJECT); 73 enumMapping.put(COPY_OBJECT.getNativeValue(), COPY_OBJECT); 74 return enumMapping; 75 } 76 77 private int nativeValue; 78 79 private static Map<Integer, MetaRequestType> enumMapping = buildEnumMapping(); 80 } 81 82 private MetaRequestType metaRequestType; 83 private ChecksumConfig checksumConfig; 84 private HttpRequest httpRequest; 85 private Path requestFilePath; 86 private S3MetaRequestResponseHandler responseHandler; 87 private CredentialsProvider credentialsProvider; 88 private AwsSigningConfig signingConfig; 89 private URI endpoint; 90 private ResumeToken resumeToken; 91 withMetaRequestType(MetaRequestType metaRequestType)92 public S3MetaRequestOptions withMetaRequestType(MetaRequestType metaRequestType) { 93 this.metaRequestType = metaRequestType; 94 return this; 95 } 96 getMetaRequestType()97 public MetaRequestType getMetaRequestType() { 98 return metaRequestType; 99 } 100 101 /** 102 * The config related to checksum used for the meta request. See {@link ChecksumConfig} for details. 103 * @param checksumConfig The checksum config used for the meta request 104 * @return this 105 */ withChecksumConfig(ChecksumConfig checksumConfig)106 public S3MetaRequestOptions withChecksumConfig(ChecksumConfig checksumConfig) { 107 this.checksumConfig = checksumConfig; 108 return this; 109 } 110 getChecksumConfig()111 public ChecksumConfig getChecksumConfig() { 112 return this.checksumConfig; 113 } 114 115 /** 116 * @deprecated Please use {@link #withChecksumConfig(ChecksumConfig)} instead. 117 * Specify the checksum algorithm to use use for put requests, if unset defaults to NONE and no checksum will be calculated. 118 * The location of the checksum will be default to trailer. 119 * 120 * @param checksumAlgorithm the checksum algorithm to use use for put requests 121 * @return this 122 */ withChecksumAlgorithm(ChecksumAlgorithm checksumAlgorithm)123 public S3MetaRequestOptions withChecksumAlgorithm(ChecksumAlgorithm checksumAlgorithm) { 124 ChecksumConfig config = new ChecksumConfig().withChecksumAlgorithm(checksumAlgorithm).withChecksumLocation(ChecksumConfig.ChecksumLocation.TRAILER); 125 this.checksumConfig = config; 126 return this; 127 } 128 129 /** 130 * @deprecated 131 * @return the checksum algorithm to use use for put requests 132 */ getChecksumAlgorithm()133 public ChecksumAlgorithm getChecksumAlgorithm() { 134 return this.checksumConfig.getChecksumAlgorithm(); 135 } 136 137 /** 138 * @deprecated Please use {@link #withChecksumConfig(ChecksumConfig)} instead. 139 * validateChecksum defaults to false, if set to true, it will cause the client to compare a streamed 140 * calculation of the objects checksum to a remotely stored checksum assigned to the object if one exists. 141 * The checksumValidated field passed in parameter of the finished callback will inform 142 * the user if validation ocurred. A mismatch will result in a AWS_ERROR_S3_RESPONSE_CHECKSUM_MISMATCH error 143 * 144 * @param validateChecksum Validate the checksum of response if server provides. 145 * @return this 146 */ withValidateChecksum(boolean validateChecksum)147 public S3MetaRequestOptions withValidateChecksum(boolean validateChecksum) { 148 ChecksumConfig config = new ChecksumConfig().withValidateChecksum(validateChecksum); 149 this.checksumConfig = config; 150 return this; 151 } 152 153 /** 154 * @deprecated 155 * @return Validate the checksum of response if server provides. 156 */ getValidateChecksum()157 public boolean getValidateChecksum() { 158 return checksumConfig.getValidateChecksum(); 159 } 160 161 /** 162 * Set the initial HTTP request. 163 * 164 * Note: When uploading a file, you can get better performance by setting 165 * {@link withRequestFilePath} instead of setting a body stream on the HttpRequest. 166 * (If both are set, the file path is used and body stream is ignored) 167 * 168 * @param httpRequest initial HTTP request message. 169 * @return this 170 */ withHttpRequest(HttpRequest httpRequest)171 public S3MetaRequestOptions withHttpRequest(HttpRequest httpRequest) { 172 this.httpRequest = httpRequest; 173 return this; 174 } 175 getHttpRequest()176 public HttpRequest getHttpRequest() { 177 return httpRequest; 178 } 179 180 /** 181 * If set, this file is sent as the request's body, and the {@link withHttpRequest} body stream is ignored. 182 * 183 * This can give better upload performance than sending data using the body stream. 184 * 185 * @param requestFilePath path to file to send as the request's body. 186 * @return this 187 */ withRequestFilePath(Path requestFilePath)188 public S3MetaRequestOptions withRequestFilePath(Path requestFilePath) { 189 this.requestFilePath = requestFilePath; 190 return this; 191 } 192 getRequestFilePath()193 public Path getRequestFilePath() { 194 return requestFilePath; 195 } 196 withResponseHandler(S3MetaRequestResponseHandler responseHandler)197 public S3MetaRequestOptions withResponseHandler(S3MetaRequestResponseHandler responseHandler) { 198 this.responseHandler = responseHandler; 199 return this; 200 } 201 getResponseHandler()202 public S3MetaRequestResponseHandler getResponseHandler() { 203 return responseHandler; 204 } 205 206 /** 207 * @deprecated Please use {@link #withSigningConfig(AwsSigningConfig)} instead. 208 * The credentials provider will be used to create the signing Config to override the client level config. 209 * The client config will be used. 210 * 211 * @param credentialsProvider provide credentials for signing. 212 * @return this 213 */ withCredentialsProvider(CredentialsProvider credentialsProvider)214 public S3MetaRequestOptions withCredentialsProvider(CredentialsProvider credentialsProvider) { 215 this.credentialsProvider = credentialsProvider; 216 return this; 217 } 218 getCredentialsProvider()219 public CredentialsProvider getCredentialsProvider() { 220 return credentialsProvider; 221 } 222 223 /** 224 * The configuration related to signing used by S3 client. It will override the client level configuration if provided. 225 * `AwsSigningConfig.getDefaultS3SigningConfig(region, credentialsProvider);` can be used as helper to create the default configuration to be used for S3. 226 * 227 * If not set, the client configuration will be used. 228 * If set: 229 * - All fields are optional. The credentials will be resolve from client if not set. 230 * - S3 Client will derive the right config for signing process based on this. 231 * 232 * Notes: 233 * - For SIGV4_S3EXPRESS, S3 client will use the credentials in the config to derive the S3Express 234 * credentials that are used in the signing process. 235 * - Client may make modifications to signing config before passing it on to signer. 236 * 237 * @param signingConfig configuration related to signing via an AWS signing process. 238 * @return this 239 */ withSigningConfig(AwsSigningConfig signingConfig)240 public S3MetaRequestOptions withSigningConfig(AwsSigningConfig signingConfig) { 241 this.signingConfig = signingConfig; 242 return this; 243 } 244 getSigningConfig()245 public AwsSigningConfig getSigningConfig() { 246 return signingConfig; 247 } 248 withEndpoint(URI endpoint)249 public S3MetaRequestOptions withEndpoint(URI endpoint) { 250 this.endpoint = endpoint; 251 return this; 252 } 253 getEndpoint()254 public URI getEndpoint() { 255 return endpoint; 256 } 257 withResumeToken(ResumeToken resumeToken)258 public S3MetaRequestOptions withResumeToken(ResumeToken resumeToken) { 259 this.resumeToken = resumeToken; 260 return this; 261 } 262 getResumeToken()263 public ResumeToken getResumeToken() { 264 return resumeToken; 265 } 266 } 267