示例#1
0
func (c *Client) AppendObject(bucketName, objectKey string, offset int, data interface{},
	metadata *ObjectMetadata, option *bce.SignOption) (AppendObjectResponse, error) {

	checkBucketName(bucketName)
	checkObjectKey(objectKey)

	var reader io.Reader

	if str, ok := data.(string); ok {
		reader = strings.NewReader(str)
	} else if byteArray, ok := data.([]byte); ok {
		reader = bytes.NewReader(byteArray)
	} else if r, ok := data.(io.Reader); ok {
		byteArray, err := ioutil.ReadAll(r)

		if err != nil {
			return nil, err
		}

		reader = bytes.NewReader(byteArray)
	} else {
		panic("data type should be string or []byte or io.Reader.")
	}

	params := map[string]string{"append": ""}

	if offset > 0 {
		params["offset"] = strconv.Itoa(offset)
	}

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

	if err != nil {
		return nil, err
	}

	option = bce.CheckSignOption(option)
	option.AddHeader("Content-Type", util.GuessMimeType(objectKey))

	if c.Checksum {
		option.AddHeader("x-bce-content-sha256", util.GetSha256(data))
	}

	if metadata != nil {
		metadata.mergeToSignOption(option)
	}

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

	if err != nil {
		return nil, err
	}

	appendObjectResponse := NewAppendObjectResponse(resp.Header)

	return appendObjectResponse, nil
}
示例#2
0
func (c *Client) InitiateMultipartUpload(initiateMultipartUploadRequest InitiateMultipartUploadRequest,
	option *bce.SignOption) (*InitiateMultipartUploadResponse, error) {

	bucketName := initiateMultipartUploadRequest.BucketName
	objectKey := initiateMultipartUploadRequest.ObjectKey

	checkBucketName(bucketName)
	checkObjectKey(objectKey)

	params := map[string]string{"uploads": ""}

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

	if err != nil {
		return nil, err
	}

	option = bce.CheckSignOption(option)
	option.AddHeader("Content-Type", util.GuessMimeType(objectKey))

	if initiateMultipartUploadRequest.ObjectMetadata != nil {
		initiateMultipartUploadRequest.ObjectMetadata.mergeToSignOption(option)
	}

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

	if err != nil {
		return nil, err
	}

	bodyContent, err := resp.GetBodyContent()

	if err != nil {
		return nil, err
	}

	var initiateMultipartUploadResponse *InitiateMultipartUploadResponse
	err = json.Unmarshal(bodyContent, &initiateMultipartUploadResponse)

	if err != nil {
		return nil, err
	}

	return initiateMultipartUploadResponse, nil
}
示例#3
0
func (c *Client) PutObject(bucketName, objectKey string, data interface{},
	metadata *ObjectMetadata, option *bce.SignOption) (PutObjectResponse, error) {

	checkObjectKey(objectKey)

	var reader io.Reader

	if str, ok := data.(string); ok {
		reader = strings.NewReader(str)
	} else if byteArray, ok := data.([]byte); ok {
		reader = bytes.NewReader(byteArray)
	} else if r, ok := data.(io.Reader); ok {
		reader = r
	} else {
		panic("data type should be string or []byte or io.Reader.")
	}

	req, err := bce.NewRequest("PUT", c.GetURL(bucketName, objectKey, nil), reader)

	if err != nil {
		return nil, err
	}

	option = bce.CheckSignOption(option)
	option.AddHeader("Content-Type", util.GuessMimeType(objectKey))

	if c.Checksum {
		option.AddHeader("x-bce-content-sha256", util.GetSha256(data))
	}

	if metadata != nil {
		metadata.mergeToSignOption(option)
	}

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

	if err != nil {
		return nil, err
	}

	putObjectResponse := NewPutObjectResponse(resp.Header)

	return putObjectResponse, nil
}