Skip to content

Commit

Permalink
feat: clumsily move to R2
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbchron committed Aug 26, 2024
1 parent 867fcd4 commit a1a226b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
14 changes: 6 additions & 8 deletions crates/artifact/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use core_types::{PrivateArtifact, PublicArtifact};

use self::methods::*;

const ARTIFACT_PRIVATE_LTS_BUCKET: &str = "picturepro-artifact-private-lts";
const ARTIFACT_PUBLIC_LTS_BUCKET: &str = "picturepro-artifact-public-lts";
const ARTIFACT_PRIVATE_LTS_BUCKET: &str = "picturepro-private";
const ARTIFACT_PUBLIC_LTS_BUCKET: &str = "picturepro-public";

type ObjectStoreGenerator =
Box<dyn Fn() -> Result<Box<dyn object_store::ObjectStore>> + Send + 'static>;
Expand Down Expand Up @@ -89,12 +89,10 @@ impl Artifact for PublicArtifact {
Self::new_with_id(id, contents)
}
fn new_with_id(id: Self::Id, contents: Option<bytes::Bytes>) -> Self {
let url = format!(
"https://s3.{}.amazonaws.com/{}/{}",
std::env::var("AWS_DEFAULT_REGION").unwrap(),
ARTIFACT_PUBLIC_LTS_BUCKET,
id.0
);
let url =
format!("https://{}.jlewis.sh/{}", ARTIFACT_PUBLIC_LTS_BUCKET, id.0);
tracing::info!("public artifact url: {}", url);

Self {
id,
url,
Expand Down
26 changes: 21 additions & 5 deletions crates/artifact/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,28 @@ use crate::ObjectStoreGenerator;
pub fn object_store_from_env(
bucket_name: &str,
) -> Result<Box<dyn object_store::ObjectStore>> {
let object_store = object_store::aws::AmazonS3Builder::from_env()
.with_region(
std::env::var("AWS_DEFAULT_REGION")
.wrap_err("Failed to get AWS region from environment")?,
)
let endpoint = format!(
"https://{}.r2.cloudflarestorage.com",
std::env::var("R2_ACCOUNT")
.wrap_err("Failed to get R2 account from environment")?
);
tracing::info!("endpoint: {}", endpoint);

let object_store = object_store::aws::AmazonS3Builder::new()
// .with_region(
// std::env::var("AWS_DEFAULT_REGION")
// .wrap_err("Failed to get AWS region from environment")?,
// )
.with_endpoint(endpoint)
.with_bucket_name(bucket_name)
.with_access_key_id(
std::env::var("AWS_ACCESS_KEY_ID")
.wrap_err("Failed to get AWS access key ID from environment")?,
)
.with_secret_access_key(
std::env::var("AWS_SECRET_ACCESS_KEY")
.wrap_err("Failed to get AWS secret access key from environment")?,
)
.build()
.wrap_err("Failed to create object store")?;

Expand Down
8 changes: 5 additions & 3 deletions crates/bl/src/upload/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub enum PhotoUploadError {
custom = RmpEncoded,
output = Json,
)]
#[cfg_attr(feature = "ssr", tracing::instrument)]
#[cfg_attr(feature = "ssr", tracing::instrument(skip(params)))]
pub async fn upload_photo_group(
params: PhotoGroupUploadParams,
) -> Result<PhotoGroupRecordId, ServerFnError<PhotoUploadError>> {
Expand Down Expand Up @@ -180,10 +180,12 @@ async fn create_photo(
let original_upload_task = tokio::spawn({
let original_artifact = original_artifact.clone();
async move {
original_artifact
let result = original_artifact
.upload_and_push()
.await
.wrap_err("Failed to create original artifact")
.wrap_err("Failed to create original artifact");
tracing::info!("original artifact: {:?}", result);
result
}
});

Expand Down

0 comments on commit a1a226b

Please sign in to comment.