Search in sources :

Example 1 with PutObjectRequest

use of com.emc.object.s3.request.PutObjectRequest 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)

Example 2 with PutObjectRequest

use of com.emc.object.s3.request.PutObjectRequest in project pravega by pravega.

the class ExtendedS3Storage method doCreate.

private SegmentProperties doCreate(String streamSegmentName) throws StreamSegmentExistsException {
    long traceId = LoggerHelpers.traceEnter(log, "create", streamSegmentName);
    if (!client.listObjects(config.getBucket(), config.getRoot() + streamSegmentName).getObjects().isEmpty()) {
        throw new StreamSegmentExistsException(streamSegmentName);
    }
    S3ObjectMetadata metadata = new S3ObjectMetadata();
    metadata.setContentLength((long) 0);
    PutObjectRequest request = new PutObjectRequest(config.getBucket(), config.getRoot() + streamSegmentName, null);
    AccessControlList acl = new AccessControlList();
    acl.addGrants(new Grant(new CanonicalUser(config.getAccessKey(), config.getAccessKey()), READ_WRITE_PERMISSION));
    request.setAcl(acl);
    /* Default behavior of putObject is to overwrite an existing object. This behavior can cause data loss.
         * Here is one of the scenarios in which data loss is observed:
         * 1. Host A owns the container and gets a create operation. It has not executed the putObject operation yet.
         * 2. Ownership changes and host B becomes the owner of the container. It picks up putObject from the queue, executes it.
         * 3. Host B gets a write operation which executes successfully.
         * 4. Now host A schedules the putObject. This will overwrite the write by host B.
         *
         * The solution for this issue is to implement put-if-absent behavior by using Set-If-None-Match header as described here:
         * http://www.emc.com/techpubs/api/ecs/v3-0-0-0/S3ObjectOperations_createOrUpdateObject_7916bd6f789d0ae0ff39961c0e660d00_ba672412ac371bb6cf4e69291344510e_detail.htm
         * But this does not work. Currently all the calls to putObject API fail if made with reqest.setIfNoneMatch("*").
         * once the issue with extended S3 API is fixed, addition of this one line will ensure put-if-absent semantics.
         * See: https://github.com/pravega/pravega/issues/1564
         *
         * This issue is fixed in some versions of extended S3 implementation. The following code sets the IfNoneMatch
         * flag based on configuration.
         */
    if (config.isUseNoneMatch()) {
        request.setIfNoneMatch("*");
    }
    client.putObject(request);
    LoggerHelpers.traceLeave(log, "create", traceId);
    return doGetStreamSegmentInfo(streamSegmentName);
}
Also used : AccessControlList(com.emc.object.s3.bean.AccessControlList) Grant(com.emc.object.s3.bean.Grant) StreamSegmentExistsException(io.pravega.segmentstore.contracts.StreamSegmentExistsException) S3ObjectMetadata(com.emc.object.s3.S3ObjectMetadata) CanonicalUser(com.emc.object.s3.bean.CanonicalUser) PutObjectRequest(com.emc.object.s3.request.PutObjectRequest)

Example 3 with PutObjectRequest

use of com.emc.object.s3.request.PutObjectRequest 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)

Example 4 with PutObjectRequest

use of com.emc.object.s3.request.PutObjectRequest in project pravega by pravega.

the class S3ProxyImpl method putObject.

@Override
public PutObjectResult putObject(PutObjectRequest request) {
    S3ObjectMetadata metadata = request.getObjectMetadata();
    if (request.getObjectMetadata() != null) {
        request.setObjectMetadata(null);
    }
    PutObjectResult retVal = client.putObject(request);
    if (request.getAcl() != null) {
        long size = 0;
        if (request.getRange() != null) {
            size = request.getRange().getLast() - 1;
        }
        aclMap.put(request.getKey(), new AclSize(request.getAcl(), size));
    }
    return retVal;
}
Also used : PutObjectResult(com.emc.object.s3.bean.PutObjectResult) S3ObjectMetadata(com.emc.object.s3.S3ObjectMetadata)

Aggregations

S3Exception (com.emc.object.s3.S3Exception)2 S3ObjectMetadata (com.emc.object.s3.S3ObjectMetadata)2 PutObjectResult (com.emc.object.s3.bean.PutObjectResult)2 PutObjectRequest (com.emc.object.s3.request.PutObjectRequest)2 IOException (java.io.IOException)2 AccessControlList (com.emc.object.s3.bean.AccessControlList)1 CanonicalUser (com.emc.object.s3.bean.CanonicalUser)1 Grant (com.emc.object.s3.bean.Grant)1 StreamSegmentExistsException (io.pravega.segmentstore.contracts.StreamSegmentExistsException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 Synchronized (lombok.Synchronized)1