You can call the AppendObject operation to append content to the existing append objects.

Usage notes

  • If the object to which you want to append content does not exist, an append object is created when you call the AppendObject operation.
  • If the object to which you want to append content exists, the following scenarios may occur when you call the AppendObject operation: If the object is an append object, and the specified position from which the append operation starts is equal to the current object length, the content is appended to the end of the object. If the object is an append object, and the specified position from which the append operation starts is not equal to the current object length, a PositionNotEqualToLength exception is thrown. If the object is not an append object, an ObjectNotAppendable exception is thrown.
  • The CopyObject operation cannot be performed on append objects.

Sample code

The following code provides an example on how to perform append upload:

package main

import (
    "fmt"
    "os"
    "strings"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
    // Create an OSSClient instance. 
    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access Object Storage Service (OSS) because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Specify the name of the bucket. Example: examplebucket. 
    bucket, err := client.Bucket("examplebucket")
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Specify the full path of the object. The full path of the object cannot contain the bucket name. Example: exampledir/exampleobject.txt. 
    objectName := "exampledir/exampleobject.txt"
    var nextPos int64 = 0
    // If the object is appended for the first time, the append position is 0. The position for the next append operation is included in the response. The position from which the next append operation starts is the current length of the object.     
    // Specify the expiration time. 
    expires := time.Date("2021, time.December, 10, 23, 0, 0, 0, time.UTC")
    option := []oss.Option{
        oss.Expires(expires),
        // Specify the caching behavior of the web page when the object is downloaded. 
        //oss.CacheControl("no-cache"),
        // Specify the name of the object when the object is downloaded. 
        //oss.ContentDisposition("attachment;filename=FileName.txt"),
        // Specify the encoding format for the content of the object. 
        //oss.ContentEncoding("gzip"),
        // Specify the storage class of the object. 
        //oss.ObjectStorageClass(oss.StorageStandard),
        // Specify the access control list (ACL) of the object. 
        //oss.ObjectACL(oss.ACLPrivate),
        // Specify the server-side encryption method. 
        //oss.ServerSideEncryption("AES256"),
        // You can add parameters whose names are prefixed with x-oss-meta- when you call the AppendObject operation to create an append object. These parameters cannot be included in the requests when you append content to an existing append object. Parameters whose names are prefixed with x-oss-meta- are considered the metadata of the object. 
        //oss.Meta("x-oss-meta-author", "Alice"),
    }


    nextPos, err = bucket.AppendObject(objectName, strings.NewReader("YourObjectAppendValue1"), nextPos,option...)
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // If you have appended content to the object, you can obtain the position from which the current append operation starts from the X-Oss-Next-Append-Position field in the response returned by the last operation or by using the bucket.GetObjectDetailedMeta method. 
    //props, err := bucket.GetObjectDetailedMeta("objectName")
    //if err != nil {
    //    fmt.Println("Error:", err)
    //    os.Exit(-1)
    //}
    //nextPos, err = strconv.ParseInt(props.Get("X-Oss-Next-Append-Position"), 10, 64)
    //if err != nil {
    //    fmt.Println("Error:", err)
    //    os.Exit(-1)
    //}

    // Perform the second append operation. 
    nextPos, err = bucket.AppendObject(objectName, strings.NewReader("YourObjectAppendValue2"), nextPos)
    if err != nil {
        fmt.Println("Error:", err,"aaa")
        os.Exit(-1)
    }

    // You can perform append operations on an object for multiple times. 
}

References

  • For more information about the complete sample code of append upload, visit GitHub.
  • For more information about the API operation that you can call to perform append upload, see AppendObject.