Skip to content

Latest commit

 

History

History
59 lines (47 loc) · 1.91 KB

delete_multiple_object.md

File metadata and controls

59 lines (47 loc) · 1.91 KB

DeleteMultipleObjects Example

Code Snippet

Initialize the Qingstor object with your AccessKeyID and SecretAccessKey.

import (
	"github.com/qingstor/qingstor-sdk-go/v4/config"
	"github.com/qingstor/qingstor-sdk-go/v4/service"
)

var conf, _ = config.New("YOUR-ACCESS-KEY-ID", "YOUR--SECRET-ACCESS-KEY")
var qingStor, _ = service.Init(conf)

Initialize a Bucket object according to the bucket name you set for subsequent creation:

bucketName := "your-bucket-name"
zoneName := "pek3b"
bucketService, _ := qingStor.Bucket(bucketName, zoneName)

Then set the input parameters used by the DeleteObject method (stored in DeleteMultipleObjectsInput). Quiet Specifies whether to return a list of deleted objects.

	objects := []string{"file_will_be_delete.jpg", "file_will_be_delete.zip"}
	var keys []*service.KeyType
	for _, objKey := range objects {
		key := objKey
		keys = append(keys, &service.KeyType{
			Key: &key,
		})
	}
	returnDeleteRes := false
	input := &service.DeleteMultipleObjectsInput{
		Objects: keys,
		Quiet:   &returnDeleteRes,
	}

Please note that not all fields in DeleteMultipleObjectsInput required to be set. For details, please refer to Official API Documentation.

Then call the DeleteMultipleObjects method to delete the object. objectKey Sets the filepath of the object to be deleted (in the current bucket).

	if output, err := bucketService.DeleteMultipleObjects(input); err != nil {
		fmt.Printf("Delete objects(name: %v) failed with given error: %s\n", objects, err)
	} else {
		fmt.Printf("The status code expected: 200(actually: %d)\n", *output.StatusCode)
		fmt.Println("=========== objects been deleted ===========")
		for _, keyType := range output.Deleted {
			fmt.Println(*keyType.Key)
		}
	}

If the operation returns correctly, the response status code will be 200.