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

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint For more information about the regions and endpoints supported by OSS, see Regions and endpoints.
  • 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 STS, see Initialization.
  • The oss:PutObject permission is required to use append upload. For more information, see Attach a custom policy to a RAM user.
  • If the object to which you want to append content does not exist, an appendable object is created when you call the AppendObject operation.
  • If the object to which you want to append content exists:
    • If the object is an appendable object and the specified position from which the append operation starts is equal to the current object size, the object is appended to the end of the object.
    • If the object is an appendable object and the specified position from which the append operation starts is not equal to the current object size, the PositionNotEqualToLength error is returned.
    • If the object is not an appendable object, the ObjectNotAppendable error is returned.
  • The CopyObject operation cannot be performed on appendable objects.

Sample code

The following sample 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. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in Object Storage Service (OSS) is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. 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. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
    objectName := "exampledir/exampleobject.txt"
    var nextPos int64 = 0
    // If the object is appended for the first time, the position from which the append operation starts 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 of the request. 
    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"),
        // When you call the AppendObject operation to create an appendable object, you can add parameters whose names are prefixed with x-oss-meta-*. These parameters cannot be included in the request in which you append content to an existing appendable 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 by using one of the following methods: X-Oss-Next-Append-Position in the response returned by the last append operation and bucket.GetObjectDetailedMeta. 
    //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 that is used to perform append upload, visit GitHub.
  • For more information about the API operation that you can call to perform append upload, see AppendObject.