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);
}
}
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);
}
}
}
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);
}
}
}
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());
}
}
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);
}
}
Aggregations