Search in sources :

Example 21 with DataStorageException

use of com.epam.pipeline.entity.datastorage.DataStorageException in project cloud-pipeline by epam.

the class S3Helper method downloadContent.

private DataStorageItemContent downloadContent(Long maxDownloadSize, S3Object objectPortion) {
    try (InputStream objectData = objectPortion.getObjectContent()) {
        DataStorageItemContent content = new DataStorageItemContent();
        content.setContentType(objectPortion.getObjectMetadata().getContentType());
        content.setTruncated(objectPortion.getObjectMetadata().getInstanceLength() > maxDownloadSize);
        byte[] byteContent = IOUtils.toByteArray(objectData);
        if (FileContentUtils.isBinaryContent(byteContent)) {
            content.setMayBeBinary(true);
        } else {
            content.setContent(byteContent);
        }
        return content;
    } catch (IOException e) {
        throw new DataStorageException(e.getMessage(), e);
    }
}
Also used : DataStorageException(com.epam.pipeline.entity.datastorage.DataStorageException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DataStorageItemContent(com.epam.pipeline.entity.datastorage.DataStorageItemContent) IOException(java.io.IOException)

Example 22 with DataStorageException

use of com.epam.pipeline.entity.datastorage.DataStorageException in project cloud-pipeline by epam.

the class S3Helper method getFileStream.

public DataStorageStreamingContent getFileStream(AbstractDataStorage dataStorage, String path, String version) {
    try {
        AmazonS3 client = getDefaultS3Client();
        GetObjectRequest rangeObjectRequest = new GetObjectRequest(dataStorage.getPath(), path, version);
        S3Object object = client.getObject(rangeObjectRequest);
        return new DataStorageStreamingContent(object.getObjectContent(), object.getKey());
    } catch (AmazonS3Exception e) {
        if (e.getStatusCode() == NOT_FOUND) {
            throw new DataStorageException(String.format("File '%s' doesn't exist", path));
        } else {
            throw new DataStorageException(e.getMessage(), e);
        }
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) DataStorageException(com.epam.pipeline.entity.datastorage.DataStorageException) DataStorageStreamingContent(com.epam.pipeline.entity.datastorage.DataStorageStreamingContent) S3Object(com.amazonaws.services.s3.model.S3Object) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest)

Example 23 with DataStorageException

use of com.epam.pipeline.entity.datastorage.DataStorageException in project cloud-pipeline by epam.

the class S3Helper method getFileContent.

public DataStorageItemContent getFileContent(AbstractDataStorage dataStorage, String path, String version, Long maxDownloadSize) {
    try {
        AmazonS3 client = getDefaultS3Client();
        GetObjectRequest rangeObjectRequest = new GetObjectRequest(dataStorage.getPath(), path, version).withRange(0, maxDownloadSize - 1);
        S3Object objectPortion = client.getObject(rangeObjectRequest);
        return downloadContent(maxDownloadSize, objectPortion);
    } catch (AmazonS3Exception e) {
        if (e.getStatusCode() == NOT_FOUND) {
            throw new DataStorageException(String.format("File '%s' doesn't exist", path));
        } else if (e.getStatusCode() == INVALID_RANGE) {
            // is thrown in case of en empty file
            LOGGER.debug(e.getMessage(), e);
            DataStorageItemContent content = new DataStorageItemContent();
            content.setTruncated(false);
            return content;
        } else {
            throw new DataStorageException(e.getMessage(), e);
        }
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) DataStorageException(com.epam.pipeline.entity.datastorage.DataStorageException) DataStorageItemContent(com.epam.pipeline.entity.datastorage.DataStorageItemContent) S3Object(com.amazonaws.services.s3.model.S3Object) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest)

Example 24 with DataStorageException

use of com.epam.pipeline.entity.datastorage.DataStorageException in project cloud-pipeline by epam.

the class S3Helper method moveS3Objects.

private void moveS3Objects(final AmazonS3 client, final String bucket, final List<MoveObjectRequest> moveRequests) {
    try (S3ObjectDeleter deleter = new S3ObjectDeleter(client, bucket)) {
        moveRequests.forEach(moveRequest -> {
            client.copyObject(moveRequest.toCopyRequest(bucket));
            deleter.deleteKey(moveRequest.getSourcePath(), moveRequest.getVersion());
        });
    } catch (SdkClientException e) {
        throw new DataStorageException(e.getMessage(), e.getCause());
    }
}
Also used : DataStorageException(com.epam.pipeline.entity.datastorage.DataStorageException) SdkClientException(com.amazonaws.SdkClientException)

Example 25 with DataStorageException

use of com.epam.pipeline.entity.datastorage.DataStorageException in project cloud-pipeline by epam.

the class S3StorageProvider method createStorage.

@Override
public String createStorage(S3bucketDataStorage storage) {
    try {
        AwsRegion awsRegion = getAwsRegion(storage);
        if (storage.getRegionId() == null) {
            storage.setRegionId(awsRegion.getId());
        }
        final ObjectMapper corsRulesMapper = JsonMapper.newInstance().addMixIn(CORSRule.class, AbstractCORSRuleMixin.class).configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
        final List<CORSRule> corsPolicyRules = JsonMapper.parseData(awsRegion.getCorsRules(), new TypeReference<List<CORSRule>>() {
        }, corsRulesMapper);
        final Map<String, String> tags = new HashMap<>();
        AwsRegionsConfiguration configuration = preferenceManager.getObjectPreferenceAs(SystemPreferences.CLUSTER_NETWORKS_CONFIG, new TypeReference<AwsRegionsConfiguration>() {
        });
        if (configuration != null && !CollectionUtils.isEmpty(configuration.getTags())) {
            tags.putAll(configuration.getTags());
        }
        return getS3Helper(storage).createS3Bucket(storage.getPath(), awsRegion.getPolicy(), corsPolicyRules, storage.getAllowedCidrs(), awsRegion, tags, storage.isShared());
    } catch (IOException e) {
        throw new DataStorageException(messageHelper.getMessage(MessageConstants.ERROR_DATASTORAGE_CREATE_FAILED, storage.getName()), e);
    }
}
Also used : HashMap(java.util.HashMap) CORSRule(com.amazonaws.services.s3.model.CORSRule) IOException(java.io.IOException) DataStorageException(com.epam.pipeline.entity.datastorage.DataStorageException) AwsRegion(com.epam.pipeline.entity.region.AwsRegion) AwsRegionsConfiguration(com.epam.pipeline.entity.cluster.AwsRegionsConfiguration) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

DataStorageException (com.epam.pipeline.entity.datastorage.DataStorageException)28 IOException (java.io.IOException)14 DataStorageFile (com.epam.pipeline.entity.datastorage.DataStorageFile)12 File (java.io.File)11 AmazonS3 (com.amazonaws.services.s3.AmazonS3)8 AbstractDataStorage (com.epam.pipeline.entity.datastorage.AbstractDataStorage)5 DataStorageItemContent (com.epam.pipeline.entity.datastorage.DataStorageItemContent)5 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)4 ListObjectsRequest (com.amazonaws.services.s3.model.ListObjectsRequest)4 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)4 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)4 DataStorageFolder (com.epam.pipeline.entity.datastorage.DataStorageFolder)4 DataStorageStreamingContent (com.epam.pipeline.entity.datastorage.DataStorageStreamingContent)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 SdkClientException (com.amazonaws.SdkClientException)3 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)3 InputStream (java.io.InputStream)3 Date (java.util.Date)3 List (java.util.List)3 Test (org.junit.Test)3