com.emc.object.s3.S3Exception Java Examples

The following examples show how to use com.emc.object.s3.S3Exception. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: S3FileSystemImpl.java    From pravega with Apache License 2.0 6 votes vote down vote up
@Synchronized
@Override
public void putObject(String bucketName, String key, Range range, Object content) {

    Path path = Paths.get(this.baseDir, bucketName, key);
    try (FileChannel channel = FileChannel.open(path, StandardOpenOption.WRITE)) {

        long startOffset = range.getFirst();
        long length = range.getLast() + 1 - range.getFirst();
        do {
            long bytesTransferred = channel.transferFrom(Channels.newChannel((InputStream) content),
                    range.getFirst(), range.getLast() + 1 - range.getFirst());
            length -= bytesTransferred;
            startOffset += bytesTransferred;
        } while (length > 0);

        AclSize aclKey = aclMap.get(key);
        aclMap.put(key, aclKey.withSize(range.getLast() + 1));
    } catch (IOException e) {
        throw new S3Exception("NoObject", 404, "NoSuchKey", key);
    }
}
 
Example #2
Source File: S3FileSystemImpl.java    From pravega with Apache License 2.0 6 votes vote down vote up
@Override
public ListObjectsResult listObjects(String bucketName, String prefix) {
    ListObjectsResult result = new ListObjectsResult();
    ArrayList<S3Object> list = new ArrayList<>();
    Path path = Paths.get(this.baseDir, bucketName, prefix);
    try {
        if (Files.exists(path)) {
            if (Files.isDirectory(path)) {
                Files.list(path).forEach(file -> {
                    addFileAsObjectToList(file, list, bucketName);
                });
            } else {
                addFileAsObjectToList(path, list, bucketName);
            }
        }
    } catch (IOException e) {
        throw new S3Exception("NoSuchKey", HttpStatus.SC_NOT_FOUND, "NoSuchKey", "");
    }
    result.setObjects(list);
    return result;
}
 
Example #3
Source File: S3ProxyImpl.java    From pravega with Apache License 2.0 6 votes vote down vote up
@Synchronized
@Override
public void putObject(String bucketName, String key, Range range, Object content) {
    byte[] existingBytes = new byte[Math.toIntExact(range.getFirst())];
    try {
        if (range.getFirst() != 0) {
            int bytesRead = client.getObject(bucketName, key).getObject().read(existingBytes, 0,
                    Math.toIntExact(range.getFirst()));
            if (bytesRead != range.getFirst()) {
                throw new S3Exception("InvalidRange", HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE, "InvalidRange", key);
            }
        }
        val contentBytes  = IOUtils.toByteArray((InputStream) content);
        if (contentBytes.length != Math.toIntExact(range.getLast()  - range.getFirst() + 1)) {
            throw new S3Exception("InvalidRange", HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE, "InvalidRange", key);
        }

        val objectAfterPut = ArrayUtils.addAll(existingBytes, contentBytes);
        client.putObject(new PutObjectRequest(bucketName, key, (Object) objectAfterPut));
        aclMap.put(key, aclMap.get(key).withSize(range.getLast() - 1));
    } catch (IOException e) {
        throw new S3Exception("NoObject", HttpStatus.SC_NOT_FOUND, "NoSuchKey", key);
    }
}
 
Example #4
Source File: ExtendedS3Storage.java    From pravega with Apache License 2.0 5 votes vote down vote up
private boolean doExists(String streamSegmentName) {
    try {
        S3ObjectMetadata result = client.getObjectMetadata(config.getBucket(),
                config.getPrefix() + streamSegmentName);
        return true;
    } catch (S3Exception e) {
        if ( e.getErrorCode().equals("NoSuchKey")) {
            return false;
        } else {
            throw e;
        }
    }
}
 
