DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • AWS CDK: Infrastructure as Abstract Data Types, Part 2
  • How to Upload/Download a File To and From the Server
  • Google Cloud KMS vs. AWS KMS: Java Encryption Guide
  • Two Engines, One Query: DuckDB Inside Neo4j

Trending

  • Why Your AI Agent Works in the Demo and Fails in Production
  • The Twelve-Factor Agents: Building Production-Ready LLM Applications
  • Vision-Language Models in 2026: From Research Breakthroughs to Cloud-Native Production Systems
  • The Window Closes: Why I Switched to Kotlin Before the Codebase Got Too Big

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Upload and Download Files to S3 Using Maven

Upload and Download Files to S3 Using Maven

Now that the Maven plugin for Amazon S3 has become more sophisticated, here's what you can do with it.

By 
Emmanouil Gkatziouras
DZone Core CORE ·
Feb. 05, 19 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
16.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Throughout the years I’ve seen many teams using Maven in many different ways. Maven can be used for many CI/CD tasks instead of using extra pipeline code, or it can be used to prepare the development environment before running some tests.

Generally, it is a convenient tool, widely used among Java teams, and will continue to do so since there is a huge ecosystem around it.

The CloudStorage Maven plugin helps you with using various cloud buckets as a private Maven repository. Recently, CloudStorageMaven for S3 got a huge upgrade, and you can use it in order to download or upload files from S3, by using it as a plugin.

The plugin assumes that your environment is configured properly to access the S3 resources needed.
This can be achieved individually through  aws configure :

aws configure

Other ways are through environmental variables or by using the appropriate iam role.

Suppose you want to download some certain files from a path in S3.

<build>
        <plugins>
            <plugin>
                <groupId>com.gkatzioura.maven.cloud</groupId>
                <artifactId>s3-storage-wagon</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <id>download-one</id>
                        <phase>package</phase>
                        <goals>
                            <goal>s3-download</goal>
                        </goals>
                        <configuration>
                            <bucket>your-bucket</bucket>
                            <downloadPath>/local/download/path</downloadPath>
                            <keys>1.txt,2.txt,directory/3.txt</keys>
                        </configuration>
                    </execution>
                <executions>
            <plugin>
        <plugins>
</build>

The files 1.txt, 2.txt, directory/3.txt ,  once the execution is finished, will reside in the local directory specified (/local/download/path).

Be aware that the file discovery on S3 is done with a prefix, so if you have file 1.txt and 1.txt.jpg both files will be downloaded.

You can also download only one file to one file that you specified locally, as long as it is one-to-one.

<execution>
    <id>download-prefix</id>
    <phase>package</phase>
    <goals>
        <goal>s3-download</goal>
    </goals>
    <configuration>
        <bucket>your-bucket</bucket>
        <downloadPath>/path/to/local/your-file.txt</downloadPath>
        <keys>a-key-to-download.txt</keys>
    </configuration>
</execution>

Apparently files with a prefix that contain directories (they are fakes ones on s3) will downloaded to the directory specified in the form of directories and sub directories

<execution>
    <id>download-prefix</id>
    <phase>package</phase>
    <goals>
        <goal>s3-download</goal>
    </goals>
    <configuration>
        <bucket>your-bucket</bucket>
        <downloadPath>/path/to/local/</downloadPath>
        <keys>s3-prefix</keys>
    </configuration>
</execution>

The next part is about uploading files to s3.

Uploading one file

<execution>
    <id>upload-one</id>
    <phase>package</phase>
    <goals>
        <goal>s3-upload</goal>
    </goals>
    <configuration>
        <bucket>your-bucket</bucket>
        <path>/path/to/local/your-file.txt</path>
        <key>key-to-download.txt</key>
    </configuration>
</execution>


Upload a directory

<execution>
    <id>upload-one</id>
    <phase>package</phase>
    <goals>
        <goal>s3-upload</goal>
    </goals>
    <configuration>
        <bucket>your-bucket</bucket>
        <path>/path/to/local/directory</path>
        <key>prefix</key>
    </configuration>
</execution>


Upload to the root of the bucket.

<execution>
    <id>upload-multiples-files-no-key</id>
    <phase>package</phase>
    <goals>
        <goal>s3-upload</goal>
    </goals>
    <configuration>
        <bucket>your-bucket</bucket>
        <path>/path/to/local/directory</path>
    </configuration>
</execution>


That's it. You can now download and upload files to S3 during the Maven phase of your choice!

AWS Apache Maven Upload Download

Opinions expressed by DZone contributors are their own.

Related

  • AWS CDK: Infrastructure as Abstract Data Types, Part 2
  • How to Upload/Download a File To and From the Server
  • Google Cloud KMS vs. AWS KMS: Java Encryption Guide
  • Two Engines, One Query: DuckDB Inside Neo4j

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook