Example usage for com.amazonaws.services.s3.model Permission Read

List of usage examples for com.amazonaws.services.s3.model Permission Read

Introduction

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

Prototype

Permission Read

To view the source code for com.amazonaws.services.s3.model Permission Read.

Click Source Link

Document

Grants permission to list the bucket when applied to a bucket.

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;// w  w  w .  j a  v a2  s  . c  o  m
    }
    /*
     * 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:alluxio.underfs.s3a.S3AUtils.java

License:Apache License

/**
 * Translates S3 bucket ACL to Alluxio owner mode.
 *
 * @param acl the acl of S3 bucket/*from   w ww . j a  v a 2  s . c  o m*/
 * @param userId the S3 user id of the Alluxio owner
 * @return the translated posix mode in short format
 */
public static short translateBucketAcl(AccessControlList acl, String userId) {
    short mode = (short) 0;
    for (Grant grant : acl.getGrantsAsList()) {
        Permission perm = grant.getPermission();
        Grantee grantee = grant.getGrantee();
        if (perm.equals(Permission.Read)) {
            if (isUserIdInGrantee(grantee, userId)) {
                // If the bucket is readable by the user, add r and x to the owner mode.
                mode |= (short) 0500;
            }
        } else if (perm.equals(Permission.Write)) {
            if (isUserIdInGrantee(grantee, userId)) {
                // If the bucket is writable by the user, +w to the owner mode.
                mode |= (short) 0200;
            }
        } else if (perm.equals(Permission.FullControl)) {
            if (isUserIdInGrantee(grantee, userId)) {
                // If the user has full control to the bucket, +rwx to the owner mode.
                mode |= (short) 0700;
            }
        }
    }
    return mode;
}

From source file:cloudExplorer.Acl.java

License:Open Source License

void setAccess(String id, int what, String access_key, String secret_key, String endpoint, String bucket) {
    try {/*from w  w  w.j a v  a2 s.c  o  m*/

        Collection<Grant> grantCollection = new ArrayList<Grant>();
        AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key);
        AmazonS3 s3Client = new AmazonS3Client(credentials,
                new ClientConfiguration().withSignerOverride("S3SignerType"));
        s3Client.setEndpoint(endpoint);
        AccessControlList bucketAcl = s3Client.getBucketAcl(bucket);
        Grant grant = null;
        if (what == 0) {

            grant = new Grant(new CanonicalGrantee(id), Permission.Read);
            grantCollection.add(grant);
        }

        if (what == 1) {
            grant = new Grant(new CanonicalGrantee(id), Permission.FullControl);
            grantCollection.add(grant);
        }

        if (what == 3) {
            bucketAcl.getGrants().clear();
        }

        bucketAcl.getGrants().addAll(grantCollection);
        s3Client.setBucketAcl(bucket, bucketAcl);

    } catch (AmazonServiceException ase) {
        NewJFrame.jTextArea1.append("\n\nError: " + ase.getErrorMessage());
    }
}

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

License:Open Source License

/**
 * {@inheritDoc}/*ww  w.ja v  a2  s .c o  m*/
 */
@Override
public PutObjectResult putObjectAllRW(AmazonS3 client, String bucketName, String key, File file) {
    Grant readGrant = new Grant(GroupGrantee.AllUsers, Permission.Read);
    Grant writeGrant = new Grant(GroupGrantee.AllUsers, Permission.Write);
    return this.putObject(client, bucketName, key, file, readGrant, writeGrant);
}

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

License:Open Source License

/**
 * {@inheritDoc}/*  w w  w. j  a  va2 s.  c  om*/
 */
@Override
public PutObjectResult putObjectAllRW(AmazonS3 client, String bucketName, String key, InputStream input,
        ObjectMetadata metadata) {
    Grant readGrant = new Grant(GroupGrantee.AllUsers, Permission.Read);
    Grant writeGrant = new Grant(GroupGrantee.AllUsers, Permission.Write);
    return this.putObject(client, bucketName, key, input, metadata, readGrant, writeGrant);
}

From source file:com.upplication.s3fs.S3FileSystemProvider.java

License:Open Source License

@Override
public void checkAccess(Path path, AccessMode... modes) throws IOException {
    S3Path s3Path = (S3Path) path;
    Preconditions.checkArgument(s3Path.isAbsolute(), "path must be absolute: %s", s3Path);

    AmazonS3Client client = s3Path.getFileSystem().getClient();

    // get ACL and check if the file exists as a side-effect
    AccessControlList acl = getAccessControl(s3Path);

    for (AccessMode accessMode : modes) {
        switch (accessMode) {
        case EXECUTE:
            throw new AccessDeniedException(s3Path.toString(), null, "file is not executable");
        case READ:
            if (!hasPermissions(acl, client.getS3AccountOwner(),
                    EnumSet.of(Permission.FullControl, Permission.Read))) {
                throw new AccessDeniedException(s3Path.toString(), null, "file is not readable");
            }// w ww. j  a  va2 s .  c  om
            break;
        case WRITE:
            if (!hasPermissions(acl, client.getS3AccountOwner(),
                    EnumSet.of(Permission.FullControl, Permission.Write))) {
                throw new AccessDeniedException(s3Path.toString(), null,
                        format("bucket '%s' is not writable", s3Path.getBucket()));
            }
            break;
        }
    }
}

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 a 2  s.  c  om*/
    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:io.milton.s3.AmazonS3ManagerImpl.java

License:Open Source License

@Override
public boolean isPublicEntity(String bucketName, String keyName) {
    LOG.info("Gets the AccessControlList (ACL) for the specified object " + keyName
            + " in the specified bucket " + bucketName);

    final String GROUPS_USERS = "http://acs.amazonaws.com/groups/global/AllUsers";
    try {/*from  w w  w.ja va  2 s .  co m*/
        AccessControlList accessControlList = amazonS3Client.getObjectAcl(bucketName, keyName);
        for (Iterator<Grant> iterator = accessControlList.getGrants().iterator(); iterator.hasNext();) {
            Grant grant = iterator.next();
            if (grant.getPermission().equals(Permission.Read)
                    && grant.getGrantee().getIdentifier().equals(GROUPS_USERS)) {
                return true;
            }
        }
    } catch (AmazonServiceException ase) {
        LOG.warn(ase.getMessage(), ase);
    } catch (AmazonClientException ace) {
        LOG.warn(ace.getMessage(), ace);
    }
    return false;
}

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

License:Apache License

/**
 * Create AccessControlList if appropriate properties are configured.
 *
 * @param context ProcessContext/*from w  w w . j  a  v a  2s .  c om*/
 * @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 www.j ava  2  s .com*/
        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);
    }
}