Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
move S3 objects with ACLs (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr authored Apr 25, 2019
1 parent d1fcfb8 commit 76fae63
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=2.2.14
version=2.2.15
awsJavaSdkVersion=1.11.500
awsKinesisClientVersion=1.8.5
gradleWrapperVersion=3.4.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,22 @@ class AmazonS3Service implements InitializingBean {
) {
try {
CopyObjectRequest request = new CopyObjectRequest(sourceBucketName, sourceKey, destinationBucketName, destinationKey)

S3Object object = client.getObject(sourceBucketName, sourceKey)

if (object.taggingCount) {
GetObjectTaggingRequest taggingRequest = new GetObjectTaggingRequest(sourceBucketName, sourceKey)
GetObjectTaggingResult taggingResult = client.getObjectTagging(taggingRequest)
request.withNewObjectTagging(new ObjectTagging(taggingResult.tagSet))
}

request.withNewObjectMetadata(object.objectMetadata)

AccessControlList acl = client.getObjectAcl(sourceBucketName, sourceKey)
AccessControlList newAcls = new AccessControlList()
newAcls.grantAllPermissions(*acl.grantsAsList)
request.withAccessControlList(newAcls)

client.copyObject(request)
client.deleteObject(sourceBucketName, sourceKey)
return client.getUrl(destinationBucketName, destinationKey)
Expand All @@ -487,7 +503,10 @@ class AmazonS3Service implements InitializingBean {
if (uri.host) {
// direct bucket URI not using any CNAME
if (uri.host.endsWith('amazonaws.com')) {
return path.substring(0, path.indexOf('/'))
if (uri.host.startsWith('s3')) {
return path.substring(0, path.indexOf('/'))
}
return uri.host.substring(0, uri.host.indexOf('.s3.'))
}
return uri.host
}
Expand All @@ -500,7 +519,7 @@ class AmazonS3Service implements InitializingBean {
String path = uri.path.startsWith('/') ? uri.path.substring(1, uri.path.length()) : uri.path
if (uri.host) {
// direct bucket URI not using any CNAME
if (uri.host.endsWith('amazonaws.com')) {
if (uri.host.endsWith('amazonaws.com') && uri.host.startsWith('s3')) {
return path.substring(path.indexOf('/') + 1, path.length())
}
return path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package grails.plugin.awssdk.s3

import com.amazonaws.AmazonClientException
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model.AccessControlList
import com.amazonaws.services.s3.model.AmazonS3Exception
import com.amazonaws.services.s3.model.CopyObjectRequest
import com.amazonaws.services.s3.model.CopyObjectResult
import com.amazonaws.services.s3.model.GetObjectTaggingResult
import com.amazonaws.services.s3.model.ObjectListing
import com.amazonaws.services.s3.model.ObjectMetadata
import com.amazonaws.services.s3.model.ObjectTagging
import com.amazonaws.services.s3.model.S3Object
import com.amazonaws.services.s3.model.S3ObjectSummary
import com.amazonaws.services.s3.model.Tag
import grails.testing.services.ServiceUnitTest
import spock.lang.Specification
import spock.lang.Unroll
Expand Down Expand Up @@ -308,19 +312,23 @@ class AmazonS3ServiceSpec extends Specification implements ServiceUnitTest<Amazo
} as CopyObjectRequest) >> new CopyObjectResult()
1 * service.client.deleteObject(BUCKET_NAME, 'uploads/key')
1 * service.client.getUrl(BUCKET_NAME.reverse(), 'files/key') >> new URL("https://s3-eu-west-1.amazonaws.com/${BUCKET_NAME}/files/key")
1 * service.client.getObject(BUCKET_NAME, 'uploads/key') >> new S3Object(taggingCount: 1)
1 * service.client.getObjectAcl(BUCKET_NAME, 'uploads/key') >> new AccessControlList()
1 * service.client.getObjectTagging(_) >> new GetObjectTaggingResult([new Tag('key', 'value')])
}

@Unroll
void 'extract bucket name #bucket and key #key from uri #uri'() {
void 'extract bucket name from uri #uri'() {
expect:
AmazonS3Service.getBucketFromUri(uri) == bucket
AmazonS3Service.getKeyFromUri(uri) == key
AmazonS3Service.getBucketFromUri(uri) == 'publishing.agorapulse.com'
AmazonS3Service.getKeyFromUri(uri) == 'publishingItemMedia/109098/f02f2a8c-1b80-50cb-f9ef-2d7850ed0525.png'
where:
uri | bucket | key
'https://s3.eu-west-1.amazonaws.com/publishing.agorapulse.com/publishingItemMedia/109098/f02f2a8c-1b80-50cb-f9ef-2d7850ed0525.png' | 'publishing.agorapulse.com' | 'publishingItemMedia/109098/f02f2a8c-1b80-50cb-f9ef-2d7850ed0525.png'
'publishing.agorapulse.com/publishingItemMedia/109098/f02f2a8c-1b80-50cb-f9ef-2d7850ed0525.png' | 'publishing.agorapulse.com' | 'publishingItemMedia/109098/f02f2a8c-1b80-50cb-f9ef-2d7850ed0525.png'
'https://publishing.agorapulse.com/publishingItemMedia/109098/f02f2a8c-1b80-50cb-f9ef-2d7850ed0525.png' | 'publishing.agorapulse.com' | 'publishingItemMedia/109098/f02f2a8c-1b80-50cb-f9ef-2d7850ed0525.png'

uri << [
'https://s3.eu-west-1.amazonaws.com/publishing.agorapulse.com/publishingItemMedia/109098/f02f2a8c-1b80-50cb-f9ef-2d7850ed0525.png',
'publishing.agorapulse.com/publishingItemMedia/109098/f02f2a8c-1b80-50cb-f9ef-2d7850ed0525.png',
'https://publishing.agorapulse.com/publishingItemMedia/109098/f02f2a8c-1b80-50cb-f9ef-2d7850ed0525.png',
'http://publishing.agorapulse.com.s3.eu-west-1.amazonaws.com/publishingItemMedia/109098/f02f2a8c-1b80-50cb-f9ef-2d7850ed0525.png',
]
}

}

0 comments on commit 76fae63

Please sign in to comment.