Esempio n. 1
0
func (c *Client) GetSessionToken(sessionTokenRequest SessionTokenRequest,
	option *SignOption) (*SessionTokenResponse, error) {

	var params map[string]string

	if sessionTokenRequest.DurationSeconds > 0 {
		params = map[string]string{"durationSeconds": strconv.Itoa(sessionTokenRequest.DurationSeconds)}
	}

	body, err := util.ToJson(sessionTokenRequest, "id", "accessControlList")

	if err != nil {
		return nil, err
	}

	uriPath := "sessionToken"

	if c.APIVersion == "" {
		uriPath = "v1/" + uriPath
	}

	req, err := NewRequest("POST", c.GetURL("sts.bj.baidubce.com", uriPath, params), bytes.NewReader(body))

	if err != nil {
		return nil, err
	}

	option = CheckSignOption(option)
	option.AddHeader("Content-Type", "application/json")

	resp, err := c.SendRequest(req, option)

	if err != nil {
		return nil, err
	}

	bodyContent, err := resp.GetBodyContent()

	if err != nil {
		return nil, err
	}

	var sessionTokenResponse *SessionTokenResponse
	err = json.Unmarshal(bodyContent, &sessionTokenResponse)

	if err != nil {
		return nil, err
	}

	return sessionTokenResponse, nil
}
Esempio n. 2
0
func (c *Client) SetBucketAcl(bucketName string, bucketAcl BucketAcl, option *bce.SignOption) error {
	byteArray, err := util.ToJson(bucketAcl, "accessControlList")

	if err != nil {
		return err
	}

	params := map[string]string{"acl": ""}
	req, err := bce.NewRequest("PUT", c.GetURL(bucketName, "", params), bytes.NewReader(byteArray))

	if err != nil {
		return err
	}

	_, err = c.SendRequest(req, option)

	return err
}
Esempio n. 3
0
func (c *Client) SetBucketCors(bucketName string, bucketCors BucketCors, option *bce.SignOption) error {
	byteArray, err := util.ToJson(bucketCors, "corsConfiguration")

	if err != nil {
		return err
	}

	params := map[string]string{"cors": ""}
	req, err := bce.NewRequest("PUT", c.GetURL(bucketName, "", params), bytes.NewReader(byteArray))

	if err != nil {
		return err
	}

	_, err = c.SendRequest(req, option)

	return err
}
Esempio n. 4
0
func (c *Client) CompleteMultipartUpload(completeMultipartUploadRequest CompleteMultipartUploadRequest,
	option *bce.SignOption) (*CompleteMultipartUploadResponse, error) {

	bucketName := completeMultipartUploadRequest.BucketName
	objectKey := completeMultipartUploadRequest.ObjectKey
	checkBucketName(bucketName)
	checkObjectKey(objectKey)

	completeMultipartUploadRequest.sort()
	params := map[string]string{"uploadId": completeMultipartUploadRequest.UploadId}
	byteArray, err := util.ToJson(completeMultipartUploadRequest, "parts")

	if err != nil {
		return nil, err
	}

	req, err := bce.NewRequest("POST", c.GetURL(bucketName, objectKey, params), bytes.NewReader(byteArray))

	if err != nil {
		return nil, err
	}

	resp, err := c.SendRequest(req, option)

	if err != nil {
		return nil, err
	}

	bodyContent, err := resp.GetBodyContent()

	if err != nil {
		return nil, err
	}

	var completeMultipartUploadResponse *CompleteMultipartUploadResponse

	err = json.Unmarshal(bodyContent, &completeMultipartUploadResponse)

	if err != nil {
		return nil, err
	}

	return completeMultipartUploadResponse, nil
}
Esempio n. 5
0
func (c *Client) SetBucketLogging(bucketName, targetBucket, targetPrefix string, option *bce.SignOption) error {
	params := map[string]string{"logging": ""}
	body, err := util.ToJson(map[string]string{
		"targetBucket": targetBucket,
		"targetPrefix": targetPrefix,
	})

	if err != nil {
		return err
	}

	req, err := bce.NewRequest("PUT", c.GetURL(bucketName, "", params), bytes.NewReader(body))

	if err != nil {
		return err
	}

	_, err = c.SendRequest(req, option)

	return err
}
Esempio n. 6
0
func (c *Client) DeleteMultipleObjects(bucketName string, objectKeys []string,
	option *bce.SignOption) (*DeleteMultipleObjectsResponse, error) {

	checkBucketName(bucketName)

	keys := make([]string, 0, len(objectKeys))

	for _, key := range objectKeys {
		if key != "" {
			keys = append(keys, key)
		}
	}

	objectKeys = keys
	length := len(objectKeys)

	if length == 0 {
		return nil, nil
	}

	objectMap := make(map[string][]map[string]string, 1)
	objects := make([]map[string]string, length, length)

	for index, value := range objectKeys {
		objects[index] = map[string]string{"key": value}
	}

	objectMap["objects"] = objects
	byteArray, err := util.ToJson(objectMap)

	if err != nil {
		return nil, err
	}

	params := map[string]string{"delete": ""}
	body := bytes.NewReader(byteArray)

	req, err := bce.NewRequest("POST", c.GetURL(bucketName, "", params), body)

	if err != nil {
		return nil, err
	}

	resp, err := c.SendRequest(req, option)

	if err != nil {
		return nil, err
	}

	bodyContent, err := resp.GetBodyContent()

	if err != nil {
		return nil, err
	}

	if len(bodyContent) > 0 {
		var deleteMultipleObjectsResponse *DeleteMultipleObjectsResponse
		err := json.Unmarshal(bodyContent, &deleteMultipleObjectsResponse)

		if err != nil {
			return nil, err
		}

		return deleteMultipleObjectsResponse, nil
	}

	return nil, nil
}