Search in sources :

Example 21 with AccessControlList

use of com.amazonaws.services.s3.model.AccessControlList in project spring-integration-aws by spring-projects.

the class S3MessageHandler method upload.

private Transfer upload(Message<?> requestMessage) {
    Object payload = requestMessage.getPayload();
    String bucketName = obtainBucket(requestMessage);
    String key = null;
    if (this.keyExpression != null) {
        key = this.keyExpression.getValue(this.evaluationContext, requestMessage, String.class);
    }
    if (payload instanceof File && ((File) payload).isDirectory()) {
        File fileToUpload = (File) payload;
        if (key == null) {
            key = fileToUpload.getName();
        }
        return this.transferManager.uploadDirectory(bucketName, key, fileToUpload, true, new MessageHeadersObjectMetadataProvider(requestMessage.getHeaders()));
    } else {
        ObjectMetadata metadata = new ObjectMetadata();
        if (this.uploadMetadataProvider != null) {
            this.uploadMetadataProvider.populateMetadata(metadata, requestMessage);
        }
        PutObjectRequest putObjectRequest = null;
        try {
            if (payload instanceof InputStream) {
                InputStream inputStream = (InputStream) payload;
                if (metadata.getContentMD5() == null) {
                    Assert.state(inputStream.markSupported(), "For an upload InputStream with no MD5 digest metadata, the " + "markSupported() method must evaluate to true. ");
                    String contentMd5 = Md5Utils.md5AsBase64(inputStream);
                    metadata.setContentMD5(contentMd5);
                    inputStream.reset();
                }
                putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, metadata);
            } else if (payload instanceof File) {
                File fileToUpload = (File) payload;
                if (key == null) {
                    key = fileToUpload.getName();
                }
                if (metadata.getContentMD5() == null) {
                    String contentMd5 = Md5Utils.md5AsBase64(fileToUpload);
                    metadata.setContentMD5(contentMd5);
                }
                if (metadata.getContentLength() == 0) {
                    metadata.setContentLength(fileToUpload.length());
                }
                if (metadata.getContentType() == null) {
                    metadata.setContentType(Mimetypes.getInstance().getMimetype(fileToUpload));
                }
                putObjectRequest = new PutObjectRequest(bucketName, key, fileToUpload).withMetadata(metadata);
            } else if (payload instanceof byte[]) {
                byte[] payloadBytes = (byte[]) payload;
                InputStream inputStream = new ByteArrayInputStream(payloadBytes);
                if (metadata.getContentMD5() == null) {
                    String contentMd5 = Md5Utils.md5AsBase64(inputStream);
                    metadata.setContentMD5(contentMd5);
                    inputStream.reset();
                }
                if (metadata.getContentLength() == 0) {
                    metadata.setContentLength(payloadBytes.length);
                }
                putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, metadata);
            } else {
                throw new IllegalArgumentException("Unsupported payload type: [" + payload.getClass() + "]. The only supported payloads for the upload request are " + "java.io.File, java.io.InputStream, byte[] and PutObjectRequest.");
            }
        } catch (IOException e) {
            throw new MessageHandlingException(requestMessage, e);
        }
        if (key == null) {
            if (this.keyExpression != null) {
                throw new IllegalStateException("The 'keyExpression' [" + this.keyExpression.getExpressionString() + "] must not evaluate to null. Root object is: " + requestMessage);
            } else {
                throw new IllegalStateException("Specify a 'keyExpression' for non-java.io.File payloads");
            }
        }
        S3ProgressListener progressListener = this.s3ProgressListener;
        if (this.objectAclExpression != null) {
            Object acl = this.objectAclExpression.getValue(this.evaluationContext, requestMessage);
            Assert.state(acl instanceof AccessControlList || acl instanceof CannedAccessControlList, "The 'objectAclExpression' [" + this.objectAclExpression.getExpressionString() + "] must evaluate to com.amazonaws.services.s3.model.AccessControlList " + "or must evaluate to com.amazonaws.services.s3.model.CannedAccessControlList. " + "Gotten: [" + acl + "]");
            SetObjectAclRequest aclRequest;
            if (acl instanceof AccessControlList) {
                aclRequest = new SetObjectAclRequest(bucketName, key, (AccessControlList) acl);
            } else {
                aclRequest = new SetObjectAclRequest(bucketName, key, (CannedAccessControlList) acl);
            }
            final SetObjectAclRequest theAclRequest = aclRequest;
            progressListener = new S3ProgressListener() {

                @Override
                public void onPersistableTransfer(PersistableTransfer persistableTransfer) {
                }

                @Override
                public void progressChanged(ProgressEvent progressEvent) {
                    if (ProgressEventType.TRANSFER_COMPLETED_EVENT.equals(progressEvent.getEventType())) {
                        S3MessageHandler.this.transferManager.getAmazonS3Client().setObjectAcl(theAclRequest);
                    }
                }
            };
            if (this.s3ProgressListener != null) {
                progressListener = new S3ProgressListenerChain(this.s3ProgressListener, progressListener);
            }
        }
        if (progressListener != null) {
            return this.transferManager.upload(putObjectRequest, progressListener);
        } else {
            return this.transferManager.upload(putObjectRequest);
        }
    }
}
Also used : CannedAccessControlList(com.amazonaws.services.s3.model.CannedAccessControlList) AccessControlList(com.amazonaws.services.s3.model.AccessControlList) SetObjectAclRequest(com.amazonaws.services.s3.model.SetObjectAclRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) CannedAccessControlList(com.amazonaws.services.s3.model.CannedAccessControlList) ProgressEvent(com.amazonaws.event.ProgressEvent) MessageHandlingException(org.springframework.messaging.MessageHandlingException) S3ProgressListenerChain(com.amazonaws.services.s3.transfer.internal.S3ProgressListenerChain) ByteArrayInputStream(java.io.ByteArrayInputStream) S3ProgressListener(com.amazonaws.services.s3.transfer.internal.S3ProgressListener) PersistableTransfer(com.amazonaws.services.s3.transfer.PersistableTransfer) File(java.io.File) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 22 with AccessControlList

