use of software.amazon.awssdk.http.HttpExecuteRequest in project aws-greengrass-nucleus by aws-greengrass.
the class DeviceProvisioningHelper method downloadFileFromURL.
/*
* Download content from a URL to a local file.
*/
@SuppressWarnings("PMD.AvoidFileStream")
private void downloadFileFromURL(String url, File f) throws IOException {
SdkHttpFullRequest request = SdkHttpFullRequest.builder().uri(URI.create(url)).method(SdkHttpMethod.GET).build();
HttpExecuteRequest executeRequest = HttpExecuteRequest.builder().request(request).build();
try (SdkHttpClient client = getSdkHttpClient()) {
HttpExecuteResponse executeResponse = client.prepareRequest(executeRequest).call();
int responseCode = executeResponse.httpResponse().statusCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new IOException("Received invalid response code: " + responseCode);
}
try (InputStream inputStream = executeResponse.responseBody().get();
OutputStream outputStream = Files.newOutputStream(f.toPath(), StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {
IoUtils.copy(inputStream, outputStream);
}
}
}
Aggregations