Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "noFilename" option: Destination bucket will be used as final key, ignoring the original filename #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/main/java/hudson/plugins/s3/Destination.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ public class Destination implements Serializable {

public Destination(final String userBucketName, final String fileName) {

if (userBucketName == null || fileName == null)
throw new IllegalArgumentException("Not defined for null parameters: "+userBucketName+","+fileName);
if (userBucketName == null)
throw new IllegalArgumentException("Not defined for null parameters: "+userBucketName);

final String[] bucketNameArray = userBucketName.split("/", 2);

bucketName = bucketNameArray[0];

if (bucketNameArray.length > 1) {

if (fileName == null) {
objectName = bucketNameArray[1];
} else if (bucketNameArray.length > 1) {
objectName = bucketNameArray[1] + "/" + fileName;
} else {
objectName = fileName;
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/hudson/plugins/s3/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ public final class Entry implements Describable<Entry> {
*/
public boolean flatten;

/**
* Never use filename, use destination bucket as full key
*/
public boolean noFilename;

@DataBoundConstructor
public Entry(String bucket, String sourceFile, String storageClass, String selectedRegion,
boolean noUploadOnFailure, boolean uploadFromSlave, boolean managedArtifacts,
boolean useServerSideEncryption, boolean flatten) {
boolean useServerSideEncryption, boolean flatten, boolean noFilename) {
this.bucket = bucket;
this.sourceFile = sourceFile;
this.storageClass = storageClass;
Expand All @@ -73,6 +78,7 @@ public Entry(String bucket, String sourceFile, String storageClass, String selec
this.managedArtifacts = managedArtifacts;
this.useServerSideEncryption = useServerSideEncryption;
this.flatten = flatten;
this.noFilename = noFilename;
}

public Descriptor<Entry> getDescriptor() {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/hudson/plugins/s3/S3BucketPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ public boolean perform(AbstractBuild<?, ?> build,

for (FilePath src : paths) {
log(listener.getLogger(), "bucket=" + bucket + ", file=" + src.getName() + " region=" + selRegion + ", upload from slave=" + entry.uploadFromSlave + " managed="+ entry.managedArtifacts + " , server encryption "+entry.useServerSideEncryption);
records.add(profile.upload(build, listener, bucket, src, searchPathLength, escapedUserMetadata, storageClass, selRegion, entry.uploadFromSlave, entry.managedArtifacts, entry.useServerSideEncryption, entry.flatten));
records.add(profile.upload(build, listener, bucket, src, searchPathLength, escapedUserMetadata, storageClass, selRegion, entry.uploadFromSlave, entry.managedArtifacts, entry.useServerSideEncryption, entry.flatten, entry.noFilename));
if (entry.noFilename)
break;
}
if (entry.managedArtifacts) {
artifacts.addAll(records);
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/hudson/plugins/s3/S3Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,19 @@ public void check() throws Exception {
}

public FingerprintRecord upload(AbstractBuild<?,?> build, final BuildListener listener, String bucketName, FilePath filePath, int searchPathLength, List<MetadataPair> userMetadata,
String storageClass, String selregion, boolean uploadFromSlave, boolean managedArtifacts,boolean useServerSideEncryption, boolean flatten) throws IOException, InterruptedException {
String storageClass, String selregion, boolean uploadFromSlave, boolean managedArtifacts,boolean useServerSideEncryption, boolean flatten, boolean noFilename) throws IOException, InterruptedException {
if (filePath.isDirectory()) {
throw new IOException(filePath + " is a directory");
}

String fileName = null;
if (flatten) {
fileName = filePath.getName();
} else {
String relativeFileName = filePath.getRemote();
fileName = relativeFileName.substring(searchPathLength);
if (!noFilename) {
if (flatten) {
fileName = filePath.getName();
} else {
String relativeFileName = filePath.getRemote();
fileName = relativeFileName.substring(searchPathLength);
}
}

Destination dest = new Destination(bucketName, fileName);
Expand Down Expand Up @@ -207,7 +209,7 @@ public FingerprintRecord upload(AbstractBuild<?,?> build, final BuildListener li
}

public List<String> list(Run build, String bucket, String expandedFilter) {
AmazonS3Client s3client = getClient();
AmazonS3Client s3client = getClient();

String buildName = build.getDisplayName();
int buildID = build.getNumber();
Expand All @@ -218,7 +220,7 @@ public List<String> list(Run build, String bucket, String expandedFilter) {
.withPrefix(dest.objectName);

List<String> files = Lists.newArrayList();

ObjectListing objectListing;
do {
objectListing = s3client.listObjects(listObjectsRequest);
Expand All @@ -227,7 +229,7 @@ public List<String> list(Run build, String bucket, String expandedFilter) {
files.add(req.getKey());
}
listObjectsRequest.setMarker(objectListing.getNextMarker());
} while (objectListing.isTruncated());
} while (objectListing.isTruncated());
return files;
}

Expand All @@ -238,7 +240,7 @@ public List<FingerprintRecord> downloadAll(Run build, List<FingerprintRecord> ar

FilenameSelector selector = new FilenameSelector();
selector.setName(expandedFilter);

List<FingerprintRecord> fingerprints = Lists.newArrayList();
for(FingerprintRecord record : artifacts) {
S3Artifact artifact = record.artifact;
Expand Down Expand Up @@ -284,7 +286,7 @@ public String getDownloadURL(Run build, FingerprintRecord record) {
ResponseHeaderOverrides headers = new ResponseHeaderOverrides();
// let the browser use the last part of the name, not the full path
// when saving.
String fileName = (new File(dest.objectName)).getName().trim();
String fileName = (new File(dest.objectName)).getName().trim();
headers.setContentDisposition("attachment; filename=\"" + fileName + "\"");
request.setResponseHeaders(headers);
URL url = getClient().generatePresignedUrl(request);
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/hudson/plugins/s3/Entry/config.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@
<f:entry field="flatten" title="Flatten directories">
<f:checkbox />
</f:entry>
<f:entry field="noFilename" title="No filename">
<f:checkbox />
</f:entry>

</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
When enabled, Jenkins will ignore the source filename when uploading. So the
Destination bucket will be used as final key for the uploaded file.
Please note: only one source file must be selected (otherwise, only the first
file will be uploaded and others will be ignored)
</div>