golang Tencent cloud cos copy and delete

Install golang version sdk

go get -u github.com/tencentyun/cos-go-sdk-v5

The current bucket is xxxx , and the area is ap-beijing

According to two examples

copy

For example, if you want to copy /temp/a.png to /app/a.png :
the received two file path parameters are as follows:

source : xxxx.cos.ap-beijing.myqcloud.com/temp/a.png
target : app/a.png

delete

For example, if you want to delete /app/a.png :
the received two file path parameters are as follows:

target : app/a.png

Usage

Generally speaking, a complete address is obtained when using it, and copy and delete are performed according to the complete address, and two simple usage examples are written.

func CosCopyFile(source string) string {
    
    
 base := "https://xxxx.cos.ap-beijing.myqcloud.com/"
 u, _ := url.Parse(base)
 b := &cos.BaseURL{
    
    BucketURL: u}
 client := cos.NewClient(b, &http.Client{
    
    
  Transport: &cos.AuthorizationTransport{
    
    
   SecretID:  "id",
   SecretKey: "secret",
  },
 })

 var target string
 target = strings.Replace(source, base, "", -1)
 source = strings.Replace(source, "https://", "", -1)
 target = strings.Replace(target, "temp", "app", -1)
 // 移动对象
 fmt.Printf("source is %+v\n", source)
 fmt.Printf("target is %+v\n", target)
 _, _, err := client.Object.Copy(context.Background(), target, source, nil)
 if err == nil {
    
    
  return base + target
 }
 fmt.Printf("err is %+v\n", err)
 return ""
}

func CosDeleteFile(target string) {
    
    
 base := "https://xxxx.cos.ap-beijing.myqcloud.com/"
 u, _ := url.Parse("https://xxxx.cos.ap-beijing.myqcloud.com/")
 b := &cos.BaseURL{
    
    BucketURL: u}
 client := cos.NewClient(b, &http.Client{
    
    
  Transport: &cos.AuthorizationTransport{
    
    
   SecretID:  "id",
   SecretKey: "secret",
  },
 })
 target = strings.Replace(target, base, "", -1)

 fmt.Printf("delete file %+v", target)
 // 删除对象
 _, err := client.Object.Delete(context.Background(), target)
 if err != nil {
    
    
  fmt.Printf("delete error %+v", err)
 }
}

Example of use:

CosCopyFile("https://xxxx.cos.ap-beijing.myqcloud.com/temp/a.png")
CosDeleteFile("https://xxxx.cos.ap-beijing.myqcloud.com/app/a.png")

Guess you like

Origin blog.csdn.net/xo19882011/article/details/131631263