You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS环境下Shiny从S3桶上传文件的技术咨询

Hey there! Since you're working in an AWS environment with RStudio and storing files in S3, here's how you can modify your Shiny app to work with S3 files instead of local uploads. I'll cover two common scenarios based on what you might need:

Scenario 1: Let users select existing files from your S3 bucket

If you want users to pick files already stored in your S3 bucket (instead of uploading local files), replace the fileInput with a dropdown that lists all files in your bucket. We'll use the aws.s3 package for this.

First, install and load the required package:

install.packages("aws.s3")
library(aws.s3)

Modified Shiny Code

ui <- shinyUI(fluidPage(
  titlePanel("S3 File Selector"),
  sidebarLayout(
    sidebarPanel(
      # Replace local file input with S3 file dropdown
      selectInput("s3_file", "Select a file from your S3 bucket", choices = NULL),
      helpText("Files are loaded directly from your configured S3 bucket"),
      tags$hr(),
      h5(helpText("Note: Ensure your R environment has valid AWS credentials"))
    ),
    mainPanel(
      # Example: Show a preview of the selected file
      tableOutput("data_preview")
    )
  )
))

server <- shinyServer(function(input, output, session) {
  # Replace with your actual S3 bucket name
  BUCKET_NAME <- "your-s3-bucket-name"
  
  # Populate the dropdown with S3 files when the app starts
  observe({
    # Get all objects in the bucket
    s3_objects <- get_bucket(BUCKET_NAME)
    # Extract the file paths (keys) from the S3 objects
    file_options <- sapply(s3_objects, function(obj) obj$Key)
    
    # Update the dropdown with the S3 file list
    updateSelectInput(session, "s3_file", choices = file_options)
  })
  
  # Read and preview the selected S3 file
  output$data_preview <- renderTable({
    # Wait until a file is selected
    req(input$s3_file)
    
    # Read the file from S3 (adjust read function for your file type, e.g., read_excel for Excel)
    selected_data <- s3read_using(
      FUN = read.csv,
      object = input$s3_file,
      bucket = BUCKET_NAME
    )
    
    # Show first 5 rows as preview
    head(selected_data)
  })
})

shinyApp(ui, server)

Scenario 2: Upload local files to S3 and process them

If you still want users to upload local files, but store/process them directly in S3 (instead of keeping them local), you can combine fileInput with S3 upload functionality.

Modified Shiny Code

library(aws.s3)

ui <- shinyUI(fluidPage(
  titlePanel("Upload Files to S3 & Process"),
  sidebarLayout(
    sidebarPanel(
      fileInput("local_file", "Upload a local file to S3"),
      helpText("Default max file size is 5MB - adjust in settings if needed"),
      tags$hr(),
      h5(helpText("File will be saved to your S3 bucket automatically"))
    ),
    mainPanel(
      textOutput("upload_status"),
      tableOutput("processed_data")
    )
  )
))

server <- shinyServer(function(input, output) {
  BUCKET_NAME <- "your-s3-bucket-name"
  
  # Handle file upload to S3 and processing
  observeEvent(input$local_file, {
    req(input$local_file)
    
    # Upload the local file to S3 (use the original filename as the S3 object key)
    upload_success <- put_object(
      file = input$local_file$datapath,
      object = input$local_file$name,
      bucket = BUCKET_NAME
    )
    
    # Show upload status
    output$upload_status <- renderText({
      if (upload_success) {
        paste0("Success! File '", input$local_file$name, "' uploaded to S3.")
      } else {
        "Upload failed - check your AWS permissions and bucket name."
      }
    })
    
    # Read the uploaded file from S3 and process it
    processed_data <- s3read_using(
      FUN = read.csv,
      object = input$local_file$name,
      bucket = BUCKET_NAME
    )
    
    # Show processed data preview
    output$processed_data <- renderTable(head(processed_data))
  })
})

# Optional: Increase max file upload size (e.g., 30MB)
options(shiny.maxRequestSize = 30 * 1024^2)

shinyApp(ui, server)

Important Notes

  • AWS Credentials: Make sure your R environment has access to S3. If running on AWS EC2/ECS, attach an IAM role with s3:ListBucket, s3:GetObject, and (for uploads) s3:PutObject permissions. If running locally, configure credentials via aws configure in the terminal.
  • File Types: Adjust the read function (e.g., read_excel from the readxl package for Excel files, jsonlite::read_json for JSON) to match your file format.
  • Large Files: If handling files larger than 5MB, update the shiny.maxRequestSize option as shown in Scenario 2.

内容的提问来源于stack exchange,提问作者Rohi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:49:57