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

如何在AWS的RStudio中保存数据到S3并同步生成的网站文件

Great questions! Let's break this down step by step for your AWS RStudio environment, where your files live in ~/Home/Myproject.


Saving Data from RStudio to S3

First, you'll need to set up your R environment to interact with AWS S3. The aws.s3 package is the most straightforward tool for this.

1. Install and load the package

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

2. Configure AWS Credentials

  • Best practice (if using AWS compute like EC2): Attach an IAM role to your RStudio instance with permissions for s3:PutObject, s3:ListBucket, and any other S3 actions you need. The aws.s3 package will automatically pick up these credentials—no manual key entry needed.
  • Manual credentials (if not using an IAM role): Set your AWS keys as environment variables in R (never hardcode them in scripts!):
    Sys.setenv(AWS_ACCESS_KEY_ID = "your-access-key-here",
               AWS_SECRET_ACCESS_KEY = "your-secret-key-here",
               AWS_REGION = "your-region") # e.g., "us-east-1"
    

Option A: Save R objects directly to S3

You can skip writing to your local filesystem and save R objects (like data frames, models) straight to S3:

  • For RDS files (ideal for preserving R object structure):
    # Save a data frame 'my_dataset' to S3
    s3write_rds(my_dataset, 
                bucket = "your-target-s3-bucket", 
                object = "path/inside/bucket/my_data.rds")
    
  • For CSV files (good for sharing with non-R users):
    s3write_csv(my_dataset, 
                bucket = "your-target-s3-bucket", 
                object = "path/inside/bucket/my_data.csv")
    

Option B: Upload existing local files to S3

If you already have a file saved in ~/Home/Myproject (like a CSV or Excel file), use put_object() to upload it:

put_object(file = "~/Home/Myproject/my_local_file.csv",
           bucket = "your-target-s3-bucket",
           object = "path/inside/bucket/my_uploaded_file.csv")

Rendering Rmarkdown Site and Transferring to S3 Website Bucket

Step 1: Find your S3 website bucket path

  • Head to the AWS S3 Console, locate the bucket you're using to host your website. The bucket name (e.g., my-personal-website-bucket) is your base "path". If you're using a subdirectory inside the bucket (like docs), your full target path is s3://my-personal-website-bucket/docs.
  • Double-check that your bucket is configured for static website hosting (you can set this up in the S3 Console under Properties > Static website hosting).

Step 2: Render your Rmarkdown site

First, make sure you're in your project directory, then run the render command. By default, this will generate all site files in a _site folder:

# Navigate to your project root
setwd("~/Home/Myproject")

# Render the site from your .Rmd files
rmarkdown::render_site()

Step 3: Sync the generated site to S3

The sync_folder() function is perfect here—it only uploads files that have changed since your last sync, saving time:

# Sync the local '_site' folder to your S3 website bucket
sync_folder(from = "~/Home/Myproject/_site",
            to = "s3://your-website-bucket/path/to/site",
            region = "your-region",
            acl = "public-read") # Add this if your site needs public access

Key Notes:

  • Ensure your IAM role/user has permissions for s3:PutObject, s3:DeleteObject, and s3:ListBucket on the website bucket to enable syncing.
  • The acl = "public-read" argument makes your site files accessible to the public—only use this if your website is meant to be public!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:07:57