use of com.amazonaws.services.s3.model.AccessControlList in project alluxio by Alluxio.

the class S3AUnderFileSystem method getPermissionsInternal.

/**
 * Since there is no group in S3 acl, the owner is reused as the group. This method calls the
 * S3 API and requires additional permissions aside from just read only. This method is best
 * effort and will continue with default permissions (no owner, no group, 0700).
 *
 * @return the permissions associated with this under storage system
 */
private ObjectPermissions getPermissionsInternal() {
    short bucketMode = ModeUtils.getUMask(mUfsConf.getString(PropertyKey.UNDERFS_S3_DEFAULT_MODE)).toShort();
    String accountOwner = DEFAULT_OWNER;
    // if ACL enabled try to inherit bucket acl for all the objects.
    if (Boolean.parseBoolean(mUfsConf.getString(PropertyKey.UNDERFS_S3_INHERIT_ACL))) {
        try {
            Owner owner = mClient.getS3AccountOwner();
            AccessControlList acl = mClient.getBucketAcl(mBucketName);
            bucketMode = S3AUtils.translateBucketAcl(acl, owner.getId());
            if (mUfsConf.isSet(PropertyKey.UNDERFS_S3_OWNER_ID_TO_USERNAME_MAPPING)) {
                // Here accountOwner can be null if there is no mapping set for this owner id
                accountOwner = CommonUtils.getValueFromStaticMapping(mUfsConf.getString(PropertyKey.UNDERFS_S3_OWNER_ID_TO_USERNAME_MAPPING), owner.getId());
            }
            if (accountOwner == null || accountOwner.equals(DEFAULT_OWNER)) {
                // If there is no user-defined mapping, use display name or id.
                accountOwner = owner.getDisplayName() != null ? owner.getDisplayName() : owner.getId();
            }
        } catch (AmazonClientException e) {
            LOG.warn("Failed to inherit bucket ACLs, proceeding with defaults. {}", e.toString());
        }
    }
    return new ObjectPermissions(accountOwner, accountOwner, bucketMode);
}
Also used : AccessControlList(com.amazonaws.services.s3.model.AccessControlList) Owner(com.amazonaws.services.s3.model.Owner) AmazonClientException(com.amazonaws.AmazonClientException)

Example 23 with AccessControlList

use of com.amazonaws.services.s3.model.AccessControlList in project alluxio by Alluxio.

the class S3AUnderFileSystemTest method getPermissionsCached.

@Test
public void getPermissionsCached() throws Exception {
    Mockito.when(mClient.getS3AccountOwner()).thenReturn(new Owner("0", "test"));
    Mockito.when(mClient.getBucketAcl(Mockito.anyString())).thenReturn(new AccessControlList());
    mS3UnderFileSystem.getPermissions();
    mS3UnderFileSystem.getPermissions();
    Mockito.verify(mClient).getS3AccountOwner();
    Mockito.verify(mClient).getBucketAcl(Mockito.anyString());
}
Also used : AccessControlList(com.amazonaws.services.s3.model.AccessControlList) Owner(com.amazonaws.services.s3.model.Owner) Test(org.junit.Test)

Example 24 with AccessControlList

use of com.amazonaws.services.s3.model.AccessControlList in project alluxio by Alluxio.

the class S3AUnderFileSystemTest method getPermissionsWithMapping.

