All Products
Search
Document Center

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

Last Updated:Nov 29, 2025

This topic describes how to upload a local file to an Object Storage Service (OSS) bucket.

Examples

The following code provides an example on how to upload a local file named examplefile.txt to a bucket named examplebucket. The uploaded file is stored as an object named exampleobject.txt in OSS.

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # Set the endpoint. This example uses the China (Hangzhou) region. Specify the endpoint for your actual region.
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# Specify the bucket name. For example, examplebucket.
bucket = client.get_bucket('examplebucket')
# Upload the file.
bucket.put_object('exampleobject.txt', :file => 'D:\\localpath\\examplefile.txt')

References

For more information about the API operation that you can call to perform simple upload, see PutObject.