use of io.pravega.segmentstore.storage.chunklayer.ChunkStorageException in project pravega by pravega.
the class ExtendedS3ChunkStorage method doCreate.
@Override
protected ChunkHandle doCreate(String chunkName) throws ChunkStorageException {
Preconditions.checkState(supportsAppend, "supportsAppend is false.");
try {
if (!client.listObjects(config.getBucket(), getObjectPath(chunkName)).getObjects().isEmpty()) {
throw new ChunkAlreadyExistsException(chunkName, "Chunk already exists");
}
S3ObjectMetadata metadata = new S3ObjectMetadata();
metadata.setContentLength((long) 0);
PutObjectRequest request = new PutObjectRequest(config.getBucket(), getObjectPath(chunkName), null).withObjectMetadata(metadata);
if (config.isUseNoneMatch()) {
request.setIfNoneMatch("*");
}
client.putObject(request);
return ChunkHandle.writeHandle(chunkName);
} catch (Exception e) {
throw convertException(chunkName, "doCreate", e);
}
}
use of io.pravega.segmentstore.storage.chunklayer.ChunkStorageException in project pravega by pravega.
the class ExtendedS3ChunkStorage method doGetInfo.
@Override
protected ChunkInfo doGetInfo(String chunkName) throws ChunkStorageException {
try {
S3ObjectMetadata result = client.getObjectMetadata(config.getBucket(), getObjectPath(chunkName));
ChunkInfo information = ChunkInfo.builder().name(chunkName).length(result.getContentLength()).build();
return information;
} catch (Exception e) {
throw convertException(chunkName, "doGetInfo", e);
}
}
use of io.pravega.segmentstore.storage.chunklayer.ChunkStorageException in project pravega by pravega.
the class ExtendedS3ChunkStorage method doCreateWithContent.
@Override
protected ChunkHandle doCreateWithContent(String chunkName, int length, InputStream data) throws ChunkStorageException {
try {
val objectPath = getObjectPath(chunkName);
S3ObjectMetadata metadata = new S3ObjectMetadata().withContentType("application/octet-stream").withContentLength(length);
val request = new PutObjectRequest(this.config.getBucket(), objectPath, data).withObjectMetadata(metadata);
client.putObject(request);
return ChunkHandle.writeHandle(chunkName);
} catch (Exception e) {
throw convertException(chunkName, "doCreateWithContent", e);
}
}
use of io.pravega.segmentstore.storage.chunklayer.ChunkStorageException in project pravega by pravega.
the class ExtendedS3ChunkStorage method convertException.
private ChunkStorageException convertException(String chunkName, String message, Exception e) {
ChunkStorageException retValue = null;
if (e instanceof ChunkStorageException) {
return (ChunkStorageException) e;
}
if (e instanceof S3Exception) {
S3Exception s3Exception = (S3Exception) e;
String errorCode = Strings.nullToEmpty(s3Exception.getErrorCode());
if (errorCode.equals("NoSuchKey")) {
retValue = new ChunkNotFoundException(chunkName, message, e);
}
if (errorCode.equals("PreconditionFailed")) {
retValue = new ChunkAlreadyExistsException(chunkName, message, e);
}
if (errorCode.equals("InvalidRange") || errorCode.equals("InvalidArgument") || errorCode.equals("MethodNotAllowed") || s3Exception.getHttpCode() == HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE) {
throw new IllegalArgumentException(chunkName, e);
}
if (errorCode.equals("AccessDenied")) {
retValue = new ChunkStorageException(chunkName, String.format("Access denied for chunk %s - %s.", chunkName, message), e);
}
}
if (retValue == null) {
retValue = new ChunkStorageException(chunkName, message, e);
}
return retValue;
}
use of io.pravega.segmentstore.storage.chunklayer.ChunkStorageException in project pravega by pravega.
the class ExtendedS3ChunkStorage method doWrite.
@Override
protected int doWrite(ChunkHandle handle, long offset, int length, InputStream data) throws ChunkStorageException {
Preconditions.checkState(supportsAppend, "supportsAppend is false.");
try {
val objectPath = getObjectPath(handle.getChunkName());
// Check object exists.
val metadata = client.getObjectMetadata(config.getBucket(), objectPath);
if (metadata.getContentLength() != offset) {
throw new InvalidOffsetException(handle.getChunkName(), metadata.getContentLength(), offset, "doWrite");
}
// Put data.
client.putObject(this.config.getBucket(), objectPath, Range.fromOffsetLength(offset, length), data);
return length;
} catch (Exception e) {
throw convertException(handle.getChunkName(), "doWrite", e);
}
}
Aggregations