Skip to content

Commit

Permalink
Merge branch 'release-2.1.0' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicWatson committed Aug 1, 2023
2 parents 12f070e + 45ce82d commit 3612a6c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Changelog

## 2.1.0

* [#13](https://github.com/pixl8/preside-ext-s3-storage-provider/issues/13) Add a getTemporaryPrivateObjectUrl() method for getting presigned URLs for private objects

## 2.0.0

* Rewrite java interactions to use an isolated OSGi bundle, disassociating the extension with specific Lucee libraries and allowing usage with multiple versions of Lucee.
* [#12](https://github.com/pixl8/preside-ext-s3-storage-provider/issues/12) Rewrite java interactions to use an isolated OSGi bundle, disassociating the extension with specific Lucee libraries and allowing usage with multiple versions of Lucee.

## 1.0.5

Expand Down
38 changes: 29 additions & 9 deletions java/src/main/java/org/pixl8/s3storageprovider/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import software.amazon.awssdk.auth.credentials.*;
import software.amazon.awssdk.services.s3.model.*;
import software.amazon.awssdk.services.s3.paginators.*;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
import software.amazon.awssdk.services.s3.presigner.model.*;
import software.amazon.awssdk.core.sync.RequestBody;

import java.nio.file.Paths;
import lucee.runtime.type.Query;
import lucee.runtime.type.Struct;
Expand All @@ -14,6 +17,7 @@
import java.util.Date;
import java.util.List;
import java.io.File;
import java.time.Duration;

/**
* This is a private service for our Lucee CFML Preside Storage
Expand All @@ -25,9 +29,10 @@
*/
public class Service {

private S3Client _s3Client;
private String _bucket;
private String _region;
private S3Client _s3Client;
private String _bucket;
private String _region;
private S3Presigner _preSigner;

/**
* Our simple service constructor takes region, bucket, accesskey and secret key.
Expand All @@ -36,12 +41,18 @@ public class Service {
*
*/
public Service( String region, String bucket, String accessKey, String secretKey ) {
_bucket = bucket;
_region = region;
_s3Client = S3Client.builder()
.region( Region.of( _region ) )
.credentialsProvider( StaticCredentialsProvider.create( AwsBasicCredentials.create( accessKey, secretKey ) ) )
.build();
AwsCredentialsProvider creds = StaticCredentialsProvider.create( AwsBasicCredentials.create( accessKey, secretKey ) );

_bucket = bucket;
_region = region;
_s3Client = S3Client.builder()
.region( Region.of( _region ) )
.credentialsProvider( creds )
.build();
_preSigner = S3Presigner.builder()
.credentialsProvider( creds )
.region( Region.of( _region ) )
.build();
}


Expand Down Expand Up @@ -184,6 +195,15 @@ public void moveObject( String sourceKey, String targetKey, String mimetype, Str
deleteObject( sourceKey );
}

public String getPresignedUrl( String objectKey, long durationInMinutes ) {
GetObjectPresignRequest req = GetObjectPresignRequest.builder()
.getObjectRequest( _buildGetObjectRequest( objectKey ) )
.signatureDuration( Duration.ofMinutes( durationInMinutes ) )
.build();

return _preSigner.presignGetObject( req ).url().toString();
}

// PRIVATE HELPERS
private GetObjectRequest _buildGetObjectRequest( String key ) {
return GetObjectRequest.builder().key( key ).bucket( _bucket ).build();
Expand Down
4 changes: 4 additions & 0 deletions services/S3StorageProvider.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ component implements="preside.system.services.fileStorage.StorageProvider" displ
return "";
}

public string function getTemporaryPrivateObjectUrl( required string path, numeric timeoutInMinutes=5 ) {
return _getS3Service().getPresignedUrl( _expandPath( argumentCollection=arguments, private=true ), JavaCast( "long", arguments.timeoutInMinutes ) );
}

// PRIVATE HELPERS
private void function _setupS3Service(
required string accessKey
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/S3StorageProviderTest.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,32 @@ component extends="testbox.system.BaseSpec" {
expect( Val( result.statuscode ) ).toBe( 200 );
} );
} );

describe( "getTemporaryPrivateObjectUrl", function(){
it( "should provide a temporary URL for downloading otherwise private access objects", function(){
var svc = _getService();
var prefix = CreateUUId();
var sourceFile = FileReadBinary( ExpandPath( "/tests/fixtures/test.png" ) );

svc.putObject( object=sourceFile, path="/#prefix#/test.png", private=true );
var objUrl = Replace( svc.getObjectUrl( "/#prefix#/test.png" ), "/public/", "/private/" );

expect( objUrl contains "amazonaws.com" ).toBeTrue();
http url=objUrl timeout=10 result="result";
expect( Val( result.statuscode ) ).toBe( 403 );
expect( result.filecontent contains "access denied" ).toBeTrue();

objUrl = svc.getTemporaryPrivateObjectUrl( "/#prefix#/test.png" );

expect( objUrl contains "amazonaws.com" ).toBeTrue();
http url=objUrl timeout=10 result="result";
debug( objUrl );
debug( result );
expect( Val( result.statuscode ) ).toBe( 200 );
expect( isBinary( result.filecontent ) ).toBeTrue();
expect( Len( result.filecontent ) ).toBe( 4255 );
} );
} );
}

// HELPERS
Expand Down

0 comments on commit 3612a6c

Please sign in to comment.