@@ -119,7 +119,7 @@ private void initialize() throws Exception {
119119 this .folder = folder ;
120120
121121 this .client = buildS3Client (accessKey , secretKey , endpointValue , clientRegion );
122- this .presigner = buildS3Presigner (accessKey , secretKey , clientRegion );
122+ this .presigner = buildS3Presigner (accessKey , secretKey , endpointValue , clientRegion );
123123 bucketExists ();
124124 }
125125 }
@@ -178,7 +178,7 @@ private S3Client buildS3Client(String accessKey, String secretKey, String endpoi
178178 return s3Client ;
179179 }
180180
181- private S3Presigner buildS3Presigner (String accessKey , String secretKey , String region ) {
181+ private S3Presigner buildS3Presigner (String accessKey , String secretKey , String endpoint , String region ) {
182182 boolean bUseIAM = !getPropertyValue (USE_IAM , "" , "" ).isEmpty () || (accessKey .equals ("" ) && secretKey .equals ("" ));
183183
184184 S3Presigner .Builder builder = S3Presigner .builder ()
@@ -193,6 +193,10 @@ private S3Presigner buildS3Presigner(String accessKey, String secretKey, String
193193 logger .debug ("Using IAM Credentials for presigner" );
194194 }
195195
196+ if (!endpoint .isEmpty () && !endpoint .contains (".amazonaws.com" )) {
197+ builder .endpointOverride (URI .create (endpoint ));
198+ }
199+
196200 return builder .build ();
197201 }
198202
@@ -568,15 +572,27 @@ else if (url.startsWith(this.getStorageUriWithoutRegion()))
568572 }
569573
570574 private String getStorageUri () {
571- return (!pathStyleUrls ) ?
572- "https://" + bucket + ".s3." + clientRegion + ".amazonaws.com/" :
573- ".s3." + clientRegion + ".amazonaws.com//" + bucket + "/" ;
575+ if (!pathStyleUrls ) {
576+ if (endpointUrl .contains (".amazonaws.com" )) {
577+ return "https://" + bucket + ".s3." + clientRegion + ".amazonaws.com/" ;
578+ } else {
579+ return endpointUrl + "/" + bucket + "/" ;
580+ }
581+ } else {
582+ return endpointUrl + "/" + bucket + "/" ;
583+ }
574584 }
575585
576586 private String getStorageUriWithoutRegion () {
577- return (!pathStyleUrls ) ?
578- "https://" + bucket + ".s3.amazonaws.com/" :
579- ".s3.amazonaws.com//" + bucket + "/" ;
587+ if (!pathStyleUrls ) {
588+ if (endpointUrl .contains (".amazonaws.com" )) {
589+ return "https://" + bucket + ".s3.amazonaws.com/" ;
590+ } else {
591+ return endpointUrl + "/" + bucket + "/" ;
592+ }
593+ } else {
594+ return endpointUrl + "/" + bucket + "/" ;
595+ }
580596 }
581597
582598 // With ACL implementation
@@ -650,19 +666,27 @@ private String getResourceUrlWithACL(String externalFileName, ResourceAccessCont
650666 } else {
651667 try {
652668 int lastIndex = Math .max (externalFileName .lastIndexOf ('/' ), externalFileName .lastIndexOf ('\\' ));
653- String path = externalFileName .substring (0 , lastIndex + 1 );
654- String fileName = externalFileName .substring (lastIndex + 1 );
669+ String path = lastIndex >= 0 ? externalFileName .substring (0 , lastIndex + 1 ) : "" ;
670+ String fileName = lastIndex >= 0 ? externalFileName .substring (lastIndex + 1 ) : externalFileName ;
655671 String encodedFileName = URLEncoder .encode (fileName , StandardCharsets .UTF_8 .toString ());
656672
657- String url = String .format (
658- "https://%s.s3.%s.amazonaws.com/%s%s" ,
659- bucket ,
660- clientRegion ,
661- path ,
662- encodedFileName
663- );
664-
665- return url ;
673+ if (endpointUrl .contains (".amazonaws.com" )) {
674+ return String .format (
675+ "https://%s.s3.%s.amazonaws.com/%s%s" ,
676+ bucket ,
677+ clientRegion ,
678+ path ,
679+ encodedFileName
680+ );
681+ } else {
682+ return String .format (
683+ "%s/%s/%s%s" ,
684+ endpointUrl ,
685+ bucket ,
686+ path ,
687+ encodedFileName
688+ );
689+ }
666690 } catch (UnsupportedEncodingException uee ) {
667691 logger .error ("Failed to encode resource URL for " + externalFileName , uee );
668692 return "" ;
@@ -771,19 +795,27 @@ private String getResourceUrlWithoutACL(String externalFileName, int expirationM
771795 } else if (ownerEnforcedBucketPrivacy == BucketPrivacy .PUBLIC ){
772796 try {
773797 int lastIndex = Math .max (externalFileName .lastIndexOf ('/' ), externalFileName .lastIndexOf ('\\' ));
774- String path = externalFileName .substring (0 , lastIndex + 1 );
775- String fileName = externalFileName .substring (lastIndex + 1 );
798+ String path = lastIndex >= 0 ? externalFileName .substring (0 , lastIndex + 1 ) : "" ;
799+ String fileName = lastIndex >= 0 ? externalFileName .substring (lastIndex + 1 ) : externalFileName ;
776800 String encodedFileName = URLEncoder .encode (fileName , StandardCharsets .UTF_8 .toString ());
777801
778- String url = String .format (
779- "https://%s.s3.%s.amazonaws.com/%s%s" ,
780- bucket ,
781- clientRegion ,
782- path ,
783- encodedFileName
784- );
785-
786- return url ;
802+ if (endpointUrl .contains (".amazonaws.com" )) {
803+ return String .format (
804+ "https://%s.s3.%s.amazonaws.com/%s%s" ,
805+ bucket ,
806+ clientRegion ,
807+ path ,
808+ encodedFileName
809+ );
810+ } else {
811+ return String .format (
812+ "%s/%s/%s%s" ,
813+ endpointUrl ,
814+ bucket ,
815+ path ,
816+ encodedFileName
817+ );
818+ }
787819 } catch (UnsupportedEncodingException uee ) {
788820 logger .error ("Failed to encode resource URL for {}" , externalFileName , uee );
789821 return "" ;
0 commit comments