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.
Usage notes
You can configure upload callbacks when you upload an object. After the object is uploaded, OSS sends an HTTP POST request to the application server address that you provide. This way, the application server is informed of the upload.
The callback URL cannot contain query strings. Query strings must be specified in the
:query
parameter.If the object is uploaded but the upload callback fails, the client returns the
CallbackError
error code. If you ignore the error, you must explicitly handle the exception.For more information about the application server that serves as the callback server, visit GitHub.
Examples
You can configure upload callbacks only when you upload an object by calling the put_object operation or the resumable_upload operation.
Configure upload callbacks when you call the put_object operation to upload an object
If you want to configure upload callbacks when you call the put_object operation to upload an object, you must add the related bucket and object information to the body field. If the application server receives the callback request, the object is uploaded to OSS.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
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 configured.
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# Specify the name of the bucket. 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}"
end
Configure upload callbacks when you call the resumable_upload operation to upload an object
You can call the resumable_upload operation in a similar manner as you call the put_object operation.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
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 configured.
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# Specify the name of the bucket. 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}"
end