After an object is uploaded, Object Storage Service (OSS) can send a callback request to the application server. To implement a callback, you need to only send a request that contains relevant callback parameters to OSS.
Notes
You can specify an upload callback when you upload a file. After a successful upload, OSS sends an HTTP POST request to your specified server address. You can then perform operations based on the received callback.
The callback URL cannot contain a query string. The query string must be specified in the
:queryparameter.If the file is uploaded successfully but the callback fails, the client throws a
CallbackError. If you want to ignore this error, you must explicitly handle the exception.For more information about the server that accepts the callback, see callback_server.rb.
Sample code
Upload callbacks are supported only for file uploads that use put_object and resumable_upload.
Specify an upload callback when using put_object
Specify an upload callback when you upload a file using put_object. Add the bucket and object information for the upload to the body of the callback. A callback received by your application server confirms that the file was successfully uploaded to OSS.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# The China (Hangzhou) region is used as an example for the endpoint. Specify the endpoint based on your region.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
# Obtain access credentials from environment variables. Before you run the 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')
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}"
endSpecify an upload callback when using resumable_upload
The procedure for resumable_upload is similar to that for put_object.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# The China (Hangzhou) region is used as an example for the endpoint. Specify the endpoint based on your region.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
# Obtain access credentials from environment variables. Before you run the 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')
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}"
endReferences
For the complete sample code for upload callbacks, see GitHub example.
For more information about the API operation for upload callbacks, see Callback.