Delete objects (Go SDK V2)

Updated at:
Copy as MD

Use the OSS SDK for Go to delete one or more objects from a bucket.

Notes

  • The sample code uses thecn-hangzhou region ID for China (Hangzhou) and a public endpoint by default. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about supported regions and endpoints, see Regions and endpoints.

  • The examples obtain access credentials from environment variables. For more information, see Configure access credentials.

  • To delete an object, you must have the oss:DeleteObject permission. For more information, see Grant custom permission policies to RAM users.

Method signatures

Delete a single object

func (c *Client) DeleteObject(ctx context.Context, request *DeleteObjectRequest, optFns ...func(*Options)) (*DeleteObjectResult, error)

Delete multiple objects

func (c *Client) DeleteMultipleObjects(ctx context.Context, request *DeleteMultipleObjectsRequest, optFns ...func(*Options)) (*DeleteMultipleObjectsResult, error)

Request parameters

Parameter

Type

Description

ctx

context.Context

The request context, which you can use to set a request timeout.

request

*DeleteObjectRequest

The request parameters, such as the object name. For more information, see DeleteObjectRequest.

*DeleteMultipleObjectsRequest

The request parameters, such as the list of objects to delete. For more information, see DeleteMultipleObjectsRequest.

optFns

...func(*Options)

(Optional) Operation-level configuration parameters. For more information, see Options.

Response parameters

Parameter

Type

Description

result

*DeleteObjectResult

The operation result. Valid only when err is nil. For more information, see DeleteObjectResult.

*DeleteMultipleObjectsResult

The operation result. Valid only when err is nil. For more information, see DeleteMultipleObjectsResult.

err

error

The request status. If the request fails, err is not nil.

Examples

Delete a single object

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Define global variables.
var (
	region     string // The bucket's region.
	bucketName string // The name of the bucket.
	objectName string // The name of the object.
)

// The init function initializes command-line arguments.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
	flag.StringVar(&objectName, "object", "", "The name of the object.")
}

func main() {
	// Parse command-line arguments.
	flag.Parse()

	// Check if the bucket name is empty.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check if the region is empty.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Check if the object name is empty.
	if len(objectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, object name required")
	}

	// Load the default configuration and set the credentials provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to delete the object.
	request := &oss.DeleteObjectRequest{
		Bucket: oss.Ptr(bucketName), // The bucket name.
		Key:    oss.Ptr(objectName), // The object name.
	}

	// Delete the object and process the result.
	result, err := client.DeleteObject(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to delete object %v", err)
	}

	// Print the result.
	log.Printf("delete object result:%#v\n", result)
}

Delete multiple objects

package main

import (
	"context"
	"flag"
	"log"
	"strings"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Define global variables.
var (
	region     string // The region where the bucket is located.
	bucketName string // The name of the bucket.
	objects    string // The object names (comma-separated).
)

// The init function initializes command-line arguments.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
	flag.StringVar(&objects, "objects", "", "The name of the objects (comma-separated).")
}

func main() {
	// Parse command-line arguments.
	flag.Parse()

	// Check if the bucket name is empty.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check if the region is empty.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Check if the list of object names is empty.
	if len(objects) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, objects name required")
	}

	// Load the default configuration and set the credentials provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Convert the comma-separated string of object names to a slice.
	var DeleteObjects []oss.DeleteObject
	objectSlice := strings.Split(objects, ",")
	for _, name := range objectSlice {
		DeleteObjects = append(DeleteObjects, oss.DeleteObject{Key: oss.Ptr(strings.TrimSpace(name))})
	}

	// Create a request to delete multiple objects.
	request := &oss.DeleteMultipleObjectsRequest{
		Bucket:  oss.Ptr(bucketName), // The bucket name.
		Delete: &oss.Delete{
			Objects: DeleteObjects, // The list of objects to delete.
		},
	}

	// Delete the objects and process the result.
	result, err := client.DeleteMultipleObjects(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to delete multiple objects %v", err)
	}

	// Print the result.
	log.Printf("delete multiple objects result:%#v\n", result)
}

References