Search in sources :

Example 11 with DataStorageException

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

the class S3Helper method listObjectTags.

public Map<String, String> listObjectTags(AbstractDataStorage dataStorage, String path, String version) {
    try {
        AmazonS3 client = getDefaultS3Client();
        GetObjectTaggingRequest getTaggingRequest = new GetObjectTaggingRequest(dataStorage.getPath(), path);
        if (!StringUtils.isNullOrEmpty(version)) {
            getTaggingRequest.withVersionId(version);
        }
        return convertAwsTagsToMap(client.getObjectTagging(getTaggingRequest).getTagSet());
    } catch (AmazonS3Exception e) {
        if (e.getStatusCode() == NOT_FOUND) {
            throw new DataStorageException(String.format("Path '%s' doesn't exist", path));
        } else {
            throw new DataStorageException(e.getMessage(), e);
        }
    }
}
Also used : GetObjectTaggingRequest(com.amazonaws.services.s3.model.GetObjectTaggingRequest) AmazonS3(com.amazonaws.services.s3.AmazonS3) DataStorageException(com.epam.pipeline.entity.datastorage.DataStorageException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception)

Example 12 with DataStorageException

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

the class S3Helper method deleteFolder.

public void deleteFolder(String bucket, String path, Boolean totally) {
    if (StringUtils.isNullOrEmpty(path)) {
        throw new DataStorageException(PATH_SHOULD_NOT_BE_EMPTY_MESSAGE);
    }
    if (!path.endsWith(S3Constants.DELIMITER)) {
        path += S3Constants.DELIMITER;
    }
    AmazonS3 client = getDefaultS3Client();
    if (!totally && !itemExists(client, bucket, path, true)) {
        throw new DataStorageException("Folder does not exist");
    }
    if (totally) {
        deleteAllVersions(client, bucket, path);
    } else {
        // indicates that only DUMMY file is present in a folder and thus it should be deleted completely
        boolean noFiles = true;
        try (S3ObjectDeleter deleter = new S3ObjectDeleter(client, bucket)) {
            ListObjectsRequest request = new ListObjectsRequest();
            request.setBucketName(bucket);
            request.setPrefix(path);
            ObjectListing listing;
            do {
                listing = client.listObjects(request);
                for (S3ObjectSummary s3ObjectSummary : listing.getObjectSummaries()) {
                    String relativePath = s3ObjectSummary.getKey();
                    if (relativePath.startsWith(path)) {
                        if (!relativePath.endsWith(S3Constants.FOLDER_TOKEN_FILE)) {
                            noFiles = false;
                        }
                        deleter.deleteKey(relativePath);
                    }
                }
                request.setMarker(listing.getNextMarker());
            } while (listing.isTruncated());
        }
        if (noFiles) {
            deleteAllVersions(client, bucket, path);
        }
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) DataStorageException(com.epam.pipeline.entity.datastorage.DataStorageException) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary)

Example 13 with DataStorageException

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

the class S3Helper method restoreFileVersion.

public void restoreFileVersion(String bucket, String path, String version) {
    AmazonS3 client = getDefaultS3Client();
    if (fileSizeExceedsLimit(client, bucket, path, version)) {
        throw new DataStorageException(String.format("Restoring file '%s' version '%s' was aborted because " + "file size exceeds the limit of %s bytes", path, version, COPYING_FILE_SIZE_LIMIT));
    }
    moveS3Object(client, bucket, new MoveObjectRequest(path, version, path));
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) DataStorageException(com.epam.pipeline.entity.datastorage.DataStorageException)

Example 14 with DataStorageException

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

the class S3Helper method moveFile.

public DataStorageFile moveFile(String bucket, String oldPath, String newPath) throws DataStorageException {
    if (StringUtils.isNullOrEmpty(oldPath) || StringUtils.isNullOrEmpty(newPath)) {
        throw new DataStorageException(PATH_SHOULD_NOT_BE_EMPTY_MESSAGE);
    }
    AmazonS3 client = getDefaultS3Client();
    if (!itemExists(client, bucket, oldPath, false)) {
        throw new DataStorageException(String.format("File '%s' does not exist", oldPath));
    }
    if (itemExists(client, bucket, newPath, false)) {
        throw new DataStorageException(String.format("File '%s' already exists", newPath));
    }
    if (fileSizeExceedsLimit(client, bucket, oldPath)) {
        throw new DataStorageException(String.format("File '%s' moving was aborted because " + "file size exceeds the limit of %s bytes", newPath, COPYING_FILE_SIZE_LIMIT));
    }
    moveS3Object(client, bucket, new MoveObjectRequest(oldPath, newPath));
    return getFile(client, bucket, newPath);
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) DataStorageException(com.epam.pipeline.entity.datastorage.DataStorageException)

Example 15 with DataStorageException

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

the class S3Helper method createFolder.

public DataStorageFolder createFolder(String bucket, String path) throws DataStorageException {
    if (StringUtils.isNullOrEmpty(path) || StringUtils.isNullOrEmpty(path.trim())) {
        throw new DataStorageException(PATH_SHOULD_NOT_BE_EMPTY_MESSAGE);
    }
    String folderPath = path.trim();
    if (!folderPath.endsWith(S3Constants.DELIMITER)) {
        folderPath += S3Constants.DELIMITER;
    }
    if (folderPath.startsWith(S3Constants.DELIMITER)) {
        folderPath = folderPath.substring(1);
    }
    final String folderFullPath = folderPath.substring(0, folderPath.length() - 1);
    AmazonS3 client = getDefaultS3Client();
    if (itemExists(client, bucket, folderPath, true)) {
        throw new DataStorageException("Folder already exists");
    }
    folderPath += S3Constants.FOLDER_TOKEN_FILE;
    String[] parts = folderPath.split(S3Constants.DELIMITER);
    final String folderName = parts[parts.length - 2];
    try {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setLastModified(new Date());
        byte[] contents = "".getBytes();
        ByteArrayInputStream byteInputStream = new ByteArrayInputStream(contents);
        client.putObject(new PutObjectRequest(bucket, folderPath, byteInputStream, objectMetadata));
        DataStorageFolder folder = new DataStorageFolder();
        folder.setName(folderName);
        folder.setPath(folderFullPath);
        return folder;
    } catch (SdkClientException e) {
        throw new DataStorageException(e.getMessage(), e.getCause());
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) DataStorageException(com.epam.pipeline.entity.datastorage.DataStorageException) SdkClientException(com.amazonaws.SdkClientException) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Date(java.util.Date) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) DataStorageFolder(com.epam.pipeline.entity.datastorage.DataStorageFolder)

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