Search in sources :

Example 6 with S3Exception

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

the class S3FileSystemImpl method putObject.

@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);
    }
}
Also used : Path(java.nio.file.Path) FileChannel(java.nio.channels.FileChannel) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) S3Exception(com.emc.object.s3.S3Exception) IOException(java.io.IOException) Synchronized(lombok.Synchronized)

Example 7 with S3Exception

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

the class S3FileSystemImpl method getObjectMetadata.

@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;
}
Also used : Path(java.nio.file.Path) S3Exception(com.emc.object.s3.S3Exception) S3ObjectMetadata(com.emc.object.s3.S3ObjectMetadata) Date(java.util.Date)

Example 8 with S3Exception

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

the class S3ProxyImpl method putObject.

@Synchronized
@Override
public void putObject(String bucketName, String key, Range range, Object content) {
    byte[] totalByes = new byte[Math.toIntExact(range.getLast() + 1)];
    try {
        if (range.getFirst() != 0) {
            int bytesRead = client.getObject(bucketName, key).getObject().read(totalByes, 0, Math.toIntExact(range.getFirst()));
            if (bytesRead != range.getFirst()) {
                throw new IllegalStateException("Unable to read from the object " + key);
            }
        }
        int bytesRead = ((InputStream) content).read(totalByes, Math.toIntExact(range.getFirst()), Math.toIntExact(range.getLast() + 1 - range.getFirst()));
        if (bytesRead != range.getLast() + 1 - range.getFirst()) {
            throw new IllegalStateException("Not able to read from input stream.");
        }
        client.putObject(new PutObjectRequest(bucketName, key, (Object) new ByteArrayInputStream(totalByes)));
        aclMap.put(key, aclMap.get(key).withSize(range.getLast() - 1));
    } catch (IOException e) {
        throw new S3Exception("NoObject", HttpStatus.SC_NOT_FOUND, "NoSuchKey", key);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) S3Exception(com.emc.object.s3.S3Exception) IOException(java.io.IOException) PutObjectRequest(com.emc.object.s3.request.PutObjectRequest) Synchronized(lombok.Synchronized)

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