After an object is uploaded, Object Storage Service (OSS) can send an HTTP POST request to your application server. Use upload callbacks to trigger server-side processing — such as updating a database record or starting a media transcoding job — immediately after a successful upload.
How it works
Configure a callback when submitting the upload request. Specify the callback URL, query parameters, and request body.
OSS uploads the object. On success, OSS sends an HTTP POST request to your callback URL with the body you specified.
If the callback request fails, the SDK raises a
CallbackErroron the client side.
Usage notes
Upload callbacks are supported only for
put_objectandresumable_upload.The callback URL cannot contain a query string. Pass query parameters in the
:queryoption instead.If the upload succeeds but the callback fails, the SDK raises
Aliyun::OSS::CallbackError. To ignore the error, rescue the exception explicitly.For a reference implementation of the callback server, see callback_server.rb.
Configure an upload callback
Both put_object and resumable_upload accept a callback: option. The callback object takes three parameters:
| Parameter | Description | Example |
|---|---|---|
url | The URL that OSS sends the HTTP POST request to | http://oss-demo.aliyuncs.com:23450 |
query | Query parameters appended to the callback request. Do not include these in url. | {user: 'put_object'} |
body | The POST body sent to your callback server. Use ${bucket} and ${object} to include the bucket name and object key in the body. | 'bucket=${bucket}&object=${object}' |
Use put_object with a callback
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# Replace with the endpoint for your region.
# The China (Hangzhou) region is used in this example.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
# Read AccessKey credentials from environment variables.
# Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this code.
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
bucket = client.get_bucket('examplebucket')
callback = Aliyun::OSS::Callback.new(
url: 'http://oss-demo.aliyuncs.com:23450',
query: {user: 'put_object'},
body: 'bucket=${bucket}&object=${object}'
)
begin
bucket.put_object('files/hello', file: '/tmp/x', callback: callback)
rescue Aliyun::OSS::CallbackError => e
puts "Callback failed: #{e.message}"
endUse resumable_upload with a callback
The callback configuration is identical to put_object. Pass the same callback: option to resumable_upload.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# Replace with the endpoint for your region.
# The China (Hangzhou) region is used in this example.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
# Read AccessKey credentials from environment variables.
# Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this code.
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
bucket = client.get_bucket('examplebucket')
callback = Aliyun::OSS::Callback.new(
url: 'http://oss-demo.aliyuncs.com:23450',
query: {user: 'put_object'},
body: 'bucket=${bucket}&object=${object}'
)
begin
bucket.resumable_upload('files/hello', '/tmp/x', callback: callback)
rescue Aliyun::OSS::CallbackError => e
puts "Callback failed: #{e.message}"
endWhat's next
Complete sample code: GitHub example
API reference: Callback