如何配置Flyway的flyway.locations参数以指定S3桶中的SQL脚本
locations for S3-Stored Migration Scripts Great question! Setting up Flyway to pull migration scripts directly from an S3 bucket is totally doable—here's a step-by-step breakdown to get it working smoothly:
1. Add Required Dependencies
Flyway’s S3 support lives in a dedicated module, so you’ll need to include it in your project (if using build tools like Maven/Gradle) or ensure it’s on your classpath if running Flyway CLI:
Maven
<dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-aws-s3</artifactId> <version>9.22.3</version> <!-- Use the latest stable version --> </dependency>
Gradle
implementation 'org.flywaydb:flyway-aws-s3:9.22.3'
If using the Flyway CLI, make sure you’ve downloaded the distribution that includes AWS support (or manually add the JAR to your classpath).
2. Configure AWS Credentials
Flyway uses the standard AWS credential chain to authenticate with S3, so you can set this up any of these ways:
- Local development: Use the
~/.aws/credentialsfile or set theAWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEYenvironment variables. - Cloud environments (EC2/EKS/ECS): Attach an IAM role with
s3:GetObjectpermissions for your target bucket and path. - Docker/Kubernetes: Inject credentials via secrets or use IAM roles for service accounts (IRSA).
Just ensure the authenticated entity has permission to read objects from your S3 bucket’s migration script path.
3. Set the flyway.locations Parameter
Use the s3:// URL scheme to point directly to your bucket and script directory. The format is:
flyway.locations=s3://<your-bucket-name>/<path-to-migration-scripts>
Example Configurations
- Command line:
flyway -locations=s3://my-flyway-migrations/prod/sql migrate - Flyway config file (
flyway.conf):flyway.locations=s3://my-flyway-migrations/prod/sql - Maven plugin:
<plugin> <groupId>org.flywaydb</groupId> <artifactId>flyway-maven-plugin</artifactId> <version>9.22.3</version> <configuration> <locations> <location>s3://my-flyway-migrations/prod/sql</location> </locations> <!-- Add your database connection details here --> </configuration> </plugin>
Key Notes
- Flyway will recursively scan all subdirectories under the specified S3 path for migration scripts (following the standard
V__<description>.sqlnaming convention). - You don’t need a trailing slash at the end of the S3 path—Flyway handles both formats equally well.
- If you’re using Flyway Teams/Enterprise, you can leverage additional features like S3 versioning support or path-based filtering, but the basic setup above works for Community Edition too.
内容的提问来源于stack exchange,提问作者AkshayBadri

