使用Maven将文件上传和下载到S3

多年来,我已经看到许多团队以许多不同的方式使用Maven。 Maven可用于许多ci / cd任务,而无需使用额外的管道代码,或者可用于在运行某些测试之前准备开发环境。
通常,它是一种方便的工具,在Java团队中广泛使用,并且会继续存在,因为周围有巨大的生态系统。

CloudStorage Maven插件可帮助您将各种云存储桶用作私有Maven存储库。 最近,针对s3的CloudStorageMaven进行了巨大的升级,您可以将其用作插件,以便从s3下载或上传文件。

该插件假定您的环境已正确配置以访问所需的s3资源。
这可以通过aws configure单独实现

aws configure

其他方法是通过环境变量或通过使用适当的IAM角色。

假设您要从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>

执行完成后,文件1.txt,2.txt,directory / 3.txt应驻留在指定的本地目录中
(/本地/下载/路径)。
请注意,在s3上的文件发现是使用前缀完成的,因此,如果您具有文件1.txt和1.txt.jpg,则应同时下载这两个文件。

您也只能将一个文件下载到本地指定的一个文件,只要是一对一的即可。

<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>

显然带有目录的前缀文件(在s3上为假文件)将下载到以目录和子目录的形式指定的目录

<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>

下一部分是关于将文件上传到s3。

上传一个文件

<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>

上载目录

<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>

上传到存储桶的根目录。

<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>

而已! 由于它是一个开源项目,因此您可以在github上贡献或发出拉取请求。

翻译自: https://www.javacodegeeks.com/2019/01/upload-download-files-s3-using-maven.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值