Skip to content

Commit

Permalink
Change version downloads archive location to static S3 bucket (#9180)
Browse files Browse the repository at this point in the history
Previously we were uploading the version downloads exports to a dedicated bucket, but the infra team decided that it would be best to use the `static` bucket instead. This changes the upload location by switching from a dedicated `ObjectStore` instance to a `PrefixStore` based on our `Storage` struct and the `archive/version-downloads` prefix.
  • Loading branch information
Turbo87 authored Jul 31, 2024
1 parent c1ee9fe commit e669bdb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 32 deletions.
9 changes: 6 additions & 3 deletions src/bin/background-worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use crates_io::db::make_manager_config;
use crates_io::fastly::Fastly;
use crates_io::storage::Storage;
use crates_io::team_repo::TeamRepoImpl;
use crates_io::worker::jobs::ArchiveVersionDownloads;
use crates_io::worker::{Environment, RunnerExt};
use crates_io::{config, Emails};
use crates_io::{db, ssh};
Expand All @@ -28,6 +27,8 @@ use crates_io_index::RepositoryConfig;
use crates_io_worker::Runner;
use diesel_async::pooled_connection::deadpool::Pool;
use diesel_async::pooled_connection::AsyncDieselConnectionManager;
use object_store::prefix::PrefixStore;
use object_store::ObjectStore;
use reqwest::Client;
use secrecy::ExposeSecret;
use std::sync::Arc;
Expand Down Expand Up @@ -71,7 +72,9 @@ fn main() -> anyhow::Result<()> {

let cloudfront = CloudFront::from_environment();
let storage = Arc::new(Storage::from_config(&config.storage));
let downloads_archive_store = ArchiveVersionDownloads::store_from_environment()?;

let downloads_archive_store = PrefixStore::new(storage.as_inner(), "archive/version-downloads");
let downloads_archive_store: Box<dyn ObjectStore> = Box::new(downloads_archive_store);

let client = Client::builder()
.timeout(Duration::from_secs(45))
Expand All @@ -92,7 +95,7 @@ fn main() -> anyhow::Result<()> {
.cloudfront(cloudfront)
.fastly(fastly)
.storage(storage)
.downloads_archive_store(downloads_archive_store)
.downloads_archive_store(Some(downloads_archive_store))
.deadpool(deadpool.clone())
.emails(emails)
.team_repo(Box::new(team_repo))
Expand Down
4 changes: 2 additions & 2 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ impl Storage {
}

/// This should only be used for assertions in the test suite!
pub fn as_inner(&self) -> &dyn ObjectStore {
&self.store
pub fn as_inner(&self) -> Arc<dyn ObjectStore> {
self.store.clone()
}

async fn delete_all_with_prefix(&self, prefix: &Path) -> Result<()> {
Expand Down
27 changes: 0 additions & 27 deletions src/worker/jobs/archive_version_downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ use crate::util::diesel::Conn;
use crate::worker::Environment;
use anyhow::{anyhow, Context};
use chrono::{NaiveDate, Utc};
use crates_io_env_vars::var;
use crates_io_worker::BackgroundJob;
use diesel::prelude::*;
use diesel::{ExpressionMethods, RunQueryDsl};
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
use diesel_async::pooled_connection::deadpool::Pool;
use diesel_async::AsyncPgConnection;
use futures_util::StreamExt;
use object_store::aws::AmazonS3Builder;
use object_store::ObjectStore;
use secrecy::{ExposeSecret, SecretString};
use std::collections::btree_map::Entry;
Expand Down Expand Up @@ -40,31 +38,6 @@ impl ArchiveVersionDownloads {
pub fn before(before: NaiveDate) -> Self {
Self { before }
}

pub fn store_from_environment() -> anyhow::Result<Option<Box<dyn ObjectStore>>> {
let Some(region) = var("DOWNLOADS_ARCHIVE_REGION")? else {
return Ok(None);
};
let Some(bucket) = var("DOWNLOADS_ARCHIVE_BUCKET")? else {
return Ok(None);
};
let Some(access_key) = var("DOWNLOADS_ARCHIVE_ACCESS_KEY")? else {
return Ok(None);
};
let Some(secret_key) = var("DOWNLOADS_ARCHIVE_SECRET_KEY")? else {
return Ok(None);
};

let store = AmazonS3Builder::new()
.with_region(region)
.with_bucket_name(bucket)
.with_access_key_id(access_key)
.with_secret_access_key(secret_key)
.build()
.context("Failed to initialize S3 code")?;

Ok(Some(Box::new(store)))
}
}

impl Default for ArchiveVersionDownloads {
Expand Down

0 comments on commit e669bdb

Please sign in to comment.