@Test
public void getPermissionsWithMapping() throws Exception {
    Map<PropertyKey, Object> conf = new HashMap<>();
    conf.put(PropertyKey.UNDERFS_S3_OWNER_ID_TO_USERNAME_MAPPING, "111=altname");
    try (Closeable c = new ConfigurationRule(conf, sConf).toResource()) {
        UnderFileSystemConfiguration ufsConf = UnderFileSystemConfiguration.defaults(sConf);
        mS3UnderFileSystem = new S3AUnderFileSystem(new AlluxioURI("s3a://" + BUCKET_NAME), mClient, BUCKET_NAME, mExecutor, mManager, UnderFileSystemConfiguration.defaults(sConf), false);
    }
    Mockito.when(mClient.getS3AccountOwner()).thenReturn(new Owner("111", "test"));
    Mockito.when(mClient.getBucketAcl(Mockito.anyString())).thenReturn(new AccessControlList());
    ObjectUnderFileSystem.ObjectPermissions permissions = mS3UnderFileSystem.getPermissions();
    Assert.assertEquals("altname", permissions.getOwner());
    Assert.assertEquals("altname", permissions.getGroup());
    Assert.assertEquals(0, permissions.getMode());
}
Also used : AccessControlList(com.amazonaws.services.s3.model.AccessControlList) Owner(com.amazonaws.services.s3.model.Owner) HashMap(java.util.HashMap) UnderFileSystemConfiguration(alluxio.underfs.UnderFileSystemConfiguration) Closeable(java.io.Closeable) ObjectUnderFileSystem(alluxio.underfs.ObjectUnderFileSystem) ConfigurationRule(alluxio.ConfigurationRule) PropertyKey(alluxio.conf.PropertyKey) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 25 with AccessControlList

use of com.amazonaws.services.s3.model.AccessControlList in project alluxio by Alluxio.

the class S3AUnderFileSystemTest method getPermissionsNoMapping.

@Test
public void getPermissionsNoMapping() throws Exception {
    Map<PropertyKey, Object> conf = new HashMap<>();
    conf.put(PropertyKey.UNDERFS_S3_OWNER_ID_TO_USERNAME_MAPPING, "111=userid");
    try (Closeable c = new ConfigurationRule(conf, sConf).toResource()) {
        UnderFileSystemConfiguration ufsConf = UnderFileSystemConfiguration.defaults(sConf);
        mS3UnderFileSystem = new S3AUnderFileSystem(new AlluxioURI("s3a://" + BUCKET_NAME), mClient, BUCKET_NAME, mExecutor, mManager, UnderFileSystemConfiguration.defaults(sConf), false);
    }
    Mockito.when(mClient.getS3AccountOwner()).thenReturn(new Owner("0", "test"));
    Mockito.when(mClient.getBucketAcl(Mockito.anyString())).thenReturn(new AccessControlList());
    ObjectUnderFileSystem.ObjectPermissions permissions = mS3UnderFileSystem.getPermissions();
    Assert.assertEquals("test", permissions.getOwner());
    Assert.assertEquals("test", permissions.getGroup());
    Assert.assertEquals(0, permissions.getMode());
}
Also used : AccessControlList(com.amazonaws.services.s3.model.AccessControlList) Owner(com.amazonaws.services.s3.model.Owner) HashMap(java.util.HashMap) UnderFileSystemConfiguration(alluxio.underfs.UnderFileSystemConfiguration) Closeable(java.io.Closeable) ObjectUnderFileSystem(alluxio.underfs.ObjectUnderFileSystem) ConfigurationRule(alluxio.ConfigurationRule) PropertyKey(alluxio.conf.PropertyKey) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Aggregations

AccessControlList (com.amazonaws.services.s3.model.AccessControlList)25 Owner (com.amazonaws.services.s3.model.Owner)8 AmazonServiceException (com.amazonaws.AmazonServiceException)6 AmazonS3 (com.amazonaws.services.s3.AmazonS3)6 CannedAccessControlList (com.amazonaws.services.s3.model.CannedAccessControlList)5 CanonicalGrantee (com.amazonaws.services.s3.model.CanonicalGrantee)5 Grant (com.amazonaws.services.s3.model.Grant)5 File (java.io.File)5 Test (org.junit.Test)5 EmailAddressGrantee (com.amazonaws.services.s3.model.EmailAddressGrantee)4 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)4 PutObjectResult (com.amazonaws.services.s3.model.PutObjectResult)4 Permission (com.amazonaws.services.s3.model.Permission)3 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)3 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 AlluxioURI (alluxio.AlluxioURI)2 ConfigurationRule (alluxio.ConfigurationRule)2 PropertyKey (alluxio.conf.PropertyKey)2 ObjectUnderFileSystem (alluxio.underfs.ObjectUnderFileSystem)2