public boolean download(final String bucketName, final String fileName, final Path destination) { try { if (Files.exists(destination)) { Files.delete(destination); } } catch (final IOException e) { LOG.error("could not delete temp snapshotfile {}", destination.toString(), e); return false; } final GetObjectRequest request = GetObjectRequest.builder().bucket(bucketName).key(fileName).build(); final GetObjectResponse getObjectResponse = s3Client.getObject(request, destination); LOG.debug("download {} from bucket {}: ", fileName, bucketName, getObjectResponse.toString()); return true; }
/** * Returns a string representation of this object. This is useful for testing and debugging. Sensitive data will be * redacted from this string using a placeholder value. */ @Override public String toString() { return ToString.builder("GetObjectRequest").add("Bucket", bucket()).add("IfMatch", ifMatch()) .add("IfModifiedSince", ifModifiedSince()).add("IfNoneMatch", ifNoneMatch()) .add("IfUnmodifiedSince", ifUnmodifiedSince()).add("Key", key()).add("Range", range()) .add("ResponseCacheControl", responseCacheControl()) .add("ResponseContentDisposition", responseContentDisposition()) .add("ResponseContentEncoding", responseContentEncoding()) .add("ResponseContentLanguage", responseContentLanguage()).add("ResponseContentType", responseContentType()) .add("ResponseExpires", responseExpires()).add("VersionId", versionId()) .add("SSECustomerAlgorithm", sseCustomerAlgorithm()) .add("SSECustomerKey", sseCustomerKey() == null ? null : "*** Sensitive Data Redacted ***") .add("SSECustomerKeyMD5", sseCustomerKeyMD5()).add("RequestPayer", requestPayerAsString()) .add("PartNumber", partNumber()).build(); }
@Override public GetObjectRequest build() { return new GetObjectRequest(this); }
@Override public GetObjectResponse getObject(final GetObjectRequest getObjectRequest, final Path filePath) throws S3Exception { final Map<String, BucketItem> bucketItemMap = bucketsWithContents.get(getObjectRequest.bucket()); final BucketItem bucketItem = bucketItemMap.get(getObjectRequest.key()); try { Files.write(filePath, bucketItem.getData()); } catch (IOException e) { throw SdkClientException.create("", e); } return GetObjectResponse.builder().build(); }
@SuppressWarnings("unchecked") @Override public ResponseInputStream<GetObjectResponse> getObject(final GetObjectRequest getObjectRequest) throws S3Exception { final Map<String, BucketItem> bucketItemMap = bucketsWithContents.get(getObjectRequest.bucket()); final BucketItem bucketItem = bucketItemMap.get(getObjectRequest.key()); if (bucketItem != null) { try { return new ResponseInputStream<>(GetObjectResponse.builder().build(), toAbortableInputStream(bucketItem)); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) { throw SdkClientException.create("", e); } } else { throw NoSuchKeyException.builder().message("NoSuchKey").awsErrorDetails(AwsErrorDetails.builder().errorCode("NoSuchKey").build()).build(); } }
return getObject(GetObjectRequest.builder().applyMutation(getObjectRequest).build(), filePath);
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof GetObjectRequest)) { return false; } GetObjectRequest other = (GetObjectRequest) obj; return Objects.equals(bucket(), other.bucket()) && Objects.equals(ifMatch(), other.ifMatch()) && Objects.equals(ifModifiedSince(), other.ifModifiedSince()) && Objects.equals(ifNoneMatch(), other.ifNoneMatch()) && Objects.equals(ifUnmodifiedSince(), other.ifUnmodifiedSince()) && Objects.equals(key(), other.key()) && Objects.equals(range(), other.range()) && Objects.equals(responseCacheControl(), other.responseCacheControl()) && Objects.equals(responseContentDisposition(), other.responseContentDisposition()) && Objects.equals(responseContentEncoding(), other.responseContentEncoding()) && Objects.equals(responseContentLanguage(), other.responseContentLanguage()) && Objects.equals(responseContentType(), other.responseContentType()) && Objects.equals(responseExpires(), other.responseExpires()) && Objects.equals(versionId(), other.versionId()) && Objects.equals(sseCustomerAlgorithm(), other.sseCustomerAlgorithm()) && Objects.equals(sseCustomerKey(), other.sseCustomerKey()) && Objects.equals(sseCustomerKeyMD5(), other.sseCustomerKeyMD5()) && Objects.equals(requestPayerAsString(), other.requestPayerAsString()) && Objects.equals(partNumber(), other.partNumber()); }
return getObject(GetObjectRequest.builder().applyMutation(getObjectRequest).build());
@Override public int hashCode() { int hashCode = 1; hashCode = 31 * hashCode + Objects.hashCode(bucket()); hashCode = 31 * hashCode + Objects.hashCode(ifMatch()); hashCode = 31 * hashCode + Objects.hashCode(ifModifiedSince()); hashCode = 31 * hashCode + Objects.hashCode(ifNoneMatch()); hashCode = 31 * hashCode + Objects.hashCode(ifUnmodifiedSince()); hashCode = 31 * hashCode + Objects.hashCode(key()); hashCode = 31 * hashCode + Objects.hashCode(range()); hashCode = 31 * hashCode + Objects.hashCode(responseCacheControl()); hashCode = 31 * hashCode + Objects.hashCode(responseContentDisposition()); hashCode = 31 * hashCode + Objects.hashCode(responseContentEncoding()); hashCode = 31 * hashCode + Objects.hashCode(responseContentLanguage()); hashCode = 31 * hashCode + Objects.hashCode(responseContentType()); hashCode = 31 * hashCode + Objects.hashCode(responseExpires()); hashCode = 31 * hashCode + Objects.hashCode(versionId()); hashCode = 31 * hashCode + Objects.hashCode(sseCustomerAlgorithm()); hashCode = 31 * hashCode + Objects.hashCode(sseCustomerKey()); hashCode = 31 * hashCode + Objects.hashCode(sseCustomerKeyMD5()); hashCode = 31 * hashCode + Objects.hashCode(requestPayerAsString()); hashCode = 31 * hashCode + Objects.hashCode(partNumber()); return hashCode; }
return getObject(GetObjectRequest.builder().applyMutation(getObjectRequest).build(), destinationPath);
public <T> Optional<T> getValueForField(String fieldName, Class<T> clazz) { switch (fieldName) { case "Bucket": return Optional.ofNullable(clazz.cast(bucket())); case "IfMatch": return Optional.ofNullable(clazz.cast(ifMatch())); case "IfModifiedSince": return Optional.ofNullable(clazz.cast(ifModifiedSince())); case "IfNoneMatch": return Optional.ofNullable(clazz.cast(ifNoneMatch())); case "IfUnmodifiedSince": return Optional.ofNullable(clazz.cast(ifUnmodifiedSince())); case "Key": return Optional.ofNullable(clazz.cast(key())); case "Range": return Optional.ofNullable(clazz.cast(range())); case "ResponseCacheControl": return Optional.ofNullable(clazz.cast(responseCacheControl())); case "ResponseContentDisposition": return Optional.ofNullable(clazz.cast(responseContentDisposition())); case "ResponseContentEncoding": return Optional.ofNullable(clazz.cast(responseContentEncoding())); case "ResponseContentLanguage": return Optional.ofNullable(clazz.cast(responseContentLanguage())); case "ResponseContentType": return Optional.ofNullable(clazz.cast(responseContentType())); case "ResponseExpires": return Optional.ofNullable(clazz.cast(responseExpires())); case "VersionId": return Optional.ofNullable(clazz.cast(versionId()));
return getObject(GetObjectRequest.builder().applyMutation(getObjectRequest).build(), asyncResponseTransformer);
ResponseTransformer<GetObjectResponse, ReturnT> responseTransformer) throws NoSuchKeyException, AwsServiceException, SdkClientException, S3Exception { return getObject(GetObjectRequest.builder().applyMutation(getObjectRequest).build(), responseTransformer);
return getObjectAsBytes(GetObjectRequest.builder().applyMutation(getObjectRequest).build());
@Override public FeatureState getFeatureState(final Feature feature) { final GetObjectRequest getRequest = GetObjectRequest.builder() .bucket(s3TogglzProperties.getBucketName()) .key(keyForFeature(feature)) .build(); try (ResponseInputStream<GetObjectResponse> responseStream = s3Client.getObject(getRequest)) { if (responseStream != null) { final FeatureStateStorageWrapper wrapper = objectMapper.reader() .forType(FeatureStateStorageWrapper.class) .readValue(responseStream); return FeatureStateStorageWrapper.featureStateForWrapper(feature, wrapper); } } catch (final S3Exception ae) { if (ERR_NO_SUCH_KEY.equals(ae.awsErrorDetails().errorCode())) { return null; } throw ae; } catch (final IOException e) { throw new RuntimeException("Failed to get the feature state", e); } return null; }
public FeatureState retrieveFeatureStateFromS3(final Feature feature) { final GetObjectRequest getRequest = GetObjectRequest.builder() .bucket(togglzProperties.getS3().getBucketName()) .key(keyForFeature(feature)) .build(); try (final ResponseInputStream<GetObjectResponse> responseStream = s3Client.getObject(getRequest)) { if (responseStream != null) { final FeatureStateStorageWrapper wrapper = objectMapper.reader() .forType(FeatureStateStorageWrapper.class) .readValue(responseStream); return FeatureStateStorageWrapper.featureStateForWrapper(feature, wrapper); } } catch (final S3Exception ae) { if (ERR_NO_SUCH_KEY.equals(ae.awsErrorDetails().errorCode()) || ae.awsErrorDetails().sdkHttpResponse().statusCode() == 404) { return null; } throw ae; } catch (final IOException e) { throw new RuntimeException("Failed to get the feature state", e); } return null; }
@Test public void getObjectShouldCreateFileWithData() throws Exception { // given testee.putObject(PutObjectRequest.builder() .bucket("someBucket") .key("someKey") .build(), RequestBody.fromString("testdata")); //when Path tempFile = Files.createTempFile("test", "tmp"); testee.getObject(GetObjectRequest.builder() .bucket("someBucket") .key("someKey") .build(), tempFile); //then List<String> lines = Files.readAllLines(tempFile); assertThat(lines.get(0), is("testdata")); }
GetObjectRequest request = GetObjectRequest.builder().bucket(options.getBucketName()).build(); client.getObject(request, new AsyncResponseTransformer<GetObjectResponse, String>() {
@Test public void getObjectShouldReturnStreamWithData() throws Exception { // given testee.putObject(PutObjectRequest.builder() .bucket("someBucket") .key("someKey") .build(), RequestBody.fromString("testdata")); //when ResponseInputStream<GetObjectResponse> inputStream = testee.getObject(GetObjectRequest.builder() .bucket("someBucket") .key("someKey") .build()); //then String data = IoUtils.toUtf8String(inputStream); assertThat(data, is("testdata")); }