Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-40654] @Jimilian’s recommendations
* Document `classpath://com/amazonaws/partitions/override/endpoints.json` and `hudson.plugins.s3.DEFAULT_AMAZON_S3_REGION` in README.md
* Use `@nonnull` and `@nullable` in helper methods (not in the whole ClientHelper class to not change too many things at once)
* Cleanup imports
  • Loading branch information
cyrille-leclerc committed Dec 23, 2016
1 parent ff7d349 commit 2d1b795
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -25,6 +25,23 @@ and a post-build action called `Publish Artifacts to S3 Bucket`.
For Pipeline users, the same two actions are available via the
`step` step. You can use the snippet generator to get started.

When using an Amazon S3 compatible storage system (OpenStack Swift, EMC Atmos...),
the list of AWS regions can be overridden specifying a file
`classpath://com/amazonaws/partitions/override/endpoints.json` matching the format
defined in AWS SDK's [endpoints.json](https://github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-core/src/main/resources/com/amazonaws/partitions/endpoints.json).

A solution to add this `endpoints.json` file in the classpath of Jenkins is to use the
`java` command line parameter `-Xbootclasspath/a:/path/to/boot/classpath/folder/` and
to locate `com/amazonaws/partitions/override/endpoints.json` in `/path/to/boot/classpath/folder/`.


Even if most of the features of the Jenkins S3 Plugin require the user to specify the target region,
some feature rely on a default Amazon S3 region which is by default the "US Standard Amazon S3 Region"
and its endpoint `s3.amazonaws.com`. This default region can be overridden with the system property
`hudson.plugins.s3.DEFAULT_AMAZON_S3_REGION`.
Note that this default region name MUST match with a region define in the AWS SDK configuration file `endpoints.json`
(see above).

Notes
=====

Expand Down
23 changes: 20 additions & 3 deletions src/main/java/hudson/plugins/s3/ClientHelper.java
Expand Up @@ -11,6 +11,9 @@

import java.util.regex.Pattern;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class ClientHelper {

public static String DEFAULT_AMAZON_S3_REGION_NAME = System.getProperty(
Expand Down Expand Up @@ -44,13 +47,22 @@ public static AmazonS3Client createClient(String accessKey, String secretKey, bo
}

/**
* Gets the {@link Region} from its name with backward compatibility concerns and defaulting
*
* @param regionName nullable region name
* @return AWS region, never {@code null}, defaults to {@link com.amazonaws.services.s3.model.Region#US_Standard}
*/
private static Region getRegionFromString(String regionName) {
@Nonnull
private static Region getRegionFromString(@Nullable String regionName) {
Region region = null;

if (regionName == null || regionName.isEmpty()) {
region = RegionUtils.getRegion(DEFAULT_AMAZON_S3_REGION_NAME);
}
// In 0.7, selregion comes from Regions#name
Region region = RegionUtils.getRegion(regionName);
if (region == null) {
region = RegionUtils.getRegion(regionName);
}

// In 0.6, selregion comes from Regions#valueOf
if (region == null) {
Expand All @@ -60,10 +72,15 @@ private static Region getRegionFromString(String regionName) {
if (region == null) {
region = RegionUtils.getRegion(DEFAULT_AMAZON_S3_REGION_NAME);
}

if (region == null) {
throw new IllegalStateException("No AWS Region found for name '" + regionName + "' and default region '" + DEFAULT_AMAZON_S3_REGION_NAME + "'");
}
return region;
}

public static ClientConfiguration getClientConfiguration(ProxyConfiguration proxy, Region region) {
@Nonnull
public static ClientConfiguration getClientConfiguration(@Nonnull ProxyConfiguration proxy, @Nonnull Region region) {
final ClientConfiguration clientConfiguration = new ClientConfiguration();

String s3Endpoint = region.getServiceEndpoint(AmazonS3.ENDPOINT_PREFIX);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/hudson/plugins/s3/Entry.java
Expand Up @@ -2,7 +2,6 @@

import com.amazonaws.regions.Region;
import com.amazonaws.regions.RegionUtils;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import hudson.Extension;
import hudson.model.Describable;
Expand Down

0 comments on commit 2d1b795

Please sign in to comment.