All Products
Search
Document Center

Object Storage Service:Append upload

Last Updated:Oct 08, 2023

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, 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 Initialization.

  • To use append upload, you must have the oss:PutObject permission. For more information, see Common examples of RAM policies.

  • 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 data 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() {
    /// 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 configured. 
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // 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. 
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    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 it is downloaded. 
        //oss.ContentDisposition("attachment;filename=FileName.txt"),
        // Specify the content encoding format of the object. 
        //oss.ContentEncoding("gzip"),
        // Specify the storage class of the object. 
        //oss.ObjectStorageClass(oss.StorageStandard),
        // Specify the 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 the complete sample code that is used to perform append upload, visit GitHub.

  • For more information about the API operation for append upload, see AppendObject.