全部产品
Search
文档中心

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

更新时间:Nov 30, 2025

Topik ini menjelaskan cara mengunggah file lokal ke bucket Object Storage Service (OSS).

Contoh

Kode berikut menunjukkan cara mengunggah file lokal bernama examplefile.txt ke bucket bernama examplebucket. File yang diunggah akan disimpan sebagai objek bernama exampleobject.txt di 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')

Referensi

Untuk informasi lebih lanjut tentang operasi API yang dapat digunakan untuk melakukan unggahan sederhana, lihat PutObject.