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

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

Introduction

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

Prototype

AccessControlList

Source Link

Usage

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 .ja  v a 2  s.  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;
    }/* w w w.  j a v  a2  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);
        }//from  ww w  . java2  s.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

@Override
public CopyObjectResult copyObject(String sourceBucketName, String sourceKey, String destinationBucketName,
        String destinationKey) throws AmazonClientException {

    S3Element element = find(sourceBucketName, sourceKey);

    if (element != null) {

        S3Object objectSource = element.getS3Object();
        // copy object with
        S3Object resObj = new S3Object();
        resObj.setBucketName(destinationBucketName);
        resObj.setKey(destinationKey);//from  w ww . j  av  a 2s.  co m
        resObj.setObjectContent(objectSource.getObjectContent());
        resObj.setObjectMetadata(objectSource.getObjectMetadata());
        resObj.setRedirectLocation(objectSource.getRedirectLocation());
        // copy permission
        AccessControlList permission = new AccessControlList();
        permission.setOwner(element.getPermission().getOwner());
        permission.grantAllPermissions(element.getPermission().getGrants().toArray(new Grant[0]));
        S3Element elementResult = new S3Element(resObj, permission, sourceKey.endsWith("/"));
        // TODO: add should replace existing
        objects.get(find(destinationBucketName)).remove(elementResult);
        objects.get(find(destinationBucketName)).add(elementResult);

        return new CopyObjectResult();
    }

    throw new AmazonServiceException("object source not found");
}

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

License:Open Source License

private AccessControlList createAllPermission() {
    AccessControlList res = new AccessControlList();
    res.setOwner(getS3AccountOwner());//from  ww w .  j a v a2  s .  co 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:com.wowza.wms.plugin.s3upload.ModuleS3Upload.java

License:Open Source License

public void onAppStart(IApplicationInstance appInstance) {
    logger = WMSLoggerFactory.getLoggerObj(appInstance);
    this.appInstance = appInstance;

    try {//from www  . j ava 2  s  .  c o  m
        WMSProperties props = appInstance.getProperties();
        accessKey = props.getPropertyStr("s3UploadAccessKey", accessKey);
        secretKey = props.getPropertyStr("s3UploadSecretKey", secretKey);
        bucketName = props.getPropertyStr("s3UploadBucketName", bucketName);
        endpoint = props.getPropertyStr("s3UploadEndpoint", endpoint);
        resumeUploads = props.getPropertyBoolean("s3UploadResumeUploads", resumeUploads);
        deleteOriginalFiles = props.getPropertyBoolean("s3UploadDeletOriginalFiles", deleteOriginalFiles);
        // fix typo in property name
        deleteOriginalFiles = props.getPropertyBoolean("s3UploadDeleteOriginalFiles", deleteOriginalFiles);

        // This value should be the URI representation of the "Group Grantee" found here http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html under "Amazon S3 Predefined Groups"
        aclGroupGranteeUri = props.getPropertyStr("s3UploadACLGroupGranteeUri", aclGroupGranteeUri);
        // This should be a string that represents the level of permissions we want to grant to the "Group Grantee" access to the file to be uploaded
        aclPermissionRule = props.getPropertyStr("s3UploadACLPermissionRule", aclPermissionRule);

        // With the passed property, check if it maps to a specified GroupGrantee
        GroupGrantee grantee = GroupGrantee.parseGroupGrantee(aclGroupGranteeUri);
        // In order for the parsing to work correctly, we will go ahead and force uppercase on the string passed
        Permission permission = Permission.parsePermission(aclPermissionRule.toUpperCase());

        // If we have properties for specifying permisions on the file upload, create the AccessControlList object and set the Grantee and Permissions
        if (grantee != null && permission != null) {
            acl = new AccessControlList();
            acl.grantPermission(grantee, permission);
        }

        if (StringUtils.isEmpty(accessKey) || StringUtils.isEmpty(secretKey)) {
            logger.warn(
                    MODULE_NAME + ".onAppStart: [" + appInstance.getContextStr() + "] missing S3 credentials",
                    WMSLoggerIDs.CAT_application, WMSLoggerIDs.EVT_comment);
            return;
        }

        AmazonS3 s3Client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey));

        if (!StringUtils.isEmpty(endpoint))
            s3Client.setEndpoint(endpoint);

        if (!StringUtils.isEmpty(bucketName)) {
            boolean hasBucket = false;
            List<Bucket> buckets = s3Client.listBuckets();
            for (Bucket bucket : buckets) {
                if (bucket.getName().equals(bucketName)) {
                    hasBucket = true;
                    break;
                }
            }
            if (!hasBucket) {
                logger.warn(MODULE_NAME + ".onAppStart: [" + appInstance.getContextStr()
                        + "] missing S3 bucket: " + bucketName, WMSLoggerIDs.CAT_application,
                        WMSLoggerIDs.EVT_comment);
                return;
            }
        }

        logger.info(MODULE_NAME + ".onAppStart [" + appInstance.getContextStr() + "] S3 Bucket Name: "
                + bucketName + ", Resume Uploads: " + resumeUploads + ", Delete Original Files: "
                + deleteOriginalFiles, WMSLoggerIDs.CAT_application, WMSLoggerIDs.EVT_comment);
        transferManager = new TransferManager(s3Client);
        resumeUploads();

        appInstance.addMediaWriterListener(new WriteListener());
    } catch (AmazonS3Exception ase) {
        logger.error(MODULE_NAME + ".onAppStart [" + appInstance.getContextStr() + "] AmazonS3Exception: "
                + ase.getMessage());
    } catch (Exception e) {
        logger.error(
                MODULE_NAME + ".onAppStart [" + appInstance.getContextStr() + "] exception: " + e.getMessage(),
                e);
    } catch (Throwable t) {
        logger.error(MODULE_NAME + ".onAppStart [" + appInstance.getContextStr() + "] throwable exception: "
                + t.getMessage(), t);
    }
}

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  ww. j  a v a  2  s  . co 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  ava  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.weakref.s3fs.util.AmazonS3ClientMock.java

License:Apache License

@Override
public CopyObjectResult copyObject(String sourceBucketName, String sourceKey, String destinationBucketName,
        String destinationKey) throws AmazonClientException, AmazonServiceException {

    S3Element element = find(sourceBucketName, sourceKey);

    if (element != null) {

        S3Object objectSource = element.getS3Object();
        // copy object with
        S3Object resObj = new S3Object();
        resObj.setBucketName(destinationBucketName);
        resObj.setKey(destinationKey);//from   w w w. j a v  a 2s .c  o m
        resObj.setObjectContent(objectSource.getObjectContent());
        resObj.setObjectMetadata(objectSource.getObjectMetadata());
        resObj.setRedirectLocation(objectSource.getRedirectLocation());
        // copy perission
        AccessControlList permission = new AccessControlList();
        permission.setOwner(element.getPermission().getOwner());
        permission.grantAllPermissions(element.getPermission().getGrants().toArray(new Grant[0]));
        // maybe not exists key TODO
        objects.get(find(destinationBucketName))
                .add(new S3Element(resObj, permission, sourceKey.endsWith("/")));

        return new CopyObjectResult();
    }

    throw new AmazonServiceException("object source not found");
}