本文整理匯總了Golang中github.com/forhappy/cos-go-sdk/http.Request類的典型用法代碼示例。如果您正苦於以下問題:Golang Request類的具體用法?Golang Request怎麽用?Golang Request使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Request類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: StatFolder
// 查詢目錄屬性
// bucket: Bucket 名稱
// path: 目錄路徑
//
// 示例:
//
// client := cos.NewClient(appId, secretId, secretKey)
//
// res, err := client.StatFolder("cosdemo", "/hello")
// if err != nil {
// fmt.Println(err)
// return
// }
//
// fmt.Println("Code:", res.Code,
// "\nMessage:", res.Message,
// "\nName:", res.Data.Name,
// "\nBizAttr:", res.Data.BizAttr,
// "\nCtime:", res.Data.Ctime,
// "\nMtime:", res.Data.Mtime)
//
func (c *Client) StatFolder(bucket, path string) (*StatFolderResponse, error) {
var cosRequest StatFolderRequest
var cosResponse StatFolderResponse
encodedPath := utils.UrlEncode(c.validateFolderPath(path))
resource := c.generateResourceUrl(bucket, encodedPath)
now := time.Now().Unix()
strNow := fmt.Sprintf("%d", now)
strExpires := fmt.Sprintf("%d", now+EXPIRES)
strRand := fmt.Sprintf("%d", rand.Int31())
signer := auth.NewSignature(c.AppId, bucket, c.SecretId, strExpires, strNow, strRand, "")
sign := signer.Sign(c.SecretKey)
cosRequest.Op = "stat"
httpRequest := http.Request{
Method: "GET",
Uri: resource,
Timeout: c.Timeout,
QueryString: cosRequest.Request,
}
httpRequest.AddHeader("Authorization", sign)
httpResponse, err := httpRequest.Do()
if err != nil {
return nil, err
}
err = httpResponse.Body.Unmarshal(&cosResponse)
if err != nil {
return nil, err
}
return &cosResponse, nil
}
示例2: PrepareToUploadSlice
// 大文件分片上傳(首次上傳)
// bucket: Bucket 名稱
// dstpath: 目的文件路徑, 須設置為騰訊雲端文件路徑
// srcPath: 本地文件路徑
// bizAttr: 文件屬性, 由業務端維護
// session: 唯一標識此文件傳輸過程的id, 由後台下發, 調用方透傳
// sliceSize: 分片大小, 用戶可以根據網絡狀況自行設置
//
// 示例:
// client := cos.NewClient(appId, secretId, secretKey)
//
// res, err := client.PrepareToUploadSlice("cosdemo", "/hello/hello.bin", "/Users/bigfile.bin", "file attr", "", 512 * 1024)
// if err != nil {
// fmt.Println(err)
// return
// }
//
// fmt.Println("Code:", res.Code,
// "\nMessage:", res.Message,
// "\nSession:", res.Session,
// "\nOffset:", res.Offset,
// "\nSliceSize:", res.SliceSize,
// "\nUrl:", res.Data.Url,
// "\nResourcePath:", res.Data.ResourcePath,
// "\nAccess Url:", res.Data.AccessUrl)
//
func (c *Client) PrepareToUploadSlice(bucket, dstPath, srcPath, bizAttr, session string, sliceSize int64) (*UploadSliceResponse, error) {
var cosRequest PrepareToUploadSliceRequest
var cosResponse UploadSliceResponse
sha, size, err := utils.HashFileWithSha1(srcPath)
if err != nil {
return nil, err
}
encodedPath := utils.UrlEncode(c.validateFilePath(dstPath))
resource := c.generateResourceUrl(bucket, encodedPath)
now := time.Now().Unix()
strNow := fmt.Sprintf("%d", now)
strExpires := fmt.Sprintf("%d", now+EXPIRES)
strRand := fmt.Sprintf("%d", rand.Int31())
signer := auth.NewSignature(c.AppId, bucket, c.SecretId, strExpires, strNow, strRand, "")
sign := signer.Sign(c.SecretKey)
cosRequest.Op = "upload_slice"
cosRequest.FileSize = size
cosRequest.Sha = sha
cosRequest.BizAttr = bizAttr
cosRequest.Session = session
cosRequest.SliceSize = sliceSize
httpRequest := http.Request{
Method: "POST",
Uri: resource,
Timeout: c.Timeout,
Body: cosRequest,
Multipart: true,
}
httpRequest.AddHeader("Authorization", sign)
httpResponse, err := httpRequest.Do()
if err != nil {
return nil, err
}
err = httpResponse.Body.Unmarshal(&cosResponse)
if err != nil {
return nil, err
}
return &cosResponse, nil
}
示例3: ContinueUploadingSliceData
// 大文件分片上傳(後續上傳)
// bucket: Bucket 名稱
// dstpath: 目的文件路徑, 須設置為騰訊雲端文件路徑
// dataSlice: 本地文件路徑
// session: 唯一標識此文件傳輸過程的id, 由後台下發, 調用方透傳
// offset: 本次分片位移
//
// 示例:
// client := cos.NewClient(appId, secretId, secretKey)
//
// res, err := client.ContinueUploadingSliceData("cosdemo", "/hello/bigfile.bin", []byte("data.bin"), "c0bd94d0-3956-4664-b99f-2658eef8e5f5+CpcFEtEHAA==", 1045248)
// if err != nil {
// fmt.Println(err)
// return
// }
//
// fmt.Println("Code:", res.Code,
// "\nMessage:", res.Message,
// "\nSession:", res.Session,
// "\nOffset:", res.Offset,
// "\nUrl:", res.Data.Url,
// "\nResourcePath:", res.Data.ResourcePath,
// "\nAccess Url:", res.Data.AccessUrl)
//
func (c *Client) ContinueUploadingSliceData(bucket, dstPath string, dataSlice []byte, session string, offset int64) (*UploadSliceResponse, error) {
var cosRequest UploadSliceRequest
var cosResponse UploadSliceResponse
sha := utils.HashBufferWithSha1(dataSlice)
encodedPath := utils.UrlEncode(c.validateFilePath(dstPath))
resource := c.generateResourceUrl(bucket, encodedPath)
now := time.Now().Unix()
strNow := fmt.Sprintf("%d", now)
strExpires := fmt.Sprintf("%d", now+EXPIRES)
strRand := fmt.Sprintf("%d", rand.Int31())
signer := auth.NewSignature(c.AppId, bucket, c.SecretId, strExpires, strNow, strRand, "")
sign := signer.Sign(c.SecretKey)
cosRequest.Op = "upload_slice"
cosRequest.FileContent = string(dataSlice)
cosRequest.Sha = sha
cosRequest.Session = session
cosRequest.Offset = offset
httpRequest := http.Request{
Method: "POST",
Uri: resource,
Timeout: c.Timeout,
Body: cosRequest,
Multipart: true,
}
httpRequest.AddHeader("Authorization", sign)
httpResponse, err := httpRequest.Do()
if err != nil {
return nil, err
}
err = httpResponse.Body.Unmarshal(&cosResponse)
if err != nil {
return nil, err
}
return &cosResponse, nil
}
示例4: PrefixSearch
// 前綴搜索目錄和文件
// bucket: Bucket 名稱
// path: 目錄路徑
// prefix: 搜索前綴
// context: Context 用於翻頁, 需要往前/往後翻頁需透傳回來
// pattern: 目錄列舉模式, 可選值為 Both(列舉目錄和文件), DirectoryOnly(僅列舉目錄), FileOnly(僅列舉文件)
// num: 本次拉取的目錄和文件總數
// order: 目錄列舉排序規則, 可選值為 Asc(正序), Desc(反序)
//
// 示例:
//
// client := cos.NewClient(appId, secretId, secretKey)
//
// res, err := client.PrefixSearch("cosdemo", "/hello", "A", "", cos.Both, 100, cos.Asc)
// if err != nil {
// fmt.Println(err)
// return
// }
//
// fmt.Println("Code:", res.Code,
// "\nMessage:", res.Message,
// "\nContext:", res.Data.Context,
// "\nHasMore:", res.Data.HasMore,
// "\nDirCount:", res.Data.DirCount,
// "\nFileCount:", res.Data.FileCount,
// )
//
// fmt.Println("*************************************")
// for _, info := range res.Data.Infos {
// fmt.Println("Name:", info.Name,
// "\nBizAttr:", info.BizAttr,
// "\nFileSize:", info.FileSize,
// "\nFileLen:", info.FileLen,
// "\nSha:", info.Sha,
// "\nCtime:", info.Ctime,
// "\nMtime:", info.Mtime,
// "\nAccess URL:", info.AccessUrl,
// )
// fmt.Println("*************************************")
// }
//
func (c *Client) PrefixSearch(bucket, path, prefix, context string, pattern ListPattern, num int, order ListOrder) (*ListFolderResponse, error) {
var cosRequest ListFolderRequest
var cosResponse ListFolderResponse
encodedPath := utils.UrlEncode(c.validateFolderPath(path) + prefix)
resource := c.generateResourceUrl(bucket, encodedPath)
now := time.Now().Unix()
strNow := fmt.Sprintf("%d", now)
strExpires := fmt.Sprintf("%d", now+EXPIRES)
strRand := fmt.Sprintf("%d", rand.Int31())
signer := auth.NewSignature(c.AppId, bucket, c.SecretId, strExpires, strNow, strRand, "")
sign := signer.Sign(c.SecretKey)
cosRequest.Op = "list"
cosRequest.Pattern = pattern.String()
cosRequest.Context = context
cosRequest.Order = order.Int()
cosRequest.Num = num
httpRequest := http.Request{
Method: "GET",
Uri: resource,
Timeout: c.Timeout,
QueryString: cosRequest,
}
httpRequest.AddHeader("Authorization", sign)
httpResponse, err := httpRequest.Do()
if err != nil {
return nil, err
}
err = httpResponse.Body.Unmarshal(&cosResponse)
if err != nil {
return nil, err
}
return &cosResponse, nil
}
示例5: UpdateFolder
// 更新目錄屬性
// bucket: Bucket 名稱
// path: 目錄路徑
// bizAttr: 目錄屬性, 由業務端維護
//
// 示例:
//
// client := cos.NewClient(appId, secretId, secretKey)
//
// res, err := client.UpdateFolder("cosdemo", "/hello", "hello-new-attr")
// if err != nil {
// fmt.Println(err)
// return
// }
//
// fmt.Println("Code:", res.Code,
// "\nMessage:", res.Message)
//
func (c *Client) UpdateFolder(bucket, path, bizAttr string) (*UpdateFolderResponse, error) {
var cosRequest UpdateFolderRequest
var cosResponse UpdateFolderResponse
encodedPath := utils.UrlEncode(c.validateFolderPath(path))
fileId := c.generateFileId(bucket, encodedPath)
resource := c.generateResourceUrl(bucket, encodedPath)
now := time.Now().Unix()
strNow := fmt.Sprintf("%d", now)
strRand := fmt.Sprintf("%d", rand.Int31())
signer := auth.NewSignature(c.AppId, bucket, c.SecretId, "", strNow, strRand, fileId)
sign := signer.SignOnce(c.SecretKey)
cosRequest.Op = "update"
cosRequest.BizAttr = bizAttr
httpRequest := http.Request{
Method: "POST",
Uri: resource,
Timeout: c.Timeout,
ContentType: "application/json",
Body: cosRequest,
}
httpRequest.AddHeader("Authorization", sign)
httpResponse, err := httpRequest.Do()
if err != nil {
return nil, err
}
err = httpResponse.Body.Unmarshal(&cosResponse)
if err != nil {
return nil, err
}
return &cosResponse, nil
}