如何使用R连接IBM Cloud Object Storage Swift?已试Git包未果
Hey there! Let’s get you connected to IBM Cloud Object Storage (Swift) using your existing credentials. Since you mentioned hitting roadblocks with Git-hosted R packages, I’ve put together a reliable solution using the httr and jsonlite packages—these are CRAN-maintained, widely used, and less prone to compatibility issues.
Step 1: Install & Load Required Packages
First, make sure you have the necessary tools installed:
install.packages(c("httr", "jsonlite")) library(httr) library(jsonlite)
Step 2: Plug in Your Credentials
Populate these variables with your provided config details (no need to change the structure):
# Your IBM COS Swift credentials auth_url <- "https://dal05.objectstorage.softlayer.net/auth/v1.0" username <- "my_user_name" password <- "my_pass" container <- "my_container" filename <- "my_filename.csv"
Step 3: Authenticate to Get Auth Token & Storage URL
IBM COS Swift uses v1 authentication, so we’ll send a request to your auth URL to get the tokens needed for subsequent operations:
# Send authentication request auth_response <- GET( auth_url, add_headers( "X-Storage-User" = username, "X-Storage-Password" = password ) ) # Check if authentication succeeded (throws error if not) stop_for_status(auth_response, "authenticate to IBM COS Swift") # Extract critical values from response headers auth_token <- headers(auth_response)$`x-auth-token` storage_url <- headers(auth_response)$`x-storage-url`
Step 4: Core Operations (Upload/Download Files)
Upload a Local CSV to COS
Replace the upload_file() path with your local file’s location:
upload_response <- PUT( url = paste0(storage_url, "/", container, "/", filename), add_headers("X-Auth-Token" = auth_token), body = upload_file("path/to/your/local/file.csv") # Update this path! ) stop_for_status(upload_response, "upload your file") cat("File uploaded successfully to IBM COS!\n")
Download the CSV from COS to Your Local Machine
Replace the write_disk() path with where you want to save the file:
download_response <- GET( url = paste0(storage_url, "/", container, "/", filename), add_headers("X-Auth-Token" = auth_token), write_disk("path/to/save/your/file.csv", overwrite = TRUE) # Update this path! ) stop_for_status(download_response, "download your file") cat("File downloaded successfully from IBM COS!\n")
Bonus: List All Files in Your Container
If you want to verify what’s in your container, run this:
list_response <- GET( url = paste0(storage_url, "/", container), add_headers("X-Auth-Token" = auth_token) ) stop_for_status(list_response, "list files in container") files <- content(list_response, "text") cat("Files in your container:\n", files, "\n")
Why This Works
This approach uses IBM COS Swift’s native REST API directly, so you avoid any dependency issues with third-party packages. httr is a robust, well-supported package that handles HTTP requests reliably.
内容的提问来源于stack exchange,提问作者Kirill Panin

