This document provides an overview of APIs and SDK code samples related to object deletion.
API | Operation | Description |
---|---|---|
DELETE Object | Deleting an object | Deletes an object from a bucket. |
DELETE Multiple Objects | Deleting multiple objects | Deletes multiple objects from a bucket. |
This API is used to delete an object (folder) from a bucket.
func (s *ObjectService) Delete(ctx context.Context, key string) (*Response, error)
package main
import (
"context"
"github.com/tencentyun/cos-go-sdk-v5"
"net/http"
"net/url"
"os"
)
func main() {
// Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket
// Replace it with your region, which can be viewed in the COS console at https://console.cloud.tencent.com/. For more information about regions, see https://intl.cloud.tencent.com/document/product/436/6224.
u, _ := url.Parse("https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
client := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
// Get the key from environment variables
// Environment variable `SECRETID` refers to the user's SecretId, which can be viewed at https://console.cloud.tencent.com/cam/capi
SecretID: os.Getenv("SECRETID"),
// Environment variable `SECRETKEY` refers to the user's SecretKey, which can be viewed at https://console.cloud.tencent.com/cam/capi
SecretKey: os.Getenv("SECRETKEY"),
},
})
key := "exampleobject"
_, err := client.Object.Delete(context.Background(), key)
if err != nil {
panic(err)
}
}
This request does not delete objects in the folder but only the specified key.
package main
import (
"context"
"github.com/tencentyun/cos-go-sdk-v5"
"net/http"
"net/url"
"os"
)
func main() {
// Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket
// Replace it with your region, which can be viewed in the COS console at https://console.cloud.tencent.com/. For more information about regions, see https://intl.cloud.tencent.com/document/product/436/6224.
u, _ := url.Parse("https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
client := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
// Get the key from environment variables
// Environment variable `SECRETID` refers to the user's SecretId, which can be viewed at https://console.cloud.tencent.com/cam/capi
SecretID: os.Getenv("SECRETID"),
// Environment variable `SECRETKEY` refers to the user's SecretKey, which can be viewed at https://console.cloud.tencent.com/cam/capi
SecretKey: os.Getenv("SECRETKEY"),
},
})
key := "folder/"
_, err := client.Object.Delete(context.Background(), key)
if err != nil {
panic(err)
}
}
Parameter | Description | Type | Required |
---|---|---|---|
key | Object key, the unique identifier of an object in a bucket. For example, if the object endpoint is examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg , its object key is doc/pic.jpg . |
String | Yes |
This API is used to delete multiple objects from a bucket. You can delete up to 1,000 objects in one request.
func (s *ObjectService) DeleteMulti(ctx context.Context, opt *ObjectDeleteMultiOptions) (*ObjectDeleteMultiResult, *Response, error)
package main
import (
"context"
"github.com/tencentyun/cos-go-sdk-v5"
"net/http"
"net/url"
"os"
)
func main() {
// Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket
// Replace it with your region, which can be viewed in the COS console at https://console.cloud.tencent.com/. For more information about regions, see https://intl.cloud.tencent.com/document/product/436/6224.
u, _ := url.Parse("https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
client := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
// Get the key from environment variables
// Environment variable `SECRETID` refers to the user's SecretId, which can be viewed at https://console.cloud.tencent.com/cam/capi
SecretID: os.Getenv("SECRETID"),
// Environment variable `SECRETKEY` refers to the user's SecretKey, which can be viewed at https://console.cloud.tencent.com/cam/capi
SecretKey: os.Getenv("SECRETKEY"),
},
})
var objects []string
objects = append(objects, []string{"a", "b", "c"}...)
obs := []cos.Object{}
for _, v := range objects {
obs = append(obs, cos.Object{Key: v})
}
opt := &cos.ObjectDeleteMultiOptions{
Objects: obs,
// Boolean, which indicates whether to enable the Quiet or Verbose mode.
// `true` indicates the Quiet mode and `false` (default) indicates the Verbose mode.
// Quiet: true,
}
_, _, err := client.Object.DeleteMulti(context.Background(), opt)
if err != nil {
panic(err)
}
}
COS uses slashes (/) to separate object paths to simulate the effect of a file system. Therefore, deleting a folder in COS means deleting all objects that have a specified prefix. For example, the folder 'prefix/' contains all objects prefixed with 'prefix/'. In other words, you can delete all objects prefixed with 'prefix/' to delete the 'prefix/' folder.
Currently, the COS Go SDK does not provide an API to perform this operation. However, you can still use a combination of basic operations to do so.
package main
import (
"context"
"github.com/tencentyun/cos-go-sdk-v5"
"net/http"
"net/url"
"os"
)
func main() {
// Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket
// Replace it with your region, which can be viewed in the COS console at https://console.cloud.tencent.com/. For more information about regions, see https://intl.cloud.tencent.com/document/product/436/6224.
u, _ := url.Parse("https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
client := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
// Get the key from environment variables
// Environment variable `SECRETID` refers to the user's SecretId, which can be viewed at https://console.cloud.tencent.com/cam/capi
SecretID: os.Getenv("SECRETID"),
// Environment variable `SECRETKEY` refers to the user's SecretKey, which can be viewed at https://console.cloud.tencent.com/cam/capi
SecretKey: os.Getenv("SECRETKEY"),
},
})
dir := "exampledir/"
var marker string
opt := &cos.BucketGetOptions{
Prefix: dir,
MaxKeys: 1000,
}
isTruncated := true
for isTruncated {
opt.Marker = marker
v, _, err := client.Bucket.Get(context.Background(), opt)
if err != nil {
// Error
break
}
for _, content := range v.Contents {
_, err = client.Object.Delete(context.Background(), content.Key)
if err != nil {
// Error
}
}
isTruncated = v.IsTruncated
marker = v.NextMarker
}
}
type ObjectDeleteMultiOptions struct {
Quiet bool
Objects []Object
}
// “Object” stores object metadata
type Object struct {
Key string
// Other parameters are not related to this API
}
Parameter | Description | Type | Required |
---|---|---|---|
Quiet | Indicates whether to enable the Quiet mode for the response result. Valid values:true : enables the Quiet mode that only returns information on objects that failfalse (default): enables the Verbose mode that returns the deletion result for each object |
Boolean | No |
Objects | Describes each destination object to be deleted | Container | Yes |
Key | Name of the destination object | String | Yes |
Attributes of the uploaded file:
// “ObjectDeleteMultiResult” saves the DeleteMulti result
type ObjectDeleteMultiResult struct {
DeletedObjects []Object
Errors []struct {
Key string
Code string
Message string
}
}
Parameter | Description | Type |
---|---|---|
DeletedObjects | Describes each object deleted | Container |
Errors | Describes objects that failed to be deleted | Container |
Key | Names of the objects that failed to be deleted | String |
Code | Error code for the deletion failure | String |
Message | Error message for the deletion failure | String |
Was this page helpful?