Beispiel #1
0
// 查询目录属性
//     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
}
Beispiel #2
0
// 大文件分片上传(首次上传)
//     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
}
Beispiel #3
0
// 大文件分片上传(后续上传)
//     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
}
Beispiel #4
0
// 前缀搜索目录和文件
//     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
}
Beispiel #5
0
// 更新目录属性
//     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
}