Search in sources :

Example 1 with S3Exception

use of com.emc.object.s3.S3Exception in project pravega by pravega.

the class S3FileSystemImpl method completeMultipartUpload.

@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() != 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();
}
Also used : Path(java.nio.file.Path) CopyPartRequest(com.emc.object.s3.request.CopyPartRequest) S3Exception(com.emc.object.s3.S3Exception) FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) CompleteMultipartUploadResult(com.emc.object.s3.bean.CompleteMultipartUploadResult) Synchronized(lombok.Synchronized)

Example 2 with S3Exception

use of com.emc.object.s3.S3Exception in project pravega by pravega.

the class S3FileSystemImpl method listObjects.

@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;
}
Also used : Path(java.nio.file.Path) S3Exception(com.emc.object.s3.S3Exception) ArrayList(java.util.ArrayList) S3Object(com.emc.object.s3.bean.S3Object) IOException(java.io.IOException) ListObjectsResult(com.emc.object.s3.bean.ListObjectsResult)

Example 3 with S3Exception

use of com.emc.object.s3.S3Exception in project pravega by pravega.

the class S3FileSystemImpl method copyPart.

@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;
}
Also used : CopyPartRequest(com.emc.object.s3.request.CopyPartRequest) S3Exception(com.emc.object.s3.S3Exception) CopyPartResult(com.emc.object.s3.bean.CopyPartResult)

Example 4 with S3Exception

use of com.emc.object.s3.S3Exception in project pravega by pravega.

the class S3FileSystemImpl method readObjectStream.

@Override
public InputStream readObjectStream(String bucketName, String key, Range range) {
    byte[] bytes = new byte[Math.toIntExact(range.getLast() + 1 - range.getFirst())];
    Path path = Paths.get(this.baseDir, bucketName, key);
    FileInputStream returnStream;
    try {
        returnStream = new FileInputStream(path.toFile());
        if (range.getFirst() != 0) {
            long bytesSkipped = 0;
            do {
                bytesSkipped += returnStream.skip(range.getFirst());
            } while (bytesSkipped < range.getFirst());
        }
        StreamHelpers.readAll(returnStream, bytes, 0, bytes.length);
        return new ByteArrayInputStream(bytes);
    } catch (IOException e) {
        throw new S3Exception("NoSuchKey", HttpStatus.SC_NOT_FOUND, "NoSuchKey", "");
    }
}
Also used : Path(java.nio.file.Path) ByteArrayInputStream(java.io.ByteArrayInputStream) S3Exception(com.emc.object.s3.S3Exception) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 5 with S3Exception

use of com.emc.object.s3.S3Exception in project pravega by pravega.

the class S3FileSystemImpl method putObject.

@Override
public PutObjectResult putObject(PutObjectRequest request) {
    if (request.getObjectMetadata() != null) {
        request.setObjectMetadata(null);
    }
    try {
        Path path = Paths.get(this.baseDir, request.getBucketName(), request.getKey());
        Files.createDirectories(path.getParent());
        Files.createFile(path);
    } catch (IOException e) {
        throw new S3Exception(e.getMessage(), 0, e);
    }
    PutObjectResult retVal = new PutObjectResult();
    if (request.getAcl() != null) {
        long size = 0;
        if (request.getRange() != null) {
            size = request.getRange().getLast() + 1;
        }
        aclMap.putIfAbsent(request.getKey(), new AclSize(request.getAcl(), size));
    }
    return retVal;
}
Also used : Path(java.nio.file.Path) PutObjectResult(com.emc.object.s3.bean.PutObjectResult) S3Exception(com.emc.object.s3.S3Exception) IOException(java.io.IOException)

Aggregations

S3Exception (com.emc.object.s3.S3Exception)8 IOException (java.io.IOException)6 Path (java.nio.file.Path)6 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Synchronized (lombok.Synchronized)3 CopyPartRequest (com.emc.object.s3.request.CopyPartRequest)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 FileChannel (java.nio.channels.FileChannel)2 S3ObjectMetadata (com.emc.object.s3.S3ObjectMetadata)1 CompleteMultipartUploadResult (com.emc.object.s3.bean.CompleteMultipartUploadResult)1 CopyPartResult (com.emc.object.s3.bean.CopyPartResult)1 ListObjectsResult (com.emc.object.s3.bean.ListObjectsResult)1 PutObjectResult (com.emc.object.s3.bean.PutObjectResult)1 S3Object (com.emc.object.s3.bean.S3Object)1 PutObjectRequest (com.emc.object.s3.request.PutObjectRequest)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1