Append upload lets you add content to the end of an appendable object using the AppendObject method.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configure OSSClient instances.
If the object does not exist, the AppendObject operation creates an appendable object.
If the object exists:
If the object is an appendable object and the specified append position matches the current length of the object, the content is appended to the end of the object.
If the object is an appendable object but the specified append position does not match the current length of the object, a PositionNotEqualToLength exception is thrown.
If the object is not an appendable object, such as a Normal object uploaded using 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 Policy or Bucket policies.
API | Action | Definition |
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:
package main
import (
"log"
"strings"
"time"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// Create an OSSClient instance.
// Set yourEndpoint to the Endpoint of the bucket. For example, for the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual Endpoint.
// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
log.Fatalf("Failed to create OSS client: %v", err)
}
// Specify the bucket name, for example, examplebucket.
bucketName := "examplebucket" // Replace with the actual bucket name.
bucket, err := client.Bucket(bucketName)
if err != nil {
log.Fatalf("Failed to get bucket: %v", err)
}
// Specify the full path of the object. Do not include the bucket name. For example, appendobject.txt.
objectName := "appendobject.txt"
var nextPos int64 = 0
// Specify the expiration time.
expires := time.Date(2025, time.December, 10, 23, 0, 0, 0, time.UTC)
options := []oss.Option{
oss.Expires(expires),
}
// The position for the first append upload is 0. The return value is the position for the next append upload.
appendValue1 := "YourObjectAppendValue1"
nextPos, err = bucket.AppendObject(objectName, strings.NewReader(appendValue1), nextPos, options...)
if err != nil {
log.Fatalf("Failed to append first value: %v", err)
}
// Perform the second append upload.
appendValue2 := "YourObjectAppendValue2"
nextPos, err = bucket.AppendObject(objectName, strings.NewReader(appendValue2), nextPos)
if err != nil {
log.Fatalf("Failed to append second value: %v", err)
}
log.Println("Append uploads completed successfully.")
}
FAQ
References
For the complete sample code for append uploads, see the GitHub example.
For more information about the AppendObject API operation, see AppendObject.