Example usage for com.amazonaws.services.s3.model GroupGrantee AllUsers

List of usage examples for com.amazonaws.services.s3.model GroupGrantee AllUsers

Introduction

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

Prototype

GroupGrantee AllUsers

To view the source code for com.amazonaws.services.s3.model GroupGrantee AllUsers.

Click Source Link

Document

Grants anonymous access to any Amazon S3 object or 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;//from ww  w . j  a  v a 2s. 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

private static boolean isUserIdInGrantee(Grantee grantee, String userId) {
    return grantee.getIdentifier().equals(userId) || grantee.equals(GroupGrantee.AllUsers)
            || grantee.equals(GroupGrantee.AuthenticatedUsers);
}

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

License:Open Source License

/**
 * {@inheritDoc}/* w w  w  .  j av  a  2s. co  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  .jav a2s  . c  o  m*/
 */
@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:org.benetech.secureapp.generator.AmazonS3Utils.java

License:Open Source License

static public void uploadToAmazonS3(HttpSession session, File fileToUpload) throws S3Exception {
    try {/*  w w w.  ja va 2s  .c o  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.reswitchboard.utils.s3.access.App.java

License:Open Source License

public static void main(String[] args) {
    try {/*from w w w  .  j a  v a  2s  .c o m*/
        if (args.length == 0 || StringUtils.isNullOrEmpty(args[0]))
            throw new IllegalArgumentException("Bucket name can not be empty");

        String bucketName = args[0];
        String prefix = null;
        if (args.length > 1)
            prefix = args[1];

        AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());

        ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName);

        if (!StringUtils.isNullOrEmpty(prefix))
            listObjectsRequest.setPrefix(prefix);

        ObjectListing objectListing;

        do {
            objectListing = s3client.listObjects(listObjectsRequest);
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                String key = objectSummary.getKey();
                System.out.println(" - " + key);

                for (int nAttempt = 1;; ++nAttempt) {
                    try {

                        AccessControlList acl = s3client.getObjectAcl(bucketName, key);
                        List<Grant> grants = acl.getGrantsAsList();
                        for (Grant grant : grants) {
                            //   System.out.println( "      Grant: " + grant.toString());

                            if (grant.getGrantee().equals(GroupGrantee.AllUsers)) {
                                System.out.println("      Revoking public access");

                                acl.revokeAllPermissions(GroupGrantee.AllUsers);
                                s3client.setObjectAcl(bucketName, key, acl);

                                break;
                            }
                        }

                        break;
                    } catch (Exception e) {
                        System.out.println("Error: " + e.toString());

                        if (nAttempt >= 10) {
                            throw new Exception("Maximum number of invalid attempts has been reeched");
                        }

                        // double back-off delay
                        Thread.sleep((long) (Math.pow(2, nAttempt) * 50));
                    }
                }

            }
            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());
    } catch (Exception e) {
        e.printStackTrace();
    }
}