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; }
return getObject(GetObjectRequest.builder().applyMutation(getObjectRequest).build(), filePath);
return getObjectAsBytes(GetObjectRequest.builder().applyMutation(getObjectRequest).build());
return getObject(GetObjectRequest.builder().applyMutation(getObjectRequest).build(), destinationPath);
return getObject(GetObjectRequest.builder().applyMutation(getObjectRequest).build());
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);
@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; }
GetObjectRequest request = GetObjectRequest.builder().bucket(options.getBucketName()).build(); client.getObject(request, new AsyncResponseTransformer<GetObjectResponse, String>() {
@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")); }
@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")); }