use of net.solarnetwork.central.datum.export.domain.DestinationConfiguration in project solarnetwork-central by SolarNetwork.
the class S3DatumExportDestinationService method export.
@Override
public void export(Configuration config, Iterable<DatumExportResource> resources, Map<String, ?> runtimeProperties, ProgressListener<DatumExportService> progressListener) throws IOException {
if (config == null) {
throw new IOException("No configuration provided.");
}
if (resources == null) {
throw new IOException("No export resource provided.");
}
DestinationConfiguration destConfig = config.getDestinationConfiguration();
if (destConfig == null) {
throw new IOException("No destination configuration provided.");
}
S3DestinationProperties props = new S3DestinationProperties();
ClassUtils.setBeanProperties(props, destConfig.getServiceProperties(), true);
if (!props.isValid()) {
throw new IOException("Service configuration is not valid.");
}
List<DatumExportResource> resourceList = StreamSupport.stream(resources.spliterator(), false).collect(Collectors.toList());
final int resourceCount = resourceList.size();
final com.amazonaws.event.ProgressListener s3ProgressListener = new com.amazonaws.event.ProgressListener() {
private double overallProgress = 0;
@Override
public void progressChanged(ProgressEvent progressEvent) {
ProgressEventType type = progressEvent.getEventType();
if (type == ProgressEventType.REQUEST_BYTE_TRANSFER_EVENT) {
double resourceProgress = (double) progressEvent.getBytesTransferred() / (double) progressEvent.getBytes();
overallProgress += (resourceProgress / resourceCount);
progressListener.progressChanged(S3DatumExportDestinationService.this, overallProgress);
}
}
};
AmazonS3 client = getClient(props);
AmazonS3URI uri = props.getUri();
for (ListIterator<DatumExportResource> itr = resourceList.listIterator(); itr.hasNext(); ) {
// if we have >1 resource to upload, then we'll insert an index suffix in the key name, like -1, -2, etc.
int idx = (resourceCount > 1 ? itr.nextIndex() + 1 : 0);
DatumExportResource resource = itr.next();
String key = getDestinationPath(props, runtimeProperties, idx);
ObjectMetadata objectMetadata = new ObjectMetadata();
if (resource.getContentType() != null) {
objectMetadata.setContentType(resource.getContentType());
}
objectMetadata.setContentLength(resource.contentLength());
objectMetadata.setLastModified(new Date(resource.lastModified()));
try (InputStream in = resource.getInputStream()) {
PutObjectRequest req = new PutObjectRequest(uri.getBucket(), key, in, objectMetadata);
if (props.getStorageClass() != null) {
req.setStorageClass(props.getStorageClass());
}
if (progressListener != null) {
req.withGeneralProgressListener(s3ProgressListener);
}
client.putObject(req);
} catch (AmazonServiceException e) {
log.warn("AWS error: {}; HTTP code {}; AWS code {}; type {}; request ID {}", e.getMessage(), e.getStatusCode(), e.getErrorCode(), e.getErrorType(), e.getRequestId());
throw new RemoteServiceException("Error putting S3 object at " + key, e);
} catch (AmazonClientException e) {
log.debug("Error communicating with AWS: {}", e.getMessage());
throw new IOException("Error communicating with AWS", e);
}
}
}
Aggregations