Skip to content

Commit

Permalink
Pass configured AWS S3 region to client
Browse files Browse the repository at this point in the history
  • Loading branch information
repolevedavaj committed Jun 24, 2022
1 parent 632dc90 commit 2c28820
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,14 @@ public boolean getParallelDownloads() {

@Override
public S3ObjectPath getObjectPath(Item item, String path) {
S3Profile profile = new S3Profile(lookupCredentials(), endpoint, signerVersion, pathStyleAccess, parallelDownloads);

return new S3ObjectPath(profile, bucketName, region, item.getFullName(), path);
return new S3ObjectPath(createS3Profile(), bucketName, region, item.getFullName(), path);
}

@Override
public S3ObjectPath getObjectPathForBranch(Item item, String path, String branch) {
S3Profile profile = new S3Profile(lookupCredentials(), endpoint, signerVersion, pathStyleAccess, parallelDownloads);

String branchPath = new File(item.getFullName()).getParent() + "/" + branch;

return new S3ObjectPath(profile, bucketName, region, branchPath, path);
return new S3ObjectPath(createS3Profile(), bucketName, region, branchPath, path);
}

private AmazonWebServicesCredentials lookupCredentials() {
Expand All @@ -143,6 +139,10 @@ private static List<AmazonWebServicesCredentials> possibleCredentials() {
ACL.SYSTEM, Collections.emptyList());
}

private S3Profile createS3Profile() {
return new S3Profile(lookupCredentials(), endpoint, region, signerVersion, pathStyleAccess, parallelDownloads);
}

@Extension(optional = true)
public static final class DescriptorImpl extends ItemStorageDescriptor<S3ObjectPath> {

Expand All @@ -158,7 +158,7 @@ public ListBoxModel doFillCredentialsIdItems(@QueryParameter String value) {
return new ListBoxModel();
}
return new StandardListBoxModel()
.withAll(possibleCredentials());
.withAll(possibleCredentials());
}

@SuppressWarnings("unused")
Expand All @@ -178,30 +178,25 @@ public static final class S3ItemListener extends ItemListener {
@Override
public void onDeleted(Item item) {
NonAWSS3ItemStorage s3Storage = lookupS3Storage();

if (s3Storage == null) {
return;
}

S3Profile profile = new S3Profile(s3Storage.lookupCredentials(), s3Storage.getEndpoint(), s3Storage.getSignerVersion(), s3Storage.getPathStyleAccess(), s3Storage.getParallelDownloads());
profile.delete(s3Storage.bucketName, item.getFullName());
s3Storage.createS3Profile().delete(s3Storage.bucketName, item.getFullName());
}

@Override
public void onLocationChanged(Item item, String oldFullName, String newFullName) {
NonAWSS3ItemStorage s3Storage = lookupS3Storage();

if (s3Storage == null) {
return;
}

S3Profile profile = new S3Profile(s3Storage.lookupCredentials(), s3Storage.getEndpoint(), s3Storage.getSignerVersion(), s3Storage.getPathStyleAccess(), s3Storage.getParallelDownloads());
profile.rename(s3Storage.bucketName, oldFullName, newFullName);
s3Storage.createS3Profile().rename(s3Storage.bucketName, oldFullName, newFullName);
}

private NonAWSS3ItemStorage lookupS3Storage() {
ItemStorage<?> storage = GlobalItemStorage.get().getStorage();

if (storage instanceof NonAWSS3ItemStorage) {
return (NonAWSS3ItemStorage) storage;
} else {
Expand Down
23 changes: 11 additions & 12 deletions src/main/java/jenkins/plugins/itemstorage/s3/S3ItemStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,14 @@ public String getCredentialsId() {

@Override
public S3ObjectPath getObjectPath(Item item, String path) {
S3Profile profile = new S3Profile(lookupCredentials(), null, null, false, true);

return new S3ObjectPath(profile, bucketName, region, item.getFullName(), path);
return new S3ObjectPath(createS3Profile(), bucketName, region, item.getFullName(), path);
}

@Override
public S3ObjectPath getObjectPathForBranch(Item item, String path, String branch) {
S3Profile profile = new S3Profile(lookupCredentials(), null, null, false, true);
String branchPath = new File(item.getFullName()).getParent() + "/" + branch;

return new S3ObjectPath(profile, bucketName, region, branchPath, path);
return new S3ObjectPath(createS3Profile(), bucketName, region, branchPath, path);
}

private AmazonWebServicesCredentials lookupCredentials() {
Expand All @@ -106,6 +103,10 @@ private static List<AmazonWebServicesCredentials> possibleCredentials() {
ACL.SYSTEM, Collections.emptyList());
}

private S3Profile createS3Profile() {
return new S3Profile(lookupCredentials(), null, region, null, false, true);
}

@Extension(optional = true)
public static final class DescriptorImpl extends ItemStorageDescriptor<S3ObjectPath> {

Expand Down Expand Up @@ -141,23 +142,21 @@ public static final class S3ItemListener extends ItemListener {
@Override
public void onDeleted(Item item) {
S3ItemStorage s3Storage = lookupS3Storage();
if (s3Storage == null) {
return;
}

if (s3Storage == null) return;

S3Profile profile = new S3Profile(s3Storage.lookupCredentials(), null, null, false, true);
profile.delete(s3Storage.bucketName, item.getFullName());
s3Storage.createS3Profile().delete(s3Storage.bucketName, item.getFullName());
}

@Override
public void onLocationChanged(Item item, String oldFullName, String newFullName) {
S3ItemStorage s3Storage = lookupS3Storage();

if (s3Storage == null) {
return;
}

S3Profile profile = new S3Profile(s3Storage.lookupCredentials(), null, null, false, true);
profile.rename(s3Storage.bucketName, oldFullName, newFullName);
s3Storage.createS3Profile().rename(s3Storage.bucketName, oldFullName, newFullName);
}

private S3ItemStorage lookupS3Storage() {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/jenkins/plugins/itemstorage/s3/S3Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

/**
* Based on same named class in S3 Jenkins Plugin
*
* <p>
* Reusable class for interacting with S3 for file operations
*
* @author Peter Hayes
Expand All @@ -51,8 +51,8 @@ public class S3Profile {
private final ClientHelper helper;

@DataBoundConstructor
public S3Profile(AmazonWebServicesCredentials credentials, String endpoint, String signerVersion, boolean pathStyleAccess, boolean parallelDownloads) {
this.helper = new ClientHelper(credentials != null ? credentials.getCredentials() : null, endpoint, null, getProxy(), signerVersion, pathStyleAccess, parallelDownloads);
public S3Profile(AmazonWebServicesCredentials credentials, String endpoint, String region, String signerVersion, boolean pathStyleAccess, boolean parallelDownloads) {
this.helper = new ClientHelper(credentials != null ? credentials.getCredentials() : null, endpoint, region, getProxy(), signerVersion, pathStyleAccess, parallelDownloads);
}

public void upload(String bucketName,
Expand Down

0 comments on commit 2c28820

Please sign in to comment.