Search in sources :

Example 1 with DataStorageItemContent

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

the class NFSStorageProvider method getFile.

@Override
public DataStorageItemContent getFile(NFSDataStorage dataStorage, String path, String version, Long maxDownloadSize) {
    File mntDir = mount(dataStorage);
    File file = new File(mntDir, path);
    try (FileInputStream fis = new FileInputStream(file)) {
        DataStorageItemContent content = new DataStorageItemContent();
        long bytesToRead = file.length();
        if (file.length() > maxDownloadSize) {
            content.setTruncated(true);
            bytesToRead = maxDownloadSize;
        }
        byte[] contentBytes = IOUtils.toByteArray(fis, bytesToRead);
        if (FileContentUtils.isBinaryContent(contentBytes)) {
            content.setMayBeBinary(true);
        } else {
            content.setContent(contentBytes);
        }
        return content;
    } catch (IOException e) {
        throw new DataStorageException(e);
    }
}
Also used : DataStorageException(com.epam.pipeline.entity.datastorage.DataStorageException) DataStorageItemContent(com.epam.pipeline.entity.datastorage.DataStorageItemContent) IOException(java.io.IOException) DataStorageFile(com.epam.pipeline.entity.datastorage.DataStorageFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 2 with DataStorageItemContent

use of com.epam.pipeline.entity.datastorage.DataStorageItemContent 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 3 with DataStorageItemContent

use of com.epam.pipeline.entity.datastorage.DataStorageItemContent 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)

Aggregations

DataStorageException (com.epam.pipeline.entity.datastorage.DataStorageException)3 DataStorageItemContent (com.epam.pipeline.entity.datastorage.DataStorageItemContent)3 IOException (java.io.IOException)2 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)1 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)1 S3Object (com.amazonaws.services.s3.model.S3Object)1 DataStorageFile (com.epam.pipeline.entity.datastorage.DataStorageFile)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1