Allow for uploading of directories
This commit is contained in:
parent
e98d94316c
commit
7d3c8c4923
14
.gitignore
vendored
Normal file
14
.gitignore
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
target
|
||||
test-output
|
||||
.idea
|
||||
.DS_Store
|
||||
*.iml
|
||||
*.ipr
|
||||
*.iws
|
||||
*.class
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
|
||||
50
readme.md
50
readme.md
@ -1,10 +1,46 @@
|
||||
s3-upload-maven-plugin
|
||||
======================
|
||||
Uploads a file to S3 from maven.
|
||||
Uploads a file or (recursively) the contents of a directory to S3.
|
||||
|
||||
Example
|
||||
=======
|
||||
Configuration parameters
|
||||
------------------------
|
||||
|
||||
| Parameter | Description | Required | Default |
|
||||
|-----------|-------------|----------|---------|
|
||||
|bucketName|The name of the bucket|*yes*| |
|
||||
|source|The source file or folder (was sourceFile before 1.2)|*yes*| |
|
||||
|destination|The destination file or destination folder (was destinationFile before 1.2)| *yes*| |
|
||||
|recursive|If this is a directory copy, recursively copy all contents (since 1.2)| *no* | false |
|
||||
|accessKey|S3 access key | *no* | if unspecified, uses the Default Provider, falling back to env variables |
|
||||
|secretKey|S3 secret key | *no* | if unspecified, uses the Default Provider, falling back to env variables |
|
||||
|doNotUpload|Dry run| *no* | false |
|
||||
|endpoint|Use a different s3 endpoint| *no* | s3.amazonaws.com |
|
||||
|
||||
Example: Upload a file
|
||||
----------------------
|
||||
```xml
|
||||
<build>
|
||||
...
|
||||
|
||||
<plugins>
|
||||
...
|
||||
|
||||
<plugin>
|
||||
<groupId>com.bazaarvoice.maven.plugins</groupId>
|
||||
<artifactId>s3-upload-maven-plugin</artifactId>
|
||||
<version>1.2</version>
|
||||
<configuration>
|
||||
<bucketName>my-s3-bucket</bucketName>
|
||||
<source>dir/filename.txt</source>
|
||||
<destination>remote-dir/remote-filename.txt</destination>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
```
|
||||
|
||||
Example: Recursively upload a folder
|
||||
------------------------------------
|
||||
```xml
|
||||
<build>
|
||||
...
|
||||
@ -18,10 +54,12 @@ Example
|
||||
<version>1.0</version>
|
||||
<configuration>
|
||||
<bucketName>my-s3-bucket</bucketName>
|
||||
<sourceFile>dir/filename.txt</sourceFile>
|
||||
<destinationFile>remote-dir/remote-filename.txt</destinationFile>
|
||||
<source>dir</source>
|
||||
<destination>remote-dir</destination>
|
||||
<recursive>true</recursive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
```
|
||||
```
|
||||
|
||||
|
||||
@ -7,8 +7,8 @@ import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
|
||||
import com.amazonaws.internal.StaticCredentialsProvider;
|
||||
import com.amazonaws.services.s3.AmazonS3;
|
||||
import com.amazonaws.services.s3.AmazonS3Client;
|
||||
import com.amazonaws.services.s3.transfer.Transfer;
|
||||
import com.amazonaws.services.s3.transfer.TransferManager;
|
||||
import com.amazonaws.services.s3.transfer.Upload;
|
||||
import org.apache.maven.plugin.AbstractMojo;
|
||||
import org.apache.maven.plugin.MojoExecutionException;
|
||||
import org.apache.maven.plugins.annotations.Mojo;
|
||||
@ -28,31 +28,35 @@ public class S3UploadMojo extends AbstractMojo
|
||||
private String secretKey;
|
||||
|
||||
/** Execute all steps up except the upload to the S3. This can be set to true to perform a "dryRun" execution. */
|
||||
@Parameter(property = "s3repo.doNotUpload", defaultValue = "false")
|
||||
@Parameter(property = "s3-upload.doNotUpload", defaultValue = "false")
|
||||
private boolean doNotUpload;
|
||||
|
||||
/** The file to upload. */
|
||||
@Parameter(property = "s3-upload.sourceFile", required = true)
|
||||
private String sourceFile;
|
||||
/** The file/folder to upload. */
|
||||
@Parameter(property = "s3-upload.source", required = true)
|
||||
private String source;
|
||||
|
||||
/** The bucket to upload into. */
|
||||
@Parameter(property = "s3-upload.bucketName", required = true)
|
||||
private String bucketName;
|
||||
|
||||
/** The file (in the bucket) to create. */
|
||||
@Parameter(property = "s3-upload.destinationFile", required = true)
|
||||
private String destinationFile;
|
||||
/** The file/folder (in the bucket) to create. */
|
||||
@Parameter(property = "s3-upload.destination", required = true)
|
||||
private String destination;
|
||||
|
||||
/** Force override of endpoint for S3 regions such as EU. */
|
||||
@Parameter(property = "s3-upload.endpoint")
|
||||
private String endpoint;
|
||||
|
||||
/** In the case of a directory upload, recursively upload the contents. */
|
||||
@Parameter(property = "s3-upload.recursive", defaultValue = "false")
|
||||
private boolean recursive;
|
||||
|
||||
@Override
|
||||
public void execute() throws MojoExecutionException
|
||||
{
|
||||
File source = new File(sourceFile);
|
||||
if (!source.exists()) {
|
||||
throw new MojoExecutionException("File doesn't exist: " + sourceFile);
|
||||
File sourceFile = new File(source);
|
||||
if (!sourceFile.exists()) {
|
||||
throw new MojoExecutionException("File/folder doesn't exist: " + source);
|
||||
}
|
||||
|
||||
AmazonS3 s3 = getS3Client(accessKey, secretKey);
|
||||
@ -64,12 +68,16 @@ public class S3UploadMojo extends AbstractMojo
|
||||
throw new MojoExecutionException("Bucket doesn't exist: " + bucketName);
|
||||
}
|
||||
|
||||
boolean success = upload(s3, bucketName, destinationFile, source);
|
||||
if (!success) {
|
||||
throw new MojoExecutionException("Unable to upload file to S3.");
|
||||
}
|
||||
if (doNotUpload) {
|
||||
getLog().info("File " + sourceFile + " would have be uploaded to s3://" + bucketName + "/" + destination + " (dry run)");
|
||||
} else {
|
||||
boolean success = upload(s3, sourceFile);
|
||||
if (!success) {
|
||||
throw new MojoExecutionException("Unable to upload file to S3.");
|
||||
}
|
||||
|
||||
getLog().info("File " + source + " uploaded to s3://" + bucketName + "/" + destinationFile);
|
||||
getLog().info("File " + sourceFile + " uploaded to s3://" + bucketName + "/" + destination);
|
||||
}
|
||||
}
|
||||
|
||||
private static AmazonS3 getS3Client(String accessKey, String secretKey)
|
||||
@ -85,13 +93,22 @@ public class S3UploadMojo extends AbstractMojo
|
||||
return new AmazonS3Client(provider);
|
||||
}
|
||||
|
||||
private static boolean upload(AmazonS3 s3, String bucketName, String destinationFile, File source)
|
||||
private boolean upload(AmazonS3 s3, File sourceFile) throws MojoExecutionException
|
||||
{
|
||||
TransferManager mgr = new TransferManager(s3);
|
||||
Upload upload = mgr.upload(bucketName, destinationFile, source);
|
||||
|
||||
Transfer transfer;
|
||||
if (sourceFile.isFile()) {
|
||||
transfer = mgr.upload(bucketName, destination, sourceFile);
|
||||
} else if (sourceFile.isDirectory()) {
|
||||
transfer = mgr.uploadDirectory(bucketName, destination, sourceFile, recursive);
|
||||
} else {
|
||||
throw new MojoExecutionException("File is neither a regular file nor a directory " + sourceFile);
|
||||
}
|
||||
try {
|
||||
upload.waitForUploadResult();
|
||||
getLog().debug("Transferring " + transfer.getProgress().getTotalBytesToTransfer() + " bytes...");
|
||||
transfer.waitForCompletion();
|
||||
getLog().info("Transferred " + transfer.getProgress().getBytesTransfered() + " bytes.");
|
||||
} catch (InterruptedException e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user