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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Enable Faster Uploads and Downloads with Your S3 Bucket
  • AWS CDK: Infrastructure as Abstract Data Types, Part 2
  • How to Upload/Download a File To and From the Server
  • Step By Step Guide To Using Mule ESB

Trending

  • Contextual AI Integration for Agile Product Teams
  • How to Format Articles for DZone
  • Unlocking Data with Language: Real-World Applications of Text-to-SQL Interfaces
  • How Clojure Shapes Teams and Products
  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 user avatar
Emmanouil Gkatziouras
DZone Core CORE ·
Feb. 05, 19 · Code Snippet
Likes (4)
Comment
Save
Tweet
Share
15.5K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Enable Faster Uploads and Downloads with Your S3 Bucket
  • AWS CDK: Infrastructure as Abstract Data Types, Part 2
  • How to Upload/Download a File To and From the Server
  • Step By Step Guide To Using Mule ESB

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!