Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move from Azure to AWS S3 for blob storage #5023

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Move from Azure to AWS S3 for blob storage #5023

wants to merge 4 commits into from

Conversation

dorner
Copy link
Collaborator

@dorner dorner commented Feb 17, 2025

This PR does the following:

  • Adds an amazon storage provider. I've set up blob storage buckets for both production and staging.
  • Changes the storage provider to amazon for staging only for now.
  • Adds a migration for staging only which downloads / uploads the contents of the blobs to the S3 bucket.
  • Updates the database backup and fetch script to use S3. There is a third bucket set up which both staging and prod can write to where the DBs will go. This bucket is set up to have a TTL of 14 days (so all files added to it will be deleted after 14 days).

My suggestion for testing:

  • Add some blobs on staging by attaching some files to various records.
  • Deploy the branch so the migration runs.
  • Validate that you can still download the existing blobs.
  • Add some more blobs and validate that they upload correctly and you can download them.

Once testing is complete, this branch is safe to merge, and we can put in a further PR that will apply these changes to prod.

@dorner dorner requested a review from awwaiid February 17, 2025 00:18
@@ -6,18 +8,12 @@ task :backup_db_rds => :environment do
current_time = Time.current.strftime("%Y%m%d%H%M%S")

logger.info("Copying the database...")
backup_filename = "#{current_time}.rds.dump"
backup_filename = "#{Rails.env}-#{current_time}.rds.dump"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

storage_account_name: account_name,
storage_access_key: account_key
)
client = Aws::S3::Client.new(region: 'us-east-2')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the same as

client = ActiveStorage::Blob.services.fetch(:amazon)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically. In this case I'm going straight to the client rather than doing it in a roundabout way through ActiveStorage.

@danielabar
Copy link
Collaborator

Is CORS enabled on S3? Will be needed for direct uploads, introduced here: #4937

@dorner
Copy link
Collaborator Author

dorner commented Feb 26, 2025

Yep, it should be!

class MoveAzureBlobsToS3 < ActiveRecord::Migration[7.2]
# https://stackoverflow.com/questions/71699789/activestorage-transfer-all-assets-from-one-bucket-to-another-bucket
def up
return unless Rails.env.staging?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so only run this migration in staging. I see.

Copy link
Collaborator

@awwaiid awwaiid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good!

@awwaiid
Copy link
Collaborator

awwaiid commented Feb 26, 2025

We'll let @cielf merge as part of a controlled staging validation (upload files, merge this, see that they're still there, upload more files)

@cielf
Copy link
Collaborator

cielf commented Feb 26, 2025

I was hoping to get to this today but life interfered. Hopefully Friday.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants