コード例 #1
0
ファイル: client.go プロジェクト: guoyao/baidubce-sdk-go
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
ファイル: client.go プロジェクト: guoyao/baidubce-sdk-go
func (c *Client) UploadPart(uploadPartRequest UploadPartRequest,
	option *bce.SignOption) (UploadPartResponse, error) {

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

	if uploadPartRequest.PartNumber < MIN_PART_NUMBER || uploadPartRequest.PartNumber > MAX_PART_NUMBER {
		panic(fmt.Sprintf("Invalid partNumber %d. The valid range is from %d to %d.",
			uploadPartRequest.PartNumber, MIN_PART_NUMBER, MAX_PART_NUMBER))
	}

	if uploadPartRequest.PartSize > 1024*1024*1024*5 {
		panic(fmt.Sprintf("PartNumber %d: Part Size should not be more than 5GB.", uploadPartRequest.PartSize))
	}

	params := map[string]string{
		"partNumber": strconv.Itoa(uploadPartRequest.PartNumber),
		"uploadId":   uploadPartRequest.UploadId,
	}

	req, err := bce.NewRequest("PUT", c.GetURL(bucketName, objectKey, params), uploadPartRequest.PartData)

	if err != nil {
		return nil, err
	}

	option = bce.CheckSignOption(option)
	option.AddHeaders(map[string]string{
		"Content-Length": strconv.FormatInt(uploadPartRequest.PartSize, 10),
		"Content-Type":   "application/octet-stream",
	})

	if _, ok := option.Headers["Content-MD5"]; !ok {
		option.AddHeader("Content-MD5", util.GetMD5(uploadPartRequest.PartData, true))
	}

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

	if err != nil {
		return nil, err
	}

	uploadPartResponse := NewUploadPartResponse(resp.Header)

	return uploadPartResponse, nil
}
コード例 #3
0
ファイル: client.go プロジェクト: guoyao/baidubce-sdk-go
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
}
コード例 #4
0
ファイル: client.go プロジェクト: guoyao/baidubce-sdk-go
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
}
コード例 #5
0
ファイル: client.go プロジェクト: guoyao/baidubce-sdk-go
func (c *Client) CopyObjectFromRequest(copyObjectRequest CopyObjectRequest,
	option *bce.SignOption) (*CopyObjectResponse, error) {

	checkBucketName(copyObjectRequest.SrcBucketName)
	checkBucketName(copyObjectRequest.DestBucketName)
	checkObjectKey(copyObjectRequest.SrcKey)
	checkObjectKey(copyObjectRequest.DestKey)

	req, err := bce.NewRequest("PUT", c.GetURL(copyObjectRequest.DestBucketName, copyObjectRequest.DestKey, nil), nil)

	if err != nil {
		return nil, err
	}

	option = bce.CheckSignOption(option)

	source := util.URIEncodeExceptSlash(fmt.Sprintf("/%s/%s", copyObjectRequest.SrcBucketName,
		copyObjectRequest.SrcKey))

	option.AddHeader("x-bce-copy-source", source)
	copyObjectRequest.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 copyObjectResponse *CopyObjectResponse
	err = json.Unmarshal(bodyContent, &copyObjectResponse)

	if err != nil {
		return nil, err
	}

	return copyObjectResponse, nil
}
コード例 #6
0
ファイル: bucket.go プロジェクト: guoyao/baidubce-sdk-go
func (copyObjectRequest CopyObjectRequest) mergeToSignOption(option *bce.SignOption) {
	m, err := util.ToMap(copyObjectRequest)

	if err != nil {
		return
	}

	headerMap := make(map[string]string)

	for key, value := range m {
		if str, ok := value.(string); ok {
			headerMap[key] = str
		}
	}

	option.AddHeaders(headerMap)

	if copyObjectRequest.ObjectMetadata != nil {
		option.AddHeader("x-bce-metadata-directive", "replace")
		copyObjectRequest.ObjectMetadata.mergeToSignOption(option)
	} else {
		option.AddHeader("x-bce-metadata-directive", "copy")
	}
}
コード例 #7
0
ファイル: bucket.go プロジェクト: guoyao/baidubce-sdk-go
func (getObjectRequest *GetObjectRequest) MergeToSignOption(option *bce.SignOption) {
	if getObjectRequest.Range != "" {
		option.AddHeader("Range", "bytes="+getObjectRequest.Range)
	}
}
コード例 #8
0
ファイル: bucket.go プロジェクト: guoyao/baidubce-sdk-go
func (metadata *ObjectMetadata) mergeToSignOption(option *bce.SignOption) {
	if metadata.CacheControl != "" {
		option.AddHeader("Cache-Control", metadata.CacheControl)
	}

	if metadata.ContentDisposition != "" {
		option.AddHeader("Content-Disposition", metadata.ContentDisposition)
	}

	if metadata.ContentLength != 0 {
		option.AddHeader("Content-Length", strconv.FormatInt(metadata.ContentLength, 10))
	}

	if metadata.ContentMD5 != "" {
		option.AddHeader("Content-MD5", metadata.ContentMD5)
	}

	if metadata.ContentType != "" {
		option.AddHeader("Content-Type", metadata.ContentType)
	}

	if metadata.Expires != "" {
		option.AddHeader("Expires", metadata.Expires)
	}

	if metadata.ContentSha256 != "" {
		option.AddHeader("x-bce-content-sha256", metadata.ContentSha256)
	}

	for key, value := range metadata.UserMetadata {
		option.AddHeader(ToUserDefinedMetadata(key), value)
	}
}