Paws AWS SDK for R

repository·main·Indexed 43 days ago

https://github.com/paws-r/paws

A comprehensive AWS SDK for the R programming language that provides programmatic access to the full suite of Amazon Web Services. It is architected as a collection of packages, including the main paws loader, service-specific paws.<category> packages, and the foundational paws.common utility package.

Tokens
9.8K
Snippets
38
Records
48
Agent score
56%

What's inside Paws

  1. What is paws.common?

    main
    paws.common is a foundational package for the Paws ecosystem. It provides the core utility functions required to interact with Amazon Web Services (AWS) APIs, specifically focusing on the mechanics of building and sending requests. It serves as the underlying engine for individual Paws service packages (such as paws.ec2), ensuring a consistent way to communicate with AWS services across the entire SDK.
  2. Understand the Paws software components

    main

    Paws is architected as a collection of specialized packages to manage AWS service code while staying within CRAN package size limits:

    • paws: The main package installed from CRAN. It acts as a loader for the various paws.<category> packages.
    • paws.<category>: Service-specific packages (e.g., paws.storage for S3). Each category contains the code for a subset of AWS services.
    • paws.common: A core package containing shared logic for making AWS requests and decoding responses. All paws.<category> packages depend on this.
    • make.paws: An internal code generator (not on CRAN) that reads JSON metadata from the official AWS SDK for JavaScript and produces the R code for the paws and paws.<category> packages.
  3. Credential Priority for Bedrock Services

    main

    When using Bedrock services, paws checks for credentials in the following order. If AWS_BEARER_TOKEN_BEDROCK is present, it will be used for Bedrock API authentication, overriding traditional IAM credentials for supported operations:

    1. Bearer token from AWS_BEARER_TOKEN_BEDROCK environment variable
    2. Service settings (access key, secret key)
    3. Environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
    4. AWS shared credentials file (~/.aws/credentials)
    5. AWS config file (~/.aws/config)
    6. EC2 instance or container IAM role
  4. Understand the priority of credentials and options

    main

    Paws resolves credentials and configuration options using a specific hierarchy. If a setting is defined in multiple places, the one higher in this list takes precedence:

    1. Service settings: Provided directly when creating a service object via paws::svc(config = ...).
    2. Environment variables: System-level variables like AWS_ACCESS_KEY_ID or AWS_REGION.
    3. AWS shared files: Settings found in ~/.aws/credentials and ~/.aws/config.
    4. IAM Roles: Credentials retrieved from an EC2 instance or container role.
  5. Use paginators to retrieve complete AWS result sets

    main

    Some AWS operations return incomplete results that require subsequent requests to retrieve the full set (e.g., s3$list_objects). Paws provides three paginator methods to automate this process. As of paws v0.4.0+, you can use these methods to handle pagination automatically.

    • paginate: Returns all response objects from the operation.
    • paginate_lapply: Applies a function to each response object and returns the results as a list.
    • paginate_sapply: Applies a function to each response object and simplifies the final result (similar to base::sapply).
    library(paws)
    svc <- s3(region = "us-west-2")
    
    # Returns all response objects
    results <- paginate(svc$list_objects(Bucket = "my-bucket"))
    
    # Returns a list of specific fields from each response
    results <- paginate_lapply(svc$list_objects(Bucket = "my-bucket"), \(resp) resp$Contents)
    
    # Returns a simplified vector/array of specific fields
    results <- paginate_sapply(
        svc$list_objects(Bucket = "my-bucket"),
        \(resp) resp$Contents,
        simplify = T
    )
  6. Configure AWS Credentials and Region

    main

    Paws requires AWS credentials and a region to function. You can configure these per-service or globally using R environment variables, OS environment variables, AWS credential files, or IAM roles.

    Warning: Avoid hardcoding credentials directly in your scripts. Use RStudio's best practices for securing credentials (e.g., using keyring) or environment variables.

    Example using R environment variables:

    Sys.setenv(
      AWS_ACCESS_KEY_ID = "abc",
      AWS_SECRET_ACCESS_KEY = "123",
      AWS_REGION = "us-east-1"
    )
    Sys.setenv(
      AWS_ACCESS_KEY_ID = "abc",
      AWS_SECRET_ACCESS_KEY = "123",
      AWS_REGION = "us-east-1"
    )
  7. Manage NAMESPACE and DESCRIPTION when using make.paws

    main

    Roxygen documentation is disabled in the make.paws package because it uses templated strings containing Roxygen directives that can cause interpretation errors or unintended side effects.

    Because Roxygen is turned off, you must manually perform the following maintenance tasks if you modify the package structure:

    1. Update NAMESPACE: Manually update this file if you change any @import or @export directives.
    2. Update DESCRIPTION: Manually update the Collate field if you change any @include directives within the package.
  8. Build the Paws packages

    main

    The paws and paws.<category> packages are generated using make.paws. To build the packages from source, use the make build command.

    Note that building is a slow process, but it is cached to speed up subsequent builds. If you have modified the code generator and need to clear the cache, use make clean.

    make build
    # To clear the cache:
    make clean
    # To see all available recipes:
    make
  9. Install Paws with renv using R-Universe

    main

    When using renv for project dependency management, you can configure it to use the R-Universe binary source to speed up installation.

    # In your project, configure renv to use R-Universe
    options(repos = c(
      pawsr = 'https://paws-r.r-universe.dev',
      CRAN = 'https://cloud.r-project.org'
    ))
    
    renv::install('paws')
  10. Set a specific AWS profile

    main

    By default, Paws uses the default profile. To use a different profile from your AWS shared credentials and config files, you can either set the AWS_PROFILE environment variable or specify the profile within the service configuration.

    # Option 1: Using environment variables
    Sys.setenv(AWS_PROFILE = "my-profile")
    
    # Option 2: Using the profile parameter in the service object
    svc <- paws::s3(
        config = list(
            credentials = list(
                profile = "my-profile"
            )
        )
    )