-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lilang
committed
May 12, 2023
1 parent
d9744e7
commit 7bdcb1d
Showing
9 changed files
with
966 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
|
||
"github.com/tencentyun/cos-go-sdk-v5" | ||
"github.com/tencentyun/cos-go-sdk-v5/debug" | ||
) | ||
|
||
func log_status(err error) { | ||
if err == nil { | ||
return | ||
} | ||
if cos.IsNotFoundError(err) { | ||
// WARN | ||
fmt.Println("WARN: Resource is not existed") | ||
} else if e, ok := cos.IsCOSError(err); ok { | ||
fmt.Printf("ERROR: Code: %v\n", e.Code) | ||
fmt.Printf("ERROR: Message: %v\n", e.Message) | ||
fmt.Printf("ERROR: Resource: %v\n", e.Resource) | ||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID) | ||
// ERROR | ||
} else { | ||
fmt.Printf("ERROR: %v\n", err) | ||
// ERROR | ||
} | ||
} | ||
|
||
// cropImageWhenDownload 图像裁剪, 下载时处理 | ||
func cropImageWhenDownload() { | ||
u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com") | ||
b := &cos.BaseURL{BucketURL: u} | ||
c := cos.NewClient(b, &http.Client{ | ||
Transport: &cos.AuthorizationTransport{ | ||
SecretID: os.Getenv("COS_SECRETID"), | ||
SecretKey: os.Getenv("COS_SECRETKEY"), | ||
Transport: &debug.DebugRequestTransport{ | ||
RequestHeader: true, | ||
// Notice when put a large file and set need the request body, might happend out of memory error. | ||
RequestBody: false, | ||
ResponseHeader: true, | ||
ResponseBody: false, | ||
}, | ||
}, | ||
}) | ||
obj := "pic/walk.jpeg" | ||
localPath := "test.jpeg" | ||
opt := &cos.AIImageCropOptions{ | ||
Width: 128, | ||
Height: 96, | ||
} | ||
resp, err := c.CI.GetAIImageCrop(context.Background(), obj, opt) | ||
log_status(err) | ||
if err == nil { | ||
fd, _ := os.OpenFile(localPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660) | ||
io.Copy(fd, resp.Body) | ||
fd.Close() | ||
} | ||
} | ||
|
||
// 图像裁剪, 上传时处理 | ||
func cropImageWhenUpload() { | ||
u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com") | ||
b := &cos.BaseURL{BucketURL: u} | ||
c := cos.NewClient(b, &http.Client{ | ||
Transport: &cos.AuthorizationTransport{ | ||
SecretID: os.Getenv("COS_SECRETID"), | ||
SecretKey: os.Getenv("COS_SECRETKEY"), | ||
Transport: &debug.DebugRequestTransport{ | ||
RequestHeader: true, | ||
// Notice when put a large file and set need the request body, might happend out of memory error. | ||
RequestBody: false, | ||
ResponseHeader: true, | ||
ResponseBody: false, | ||
}, | ||
}, | ||
}) | ||
opt := &cos.ObjectPutOptions{ | ||
nil, | ||
&cos.ObjectPutHeaderOptions{ | ||
XOptionHeader: &http.Header{}, | ||
}, | ||
} | ||
pic := &cos.PicOperations{ | ||
IsPicInfo: 1, | ||
Rules: []cos.PicOperationsRules{ | ||
{ | ||
FileId: "crop.jpeg", | ||
Rule: "ci-process=AIImageCrop&width=200&height=150", | ||
}, | ||
}, | ||
} | ||
opt.XOptionHeader.Add("Pic-Operations", cos.EncodePicOperations(pic)) | ||
name := "pic/crop/walk.jpeg" | ||
local_filename := "./walk.jpeg" | ||
res, _, err := c.CI.PutFromFile(context.Background(), name, local_filename, opt) | ||
log_status(err) | ||
fmt.Printf("%+v\n", res) | ||
fmt.Printf("%+v\n", res.OriginalInfo) | ||
fmt.Printf("%+v\n", res.ProcessResults) | ||
} | ||
|
||
// 图像裁剪, 云上处理 | ||
func cropImageWhenCloud() { | ||
u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com") | ||
b := &cos.BaseURL{BucketURL: u} | ||
c := cos.NewClient(b, &http.Client{ | ||
Transport: &cos.AuthorizationTransport{ | ||
SecretID: os.Getenv("COS_SECRETID"), | ||
SecretKey: os.Getenv("COS_SECRETKEY"), | ||
Transport: &debug.DebugRequestTransport{ | ||
RequestHeader: true, | ||
// Notice when put a large file and set need the request body, might happend out of memory error. | ||
RequestBody: false, | ||
ResponseHeader: true, | ||
ResponseBody: true, | ||
}, | ||
}, | ||
}) | ||
|
||
pic := &cos.PicOperations{ | ||
IsPicInfo: 1, | ||
Rules: []cos.PicOperationsRules{ | ||
{ | ||
FileId: "crop.jpeg", | ||
Rule: "ci-process=AIImageCrop&width=200&height=150", | ||
}, | ||
}, | ||
} | ||
|
||
key := "pic/walk.jpeg" | ||
res, _, err := c.CI.ImageProcess(context.Background(), key, pic) | ||
log_status(err) | ||
fmt.Printf("%+v\n", res) | ||
} | ||
|
||
func main() { | ||
cropImageWhenDownload() | ||
// cropImageWhenUpload() | ||
// cropImageWhenCloud() | ||
} |
Oops, something went wrong.