Append upload adds content to the end of an appendable object using the AppendObject API. Use it for scenarios such as writing log entries from a running process or sequentially appending chunks of streaming data.
Usage notes
The examples in this topic use the public endpoint of the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use the internal endpoint instead. For a list of supported regions and endpoints, see Regions and endpoints.
The examples create an OSSClient instance using an OSS endpoint. To create an OSSClient instance using a custom domain name or Security Token Service (STS), see Configuration examples for common scenarios.
Bucket#append_objectbehaves differently depending on whether the target object exists and its type:Condition Result Object does not exist Creates a new appendable object and appends the content Object is appendable and the position matches its current size Appends content to the end of the object Object is appendable but the position does not match its current size Throws PositionNotEqualToLengthObject exists but is not appendable (for example, uploaded via simple upload) Throws ObjectNotAppendable
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default. Grant the required permissions through RAM Policy or Bucket policies.
| API | Required action | Description |
|---|---|---|
| AppendObject | oss:PutObject | Upload an object by appending to an existing object |
| AppendObject | oss:PutObjectTagging | Required when specifying object tags via x-oss-tagging |
Sample code
Initialize the client
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# Replace with your actual region endpoint.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
# Read 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')Append content to an object
Use a block to stream content into the object. Pass 0 as the position for the first append. Each call returns the position to use for the next append.
# First append: position must be 0 for a new appendable object.
next_pos = bucket.append_object('my-object', 0) do |stream|
100.times { |i| stream << i.to_s }
end
# Subsequent appends: use next_pos from the previous call.
next_pos = bucket.append_object('my-object', next_pos) do |stream|
stream << 'additional content'
endAppend a local file
To append the contents of a local file, pass the :file option instead of a block.
# First append from a local file.
next_pos = bucket.append_object('my-object', 0, :file => 'local-file-1')
# Chain multiple files using the returned position.
next_pos = bucket.append_object('my-object', next_pos, :file => 'local-file-2')References
For the full AppendObject API reference, see AppendObject.