Example usage for com.amazonaws.services.s3.model AccessControlList grantPermission

List of usage examples for com.amazonaws.services.s3.model AccessControlList grantPermission

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model AccessControlList grantPermission.

Prototype

public void grantPermission(Grantee grantee, Permission permission) 

Source Link

Document

Adds a grantee to the access control list (ACL) with the given permission.

Usage

From source file:Uploader.java

License:Open Source License

private static void uploadFile(AmazonS3 s3, String bucketName, String key, File file) {
    if (!file.exists()) {
        System.out.println("File does not exist: " + file.getAbsolutePath());
        return;/*from  ww w .j  a  v  a 2  s  . com*/
    }
    /*
     * Upload an object to your bucket - You can easily upload a file to S3,
     * or upload directly an InputStream if you know the length of the data
     * in the stream. You can also specify your own metadata when uploading
     * to S3, which allows you set a variety of options like content-type
     * and content-encoding, plus additional metadata specific to your
     * applications.
     */
    try {
        System.out.println(file.getAbsolutePath() + " ---> " + key + "\n");
        s3.putObject(new PutObjectRequest(bucketName, key, file));
        // Change permissions. Grant all users the read permission.
        AccessControlList acl = s3.getObjectAcl(bucketName, key);
        Permission permission = Permission.Read;
        Grantee grantee = GroupGrantee.AllUsers;
        acl.grantPermission(grantee, permission);
        s3.setObjectAcl(bucketName, key, acl);
    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which means your request made it "
                + "to Amazon S3, but was rejected with an error response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }

}

From source file:aws.example.s3.SetAcl.java

License:Open Source License

public static void setBucketAcl(String bucket_name, String email, String access) {
    System.out.format("Setting %s access for %s\n", access, email);
    System.out.println("on bucket: " + bucket_name);

    final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
    try {//from  w  ww . j  av  a 2 s.co  m
        // get the current ACL
        AccessControlList acl = s3.getBucketAcl(bucket_name);
        // set access for the grantee
        EmailAddressGrantee grantee = new EmailAddressGrantee(email);
        Permission permission = Permission.valueOf(access);
        acl.grantPermission(grantee, permission);
        s3.setBucketAcl(bucket_name, acl);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
}

From source file:aws.example.s3.SetAcl.java

License:Open Source License

public static void setObjectAcl(String bucket_name, String object_key, String email, String access) {
    System.out.format("Setting %s access for %s\n", access, email);
    System.out.println("for object: " + object_key);
    System.out.println(" in bucket: " + bucket_name);

    final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
    try {/*from   w  w  w .  java2  s .  co m*/
        // get the current ACL
        AccessControlList acl = s3.getObjectAcl(bucket_name, object_key);
        // set access for the grantee
        EmailAddressGrantee grantee = new EmailAddressGrantee(email);
        Permission permission = Permission.valueOf(access);
        acl.grantPermission(grantee, permission);
        s3.setObjectAcl(bucket_name, object_key, acl);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
}

From source file:com.easarrive.aws.plugins.common.service.impl.S3Service.java

License:Open Source License

private PutObjectResult putObject(AmazonS3 client, String bucketName, String key, File file, Grantee grantee,
        Permission permission, Grant... grantsVarArg) {
    if (client == null) {
        return null;
    } else if (StringUtil.isEmpty(bucketName)) {
        return null;
    } else if (StringUtil.isEmpty(key)) {
        return null;
    } else if (file == null) {
        return null;
    } else if ((grantee == null || permission == null) && (grantsVarArg == null || grantsVarArg.length < 1)) {
        return null;
    }// w  ww . j av  a2s.c om
    PutObjectResult result = null;
    AccessControlList accessControlList = new AccessControlList();
    if (grantee != null && permission != null) {
        accessControlList.grantPermission(grantee, permission);
    }
    if (grantsVarArg != null && grantsVarArg.length > 0) {
        accessControlList.grantAllPermissions(grantsVarArg);
    }
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file)
            .withAccessControlList(accessControlList);
    result = client.putObject(putObjectRequest);
    return result;
}

From source file:com.easarrive.aws.plugins.common.service.impl.S3Service.java

License:Open Source License

private PutObjectResult putObject(AmazonS3 client, String bucketName, String key, InputStream input,
        ObjectMetadata metadata, Grantee grantee, Permission permission, Grant... grantsVarArg) {
    if (client == null) {
        return null;
    } else if (StringUtil.isEmpty(bucketName)) {
        return null;
    } else if (StringUtil.isEmpty(key)) {
        return null;
    } else if (input == null) {
        return null;
    } else if (metadata == null) {
        return null;
    } else if ((grantee == null || permission == null) && (grantsVarArg == null || grantsVarArg.length < 1)) {
        return null;
    }//from  ww w .j av a 2  s  .  c o m
    PutObjectResult result = null;
    AccessControlList accessControlList = new AccessControlList();
    if (grantee != null && permission != null) {
        accessControlList.grantPermission(grantee, permission);
    }
    if (grantsVarArg != null && grantsVarArg.length > 0) {
        accessControlList.grantAllPermissions(grantsVarArg);
    }
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, input, metadata)
            .withAccessControlList(accessControlList);
    result = client.putObject(putObjectRequest);
    return result;
}

From source file:com.emc.ecs.sync.util.AwsS3Util.java

License:Open Source License

public static AccessControlList s3AclFromSyncAcl(SyncAcl syncAcl, boolean ignoreInvalid) {
    AccessControlList s3Acl = new AccessControlList();

    s3Acl.setOwner(new Owner(syncAcl.getOwner(), syncAcl.getOwner()));

    for (String user : syncAcl.getUserGrants().keySet()) {
        Grantee grantee = new CanonicalGrantee(user);
        for (String permission : syncAcl.getUserGrants().get(user)) {
            Permission perm = getS3Permission(permission, ignoreInvalid);
            if (perm != null)
                s3Acl.grantPermission(grantee, perm);
        }// w  w w. ja v  a  2s  .  c o m
    }

    for (String group : syncAcl.getGroupGrants().keySet()) {
        Grantee grantee = GroupGrantee.parseGroupGrantee(group);
        if (grantee == null) {
            if (ignoreInvalid)
                log.warn("{} is not a valid S3 group", group);
            else
                throw new RuntimeException(group + " is not a valid S3 group");
        }
        for (String permission : syncAcl.getGroupGrants().get(group)) {
            Permission perm = getS3Permission(permission, ignoreInvalid);
            if (perm != null)
                s3Acl.grantPermission(grantee, perm);
        }
    }

    return s3Acl;
}

From source file:com.upplication.s3fs.util.AmazonS3ClientMock.java

License:Open Source License

private AccessControlList createAllPermission() {
    AccessControlList res = new AccessControlList();
    res.setOwner(getS3AccountOwner());/*from w w w  .j  a v a2 s  .  c o m*/
    Grantee grant = new Grantee() {

        @Override
        public void setIdentifier(String id) {
        }

        @Override
        public String getTypeIdentifier() {
            return getS3AccountOwner().getId();
        }

        @Override
        public String getIdentifier() {
            return getS3AccountOwner().getId();
        }
    };

    res.grantPermission(grant, Permission.FullControl);
    res.grantPermission(grant, Permission.Read);
    res.grantPermission(grant, Permission.Write);
    return res;
}

From source file:org.apache.nifi.processors.aws.s3.AbstractS3Processor.java

License:Apache License

/**
 * Create AccessControlList if appropriate properties are configured.
 *
 * @param context ProcessContext// ww  w  .  j  a v  a 2  s.  c  o m
 * @param flowFile FlowFile
 * @return AccessControlList or null if no ACL properties were specified
 */
protected final AccessControlList createACL(final ProcessContext context, final FlowFile flowFile) {
    // lazy-initialize ACL, as it should not be used if no properties were specified
    AccessControlList acl = null;

    final String ownerId = context.getProperty(OWNER).evaluateAttributeExpressions(flowFile).getValue();
    if (!StringUtils.isEmpty(ownerId)) {
        final Owner owner = new Owner();
        owner.setId(ownerId);
        if (acl == null) {
            acl = new AccessControlList();
        }
        acl.setOwner(owner);
    }

    for (final Grantee grantee : createGrantees(
            context.getProperty(FULL_CONTROL_USER_LIST).evaluateAttributeExpressions(flowFile).getValue())) {
        if (acl == null) {
            acl = new AccessControlList();
        }
        acl.grantPermission(grantee, Permission.FullControl);
    }

    for (final Grantee grantee : createGrantees(
            context.getProperty(READ_USER_LIST).evaluateAttributeExpressions(flowFile).getValue())) {
        if (acl == null) {
            acl = new AccessControlList();
        }
        acl.grantPermission(grantee, Permission.Read);
    }

    for (final Grantee grantee : createGrantees(
            context.getProperty(WRITE_USER_LIST).evaluateAttributeExpressions(flowFile).getValue())) {
        if (acl == null) {
            acl = new AccessControlList();
        }
        acl.grantPermission(grantee, Permission.Write);
    }

    for (final Grantee grantee : createGrantees(
            context.getProperty(READ_ACL_LIST).evaluateAttributeExpressions(flowFile).getValue())) {
        if (acl == null) {
            acl = new AccessControlList();
        }
        acl.grantPermission(grantee, Permission.ReadAcp);
    }

    for (final Grantee grantee : createGrantees(
            context.getProperty(WRITE_ACL_LIST).evaluateAttributeExpressions(flowFile).getValue())) {
        if (acl == null) {
            acl = new AccessControlList();
        }
        acl.grantPermission(grantee, Permission.WriteAcp);
    }

    return acl;
}

From source file:org.benetech.secureapp.generator.AmazonS3Utils.java

License:Open Source License

static public void uploadToAmazonS3(HttpSession session, File fileToUpload) throws S3Exception {
    try {//from w w w . j av a  2  s  .  co  m
        AmazonS3 s3client = getS3();
        String bucketName = getDownloadS3Bucket();
        if (!s3client.doesBucketExist(bucketName))
            SagLogger.logError(session, "Does not exist?  S3 Bucket :" + bucketName);

        AccessControlList acl = new AccessControlList();
        acl.grantPermission(GroupGrantee.AllUsers, Permission.Read);
        s3client.putObject(
                new PutObjectRequest(bucketName, getAPKDownloadFilePathWithFile(fileToUpload.getName()),
                        fileToUpload).withAccessControlList(acl));

        SagLogger.logInfo(session, "Finished uploading to S3");
    } catch (Exception e) {
        SagLogger.logException(session, e);
        throw new S3Exception(e);
    }
}

From source file:org.springframework.integration.aws.s3.core.AmazonS3OperationsImpl.java

License:Apache License

/**
 * Gets the {@link AccessControlList} from the given {@link AmazonS3ObjectACL} 
 * @param acl//w ww. ja  v a  2s .  c o m
 * @return 
 */
private AccessControlList getAccessControlList(String bucketName, String key, AmazonS3ObjectACL acl) {
    AccessControlList accessControlList = null;
    if (acl != null) {
        if (!acl.getGrants().isEmpty()) {
            accessControlList = client.getObjectAcl(bucketName, key);
            for (ObjectGrant objGrant : acl.getGrants()) {
                Grantee grantee = objGrant.getGrantee();
                com.amazonaws.services.s3.model.Grantee awsGrantee;
                if (grantee.getGranteeType() == GranteeType.CANONICAL_GRANTEE_TYPE) {
                    awsGrantee = new CanonicalGrantee(grantee.getIdentifier());
                } else if (grantee.getGranteeType() == GranteeType.EMAIL_GRANTEE_TYPE) {
                    awsGrantee = new EmailAddressGrantee(grantee.getIdentifier());
                } else {
                    awsGrantee = GroupGrantee.parseGroupGrantee(grantee.getIdentifier());
                    if (awsGrantee == null) {
                        logger.warn("Group grantee with identifier: \"" + grantee.getIdentifier()
                                + "\" not found. skipping this grant");
                        continue;
                    }
                }
                ObjectPermissions perm = objGrant.getPermission();
                Permission permission;
                if (perm == ObjectPermissions.READ) {
                    permission = Permission.Read;
                } else if (perm == ObjectPermissions.READ_ACP) {
                    permission = Permission.ReadAcp;
                } else
                    permission = Permission.WriteAcp;

                accessControlList.grantPermission(awsGrantee, permission);
            }
        }
    }
    return accessControlList;
}