All Products
Search
Document Center

Object Storage Service:Upload a local file (Ruby SDK)

Last Updated:Mar 20, 2026

Upload a local file to an Object Storage Service (OSS) bucket using the aliyun/oss Ruby gem and the put_object method.

Prerequisites

Before you begin, ensure that you have:

  • The aliyun/oss gem installed

  • An OSS bucket

  • An AccessKey ID and AccessKey secret with write permissions on the bucket

Set up credentials

Store your AccessKey ID and AccessKey secret as environment variables to keep credentials out of your source code.

export OSS_ACCESS_KEY_ID=<your-access-key-id>
export OSS_ACCESS_KEY_SECRET=<your-access-key-secret>

Replace <your-access-key-id> and <your-access-key-secret> with your actual credentials.

Upload a file

The following example uploads a local file named examplefile.txt to examplebucket. The file is stored as an object named exampleobject.txt in OSS.

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # Replace with the endpoint for your bucket's region.
  # This example uses the China (Hangzhou) region.
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # Read credentials from environment variables.
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)

bucket = client.get_bucket('examplebucket')

# Upload a local file. Specify the object name and the local file path.
bucket.put_object('exampleobject.txt', :file => 'D:\\localpath\\examplefile.txt')
Note

The example uses a Windows-style file path (D:\\localpath\\examplefile.txt). On Linux or macOS, use a POSIX path, such as /local/path/examplefile.txt.

What's next