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