Пример #1
0
func Sign(credentials *BceCredentials, timestamp, httpMethod, path, query string,
	headers map[string]string) string {

	if path[0] != '/' {
		path = "/" + path
	}

	var expirationPeriodInSeconds = 1800
	authStringPrefix := fmt.Sprintf("bce-auth-v1/%s/%s/%d", credentials.AccessKeyId,
		timestamp, expirationPeriodInSeconds)
	//fmt.Println(authStringPrefix)

	mac := hmac.New(sha256.New, []byte(credentials.SecretAccessKey))
	mac.Write([]byte(authStringPrefix))
	signingKey := fmt.Sprintf("%x", mac.Sum(nil))
	//fmt.Printf(signingKey)

	CanonicalURI := utils.UriEncodeExceptSlash(path)
	CanonicalQueryString := getCannonicalQuery(query)
	CanonicalHeaders, signedHeaders := getCanonicalHeaders(headers, nil)
	CanonicalRequest := fmt.Sprintf("%s\n%s\n%s\n%s", httpMethod, CanonicalURI,
		CanonicalQueryString, CanonicalHeaders)

	mac = hmac.New(sha256.New, []byte(signingKey))
	mac.Write([]byte(CanonicalRequest))
	signature := fmt.Sprintf("%x", mac.Sum(nil))
	//fmt.Println(signature)

	authorization := fmt.Sprintf("%s/%s/%s", authStringPrefix, signedHeaders, signature)
	if Debug {
		fmt.Println(CanonicalRequest)
		fmt.Println(authorization)
	}
	return authorization
}
Пример #2
0
func (c Client) CopyObject(srcBucketName, srcObjectName, destBucketName, destObjectName, eTag, metaDirect string) (output CopyObjectResponse, err error) {
	destObjectName = c.formatPath(destObjectName)
	req := &httplib.Request{
		Method:  httplib.PUT,
		Headers: map[string]string{},
		Path:    c.APIVersion + "/" + destBucketName + "/" + destObjectName,
	}
	req.Headers[httplib.BCE_COPY_SOURCE] = utils.UriEncodeExceptSlash("/" + srcBucketName + "/" + srcObjectName)

	if eTag != "" {
		req.Headers[httplib.BCE_COPY_SOURCE_IF_MATCH] = eTag
	}

	if metaDirect == "copy" || metaDirect == "replace" {
		req.Headers[httplib.BCE_COPY_METADATA_DIRECTIVE] = metaDirect
	}

	res, err := c.doRequest(req)
	if err != nil {
		return
	}

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return
	}

	j := json.NewDecoder(strings.NewReader(string(body)))
	j.Decode(&output)

	return
}