By default, if you upload an object that has the same name of an existing object, the existing object is overwritten by the uploaded object. This topic describes how to set the x-oss-forbid-overwrite request header to disable overwrite for objects with the same name when you copy objects or use the simple upload and multipart upload methods.

Simple upload

The following code provides an example on how to disable overwrite for an object with the same name when you use simple upload:

package main

import (
    "fmt"
    "os"
    "strings"

    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
    // Create an OSSClient instance.
    client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

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

    // Specify whether to overwrite the object with the same name.
    // By default, if oss.ForbidOverWrite is not specified, the object with the same name is overwritten.
    // If oss.ForbidOverWrite is set to false, the object with the same name is overwritten.
    // If oss.ForbidOverWrite is set to true, the object with the same name is not overwritten. If an object with the same name already exists, an error is reported.
    forbidWrite := oss.ForbidOverWrite(true)

    // Upload a string.
    err = bucket.PutObject("<yourObjectName>", strings.NewReader("yourObjectValue"), forbidWrite)
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
}

For more information about simple upload, see PutObject.

Object copy

The following code provides an example on how to disable overwrite for the object with the same name when you copy an object:

For more information about how to copy an object, see CopyObject.

func main() {
    // Create an OSSClient instance.
    client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    bucketName := "<yourBucketName>"
    objectName := "<yourObjectName>"
    objectName := "<yourObjectName>"

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

    // Specify whether to overwrite the target object with the same name.
    // By default, if oss.ForbidOverWrite is not specified, the object with the same name is overwritten.
    // If oss.ForbidOverWrite is set to false, the object with the same name is overwritten.
    // If oss.ForbidOverWrite is set to true, the object with the same name is not overwritten. If an object with the same name already exists, an error is reported.
    forbidWrite := oss.ForbidOverWrite(true)

    // Copy the object to another object in the same bucket.
    _, err = bucket.CopyObject(objectName, destObjectName, forbidWrite)
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
}

Multipart upload

The following code provides an example on how to disable overwrite for the object with the same name when you use multipart upload:

func main() {
    // Create an OSSClient instance.
    client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    bucketName := "<yourBucketName>"
    objectName := "<yourObjectName>"
    String localFileName = "<yourLocalFileName>"

    // Specify the bucket name.
    bucket, err := client.Bucket(bucketName)
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    chunks, err := oss.SplitFileByPartNum("localFile", 3)
    fd1, err := os.Open(localFileOne)
    defer fd.Close()

    // Specify whether to overwrite the object with the same name.
    // By default, if oss.ForbidOverWrite is not specified, the object with the same name is overwritten.
    // If oss.ForbidOverWrite is set to false, the object with the same name is overwritten.
    // If oss.ForbidOverWrite is set to true, the object with the same name is not overwritten. If an object with the same name already exists, an error is reported.
    forbidWrite := oss.ForbidOverWrite(true)

    // Step 1: Initiate a multipart part upload task.
    imur, err := bucket.InitiateMultipartUpload(objectName, forbidWrite)
    // Step 2: Upload the parts.
    var parts []oss.UploadPart
    for _, chunk := range chunks {
        fd.Seek(chunk.Offset, os.SEEK_SET)
        // Call the UploadPart method to upload each part.
        part, err := bucket.UploadPart(imur, fd, chunk.Size, chunk.Number)
        if err ! = nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
        }
        parts = append(parts, part)
    }

    // Step 3: Complete the multipart upload task. Disable overwrite for the object with the same name.
    cmur, err := bucket.CompleteMultipartUpload(imur, parts, forbidWrite)
    if err ! = nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    fmt.Println("cmur:", cmur)
}
			

For more information about multipart upload, see InitiateMultipartUpload and CompleteMultipartUpload.