- 
                Notifications
    You must be signed in to change notification settings 
- Fork 69
s3 uploads #543 #659
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
          
     Open
      
      
            metame
  wants to merge
  5
  commits into
  atomicdata-dev:develop
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
metame:543-s3-uploads
  
      
      
   
  
    
  
  
  
 
  
      
    base: develop
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    s3 uploads #543 #659
Changes from all commits
      Commits
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
      
      Oops, something went wrong.
      
    
  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| use std::{fmt, fs, io::Write, path::PathBuf, time::Duration}; | ||
|  | ||
| use actix_multipart::Field; | ||
| use futures::StreamExt; | ||
| use opendal::{services::S3, Operator}; | ||
|  | ||
| use crate::{appstate::AppState, config::Config, errors::AtomicServerResult}; | ||
|  | ||
| #[derive(Clone, Debug, PartialEq)] | ||
| pub enum FileStore { | ||
| S3(S3Config), | ||
| FS(FSConfig), | ||
| } | ||
|  | ||
| #[derive(Clone, Debug, PartialEq)] | ||
| pub struct S3Config { | ||
| pub bucket: String, | ||
| pub path: String, | ||
| pub endpoint: Option<String>, | ||
| pub region: Option<String>, | ||
| } | ||
|  | ||
| #[derive(Clone, Debug, PartialEq)] | ||
| pub struct FSConfig { | ||
| pub path: PathBuf, | ||
| } | ||
|  | ||
| impl FileStore { | ||
| const S3_PREFIX: &'static str = "s3:"; | ||
| const FS_PREFIX: &'static str = "fs:"; | ||
|  | ||
| pub fn init_fs_from_config(config: &Config) -> FileStore { | ||
| FileStore::FS(FSConfig { | ||
| path: config.uploads_path.clone(), | ||
| }) | ||
| } | ||
|  | ||
| pub fn init_from_config(config: &Config, fs_file_store: FileStore) -> FileStore { | ||
| let opts = &config.opts; | ||
| if let Some(bucket) = &opts.s3_bucket { | ||
|         
                  metame marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| let config = S3Config { | ||
| bucket: bucket.clone(), | ||
| endpoint: opts.s3_endpoint.clone(), | ||
| region: opts.s3_region.clone(), | ||
| path: opts.s3_path.clone().unwrap_or("uploads".to_string()), | ||
| }; | ||
| FileStore::S3(config) | ||
| } else { | ||
| fs_file_store | ||
| } | ||
| } | ||
|  | ||
| pub fn get_subject_file_store<'a>(appstate: &'a AppState, subject: &str) -> &'a FileStore { | ||
|         
                  metame marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| if subject.contains(Self::S3_PREFIX) { | ||
| &appstate.file_store | ||
| } else { | ||
| &appstate.fs_file_store | ||
| } | ||
| } | ||
|  | ||
| pub fn get_fs_file_path(&self, file_id: &str) -> AtomicServerResult<PathBuf> { | ||
| tracing::info!("fs_file_path: {}", file_id); | ||
| if let FileStore::FS(config) = self { | ||
| let fs_file_id = file_id.strip_prefix(Self::FS_PREFIX).unwrap_or(file_id); | ||
| let mut file_path = config.path.clone(); | ||
| file_path.push(fs_file_id); | ||
| Ok(file_path) | ||
| } else { | ||
| Err("Wrong FileStore passed to get_fs_file_path".into()) | ||
| } | ||
| } | ||
|  | ||
| pub fn prefix(&self) -> &str { | ||
| match self { | ||
| Self::S3(_) => Self::S3_PREFIX, | ||
| Self::FS(_) => Self::FS_PREFIX, | ||
| } | ||
| } | ||
|  | ||
| pub fn encoded(&self) -> String { | ||
| urlencoding::encode(self.prefix()).into_owned() | ||
| } | ||
|  | ||
| pub async fn upload_file(&self, file_id: &str, field: Field) -> AtomicServerResult<i64> { | ||
| match self { | ||
| FileStore::S3(_) => s3_upload(self, file_id, field).await, | ||
| FileStore::FS(config) => fs_upload(self, config, file_id, field).await, | ||
| } | ||
| } | ||
| } | ||
|  | ||
| impl fmt::Display for FileStore { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| write!(f, "{}", self.prefix()) | ||
| } | ||
| } | ||
|  | ||
| async fn fs_upload( | ||
| file_store: &FileStore, | ||
| config: &FSConfig, | ||
| file_id: &str, | ||
| mut field: Field, | ||
| ) -> AtomicServerResult<i64> { | ||
| std::fs::create_dir_all(config.path.clone())?; | ||
|  | ||
| let mut file = fs::File::create(file_store.get_fs_file_path(file_id)?)?; | ||
|  | ||
| let byte_count: i64 = file | ||
| .metadata()? | ||
| .len() | ||
| .try_into() | ||
| .map_err(|_e| "Too large")?; | ||
|  | ||
| // Field in turn is stream of *Bytes* object | ||
| while let Some(chunk) = field.next().await { | ||
| let data = chunk.map_err(|e| format!("Error while reading multipart data. {}", e))?; | ||
| // TODO: Update a SHA256 hash here for checksum | ||
| file.write_all(&data)?; | ||
| } | ||
|  | ||
| Ok(byte_count) | ||
| } | ||
|  | ||
| async fn s3_upload( | ||
| file_store: &FileStore, | ||
| file_id: &str, | ||
| mut field: Field, | ||
| ) -> AtomicServerResult<i64> { | ||
| let mut builder = S3::default(); | ||
|  | ||
| if let FileStore::S3(config) = file_store { | ||
| builder.bucket(&config.bucket); | ||
| builder.root(&config.path); | ||
| config.region.as_ref().map(|r| builder.region(r)); | ||
| config.endpoint.as_ref().map(|e| builder.endpoint(e)); | ||
| } else { | ||
| return Err("Uploading to S3 but no S3 config provided".into()); | ||
| } | ||
|  | ||
| let op: Operator = Operator::new(builder)?.finish(); | ||
| let mut w = op.writer(file_id).await?; | ||
| let mut len = 0; | ||
| while let Some(chunk) = field.next().await { | ||
| let data = chunk.map_err(|e| format!("Error while reading multipart data. {}", e))?; | ||
| len += data.len(); | ||
| w.write(data).await?; | ||
| } | ||
|  | ||
| let byte_length: i64 = len.try_into().map_err(|_e| "Too large")?; | ||
| w.close().await?; | ||
| Ok(byte_length) | ||
| } | ||
|  | ||
| pub async fn get_s3_signed_url( | ||
| file_store: &FileStore, | ||
| duration: Duration, | ||
| file_id: &str, | ||
| ) -> AtomicServerResult<String> { | ||
| let mut builder = S3::default(); | ||
|  | ||
| if let FileStore::S3(config) = file_store { | ||
| builder.bucket(&config.bucket); | ||
| builder.root(&config.path); | ||
| config.region.as_ref().map(|r| builder.region(r)); | ||
| config.endpoint.as_ref().map(|e| builder.endpoint(e)); | ||
| } else { | ||
| return Err("Downloading from S3 but no S3 config provided".into()); | ||
| } | ||
|  | ||
| let op: Operator = Operator::new(builder)?.finish(); | ||
|  | ||
| let uri = op.presign_read(file_id, duration).await?.uri().to_string(); | ||
|  | ||
| Ok(uri) | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.