Append uploads (Ruby SDK)
You can call the AppendObject operation to append content to existing appendable objects.
Notes
-
In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.
-
This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Client configuration.
-
If the file does not exist, the
Bucket#append_objectmethod creates an appendable object. -
If the file already exists:
-
If the file is an appendable object and the specified append position matches the current length of the file, the content is appended to the end of the file.
-
If the file is an appendable object but the specified append position does not match the current length of the file, a PositionNotEqualToLength exception is thrown.
-
If the file is not an appendable object, such as a normal object uploaded by a simple upload, an ObjectNotAppendable exception is thrown.
-
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM policies or Bucket Policy.
|
API |
Action |
Description |
|
AppendObject |
|
You can call this operation to upload an object by appending the object to an existing object. |
|
|
When uploading an object by appending the object to an existing object, if you specify object tags through x-oss-tagging, this permission is required. |
Sample code
The following code shows how to perform an append upload.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# The China (Hangzhou) region is used as an example. Specify the actual 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')
# Specify the full path of the object. The full path cannot contain the bucket name.
bucket.append_object('my-object', 0)
# Append content to the end of the file.
next_pos = bucket.append_object('my-object', 0) do |stream|
100.times { |i| stream << i.to_s }
end
next_pos = bucket.append_object('my-object', next_pos, :file => 'local-file-1')
next_pos = bucket.append_object('my-object', next_pos, :file => 'local-file-2')
References
For more information about the AppendObject API operation, see AppendObject.