Example #5
Source File: ExtendedS3Storage.java    From pravega with Apache License 2.0 5 votes vote down vote up
private <T> T throwException(String segmentName, Exception e) throws StreamSegmentException {
    if (e instanceof S3Exception) {
        S3Exception s3Exception = (S3Exception) e;
        String errorCode = Strings.nullToEmpty(s3Exception.getErrorCode());

        if (errorCode.equals("NoSuchKey")) {
            throw new StreamSegmentNotExistsException(segmentName);
        }

        if (errorCode.equals("PreconditionFailed")) {
            throw new StreamSegmentExistsException(segmentName);
        }

        if (errorCode.equals("InvalidRange")
                || errorCode.equals("InvalidArgument")
                || errorCode.equals("MethodNotAllowed")
                || s3Exception.getHttpCode() == HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE) {
            throw new IllegalArgumentException(segmentName, e);
        }

        if (errorCode.equals("AccessDenied")) {
            throw new StreamSegmentSealedException(segmentName, e);
        }
    }

    if (e instanceof IndexOutOfBoundsException) {
        throw new ArrayIndexOutOfBoundsException(e.getMessage());
    }

    throw Exceptions.sneakyThrow(e);
}
 
Example #6
Source File: S3FileSystemImpl.java    From pravega with Apache License 2.0 5 votes vote down vote up
@Synchronized
@Override
public void setObjectAcl(String bucketName, String key, AccessControlList acl) {
    AclSize retVal = aclMap.get(key);
    if (retVal == null) {
        throw new S3Exception("NoObject", HttpStatus.SC_NOT_FOUND, "NoSuchKey", key);
    }
    aclMap.put(key, retVal.withAcl(acl));
}
 
Example #7
Source File: S3FileSystemImpl.java    From pravega with Apache License 2.0 5 votes vote down vote up
@Synchronized
@Override
public void setObjectAcl(SetObjectAclRequest request) {
    AclSize retVal = aclMap.get(request.getKey());
    if (retVal == null) {
        throw new S3Exception("NoObject", HttpStatus.SC_NOT_FOUND, "NoSuchKey", request.getKey());
    }
    aclMap.put(request.getKey(), retVal.withAcl(request.getAcl()));
}
 
Example #8
Source File: S3FileSystemImpl.java    From pravega with Apache License 2.0 5 votes vote down vote up
@Override
public AccessControlList getObjectAcl(String bucketName, String key) {
    AclSize retVal = aclMap.get(key);
    if (retVal == null) {
        throw new S3Exception("NoObject", HttpStatus.SC_NOT_FOUND, "NoSuchKey", key);
    }
    return retVal.getAcl();
}
 
Example #9
Source File: S3FileSystemImpl.java    From pravega with Apache License 2.0 5 votes vote down vote up
@Override
public CopyPartResult copyPart(CopyPartRequest request) {
    Map<Integer, CopyPartRequest> partMap = multipartUploads.get(request.getKey());
    if (partMap == null) {
        throw new S3Exception("NoSuchKey", HttpStatus.SC_NOT_FOUND, "NoSuchKey", "");
    }
    partMap.put(request.getPartNumber(), request);
    CopyPartResult result = new CopyPartResult();
    result.setPartNumber(request.getPartNumber());
    result.setETag(request.getUploadId());
    return result;
}
 
Example #10
Source File: S3FileSystemImpl.java    From pravega with Apache License 2.0 5 votes vote down vote up
@Override
public void deleteObject(String bucketName, String key) {
    Path path = Paths.get(this.baseDir, bucketName, key);
    try {
        Files.delete(path);
    } catch (IOException e) {
        throw new S3Exception("NoSuchKey", HttpStatus.SC_NOT_FOUND, "NoSuchKey", "");
    }
}
 
