By default, if you upload an object that has the same name as an existing object, the existing object is overwritten by the uploaded object. This topic describes how to configure the x-oss-forbid-overwrite request header to prevent objects from being overwritten by objects with the same names when you copy objects or perform simple upload or multipart upload in Object Storage Service (OSS).
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.
Prevent overwrites in a simple upload task
The following sample code provides an example on how to prevent objects from being overwritten by objects that have the same names in simple 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.
bucket, err := client.Bucket("yourBucketName")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Specify whether to overwrite an existing object with the same name.
// By default, if you do not specify oss.ForbidOverWrite, an object that has the same name is overwritten.
// If you set oss.ForbidOverWrite to false, an object that has the same name is overwritten.
// If you set oss.ForbidOverWrite to true, an object that has the same name is not overwritten. If such an object exists, an error is reported.
forbidWrite := oss.ForbidOverWrite(true)
// Upload the string.
// Specify the full path of the object. Do not include the bucket name in the full path.
err = bucket.PutObject("yourObjectName", strings.NewReader("yourObjectValue"), forbidWrite)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
Prevent overwrites in an object copy task
The following sample code provides an example on how to prevent existing objects from being overwritten by newly copied objects that have the same names:
package main
import (
"fmt"
"os"
"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.
bucketName := "yourBucketName"
// Specify the full path of the source object. Do not include the bucket name in the full path.
objectName := "yourObjectName"
// Specify the full path of the destination object. Do not include the bucket name in the full path.
destObjectName := "yourDestObjectName"
bucket, err := client.Bucket(bucketName)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Specify whether to overwrite an object that has the same name as the destination object.
// By default, if you do not specify oss.ForbidOverWrite, an object that has the same name is overwritten.
// If you set oss.ForbidOverWrite to false, an object that has the same name is overwritten.
// If you set oss.ForbidOverWrite to true, an object that has the same name is not overwritten. If such an object exists, an error is reported.
forbidWrite := oss.ForbidOverWrite(true)
// Copy the object to another object within the bucket.
_, err = bucket.CopyObject(objectName, destObjectName, forbidWrite)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
Prevent overwrites in a multipart upload task
The following sample code provides an example on how to prevent objects from being overwritten when you use multipart upload to upload objects with the same names:
package main
import (
"fmt"
"os"
"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.
bucketName := "yourBucketName"
// Specify the full path of the object. Do not include the bucket name in the full path.
objectName := "yourObjectName"
// Specify the full path of the local file.
locaFilename := "yourLocalFilename"
bucket, err := client.Bucket(bucketName)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
chunks, err := oss.SplitFileByPartNum(locaFilename, 3)
fd, err := os.Open(locaFilename)
defer fd.Close()
// Specify whether to overwrite an existing object with the same name.
// By default, if you do not specify oss.ForbidOverWrite, an object that has the same name is overwritten.
// If you set oss.ForbidOverWrite to false, an object that has the same name is overwritten.
// If you set oss.ForbidOverWrite to true, an object that has the same name is not overwritten. If such an object exists, an error is reported.
forbidWrite := oss.ForbidOverWrite(true)
// Step 1: Initiate a multipart 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 overwrites between objects that have the same names.
cmur, err := bucket.CompleteMultipartUpload(imur, parts, forbidWrite)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println("cmur:", cmur)
}
References
For more information about the API operation that you can call to perform simple upload, see PutObject.
For more information about the API operation that you can call to copy an object, see CopyObject.
For more information about the API operations that you can call to perform multipart upload, see InitiateMultipartUpload and CompleteMultipartUpload.