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

如何使用R连接IBM Cloud Object Storage Swift?已试Git包未果

Connecting R to IBM Cloud Object Storage (Swift) with Your Credentials

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:58:21