Search in sources :

Example 1 with RemoteServiceException

use of net.solarnetwork.central.RemoteServiceException 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);
        }
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) ProgressEventType(com.amazonaws.event.ProgressEventType) RemoteServiceException(net.solarnetwork.central.RemoteServiceException) AmazonClientException(com.amazonaws.AmazonClientException) ProgressEvent(com.amazonaws.event.ProgressEvent) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) DestinationConfiguration(net.solarnetwork.central.datum.export.domain.DestinationConfiguration) InputStream(java.io.InputStream) IOException(java.io.IOException) AmazonS3URI(com.amazonaws.services.s3.AmazonS3URI) Date(java.util.Date) ProgressListener(net.solarnetwork.service.ProgressListener) AmazonServiceException(com.amazonaws.AmazonServiceException) DatumExportResource(net.solarnetwork.central.datum.export.domain.DatumExportResource) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Aggregations

AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonServiceException (com.amazonaws.AmazonServiceException)1 ProgressEvent (com.amazonaws.event.ProgressEvent)1 ProgressEventType (com.amazonaws.event.ProgressEventType)1 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 AmazonS3URI (com.amazonaws.services.s3.AmazonS3URI)1 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)1 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Date (java.util.Date)1 RemoteServiceException (net.solarnetwork.central.RemoteServiceException)1 DatumExportResource (net.solarnetwork.central.datum.export.domain.DatumExportResource)1 DestinationConfiguration (net.solarnetwork.central.datum.export.domain.DestinationConfiguration)1 ProgressListener (net.solarnetwork.service.ProgressListener)1