Skip to content

Commit

Permalink
Working logic using our custom java lib (local tests at least 2x as f…
Browse files Browse the repository at this point in the history
…ast)
  • Loading branch information
DominicWatson committed Jul 3, 2023
1 parent 2a49111 commit 83a0e71
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 281 deletions.
3 changes: 1 addition & 2 deletions i18n/storage-providers/s3.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ field.s3rootUrl.placeholder=e.g. https://s3.amazon.com, or https://s3-eu-west-2.
field.s3subpath.title=Root path
field.s3subpath.placeholder=i.e. subfolder within your bucker. e.g. /mysite/live/uploads

validation.connection.error=There was an error connecting to S3 using the credentials you supplied: {1}
validation.bucket.not.exists=The bucket, {1}, does not exist, or you do not have access to it. Error from S3: {2}
validation.connection.error=There was an error connecting to S3 using the credentials you supplied. Either the credentials are invalid/do not have access to the bucket, the bucket does not exist, or the bucket does not exist in the supplied region.
22 changes: 12 additions & 10 deletions java/src/main/java/org/pixl8/s3storageprovider/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public Service( String region, String bucket, String accessKey, String secretKey

/**
* Returns true if there are no errors making an API call to get the configured
* bucket's location AND if the bucket's location matches our configured region.
* bucket
*/
public boolean validateConnection() {
public boolean checkBucketAccess() {
try {
GetBucketLocationResponse resp = _s3Client.getBucketLocation( GetBucketLocationRequest.builder().bucket( _bucket ).build() );
return resp.locationConstraintAsString().equals( _region );
Expand Down Expand Up @@ -139,24 +139,24 @@ public void deleteObject( String key ) {
* Put an object into the bucket from a local file. This is the preferred performant method.
*
*/
public void putObject( String key, String localFilePath, String mimetype, boolean isPrivate, boolean isTrashed ) {
_s3Client.putObject( _buildPutObjectRequest( key, mimetype, isPrivate, isTrashed ), Paths.get( localFilePath ) );
public void putObject( String key, String localFilePath, String mimetype, String disposition, boolean isPrivate, boolean isTrashed ) {
_s3Client.putObject( _buildPutObjectRequest( key, mimetype, disposition, isPrivate, isTrashed ), Paths.get( localFilePath ) );
}

/**
* Put an object into the bucket from a binary byte array. This is not the preferred method.
*
*/
public void putObject( String key, byte[] bytes, String mimetype, boolean isPrivate, boolean isTrashed ) {
_s3Client.putObject( _buildPutObjectRequest( key, mimetype, isPrivate, isTrashed ), RequestBody.fromBytes( bytes ) );
public void putObject( String key, byte[] bytes, String mimetype, String disposition, boolean isPrivate, boolean isTrashed ) {
_s3Client.putObject( _buildPutObjectRequest( key, mimetype, disposition, isPrivate, isTrashed ), RequestBody.fromBytes( bytes ) );
}

/**
* Moves an object from one location to another
* with new permissions based on whether private and trashed
*/
public void moveObject( String sourceKey, String targetKey, String mimetype, boolean isPrivate, boolean isTrashed ) {
_s3Client.copyObject( _buildCopyObjectRequest( sourceKey, targetKey, mimetype, isPrivate, isTrashed ) );
public void moveObject( String sourceKey, String targetKey, String mimetype, String disposition, boolean isPrivate, boolean isTrashed ) {
_s3Client.copyObject( _buildCopyObjectRequest( sourceKey, targetKey, mimetype, disposition, isPrivate, isTrashed ) );
deleteObject( sourceKey );
}

Expand All @@ -165,23 +165,25 @@ private GetObjectRequest _buildGetObjectRequest( String key ) {
return GetObjectRequest.builder().key( key ).bucket( _bucket ).build();
}

private PutObjectRequest _buildPutObjectRequest( String key, String mimetype, boolean isPrivate, boolean isTrashed ) {
private PutObjectRequest _buildPutObjectRequest( String key, String mimetype, String disposition, boolean isPrivate, boolean isTrashed ) {
return PutObjectRequest.builder()
.bucket( _bucket )
.key( key )
.contentType( mimetype )
.contentDisposition( disposition )
.acl( ( isPrivate || isTrashed ) ? ObjectCannedACL.PRIVATE : ObjectCannedACL.PUBLIC_READ )
.storageClass( isTrashed ? StorageClass.REDUCED_REDUNDANCY : StorageClass.STANDARD )
.build();
}

private CopyObjectRequest _buildCopyObjectRequest( String sourceKey, String targetKey, String mimetype, boolean isPrivate, boolean isTrashed ) {
private CopyObjectRequest _buildCopyObjectRequest( String sourceKey, String targetKey, String mimetype, String disposition, boolean isPrivate, boolean isTrashed ) {
return CopyObjectRequest.builder()
.sourceBucket( _bucket )
.sourceKey( sourceKey )
.destinationBucket( _bucket )
.destinationKey( targetKey )
.contentType( mimetype )
.contentDisposition( disposition )
.acl( ( isPrivate || isTrashed ) ? ObjectCannedACL.PRIVATE : ObjectCannedACL.PUBLIC_READ )
.storageClass( isTrashed ? StorageClass.REDUCED_REDUNDANCY : StorageClass.STANDARD )
.build();
Expand Down

0 comments on commit 83a0e71

Please sign in to comment.