Example #11
Source File: S3FileSystemImpl.java    From pravega with Apache License 2.0 5 votes vote down vote up
@Override
public S3ObjectMetadata getObjectMetadata(String bucketName, String key) {
    S3ObjectMetadata metadata = new S3ObjectMetadata();
    AclSize data = aclMap.get(key);
    if (data == null) {
        throw new S3Exception("NoSuchKey", HttpStatus.SC_NOT_FOUND, "NoSuchKey", "");
    }
    metadata.setContentLength(data.getSize());
    Path path = Paths.get(this.baseDir, bucketName, key);
    metadata.setLastModified(new Date(path.toFile().lastModified()));
    return metadata;
}
 
Example #12
Source File: S3FileSystemImpl.java    From pravega with Apache License 2.0 5 votes vote down vote up
@Synchronized
@Override
public CompleteMultipartUploadResult completeMultipartUpload(CompleteMultipartUploadRequest request) {
    Map<Integer, CopyPartRequest> partMap = multipartUploads.get(request.getKey());
    if (partMap == null) {
        throw new S3Exception("NoSuchKey", HttpStatus.SC_NOT_FOUND, "NoSuchKey", "");
    }
    try {
        partMap.forEach((index, copyPart) -> {
            if (!copyPart.getKey().equals(copyPart.getSourceKey())) {
                Path sourcePath = Paths.get(this.baseDir, copyPart.getBucketName(), copyPart.getSourceKey());
                Path targetPath = Paths.get(this.baseDir, copyPart.getBucketName(), copyPart.getKey());
                try (FileChannel sourceChannel = FileChannel.open(sourcePath, StandardOpenOption.READ);
                     FileChannel targetChannel = FileChannel.open(targetPath, StandardOpenOption.WRITE)) {
                    targetChannel.transferFrom(sourceChannel, Files.size(targetPath),
                            copyPart.getSourceRange().getLast() + 1 - copyPart.getSourceRange().getFirst());
                    targetChannel.close();
                    AclSize aclMap = this.aclMap.get(copyPart.getKey());
                    this.aclMap.put(copyPart.getKey(), aclMap.withSize(Files.size(targetPath)));
                } catch (IOException e) {
                    throw new S3Exception("NoSuchKey", 404, "NoSuchKey", "");
                }
            }
        });
    } finally {
        multipartUploads.remove(request.getKey());
    }

    return new CompleteMultipartUploadResult();
}
 
Example #13
Source File: S3ProxyImpl.java    From pravega with Apache License 2.0 5 votes vote down vote up
@Synchronized
@Override
public void setObjectAcl(String bucketName, String key, AccessControlList acl) {
    AclSize retVal = aclMap.get(key);
    if (retVal == null) {
        throw new S3Exception("NoObject", HttpStatus.SC_NOT_FOUND, "NoSuchKey", key);
    }
    aclMap.put(key, retVal.withAcl(acl));
}
 
Example #14
Source File: S3ProxyImpl.java    From pravega with Apache License 2.0 5 votes vote down vote up
@Synchronized
@Override
public void setObjectAcl(SetObjectAclRequest request) {
    AclSize retVal = aclMap.get(request.getKey());
    if (retVal == null) {
        throw new S3Exception("NoObject", HttpStatus.SC_NOT_FOUND, "NoSuchKey", request.getKey());
    }
    aclMap.put(request.getKey(), retVal.withAcl(request.getAcl()));
}
 
Example #15
Source File: S3ProxyImpl.java    From pravega with Apache License 2.0 5 votes vote down vote up
@Override
public AccessControlList getObjectAcl(String bucketName, String key) {
    AclSize retVal = aclMap.get(key);
    if (retVal == null) {
        throw new S3Exception("NoObject", HttpStatus.SC_NOT_FOUND, "NoSuchKey", key);
    }
    return retVal.getAcl();
}
 
Example #16
Source File: S3ProxyImpl.java    From pravega with Apache License 2.0 5 votes vote down vote up
@Override
public CopyPartResult copyPart(CopyPartRequest request) {
    if (aclMap.get(request.getKey()) == null) {
        throw new S3Exception("NoObject", HttpStatus.SC_NOT_FOUND, "NoSuchKey", request.getKey());
    }
    return client.copyPart(request);
}