Ejemplo n.º 1
0
func (c *Client) GetObjectFromRequest(getObjectRequest GetObjectRequest,
	option *bce.SignOption) (*Object, error) {

	checkBucketName(getObjectRequest.BucketName)
	checkObjectKey(getObjectRequest.ObjectKey)

	req, err := bce.NewRequest("GET", c.GetURL(getObjectRequest.BucketName, getObjectRequest.ObjectKey, nil), nil)

	if err != nil {
		return nil, err
	}

	option = bce.CheckSignOption(option)
	getObjectRequest.MergeToSignOption(option)

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

	if err != nil {
		return nil, err
	}

	object := &Object{
		ObjectMetadata: NewObjectMetadataFromHeader(resp.Header),
		ObjectContent:  resp.Body,
	}

	return object, nil
}
Ejemplo n.º 2
0
func (c *Client) GetBucketAcl(bucketName string, option *bce.SignOption) (*BucketAcl, error) {
	params := map[string]string{"acl": ""}
	req, err := bce.NewRequest("GET", c.GetURL(bucketName, "", params), nil)

	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 bucketAcl *BucketAcl
	err = json.Unmarshal(bodyContent, &bucketAcl)

	if err != nil {
		return nil, err
	}

	return bucketAcl, nil
}
Ejemplo n.º 3
0
// GetBucketLocation returns the location of a bucket.
func (c *Client) GetBucketLocation(bucketName string, option *bce.SignOption) (*Location, error) {
	bucketName = c.GetBucketName(bucketName)
	params := map[string]string{"location": ""}

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

	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 location *Location
	err = json.Unmarshal(bodyContent, &location)

	if err != nil {
		return nil, err
	}

	return location, nil
}
Ejemplo n.º 4
0
// ListBuckets is for getting a collection of bucket.
func (c *Client) ListBuckets(option *bce.SignOption) (*BucketSummary, error) {
	req, err := bce.NewRequest("GET", c.GetURL("", "", nil), nil)

	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 bucketSummary *BucketSummary
	err = json.Unmarshal(bodyContent, &bucketSummary)

	if err != nil {
		return nil, err
	}

	return bucketSummary, nil
}
Ejemplo n.º 5
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
}
Ejemplo n.º 6
0
func (c *Client) DeleteBucket(bucketName string, option *bce.SignOption) error {
	req, err := bce.NewRequest("DELETE", c.GetURL(bucketName, "", nil), nil)

	if err != nil {
		return err
	}

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

	return err
}
Ejemplo n.º 7
0
func (c *Client) DeleteBucketLogging(bucketName string, option *bce.SignOption) error {
	params := map[string]string{"logging": ""}
	req, err := bce.NewRequest("DELETE", c.GetURL(bucketName, "", params), nil)

	if err != nil {
		return err
	}

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

	return err
}
Ejemplo n.º 8
0
func (c *Client) DeleteObject(bucketName, objectKey string, option *bce.SignOption) error {
	checkObjectKey(objectKey)

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

	if err != nil {
		return err
	}

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

	return err
}
Ejemplo n.º 9
0
func (c *Client) ListMultipartUploadsFromRequest(listMultipartUploadsRequest ListMultipartUploadsRequest,
	option *bce.SignOption) (*ListMultipartUploadsResponse, error) {

	bucketName := listMultipartUploadsRequest.BucketName

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

	if listMultipartUploadsRequest.Delimiter != "" {
		params["delimiter"] = listMultipartUploadsRequest.Delimiter
	}

	if listMultipartUploadsRequest.KeyMarker != "" {
		params["keyMarker"] = listMultipartUploadsRequest.KeyMarker
	}

	if listMultipartUploadsRequest.Prefix != "" {
		params["prefix"] = listMultipartUploadsRequest.Prefix
	}

	if listMultipartUploadsRequest.MaxUploads > 0 {
		params["maxUploads"] = strconv.Itoa(listMultipartUploadsRequest.MaxUploads)
	}

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

	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 listMultipartUploadsResponse *ListMultipartUploadsResponse

	err = json.Unmarshal(bodyContent, &listMultipartUploadsResponse)

	if err != nil {
		return nil, err
	}

	return listMultipartUploadsResponse, nil
}
Ejemplo n.º 10
0
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
}
Ejemplo n.º 11
0
func (c *Client) setBucketAclFromString(bucketName, acl string, option *bce.SignOption) error {
	params := map[string]string{"acl": ""}
	req, err := bce.NewRequest("PUT", c.GetURL(bucketName, "", params), nil)

	if err != nil {
		return err
	}

	option = bce.CheckSignOption(option)

	headers := map[string]string{"x-bce-acl": acl}
	option.AddHeaders(headers)

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

	return err
}
Ejemplo n.º 12
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
}
Ejemplo n.º 13
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
}
Ejemplo n.º 14
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
}
Ejemplo n.º 15
0
func (c *Client) GeneratePresignedUrl(bucketName, objectKey string, option *bce.SignOption) (string, error) {
	checkBucketName(bucketName)
	checkObjectKey(objectKey)

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

	if err != nil {
		return "", err
	}

	option = bce.CheckSignOption(option)
	option.HeadersToSign = []string{"host"}

	authorization := bce.GenerateAuthorization(*c.Credentials, *req, option)
	url := fmt.Sprintf("%s?authorization=%s", req.URL.String(), util.URLEncode(authorization))

	return url, nil
}
Ejemplo n.º 16
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
}
Ejemplo n.º 17
0
func (c *Client) ListPartsFromRequest(listPartsRequest ListPartsRequest,
	option *bce.SignOption) (*ListPartsResponse, error) {

	bucketName := listPartsRequest.BucketName
	objectKey := listPartsRequest.ObjectKey

	params := map[string]string{"uploadId": listPartsRequest.UploadId}

	if listPartsRequest.PartNumberMarker != "" {
		params["partNumberMarker"] = listPartsRequest.PartNumberMarker
	}

	if listPartsRequest.MaxParts > 0 {
		params["maxParts"] = strconv.Itoa(listPartsRequest.MaxParts)
	}

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

	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 listPartsResponse *ListPartsResponse

	err = json.Unmarshal(bodyContent, &listPartsResponse)

	if err != nil {
		return nil, err
	}

	return listPartsResponse, nil
}
Ejemplo n.º 18
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
}
Ejemplo n.º 19
0
func (c *Client) OptionsObject(bucketName, objectKey, origin, accessControlRequestMethod,
	accessControlRequestHeaders string) (*bce.Response, error) {

	checkBucketName(bucketName)
	checkObjectKey(objectKey)

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

	if err != nil {
		return nil, err
	}

	option := bce.CheckSignOption(nil)
	option.AddHeader("Origin", origin)
	option.AddHeader("Access-Control-Request-Method", accessControlRequestMethod)
	option.AddHeader("Access-Control-Request-Headers", accessControlRequestHeaders)

	return c.SendRequest(req, option)
}
Ejemplo n.º 20
0
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
}
Ejemplo n.º 21
0
func (c *Client) GetObjectToFile(getObjectRequest *GetObjectRequest, file *os.File,
	option *bce.SignOption) (*ObjectMetadata, error) {

	defer func() {
		if file != nil {
			file.Close()
		}
	}()

	checkBucketName(getObjectRequest.BucketName)
	checkObjectKey(getObjectRequest.ObjectKey)

	req, err := bce.NewRequest("GET", c.GetURL(getObjectRequest.BucketName, getObjectRequest.ObjectKey, nil), nil)

	if err != nil {
		return nil, err
	}

	option = bce.CheckSignOption(option)
	getObjectRequest.MergeToSignOption(option)

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

	if err != nil {
		return nil, err
	}

	objectMetadata := NewObjectMetadataFromHeader(resp.Header)

	bodyContent, err := resp.GetBodyContent()

	if err != nil {
		return objectMetadata, err
	}

	_, err = file.Write(bodyContent)

	if err != nil {
		return objectMetadata, err
	}

	return objectMetadata, nil
}
Ejemplo n.º 22
0
func (c *Client) AbortMultipartUpload(abortMultipartUploadRequest AbortMultipartUploadRequest,
	option *bce.SignOption) error {

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

	params := map[string]string{"uploadId": abortMultipartUploadRequest.UploadId}

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

	if err != nil {
		return err
	}

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

	return err
}
Ejemplo n.º 23
0
func (c *Client) DoesBucketExist(bucketName string, option *bce.SignOption) (bool, error) {
	req, err := bce.NewRequest("HEAD", c.GetURL(bucketName, "", nil), nil)

	if err != nil {
		return false, err
	}

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

	if resp != nil {
		switch {
		case resp.StatusCode < http.StatusBadRequest || resp.StatusCode == http.StatusForbidden:
			return true, nil
		case resp.StatusCode == http.StatusNotFound:
			return false, nil
		}
	}

	return false, err
}
Ejemplo n.º 24
0
func (c *Client) GetObjectMetadata(bucketName, objectKey string, option *bce.SignOption) (*ObjectMetadata, error) {
	checkBucketName(bucketName)
	checkObjectKey(objectKey)

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

	if err != nil {
		return nil, err
	}

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

	if err != nil {
		return nil, err
	}

	objectMetadata := NewObjectMetadataFromHeader(resp.Header)

	return objectMetadata, nil
}
Ejemplo n.º 25
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
}
Ejemplo n.º 26
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
}