vendor: add qingstor-sdk-go for QingStor

This commit is contained in:
wuyu 2017-06-26 05:45:22 +08:00 committed by Nick Craig-Wood
parent f682002b84
commit 466dd22b44
136 changed files with 15952 additions and 1 deletions

30
vendor/github.com/yunify/qingstor-sdk-go/.gitignore generated vendored Normal file
View file

@ -0,0 +1,30 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.test
*.prof
out
gen
vendor
coverage
release

6
vendor/github.com/yunify/qingstor-sdk-go/.gitmodules generated vendored Normal file
View file

@ -0,0 +1,6 @@
[submodule "specs/qingstor"]
path = specs/qingstor
url = https://github.com/yunify/qingstor-api-specs.git
[submodule "test/features"]
path = test/features
url = https://github.com/yunify/qingstor-sdk-test-scenarios.git

51
vendor/github.com/yunify/qingstor-sdk-go/.travis.yml generated vendored Normal file
View file

@ -0,0 +1,51 @@
sudo: required
services:
- docker
language: go
go:
- 1.7
env:
matrix:
- GO_VERSION=1.8
- GO_VERSION=1.7
- GO_VERSION=1.6
- GO_VERSION=1.5
cache:
directories:
- ${HOME}/source
before_install:
- pushd ${HOME}/source
- if [[ ! -d "./make-4.0" ]]; then
wget http://ftp.gnu.org/gnu/make/make-4.0.tar.gz &&
tar -vxzf make-4.0.tar.gz &&
pushd make-4.0 && ./configure && make && popd;
fi
- pushd make-4.0 && sudo make install && popd
- if [[ ! -d "./glide-v0.12.3" ]]; then
wget https://github.com/Masterminds/glide/releases/download/v0.12.3/glide-v0.12.3-linux-amd64.tar.gz &&
tar -vxzf glide-v0.12.3-linux-amd64.tar.gz &&
mv linux-amd64 glide-v0.12.3;
fi
- pushd glide-v0.12.3 && sudo cp glide /usr/local/bin && popd
- popd
- /usr/local/bin/make --version
- /usr/local/bin/glide --version
install:
- go get -u github.com/yunify/snips
- go get -u github.com/golang/lint/golint;
- glide install
before_script:
- /usr/local/bin/make update
- /usr/local/bin/make generate
script:
- /usr/local/bin/make check
- /usr/local/bin/make release
- /usr/local/bin/make test-runtime-go-${GO_VERSION}

4
vendor/github.com/yunify/qingstor-sdk-go/AUTHORS generated vendored Normal file
View file

@ -0,0 +1,4 @@
Patches have been contributed by (ordered by the time of the first merged patch):
Jingwen Peng <pengsrc@yunify.com>
Osier Yang <osier@yunify.com>

107
vendor/github.com/yunify/qingstor-sdk-go/CHANGELOG.md generated vendored Normal file
View file

@ -0,0 +1,107 @@
# Change Log
All notable changes to QingStor SDK for Go will be documented in this file.
## [v2.2.5] - 2017-05-22
### Fixed
- Fix error in request URL query.
- Fix error in request header value.
## [v2.2.4] - 2017-03-28
### Fixed
- Fix type of Content-Type header.
- Add Content-Length to GetObjectOutput.
- Fix status code of DELETE CORS API.
- Fix type of object size for GET Bucket API.
### BREAKING CHANGES
- The type of content length and object size has been changed from `*int` to `*int64`.
## [v2.2.3] - 2017-03-10
### Added
- Allow user to append additional info to User-Agent
## [v2.2.2] - 2017-03-08
### Fixed
- Resource is not mandatory in bucket policy statement
## [v2.2.1] - 2017-03-05
### Changed
- Add "Encrypted" field to "KeyType" struct
## [v2.2.0] - 2017-02-28
### Added
- Add ListMultipartUploads API.
### Fixed
- Fix request builder & signer.
## [v2.1.2] - 2017-01-16
### Fixed
- Fix request signer.
## [v2.1.1] - 2017-01-05
### Changed
- Fix logger output format, don't parse special characters.
- Rename package "errs" to "errors".
### Added
- Add type converters.
### BREAKING CHANGES
- Change value type in input and output to pointer.
## [v2.1.0] - 2016-12-23
### Changed
- Fix signer bug.
- Add more parameters to sign.
### Added
- Add request parameters for GET Object.
- Add IP address conditions for bucket policy.
## [v2.0.1] - 2016-12-15
### Changed
- Improve the implementation of deleting multiple objects.
## [v2.0.0] - 2016-12-14
### Added
- QingStor SDK for the Go programming language.
[v2.2.5]: https://github.com/yunify/qingstor-sdk-go/compare/v2.2.4...v2.2.5
[v2.2.4]: https://github.com/yunify/qingstor-sdk-go/compare/v2.2.3...v2.2.4
[v2.2.3]: https://github.com/yunify/qingstor-sdk-go/compare/v2.2.2...v2.2.3
[v2.2.2]: https://github.com/yunify/qingstor-sdk-go/compare/v2.2.1...v2.2.2
[v2.2.1]: https://github.com/yunify/qingstor-sdk-go/compare/v2.2.0...v2.2.1
[v2.2.0]: https://github.com/yunify/qingstor-sdk-go/compare/v2.1.2...v2.2.0
[v2.1.2]: https://github.com/yunify/qingstor-sdk-go/compare/v2.1.1...v2.1.2
[v2.1.1]: https://github.com/yunify/qingstor-sdk-go/compare/v2.1.0...v2.1.1
[v2.1.0]: https://github.com/yunify/qingstor-sdk-go/compare/v2.0.1...v2.1.0
[v2.0.1]: https://github.com/yunify/qingstor-sdk-go/compare/v2.0.0...v2.0.1
[v2.0.0]: https://github.com/yunify/qingstor-sdk-go/compare/v2.0.0...v2.0.0

201
vendor/github.com/yunify/qingstor-sdk-go/LICENSE generated vendored Normal file
View file

@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2012 Yunify Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

1
vendor/github.com/yunify/qingstor-sdk-go/MAINTAINERS generated vendored Normal file
View file

@ -0,0 +1 @@
Jingwen Peng <pengsrc@yunify.com>

207
vendor/github.com/yunify/qingstor-sdk-go/Makefile generated vendored Normal file
View file

@ -0,0 +1,207 @@
SHELL := /bin/bash
PREFIX=qingstor-sdk-go
VERSION=$(shell cat version.go | grep "Version\ =" | sed -e s/^.*\ //g | sed -e s/\"//g)
DIRS_TO_CHECK=$(shell ls -d */ | grep -vE "vendor|test")
PKGS_TO_CHECK=$(shell go list ./... | grep -v "/vendor/")
PKGS_TO_RELEASE=$(shell go list ./... | grep -vE "/vendor/|/test")
FILES_TO_RELEASE=$(shell find . -name "*.go" | grep -vE "/vendor/|/test|.*_test.go")
FILES_TO_RELEASE_WITH_VENDOR=$(shell find . -name "*.go" | grep -vE "/test|.*_test.go")
.PHONY: help
help:
@echo "Please use \`make <target>\` where <target> is one of"
@echo " all to check, build, test and release this SDK"
@echo " check to vet and lint the SDK"
@echo " update to update git submodules"
@echo " generate to generate service code"
@echo " build to build the SDK"
@echo " test to run test"
@echo " test-coverage to run test with coverage"
@echo " test-race to run test with race"
@echo " test-runtime to run test in Go 1.8/1.7/1.6/1.5 in docker"
@echo " integration-test to run integration test"
@echo " release to build and release current version"
@echo " release-source to pack the source code"
@echo " clean to clean the coverage files"
.PHONY: all
all: check build unit release
.PHONY: check
check: vet lint
.PHONY: vet
vet:
@echo "go tool vet, skipping vendor packages"
@go tool vet -all ${DIRS_TO_CHECK}
@echo "ok"
.PHONY: lint
lint:
@echo "golint, skipping vendor packages"
@lint=$$(for pkg in ${PKGS_TO_CHECK}; do golint $${pkg}; done); \
lint=$$(echo "$${lint}"); \
if [[ -n $${lint} ]]; then echo "$${lint}"; exit 1; fi
@echo "ok"
.PHONY: update
update:
git submodule update --remote
@echo "ok"
.PHONY: generate
generate:
@if [[ ! -f "$$(which snips)" ]]; then \
echo "ERROR: Command \"snips\" not found."; \
fi
snips \
--service=qingstor --service-api-version=latest \
--spec="./specs" --template="./template" --output="./service"
gofmt -w .
@echo "ok"
.PHONY: build
build:
@echo "build the SDK"
GOOS=linux GOARCH=amd64 go build ${PKGS_TO_RELEASE}
GOOS=darwin GOARCH=amd64 go build ${PKGS_TO_RELEASE}
GOOS=windows GOARCH=amd64 go build ${PKGS_TO_RELEASE}
@echo "ok"
.PHONY: test
test:
@echo "run test"
go test -v ${PKGS_TO_RELEASE}
@echo "ok"
.PHONY: test-coverage
test-coverage:
@echo "run test with coverage"
for pkg in ${PKGS_TO_RELEASE}; do \
output="coverage$${pkg#github.com/yunify/qingstor-sdk-go}"; \
mkdir -p $${output}; \
go test -v -cover -coverprofile="$${output}/profile.out" $${pkg}; \
if [[ -e "$${output}/profile.out" ]]; then \
go tool cover -html="$${output}/profile.out" -o "$${output}/profile.html"; \
fi; \
done
@echo "ok"
.PHONY: test-race
test-race:
@echo "run test with race"
go test -v -race -cpu=1,2,4 ${PKGS_TO_RELEASE}
@echo "ok"
.PHONY: test-runtime
test-runtime: test-runtime-go-1.8 test-runtime-go-1.7 test-runtime-go-1.6 test-runtime-go-1.5
export define DOCKERFILE_GO_1_8
FROM golang:1.8
ADD . /go/src/github.com/yunify/qingstor-sdk-go
WORKDIR /go/src/github.com/yunify/qingstor-sdk-go
CMD ["make", "build", "test", "test-coverage"]
endef
.PHONY: test-runtime-go-1.8
test-runtime-go-1.8:
@echo "run test in go 1.8"
echo "$${DOCKERFILE_GO_1_8}" > "dockerfile_go_1.8"
docker build -f "./dockerfile_go_1.8" -t "${PREFIX}:go-1.8" .
rm -f "./dockerfile_go_1.8"
docker run --name "${PREFIX}-go-1.8-unit" -t "${PREFIX}:go-1.8"
docker rm "${PREFIX}-go-1.8-unit"
docker rmi "${PREFIX}:go-1.8"
@echo "ok"
export define DOCKERFILE_GO_1_7
FROM golang:1.7
ADD . /go/src/github.com/yunify/qingstor-sdk-go
WORKDIR /go/src/github.com/yunify/qingstor-sdk-go
CMD ["make", "build", "test", "test-coverage"]
endef
.PHONY: test-runtime-go-1.7
test-runtime-go-1.7:
@echo "run test in go 1.7"
echo "$${DOCKERFILE_GO_1_7}" > "dockerfile_go_1.7"
docker build -f "./dockerfile_go_1.7" -t "${PREFIX}:go-1.7" .
rm -f "./dockerfile_go_1.7"
docker run --name "${PREFIX}-go-1.7-unit" -t "${PREFIX}:go-1.7"
docker rm "${PREFIX}-go-1.7-unit"
docker rmi "${PREFIX}:go-1.7"
@echo "ok"
export define DOCKERFILE_GO_1_6
FROM golang:1.6
ADD . /go/src/github.com/yunify/qingstor-sdk-go
WORKDIR /go/src/github.com/yunify/qingstor-sdk-go
CMD ["make", "build", "test", "test-coverage"]
endef
.PHONY: test-runtime-go-1.6
test-runtime-go-1.6:
@echo "run test in go 1.6"
echo "$${DOCKERFILE_GO_1_6}" > "dockerfile_go_1.6"
docker build -f "./dockerfile_go_1.6" -t "${PREFIX}:go-1.6" .
rm -f "./dockerfile_go_1.6"
docker run --name "${PREFIX}-go-1.6-unit" -t "${PREFIX}:go-1.6"
docker rm "${PREFIX}-go-1.6-unit"
docker rmi "${PREFIX}:go-1.6"
@echo "ok"
export define DOCKERFILE_GO_1_5
FROM golang:1.5
ENV GO15VENDOREXPERIMENT="1"
ADD . /go/src/github.com/yunify/qingstor-sdk-go
WORKDIR /go/src/github.com/yunify/qingstor-sdk-go
CMD ["make", "build", "test", "test-coverage"]
endef
.PHONY: test-runtime-go-1.5
test-runtime-go-1.5:
@echo "run test in go 1.5"
echo "$${DOCKERFILE_GO_1_5}" > "dockerfile_go_1.5"
docker build -f "dockerfile_go_1.5" -t "${PREFIX}:go-1.5" .
rm -f "dockerfile_go_1.5"
docker run --name "${PREFIX}-go-1.5-unit" -t "${PREFIX}:go-1.5"
docker rm "${PREFIX}-go-1.5-unit"
docker rmi "${PREFIX}:go-1.5"
@echo "ok"
.PHONY: integration-test
integration-test:
@echo "run integration test"
pushd "./test"; go run *.go; popd
@echo "ok"
.PHONY: release
release: release-source release-source-with-vendor
.PHONY: release-source
release-source:
@echo "pack the source code"
mkdir -p "release"
zip -FS "release/${PREFIX}-source-v${VERSION}.zip" ${FILES_TO_RELEASE}
@echo "ok"
.PHONY: release-source-with-vendor
release-source-with-vendor:
@echo "pack the source code"
mkdir -p "release"
zip -FS "release/${PREFIX}-source-with-vendor-v${VERSION}.zip" ${FILES_TO_RELEASE_WITH_VENDOR}
@echo "ok"
.PHONY: clean
clean:
rm -rf $${PWD}/coverage
@echo "ok"

84
vendor/github.com/yunify/qingstor-sdk-go/README.md generated vendored Normal file
View file

@ -0,0 +1,84 @@
# QingStor SDK for Go
[![Build Status](https://travis-ci.org/yunify/qingstor-sdk-go.svg?branch=master)](https://travis-ci.org/yunify/qingstor-sdk-go)
[![Go Report Card](https://goreportcard.com/badge/github.com/yunify/qingstor-sdk-go)](https://goreportcard.com/report/github.com/yunify/qingstor-sdk-go)
[![API Reference](http://img.shields.io/badge/api-reference-green.svg)](http://docs.qingcloud.com/qingstor/)
[![License](http://img.shields.io/badge/license-apache%20v2-blue.svg)](https://github.com/yunify/qingstor-sdk-go/blob/master/LICENSE)
The official QingStor SDK for the Go programming language.
## Getting Started
### Installation
Refer to the [Installation Guide](docs/installation.md), and have this SDK installed.
### Preparation
Before your start, please go to [QingCloud Console](https://console.qingcloud.com/access_keys/) to create a pair of QingCloud API AccessKey.
___API AccessKey Example:___
``` yaml
access_key_id: 'ACCESS_KEY_ID_EXAMPLE'
secret_access_key: 'SECRET_ACCESS_KEY_EXAMPLE'
```
### Usage
Now you are ready to code. You can read the detailed guides in the list below to have a clear understanding or just take the quick start code example.
Checkout our [releases](https://github.com/yunify/qingstor-sdk-go/releases) and [change log](https://github.com/yunify/qingstor-sdk-go/blob/master/CHANGELOG.md) for information about the latest features, bug fixes and new ideas.
- [Configuration Guide](docs/configuration.md)
- [QingStor Service Usage Guide](docs/qingstor_service_usage.md)
___Quick Start Code Example:___
``` go
package main
import (
"fmt"
"github.com/yunify/qingstor-sdk-go/config"
qs "github.com/yunify/qingstor-sdk-go/service"
)
func main() {
conf, _ := config.New("ACCESS_KEY_ID", "SECRET_ACCESS_KEY")
// Initialize service object for QingStor.
qsService, _ := qs.Init(conf)
// List all buckets.
qsOutput, _ := qsService.ListBuckets(&qs.ListBucketsInput{})
// Print HTTP status code.
fmt.Println(qs.IntValue(qsOutput.StatusCode))
// Print the count of buckets.
fmt.Println(qs.IntValue(qsOutput.Count))
// Print the first bucket name.
fmt.Println(qs.StringValue(qsOutput.Buckets[0].Name))
}
```
## Reference Documentations
- [QingStor Documentation](https://docs.qingcloud.com/qingstor/index.html)
- [QingStor Guide](https://docs.qingcloud.com/qingstor/guide/index.html)
- [QingStor APIs](https://docs.qingcloud.com/qingstor/api/index.html)
## Contributing
1. Fork it ( https://github.com/yunify/qingstor-sdk-go/fork )
2. Create your feature branch (`git checkout -b new-feature`)
3. Commit your changes (`git commit -asm 'Add some feature'`)
4. Push to the branch (`git push origin new-feature`)
5. Create a new Pull Request
## LICENSE
The Apache License (Version 2.0, January 2004).

View file

@ -0,0 +1,172 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package config
import (
"errors"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/pengsrc/go-shared/yaml"
"github.com/yunify/qingstor-sdk-go/logger"
)
// A Config stores a configuration of this sdk.
type Config struct {
AccessKeyID string `yaml:"access_key_id"`
SecretAccessKey string `yaml:"secret_access_key"`
Host string `yaml:"host"`
Port int `yaml:"port"`
Protocol string `yaml:"protocol"`
ConnectionRetries int `yaml:"connection_retries"`
AdditionalUserAgent string `yaml:"additional_user_agent"`
LogLevel string `yaml:"log_level"`
Connection *http.Client
}
// New create a Config with given AccessKeyID and SecretAccessKey.
func New(accessKeyID, secretAccessKey string) (*Config, error) {
config, err := NewDefault()
if err != nil {
return nil, err
}
config.AccessKeyID = accessKeyID
config.SecretAccessKey = secretAccessKey
config.Connection = &http.Client{}
return config, nil
}
// NewDefault create a Config with default configuration.
func NewDefault() (*Config, error) {
config := &Config{}
err := config.LoadDefaultConfig()
if err != nil {
return nil, err
}
config.Connection = &http.Client{}
return config, nil
}
// Check checks the configuration.
func (c *Config) Check() error {
if c.AccessKeyID == "" {
return errors.New("access key ID not specified")
}
if c.SecretAccessKey == "" {
return errors.New("secret access key not specified")
}
if c.Host == "" {
return errors.New("server host not specified")
}
if c.Port <= 0 {
return errors.New("server port not specified")
}
if c.Protocol == "" {
return errors.New("server protocol not specified")
}
if c.AdditionalUserAgent != "" {
for _, x := range c.AdditionalUserAgent {
// Allow space(32) to ~(126) in ASCII Table, exclude "(34).
if int(x) < 32 || int(x) > 126 || int(x) == 32 || int(x) == 34 {
return errors.New("additional User-Agent contains characters that not allowed")
}
}
}
err := logger.CheckLevel(c.LogLevel)
if err != nil {
return err
}
return nil
}
// LoadDefaultConfig loads the default configuration for Config.
// It returns error if yaml decode failed.
func (c *Config) LoadDefaultConfig() error {
_, err := yaml.Decode([]byte(DefaultConfigFileContent), c)
if err != nil {
logger.Error("Config parse error: " + err.Error())
return err
}
logger.SetLevel(c.LogLevel)
return nil
}
// LoadUserConfig loads user configuration in ~/.qingstor/config.yaml for Config.
// It returns error if file not found.
func (c *Config) LoadUserConfig() error {
_, err := os.Stat(GetUserConfigFilePath())
if err != nil {
logger.Warn("Installing default config file to \"" + GetUserConfigFilePath() + "\"")
InstallDefaultUserConfig()
}
return c.LoadConfigFromFilePath(GetUserConfigFilePath())
}
// LoadConfigFromFilePath loads configuration from a specified local path.
// It returns error if file not found or yaml decode failed.
func (c *Config) LoadConfigFromFilePath(filepath string) error {
if strings.Index(filepath, "~/") == 0 {
filepath = strings.Replace(filepath, "~/", getHome()+"/", 1)
}
yamlString, err := ioutil.ReadFile(filepath)
if err != nil {
logger.Error("File not found: " + filepath)
return err
}
return c.LoadConfigFromContent(yamlString)
}
// LoadConfigFromContent loads configuration from a given byte slice.
// It returns error if yaml decode failed.
func (c *Config) LoadConfigFromContent(content []byte) error {
c.LoadDefaultConfig()
_, err := yaml.Decode(content, c)
if err != nil {
logger.Error("Config parse error: " + err.Error())
return err
}
err = c.Check()
if err != nil {
return err
}
logger.SetLevel(c.LogLevel)
return nil
}

View file

@ -0,0 +1,108 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package config
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/yunify/qingstor-sdk-go/logger"
)
func TestConfig(t *testing.T) {
c := Config{
AccessKeyID: "AccessKeyID",
SecretAccessKey: "SecretAccessKey",
Host: "qingstor.dev",
Port: 443,
Protocol: "https",
ConnectionRetries: 10,
LogLevel: "warn",
}
assert.Equal(t, "AccessKeyID", c.AccessKeyID)
assert.Equal(t, "SecretAccessKey", c.SecretAccessKey)
assert.Equal(t, "qingstor.dev", c.Host)
assert.Equal(t, 10, c.ConnectionRetries)
assert.Equal(t, "warn", c.LogLevel)
c.AdditionalUserAgent = `"`
assert.Error(t, c.Check())
c.AdditionalUserAgent = `test/user`
assert.NoError(t, c.Check())
}
func TestLoadDefaultConfig(t *testing.T) {
config := Config{}
config.LoadDefaultConfig()
assert.Equal(t, "", config.AccessKeyID)
assert.Equal(t, "", config.SecretAccessKey)
assert.Equal(t, "https", config.Protocol)
assert.Equal(t, "qingstor.com", config.Host)
assert.Equal(t, "", config.AdditionalUserAgent)
assert.Equal(t, "warning", logger.GetLevel())
}
func TestLoadUserConfig(t *testing.T) {
config := Config{}
config.LoadUserConfig()
assert.NotNil(t, config.Host)
assert.NotNil(t, config.Protocol)
}
func TestLoadConfigFromContent(t *testing.T) {
fileContent := `
access_key_id: 'access_key_id'
secret_access_key: 'secret_access_key'
log_level: 'debug'
`
config := Config{}
config.LoadConfigFromContent([]byte(fileContent))
assert.Equal(t, "access_key_id", config.AccessKeyID)
assert.Equal(t, "secret_access_key", config.SecretAccessKey)
assert.Equal(t, "https", config.Protocol)
assert.Equal(t, "qingstor.com", config.Host)
assert.Equal(t, "debug", logger.GetLevel())
}
func TestNewDefault(t *testing.T) {
config, err := NewDefault()
assert.Nil(t, err)
assert.Equal(t, "", config.AccessKeyID)
assert.Equal(t, "", config.SecretAccessKey)
assert.Equal(t, "https", config.Protocol)
assert.Equal(t, "qingstor.com", config.Host)
assert.Equal(t, 3, config.ConnectionRetries)
}
func TestNew(t *testing.T) {
config, err := New("AccessKeyID", "SecretAccessKey")
assert.Nil(t, err)
assert.Equal(t, "AccessKeyID", config.AccessKeyID)
assert.Equal(t, "SecretAccessKey", config.SecretAccessKey)
assert.Equal(t, "https", config.Protocol)
assert.Equal(t, "qingstor.com", config.Host)
}

View file

@ -0,0 +1,74 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package config
import (
"io/ioutil"
"os"
"path"
"runtime"
"strings"
)
// DefaultConfigFileContent is the content of default config file.
const DefaultConfigFileContent = `# QingStor services configuration
#access_key_id: ACCESS_KEY_ID
#secret_access_key: SECRET_ACCESS_KEY
host: qingstor.com
port: 443
protocol: https
connection_retries: 3
# Additional User-Agent
additional_user_agent: ""
# Valid log levels are "debug", "info", "warn", "error", and "fatal".
log_level: warn
`
// DefaultConfigFile is the filename of default config file.
const DefaultConfigFile = "~/.qingstor/config.yaml"
// GetUserConfigFilePath returns the user config file path.
func GetUserConfigFilePath() string {
return strings.Replace(DefaultConfigFile, "~/", getHome()+"/", 1)
}
// InstallDefaultUserConfig will install default config file.
func InstallDefaultUserConfig() error {
err := os.MkdirAll(path.Dir(GetUserConfigFilePath()), 0755)
if err != nil {
return err
}
return ioutil.WriteFile(GetUserConfigFilePath(), []byte(DefaultConfigFileContent), 0644)
}
func getHome() string {
home := os.Getenv("HOME")
if runtime.GOOS == "windows" {
home = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
}
return home
}

View file

@ -0,0 +1,69 @@
# Configuration Guide
## Summary
This SDK uses a structure called "Config" to store and manage configuration, read comments of public functions in ["config/config.go"](https://github.com/yunify/qingstor-sdk-go/blob/master/config/config.go) for details.
Except for Access Key, you can also configure the API endpoint for private cloud usage scenario. All available configurable items are listed in the default configuration file.
___Default Configuration File:___
``` yaml
# QingStor services configuration
access_key_id: 'ACCESS_KEY_ID'
secret_access_key: 'SECRET_ACCESS_KEY'
host: 'qingstor.com'
port: 443
protocol: 'https'
connection_retries: 3
# Valid log levels are "debug", "info", "warn", "error", and "fatal".
log_level: 'warn'
```
## Usage
Just create a config structure instance with your API Access Key, and initialize services you need with Init() function of the target service.
### Code Snippet
Create default configuration
``` go
defaultConfig, _ := config.NewDefault()
```
Create configuration from Access Key
``` go
configuration, _ := config.New("ACCESS_KEY_ID", "SECRET_ACCESS_KEY")
anotherConfiguration := config.NewDefault()
anotherConfiguration.AccessKeyID = "ACCESS_KEY_ID"
anotherConfiguration.SecretAccessKey = "SECRET_ACCESS_KEY"
```
Load user configuration
``` go
userConfig, _ := config.NewDefault().LoadUserConfig()
```
Load configuration from config file
``` go
configFromFile, _ := config.NewDefault().LoadConfigFromFilepath("PATH/TO/FILE")
```
Change API endpoint
``` go
moreConfiguration, _ := config.NewDefault()
moreConfiguration.Protocol = "http"
moreConfiguration.Host = "api.private.com"
moreConfiguration.Port = 80
```

View file

@ -0,0 +1,39 @@
# Installation Guide
## Requirement
This SDK requires Go 1.6 and higher vendor feature, the dependencies this project uses are included in the `vendor` directory. We use [glide](https://glide.sh) to manage project dependences.
___Notice:___ _You can also use Go 1.5 with the `GO15VENDOREXPERIMENT=1`._
## Install from source code
Use `go get` to download this SDK from GitHub:
``` bash
$ go get -u github.com/yunify/qingstor-sdk-go
```
You can also download a specified version of zipped source code in the repository [releases page](https://github.com/yunify/qingstor-sdk-go/releases). The zipped source code only contains golang source code without unit test files.
___Examples:___
- *[qingstor-sdk-go-source-v0.7.1.zip](https://github.com/yunify/qingstor-sdk-go/releases/download/v0.7.1/qingstor-sdk-go-source-v0.7.1.zip)*
- *[qingstor-sdk-go-source-with-vendor-v0.7.1.zip](https://github.com/yunify/qingstor-sdk-go/releases/download/v0.7.1/qingstor-sdk-go-source-with-vendor-v0.7.1.zip)*
## Install from binary release (deprecated)
After Go 1.7, there's a new feature called Binary-Only Package. It allows distributing packages in binary form without including the source code used for compiling the package. For more information about Binary-Only Package, please read [_GoLang Package Build_](https://golang.org/pkg/go/build/) to know how to use that.
We provide Linux, macOS and Windows binary packages along with a header files. A header file only contains three lines of content, "//go:binary-only-package" is the first line, the second line is blank, and the second is the package name. There's one header file named "binary.go" for each golang package.
You can download a specified version of zipped binary release in the repository [releases page](https://github.com/yunify/qingstor-sdk-go/releases).
___Notice:___ _We didn't provide 386 version binary packages, since there's almost no one using a 386 machine._
___Examples:___
- *[qingstor-sdk-go-header-v0.7.1-go-1.7.zip](https://github.com/yunify/qingstor-sdk-go/releases/download/v0.7.1/qingstor-sdk-go-header-v0.7.1-go-1.7.zip)*
- *[qingstor-sdk-go-binary-v0.7.1-linux_amd64-go-1.7.zip](https://github.com/yunify/qingstor-sdk-go/releases/download/v0.7.1/qingstor-sdk-go-binary-v0.7.1-linux_amd64-go-1.7.zip)*
- *[qingstor-sdk-go-binary-v0.7.1-darwin_amd64-go-1.7.zip](https://github.com/yunify/qingstor-sdk-go/releases/download/v0.7.1/qingstor-sdk-go-binary-v0.7.1-darwin_amd64-go-1.7.zip)*
- *[qingstor-sdk-go-binary-v0.7.1-windows_amd64-go-1.7.zip](https://github.com/yunify/qingstor-sdk-go/releases/download/v0.7.1/qingstor-sdk-go-binary-v0.7.1-windows_amd64-go-1.7.zip)*

View file

@ -0,0 +1,220 @@
# QingStor Service Usage Guide
Import the QingStor and initialize service with a config, and you are ready to use the initialized service. Service only contains one API, and it is "ListBuckets".
To use bucket related APIs, you need to initialize a bucket from service using "Bucket" function.
Each API function take a Input struct and return an Output struct. The Input struct consists of request params, request headers, request elements and request body, and the Output holds the HTTP status code, QingStor request ID, response headers, response elements, response body and error (if error occurred).
You can use a specified version of a service by import a service package with a date suffix.
``` go
import (
// Import the latest version API
qs "github.com/yunify/qingstor-sdk-go/service"
)
```
### Code Snippet
Initialize the QingStor service with a configuration
``` go
qsService, _ := qs.Init(configuration)
```
List buckets
``` go
qsOutput, _ := qsService.ListBuckets(nil)
// Print the HTTP status code.
// Example: 200
fmt.Println(qs.IntValue(qsOutput.StatusCode))
// Print the bucket count.
// Example: 5
fmt.Println(qs.IntValue(qsOutput.Count))
// Print the name of first bucket.
// Example: "test-bucket"
fmt.Println(qs.String(qsOutput.Buckets[0].Name))
```
Initialize a QingStor bucket
``` go
bucket, _ := qsService.Bucket("test-bucket", "pek3a")
```
List objects in the bucket
``` go
bOutput, _ := bucket.ListObjects(nil)
// Print the HTTP status code.
// Example: 200
fmt.Println(qs.IntValue(bOutput.StatusCode))
// Print the key count.
// Example: 7
fmt.Println(len(bOutput.Keys))
```
Set ACL of the bucket
``` go
bACLOutput, _ := bucket.PutACL(&qs.PutBucketACLInput{
ACL: []*service.ACLType{{
Grantee: &service.GranteeType{
Type: qs.String("user"),
ID: qs.String("usr-xxxxxxxx"),
},
Permission: qs.String("FULL_CONTROL"),
}},
})
// Print the HTTP status code.
// Example: 200
fmt.Println(qs.IntValue(bACLOutput.StatusCode))
```
Put object
``` go
// Open file
file, _ := os.Open("/tmp/Screenshot.jpg")
defer file.Close()
// Calculate MD5
hash := md5.New()
io.Copy(hash, file)
hashInBytes := hash.Sum(nil)[:16]
md5String := hex.EncodeToString(hashInBytes)
// Put object
oOutput, _ := bucket.PutObject(
"Screenshot.jpg",
&service.PutObjectInput{
ContentLength: qs.Int(102475), // Obtain automatically if empty
ContentType: qs.String("image/jpeg"), // Detect automatically if empty
ContentMD5: qs.String(md5String),
Body: file,
},
)
// Print the HTTP status code.
// Example: 201
fmt.Println(qs.IntValue(oOutput.StatusCode))
```
Delete object
``` go
oOutput, _ := bucket.DeleteObject("Screenshot.jpg")
// Print the HTTP status code.
// Example: 204
fmt.Println(qs.IntValue(oOutput.StatusCode))
```
Initialize Multipart Upload
``` go
aOutput, _ := bucket.InitiateMultipartUpload(
"QingCloudInsight.mov",
&service.InitiateMultipartUploadInput{
ContentType: qs.String("video/quicktime"),
},
)
// Print the HTTP status code.
// Example: 200
fmt.Println(qs.IntValue(aOutput.StatusCode))
// Print the upload ID.
// Example: "9d37dd6ccee643075ca4e597ad65655c"
fmt.Println(qs.StringValue(aOutput.UploadID))
```
Upload Multipart
``` go
aOutput, _ := bucket.UploadMultipart(
"QingCloudInsight.mov",
&service.UploadMultipartInput{
UploadID: qs.String("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"),
PartNumber: qs.Int(0),
ContentMD5: qs.String(md5String0),
Body: file0,
},
)
// Print the HTTP status code.
// Example: 201
fmt.Println(qs.IntValue(aOutput.StatusCode))
aOutput, _ = bucket.UploadMultipart(
"QingCloudInsight.mov",
&service.UploadMultipartInput{
UploadID: qs.String("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"),
PartNumber: qs.Int(1),
ContentMD5: qs.String(md5String1),
Body: file1,
},
)
// Print the HTTP status code.
// Example: 201
fmt.Println(qs.IntValue(aOutput.StatusCode))
aOutput, _ = bucket.UploadMultipart(
"QingCloudInsight.mov"
&service.UploadMultipartInput{
UploadID: qs.String("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"),
PartNumber: qs.Int(2),
ContentMD5: qs.String(md5String2),
Body: file2,
},
)
// Print the HTTP status code.
// Example: 201
fmt.Println(qs.IntValue(aOutput.StatusCode))
```
Complete Multipart Upload
``` go
aOutput, _ := bucket.CompleteMultipartUpload(
"QingCloudInsight.mov",
&service.CompleteMultipartUploadInput{
UploadID: qs.String("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"),
ObjectParts: []*service.ObjectPart{{
PartNumber: qs.Int(0),
}, {
PartNumber: qs.Int(1),
}, {
PartNumber: qs.Int(2),
}},
},
)
// Print the HTTP status code.
// Example: 200
fmt.Println(qs.IntValue(aOutput.StatusCode))
```
Abort Multipart Upload
``` go
aOutput, err := bucket.AbortMultipartUpload(
"QingCloudInsight.mov"
&service.AbortMultipartUploadInput{
UploadID: qs.String("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"),
},
)
// Print the error message.
// Example: QingStor Error: StatusCode 400, Code...
fmt.Println(err)
```

33
vendor/github.com/yunify/qingstor-sdk-go/glide.lock generated vendored Normal file
View file

@ -0,0 +1,33 @@
hash: 2e6b1ed4a2ee0638abc2e819ac3c247eaf80fa0f2053cfc41eecf883054c6032
updated: 2017-05-22T14:58:58.927797848+08:00
imports:
- name: github.com/pengsrc/go-shared
version: 454950d6a0782c34427d4f29b46c6bf447256f20
subpackages:
- check
- convert
- json
- yaml
- name: github.com/Sirupsen/logrus
version: d26492970760ca5d33129d2d799e34be5c4782eb
- name: golang.org/x/sys
version: f3918c30c5c2cb527c0b071a27c35120a6c0719a
repo: https://github.com/golang/sys.git
subpackages:
- unix
- name: gopkg.in/yaml.v2
version: a5b47d31c556af34a302ce5d659e6fea44d90de0
repo: https://github.com/go-yaml/yaml.git
testImports:
- name: github.com/davecgh/go-spew
version: 346938d642f2ec3594ed81d874461961cd0faa76
subpackages:
- spew
- name: github.com/pmezard/go-difflib
version: 792786c7400a136282c1664665ae0a8db921c6c2
subpackages:
- difflib
- name: github.com/stretchr/testify
version: 69483b4bd14f5845b5a1e55bca19e954e827f1d0
subpackages:
- assert

4
vendor/github.com/yunify/qingstor-sdk-go/glide.yaml generated vendored Normal file
View file

@ -0,0 +1,4 @@
package: github.com/yunify/qingstor-sdk-go
import:
- package: github.com/pengsrc/go-shared
version: v0.0.8

View file

@ -0,0 +1,119 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
// Package logger provides support for logging to stdout and stderr.
// Log entries will be logged with format: $timestamp $hostname [$pid]: $severity $message.
package logger
import (
"fmt"
"io"
"os"
"strings"
"time"
"github.com/Sirupsen/logrus"
)
var instance *logrus.Logger
// LogFormatter is used to format log entry.
type LogFormatter struct{}
// Format formats a given log entry, returns byte slice and error.
func (c *LogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
level := strings.ToUpper(entry.Level.String())
if level == "WARNING" {
level = "WARN"
}
if len(level) < 5 {
level = strings.Repeat(" ", 5-len(level)) + level
}
return []byte(fmt.Sprintf(
"[%s #%d] %s -- : %s\n",
time.Now().Format("2006-01-02T15:04:05.000Z"),
os.Getpid(),
level,
entry.Message)), nil
}
// SetOutput set the destination for the log output
func SetOutput(out io.Writer) {
instance.Out = out
}
// CheckLevel checks whether the log level is valid.
func CheckLevel(level string) error {
if _, err := logrus.ParseLevel(level); err != nil {
return fmt.Errorf(`log level not valid: "%s"`, level)
}
return nil
}
// GetLevel get the log level string.
func GetLevel() string {
return instance.Level.String()
}
// SetLevel sets the log level. Valid levels are "debug", "info", "warn", "error", and "fatal".
func SetLevel(level string) {
lvl, err := logrus.ParseLevel(level)
if err != nil {
Fatal(fmt.Sprintf(`log level not valid: "%s"`, level))
}
instance.Level = lvl
}
// Debug logs a message with severity DEBUG.
func Debug(format string, v ...interface{}) {
output(instance.Debug, format, v...)
}
// Info logs a message with severity INFO.
func Info(format string, v ...interface{}) {
output(instance.Info, format, v...)
}
// Warn logs a message with severity WARN.
func Warn(format string, v ...interface{}) {
output(instance.Warn, format, v...)
}
// Error logs a message with severity ERROR.
func Error(format string, v ...interface{}) {
output(instance.Error, format, v...)
}
// Fatal logs a message with severity ERROR followed by a call to os.Exit().
func Fatal(format string, v ...interface{}) {
output(instance.Fatal, format, v...)
}
func output(origin func(...interface{}), format string, v ...interface{}) {
if len(v) > 0 {
origin(fmt.Sprintf(format, v...))
} else {
origin(format)
}
}
func init() {
instance = logrus.New()
instance.Formatter = &LogFormatter{}
instance.Out = os.Stderr
instance.Level = logrus.WarnLevel
}

View file

@ -0,0 +1,303 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package builder
import (
"errors"
"fmt"
"io"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
"time"
"unicode"
"github.com/pengsrc/go-shared/convert"
"github.com/pengsrc/go-shared/json"
"github.com/yunify/qingstor-sdk-go/request/data"
"github.com/yunify/qingstor-sdk-go/utils"
)
// BaseBuilder is the base builder for all services.
type BaseBuilder struct {
parsedURL string
parsedProperties *map[string]string
parsedParams *map[string]string
parsedHeaders *map[string]string
parsedBodyString string
parsedBody io.Reader
operation *data.Operation
input *reflect.Value
}
// BuildHTTPRequest builds http request with an operation and an input.
func (b *BaseBuilder) BuildHTTPRequest(o *data.Operation, i *reflect.Value) (*http.Request, error) {
b.operation = o
b.input = i
_, err := b.parse()
if err != nil {
return nil, err
}
return b.build()
}
func (b *BaseBuilder) build() (*http.Request, error) {
httpRequest, err := http.NewRequest(b.operation.RequestMethod, b.parsedURL, b.parsedBody)
if err != nil {
return nil, err
}
err = b.setupHeaders(httpRequest)
if err != nil {
return nil, err
}
return httpRequest, nil
}
func (b *BaseBuilder) parse() (*BaseBuilder, error) {
err := b.parseRequestParamsAndHeaders()
if err != nil {
return b, err
}
err = b.parseRequestBody()
if err != nil {
return b, err
}
err = b.parseRequestProperties()
if err != nil {
return b, err
}
err = b.parseRequestURL()
if err != nil {
return b, err
}
return b, nil
}
func (b *BaseBuilder) parseRequestParamsAndHeaders() error {
requestParams := map[string]string{}
requestHeaders := map[string]string{}
maps := map[string](map[string]string){
"params": requestParams,
"headers": requestHeaders,
}
b.parsedParams = &requestParams
b.parsedHeaders = &requestHeaders
if !b.input.IsValid() {
return nil
}
fields := b.input.Elem()
if !fields.IsValid() {
return nil
}
for i := 0; i < fields.NumField(); i++ {
tagName := fields.Type().Field(i).Tag.Get("name")
tagLocation := fields.Type().Field(i).Tag.Get("location")
if tagDefault := fields.Type().Field(i).Tag.Get("default"); tagDefault != "" {
maps[tagLocation][tagName] = tagDefault
}
if tagName != "" && tagLocation != "" && maps[tagLocation] != nil {
switch value := fields.Field(i).Interface().(type) {
case *string:
if value != nil {
maps[tagLocation][tagName] = *value
}
case *int:
if value != nil {
maps[tagLocation][tagName] = strconv.Itoa(int(*value))
}
case *int64:
if value != nil {
maps[tagLocation][tagName] = strconv.FormatInt(int64(*value), 10)
}
case *bool:
case *time.Time:
if value != nil {
formatString := fields.Type().Field(i).Tag.Get("format")
format := ""
switch formatString {
case "RFC 822":
format = convert.RFC822
case "ISO 8601":
format = convert.ISO8601
}
maps[tagLocation][tagName] = convert.TimeToString(*value, format)
}
}
}
}
return nil
}
func (b *BaseBuilder) parseRequestBody() error {
requestData := map[string]interface{}{}
if !b.input.IsValid() {
return nil
}
fields := b.input.Elem()
if !fields.IsValid() {
return nil
}
for i := 0; i < fields.NumField(); i++ {
location := fields.Type().Field(i).Tag.Get("location")
if location == "elements" {
name := fields.Type().Field(i).Tag.Get("name")
requestData[name] = fields.Field(i).Interface()
}
}
if len(requestData) != 0 {
dataValue, err := json.Encode(requestData, true)
if err != nil {
return err
}
b.parsedBodyString = string(dataValue)
b.parsedBody = strings.NewReader(b.parsedBodyString)
(*b.parsedHeaders)["Content-Type"] = "application/json"
} else {
value := fields.FieldByName("Body")
if value.IsValid() {
switch value.Interface().(type) {
case string:
if value.String() != "" {
b.parsedBodyString = value.String()
b.parsedBody = strings.NewReader(value.String())
}
case io.Reader:
if value.Interface().(io.Reader) != nil {
b.parsedBody = value.Interface().(io.Reader)
}
}
}
}
return nil
}
func (b *BaseBuilder) parseRequestProperties() error {
propertiesMap := map[string]string{}
b.parsedProperties = &propertiesMap
if b.operation.Properties != nil {
fields := reflect.ValueOf(b.operation.Properties).Elem()
if fields.IsValid() {
for i := 0; i < fields.NumField(); i++ {
switch value := fields.Field(i).Interface().(type) {
case *string:
if value != nil {
propertiesMap[fields.Type().Field(i).Tag.Get("name")] = *value
}
case *int:
if value != nil {
numberString := strconv.Itoa(int(*value))
propertiesMap[fields.Type().Field(i).Tag.Get("name")] = numberString
}
}
}
}
}
return nil
}
func (b *BaseBuilder) parseRequestURL() error {
return nil
}
func (b *BaseBuilder) setupHeaders(httpRequest *http.Request) error {
if b.parsedHeaders != nil {
for headerKey, headerValue := range *b.parsedHeaders {
if headerKey == "X-QS-Fetch-Source" {
// header X-QS-Fetch-Source is a URL to fetch.
// We should first parse this URL.
requestURL, err := url.Parse(headerValue)
if err != nil {
return fmt.Errorf("invalid HTTP header value: %s", headerValue)
}
headerValue = requestURL.String()
} else {
for _, r := range headerValue {
if r > unicode.MaxASCII {
headerValue = utils.URLQueryEscape(headerValue)
break
}
}
}
httpRequest.Header.Set(headerKey, headerValue)
}
}
if httpRequest.Header.Get("Content-Length") == "" {
var length int64
switch body := b.parsedBody.(type) {
case nil:
length = 0
case io.Seeker:
//start, err := body.Seek(0, io.SeekStart)
start, err := body.Seek(0, 0)
if err != nil {
return err
}
//end, err := body.Seek(0, io.SeekEnd)
end, err := body.Seek(0, 2)
if err != nil {
return err
}
//body.Seek(0, io.SeekStart)
body.Seek(0, 0)
length = end - start
default:
return errors.New("Can not get Content-Length")
}
if length > 0 {
httpRequest.ContentLength = length
httpRequest.Header.Set("Content-Length", strconv.Itoa(int(length)))
} else {
httpRequest.Header.Set("Content-Length", "0")
}
}
length, err := strconv.Atoi(httpRequest.Header.Get("Content-Length"))
if err != nil {
return err
}
httpRequest.ContentLength = int64(length)
if httpRequest.Header.Get("Date") == "" {
httpRequest.Header.Set("Date", convert.TimeToString(time.Now(), convert.RFC822))
}
return nil
}

View file

@ -0,0 +1,125 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package builder
import (
"bytes"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/yunify/qingstor-sdk-go/config"
"github.com/yunify/qingstor-sdk-go/request/data"
)
type FakeProperties struct {
A *string `name:"a"`
B *string `name:"b"`
CD *int `name:"c-d"`
}
type FakeInput struct {
ParamA *string `location:"params" name:"a"`
ParamB *string `location:"params" name:"b"`
ParamCD *int `location:"params" name:"c_d" default:"1024"`
HeaderA *string `location:"headers" name:"A"`
HeaderB *time.Time `location:"headers" name:"B" format:"RFC 822"`
HeaderCD *int `location:"headers" name:"C-D"`
ElementA *string `location:"elements" name:"a"`
ElementB *string `location:"elements" name:"b"`
ElementCD *int64 `location:"elements" name:"cd"`
Body *string `localtion:"body"`
}
func (i *FakeInput) Validate() error {
return nil
}
func String(v string) *string {
return &v
}
func Int(v int) *int {
return &v
}
func Int64(v int64) *int64 {
return &v
}
func Time(v time.Time) *time.Time {
return &v
}
func TestBaseBuilder_BuildHTTPRequest(t *testing.T) {
conf, err := config.NewDefault()
assert.Nil(t, err)
tz, err := time.LoadLocation("Asia/Shanghai")
assert.Nil(t, err)
builder := BaseBuilder{}
operation := &data.Operation{
Config: conf,
APIName: "This is API name",
ServiceName: "Base",
Properties: &FakeProperties{
A: String("property_a"),
B: String("property_b"),
CD: Int(0),
},
RequestMethod: "GET",
RequestURI: "/hello/<a>/<c-d>/<b>/world",
StatusCodes: []int{
200,
201,
},
}
inputValue := reflect.ValueOf(&FakeInput{
ParamA: String("param_a"),
ParamCD: Int(1024),
HeaderA: String("header_a"),
HeaderB: Time(time.Date(2016, 9, 1, 15, 30, 0, 0, tz)),
ElementA: String("element_a"),
ElementB: String("element_b"),
ElementCD: Int64(0),
Body: String("This is body string"),
})
httpRequest, err := builder.BuildHTTPRequest(operation, &inputValue)
assert.Nil(t, err)
assert.Equal(t, &map[string]string{
"a": "property_a",
"b": "property_b",
"c-d": "0",
}, builder.parsedProperties)
assert.Equal(t, &map[string]string{
"a": "param_a",
"c_d": "1024",
}, builder.parsedParams)
assert.Equal(t, &map[string]string{
"A": "header_a",
"B": "Thu, 01 Sep 2016 07:30:00 GMT",
"Content-Type": "application/json",
}, builder.parsedHeaders)
assert.NotNil(t, httpRequest.Header.Get("Date"))
assert.Equal(t, "40", httpRequest.Header.Get("Content-Length"))
buffer := &bytes.Buffer{}
buffer.ReadFrom(httpRequest.Body)
httpRequest.Body.Close()
assert.Equal(t, "{\"a\":\"element_a\",\"b\":\"element_b\",\"cd\":0}", buffer.String())
}

View file

@ -0,0 +1,167 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package builder
import (
"bytes"
"crypto/md5"
"encoding/base64"
"fmt"
"io/ioutil"
"mime"
"net/http"
"net/url"
"path"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"github.com/pengsrc/go-shared/convert"
"github.com/yunify/qingstor-sdk-go"
"github.com/yunify/qingstor-sdk-go/logger"
"github.com/yunify/qingstor-sdk-go/request/data"
"github.com/yunify/qingstor-sdk-go/utils"
)
// QingStorBuilder is the request builder for QingStor service.
type QingStorBuilder struct {
baseBuilder *BaseBuilder
}
// BuildHTTPRequest builds http request with an operation and an input.
func (qb *QingStorBuilder) BuildHTTPRequest(o *data.Operation, i *reflect.Value) (*http.Request, error) {
qb.baseBuilder = &BaseBuilder{}
qb.baseBuilder.operation = o
qb.baseBuilder.input = i
_, err := qb.baseBuilder.parse()
if err != nil {
return nil, err
}
err = qb.parseURL()
if err != nil {
return nil, err
}
httpRequest, err := http.NewRequest(qb.baseBuilder.operation.RequestMethod,
qb.baseBuilder.parsedURL, qb.baseBuilder.parsedBody)
if err != nil {
return nil, err
}
err = qb.baseBuilder.setupHeaders(httpRequest)
if err != nil {
return nil, err
}
err = qb.setupHeaders(httpRequest)
if err != nil {
return nil, err
}
logger.Info(fmt.Sprintf(
"Built QingStor request: [%d] %s",
convert.StringToUnixTimestamp(httpRequest.Header.Get("Date"), convert.RFC822),
httpRequest.URL.String()),
)
logger.Info(fmt.Sprintf(
"QingStor request headers: [%d] %s",
convert.StringToUnixTimestamp(httpRequest.Header.Get("Date"), convert.RFC822),
fmt.Sprint(httpRequest.Header)),
)
if qb.baseBuilder.parsedBodyString != "" {
logger.Info(fmt.Sprintf(
"QingStor request body string: [%d] %s",
convert.StringToUnixTimestamp(httpRequest.Header.Get("Date"), convert.RFC822),
qb.baseBuilder.parsedBodyString),
)
}
return httpRequest, nil
}
func (qb *QingStorBuilder) parseURL() error {
config := qb.baseBuilder.operation.Config
zone := (*qb.baseBuilder.parsedProperties)["zone"]
port := strconv.Itoa(config.Port)
endpoint := config.Protocol + "://" + config.Host + ":" + port
if zone != "" {
endpoint = config.Protocol + "://" + zone + "." + config.Host + ":" + port
}
requestURI := qb.baseBuilder.operation.RequestURI
for key, value := range *qb.baseBuilder.parsedProperties {
endpoint = strings.Replace(endpoint, "<"+key+">", utils.URLQueryEscape(value), -1)
requestURI = strings.Replace(requestURI, "<"+key+">", utils.URLQueryEscape(value), -1)
}
requestURI = regexp.MustCompile(`/+`).ReplaceAllString(requestURI, "/")
requestURL, err := url.Parse(endpoint + requestURI)
if err != nil {
return err
}
if qb.baseBuilder.parsedParams != nil {
queryValue := requestURL.Query()
for key, value := range *qb.baseBuilder.parsedParams {
queryValue.Set(key, value)
}
requestURL.RawQuery = queryValue.Encode()
}
qb.baseBuilder.parsedURL = requestURL.String()
return nil
}
func (qb *QingStorBuilder) setupHeaders(httpRequest *http.Request) error {
method := httpRequest.Method
if method == "POST" || method == "PUT" || method == "DELETE" {
if httpRequest.Header.Get("Content-Type") == "" {
mimeType := mime.TypeByExtension(path.Ext(httpRequest.URL.Path))
if mimeType != "" {
httpRequest.Header.Set("Content-Type", mimeType)
}
}
}
if httpRequest.Header.Get("User-Agent") == "" {
version := fmt.Sprintf(`Go v%s`, strings.Replace(runtime.Version(), "go", "", -1))
system := fmt.Sprintf(`%s_%s_%s`, runtime.GOOS, runtime.GOARCH, runtime.Compiler)
ua := fmt.Sprintf(`qingstor-sdk-go/%s (%s; %s)`, sdk.Version, version, system)
if qb.baseBuilder.operation.Config.AdditionalUserAgent != "" {
ua = fmt.Sprintf(`%s %s`, ua, qb.baseBuilder.operation.Config.AdditionalUserAgent)
}
httpRequest.Header.Set("User-Agent", ua)
}
if qb.baseBuilder.operation.APIName == "Delete Multiple Objects" {
buffer := &bytes.Buffer{}
buffer.ReadFrom(httpRequest.Body)
httpRequest.Body = ioutil.NopCloser(bytes.NewReader(buffer.Bytes()))
md5Value := md5.Sum(buffer.Bytes())
httpRequest.Header.Set("Content-MD5", base64.StdEncoding.EncodeToString(md5Value[:]))
}
return nil
}

View file

@ -0,0 +1,83 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package builder
import (
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/yunify/qingstor-sdk-go/config"
"github.com/yunify/qingstor-sdk-go/request/data"
)
type ObjectSubServiceProperties struct {
BucketName *string `json:"bucket-name" name:"bucket-name"`
ObjectKey *string `json:"object-key" name:"object-key"`
Zone *string `json:"zone" name:"zone"`
}
type GetObjectInput struct {
IfMatch *string `json:"If-Match" name:"If-Match" location:"headers"`
IfModifiedSince *time.Time `json:"If-Modified-Since" name:"If-Modified-Since" format:"RFC 822" location:"headers"`
IfNoneMatch *string `json:"If-None-Match" name:"If-None-Match" location:"headers"`
IfUnmodifiedSince time.Time `json:"If-Unmodified-Since" name:"If-Unmodified-Since" format:"RFC 822" location:"headers"`
// Specified range of the Object
Range *string `json:"Range" name:"Range" location:"headers"`
}
func (i *GetObjectInput) Validate() error {
return nil
}
func TestQingStorBuilder_BuildHTTPRequest(t *testing.T) {
conf, err := config.NewDefault()
assert.Nil(t, err)
conf.Host = "qingstor.dev"
tz, err := time.LoadLocation("Asia/Shanghai")
assert.Nil(t, err)
qsBuilder := &QingStorBuilder{}
operation := &data.Operation{
Config: conf,
APIName: "GET Object",
ServiceName: "QingStor",
Properties: &ObjectSubServiceProperties{
BucketName: String("test"),
ObjectKey: String("path/to/key.txt"),
Zone: String("beta"),
},
RequestMethod: "GET",
RequestURI: "/<bucket-name>/<object-key>",
StatusCodes: []int{
201,
},
}
inputValue := reflect.ValueOf(&GetObjectInput{
IfModifiedSince: Time(time.Date(2016, 9, 1, 15, 30, 0, 0, tz)),
Range: String("100-"),
})
httpRequest, err := qsBuilder.BuildHTTPRequest(operation, &inputValue)
assert.Nil(t, err)
assert.NotNil(t, httpRequest.Header.Get("Date"))
assert.Equal(t, "0", httpRequest.Header.Get("Content-Length"))
assert.Equal(t, "", httpRequest.Header.Get("If-Match"))
assert.Equal(t, "Thu, 01 Sep 2016 07:30:00 GMT", httpRequest.Header.Get("If-Modified-Since"))
assert.Equal(t, "100-", httpRequest.Header.Get("Range"))
assert.Equal(t, "https://beta.qingstor.dev:443/test/path/to/key.txt", httpRequest.URL.String())
}

View file

@ -0,0 +1,22 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package data
// Input defines the interfaces that input should implement.
type Input interface {
Validation
}

View file

@ -0,0 +1,35 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package data
import (
"github.com/yunify/qingstor-sdk-go/config"
)
// Operation stores information of an operation.
type Operation struct {
Config *config.Config
Properties interface{}
APIName string
ServiceName string
RequestMethod string
RequestURI string
StatusCodes []int
}

View file

@ -0,0 +1,22 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package data
// Validation defines the validate interface.
type Validation interface {
Validate() error
}

View file

@ -0,0 +1,53 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package errors
import (
"fmt"
"strings"
)
// ParameterRequiredError indicates that the required parameter is missing.
type ParameterRequiredError struct {
ParameterName string
ParentName string
}
// Error returns the description of ParameterRequiredError.
func (e ParameterRequiredError) Error() string {
return fmt.Sprintf(`"%s" is required in "%s"`, e.ParameterName, e.ParentName)
}
// ParameterValueNotAllowedError indicates that the parameter value is not allowed.
type ParameterValueNotAllowedError struct {
ParameterName string
ParameterValue string
AllowedValues []string
}
// Error returns the description of ParameterValueNotAllowedError.
func (e ParameterValueNotAllowedError) Error() string {
allowedValues := []string{}
for _, value := range e.AllowedValues {
allowedValues = append(allowedValues, "\""+value+"\"")
}
return fmt.Sprintf(
`"%s" value "%s" is not allowed, should be one of %s`,
e.ParameterName,
e.ParameterValue,
strings.Join(allowedValues, ", "))
}

View file

@ -0,0 +1,36 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package errors
import "fmt"
// QingStorError stores information of an QingStor error response.
type QingStorError struct {
StatusCode int
Code string `json:"code"`
Message string `json:"message"`
RequestID string `json:"request_id"`
ReferenceURL string `json:"url"`
}
// Error returns the description of QingStor error response.
func (qse QingStorError) Error() string {
return fmt.Sprintf(
"QingStor Error: StatusCode \"%d\", Code \"%s\", Message \"%s\", Request ID \"%s\", Reference URL \"%s\"",
qse.StatusCode, qse.Code, qse.Message, qse.RequestID, qse.ReferenceURL)
}

View file

@ -0,0 +1,232 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package request
import (
"errors"
"fmt"
"net/http"
"reflect"
"time"
"github.com/pengsrc/go-shared/convert"
"github.com/yunify/qingstor-sdk-go/logger"
"github.com/yunify/qingstor-sdk-go/request/builder"
"github.com/yunify/qingstor-sdk-go/request/data"
"github.com/yunify/qingstor-sdk-go/request/signer"
"github.com/yunify/qingstor-sdk-go/request/unpacker"
)
// A Request can build, sign, send and unpack API request.
type Request struct {
Operation *data.Operation
Input *reflect.Value
Output *reflect.Value
HTTPRequest *http.Request
HTTPResponse *http.Response
}
// New create a Request from given Operation, Input and Output.
// It returns a Request.
func New(o *data.Operation, i data.Input, x interface{}) (*Request, error) {
input := reflect.ValueOf(i)
if input.IsValid() && input.Elem().IsValid() {
err := i.Validate()
if err != nil {
return nil, err
}
}
output := reflect.ValueOf(x)
return &Request{
Operation: o,
Input: &input,
Output: &output,
}, nil
}
// Send sends API request.
// It returns error if error occurred.
func (r *Request) Send() error {
err := r.check()
if err != nil {
return err
}
err = r.build()
if err != nil {
return err
}
err = r.sign()
if err != nil {
return err
}
err = r.send()
if err != nil {
return err
}
err = r.unpack()
if err != nil {
return err
}
return nil
}
// Sign sign the API request by setting the authorization header.
// It returns error if error occurred.
func (r *Request) Sign() error {
err := r.check()
if err != nil {
return err
}
err = r.build()
if err != nil {
return err
}
err = r.sign()
if err != nil {
return err
}
return nil
}
// SignQuery sign the API request by appending query string.
// It returns error if error occurred.
func (r *Request) SignQuery(timeoutSeconds int) error {
err := r.check()
if err != nil {
return err
}
err = r.build()
if err != nil {
return err
}
err = r.signQuery(int(time.Now().Unix()) + timeoutSeconds)
if err != nil {
return err
}
return nil
}
func (r *Request) check() error {
if r.Operation.Config.AccessKeyID == "" {
return errors.New("access key not provided")
}
if r.Operation.Config.SecretAccessKey == "" {
return errors.New("secret access key not provided")
}
return nil
}
func (r *Request) build() error {
b := &builder.QingStorBuilder{}
httpRequest, err := b.BuildHTTPRequest(r.Operation, r.Input)
if err != nil {
return err
}
r.HTTPRequest = httpRequest
return nil
}
func (r *Request) sign() error {
s := &signer.QingStorSigner{
AccessKeyID: r.Operation.Config.AccessKeyID,
SecretAccessKey: r.Operation.Config.SecretAccessKey,
}
err := s.WriteSignature(r.HTTPRequest)
if err != nil {
return err
}
return nil
}
func (r *Request) signQuery(expires int) error {
s := &signer.QingStorSigner{
AccessKeyID: r.Operation.Config.AccessKeyID,
SecretAccessKey: r.Operation.Config.SecretAccessKey,
}
err := s.WriteQuerySignature(r.HTTPRequest, expires)
if err != nil {
return err
}
return nil
}
func (r *Request) send() error {
var response *http.Response
var err error
if r.Operation.Config.Connection == nil {
return errors.New("connection not initialized")
}
retries := r.Operation.Config.ConnectionRetries + 1
for {
if retries > 0 {
logger.Info(fmt.Sprintf(
"Sending request: [%d] %s %s",
convert.StringToUnixTimestamp(r.HTTPRequest.Header.Get("Date"), convert.RFC822),
r.Operation.RequestMethod,
r.HTTPRequest.Host,
))
response, err = r.Operation.Config.Connection.Do(r.HTTPRequest)
if err == nil {
retries = 0
} else {
retries--
time.Sleep(time.Second)
}
} else {
break
}
}
if err != nil {
return err
}
r.HTTPResponse = response
return nil
}
func (r *Request) unpack() error {
u := &unpacker.QingStorUnpacker{}
err := u.UnpackHTTPRequest(r.Operation, r.HTTPResponse, r.Output)
if err != nil {
return err
}
return nil
}

View file

@ -0,0 +1,136 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package request
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/yunify/qingstor-sdk-go/config"
"github.com/yunify/qingstor-sdk-go/logger"
"github.com/yunify/qingstor-sdk-go/request/data"
"github.com/yunify/qingstor-sdk-go/request/errors"
)
type SomeActionProperties struct {
A *string `json:"a" name:"a"`
B *string `json:"b" name:"b"`
CD *string `json:"c-d" name:"c-d"`
}
type SomeActionInput struct {
Date *time.Time `json:"Date" name:"Date" format:"RFC 822" location:"headers"`
IfModifiedSince *time.Time `json:"If-Modified-Since" name:"If-Modified-Since" format:"RFC 822" location:"headers"`
Range *string `json:"Range" name:"Range" location:"headers"`
UploadID *string `json:"upload_id" name:"upload_id" location:"params"`
Count *int `json:"count" name:"count" location:"elements"`
}
func (s *SomeActionInput) Validate() error {
return nil
}
type SomeActionOutput struct {
StatusCode *int `location:"statusCode"`
Error *errors.QingStorError
RequestID *string `location:"requestID"`
}
func String(v string) *string {
return &v
}
func Int(v int) *int {
return &v
}
func Time(v time.Time) *time.Time {
return &v
}
func TestRequest_Send(t *testing.T) {
conf, err := config.New("ACCESS_KEY_ID", "SECRET_ACCESS_KEY")
assert.Nil(t, err)
logger.SetLevel("warn")
operation := &data.Operation{
Config: conf,
Properties: &SomeActionProperties{
A: String("aaa"),
B: String("bbb"),
CD: String("ccc-ddd"),
},
APIName: "Some Action",
RequestMethod: "GET",
RequestURI: "/<a>/<b>/<c-d>",
StatusCodes: []int{
200, // OK
206, // Partial content
304, // Not modified
412, // Precondition failed
},
}
output := &SomeActionOutput{}
r, err := New(operation, &SomeActionInput{
Date: Time(time.Date(2016, 9, 1, 15, 30, 0, 0, time.UTC)),
IfModifiedSince: Time(time.Date(2016, 9, 1, 15, 30, 0, 0, time.UTC)),
Range: String("100-"),
UploadID: String("0"),
Count: Int(23),
}, output)
assert.Nil(t, err)
err = r.build()
assert.Nil(t, err)
err = r.sign()
assert.Nil(t, err)
assert.Equal(t, r.HTTPRequest.URL.String(), "https://qingstor.com:443/aaa/bbb/ccc-ddd?upload_id=0")
assert.Equal(t, r.HTTPRequest.Header.Get("Range"), "100-")
assert.Equal(t, r.HTTPRequest.Header.Get("If-Modified-Since"), "Thu, 01 Sep 2016 15:30:00 GMT")
assert.Equal(t, r.HTTPRequest.Header.Get("Content-Length"), "12")
assert.Equal(t, r.HTTPRequest.Header.Get("Authorization"), "QS ACCESS_KEY_ID:pA7G9qo4iQ6YHu7p4fX9Wcg4V9S6Mcgvz7p/0wEdz78=")
httpResponse := &http.Response{Header: http.Header{}}
httpResponse.StatusCode = 400
httpResponse.Header.Set("Content-Type", "application/json")
responseString := `{
"code": "bad_request",
"message": "Invalid argument(s) or invalid argument value(s)",
"request_id": "1e588695254aa08cf7a43f612e6ce14b",
"url": "http://docs.qingcloud.com/object_storage/api/object/get.html"
}`
httpResponse.Body = ioutil.NopCloser(bytes.NewReader([]byte(responseString)))
assert.Nil(t, err)
r.HTTPResponse = httpResponse
err = r.unpack()
assert.NotNil(t, err)
switch e := err.(type) {
case *errors.QingStorError:
assert.Equal(t, "bad_request", e.Code)
assert.Equal(t, "1e588695254aa08cf7a43f612e6ce14b", e.RequestID)
}
}

View file

@ -0,0 +1,259 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package signer
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"net/http"
"sort"
"strings"
"github.com/pengsrc/go-shared/convert"
"github.com/yunify/qingstor-sdk-go/logger"
"github.com/yunify/qingstor-sdk-go/utils"
)
// QingStorSigner is the http request signer for QingStor service.
type QingStorSigner struct {
AccessKeyID string
SecretAccessKey string
}
// WriteSignature calculates signature and write it to http request header.
func (qss *QingStorSigner) WriteSignature(request *http.Request) error {
authorization, err := qss.BuildSignature(request)
if err != nil {
return err
}
request.Header.Set("Authorization", authorization)
return nil
}
// WriteQuerySignature calculates signature and write it to http request url.
func (qss *QingStorSigner) WriteQuerySignature(request *http.Request, expires int) error {
query, err := qss.BuildQuerySignature(request, expires)
if err != nil {
return err
}
if request.URL.RawQuery != "" {
query = "?" + request.URL.RawQuery + "&" + query
} else {
query = "?" + query
}
newRequest, err := http.NewRequest(request.Method,
request.URL.Scheme+"://"+request.URL.Host+utils.URLQueryEscape(request.URL.Path)+query, nil)
if err != nil {
return err
}
request.URL = newRequest.URL
return nil
}
// BuildSignature calculates the signature string.
func (qss *QingStorSigner) BuildSignature(request *http.Request) (string, error) {
stringToSign, err := qss.BuildStringToSign(request)
if err != nil {
return "", err
}
h := hmac.New(sha256.New, []byte(qss.SecretAccessKey))
h.Write([]byte(stringToSign))
signature := strings.TrimSpace(base64.StdEncoding.EncodeToString(h.Sum(nil)))
authorization := "QS " + qss.AccessKeyID + ":" + signature
logger.Debug(fmt.Sprintf(
"QingStor authorization: [%d] %s",
convert.StringToUnixTimestamp(request.Header.Get("Date"), convert.RFC822),
authorization),
)
return authorization, nil
}
// BuildQuerySignature calculates the signature string for query.
func (qss *QingStorSigner) BuildQuerySignature(request *http.Request, expires int) (string, error) {
stringToSign, err := qss.BuildQueryStringToSign(request, expires)
if err != nil {
return "", err
}
h := hmac.New(sha256.New, []byte(qss.SecretAccessKey))
h.Write([]byte(stringToSign))
signature := strings.TrimSpace(base64.StdEncoding.EncodeToString(h.Sum(nil)))
signature = utils.URLQueryEscape(signature)
query := fmt.Sprintf(
"access_key_id=%s&expires=%d&signature=%s",
qss.AccessKeyID, expires, signature,
)
logger.Debug(fmt.Sprintf(
"QingStor query signature: [%d] %s",
convert.StringToUnixTimestamp(request.Header.Get("Date"), convert.RFC822),
query,
))
return query, nil
}
// BuildStringToSign build the string to sign.
func (qss *QingStorSigner) BuildStringToSign(request *http.Request) (string, error) {
stringToSign := fmt.Sprintf(
"%s\n%s\n%s\n%s\n",
request.Method,
request.Header.Get("Content-MD5"),
request.Header.Get("Content-Type"),
request.Header.Get("Date"),
)
stringToSign += qss.buildCanonicalizedHeaders(request)
canonicalizedResource, err := qss.buildCanonicalizedResource(request)
if err != nil {
return "", err
}
stringToSign += canonicalizedResource
logger.Debug(fmt.Sprintf(
"QingStor string to sign: [%d] %s",
convert.StringToUnixTimestamp(request.Header.Get("Date"), convert.RFC822),
stringToSign,
))
return stringToSign, nil
}
// BuildQueryStringToSign build the string to sign for query.
func (qss *QingStorSigner) BuildQueryStringToSign(request *http.Request, expires int) (string, error) {
stringToSign := fmt.Sprintf(
"%s\n%s\n%s\n%d\n",
request.Method,
request.Header.Get("Content-MD5"),
request.Header.Get("Content-Type"),
expires,
)
stringToSign += qss.buildCanonicalizedHeaders(request)
canonicalizedResource, err := qss.buildCanonicalizedResource(request)
if err != nil {
return "", err
}
stringToSign += canonicalizedResource
logger.Debug(fmt.Sprintf(
"QingStor query string to sign: [%d] %s",
convert.StringToUnixTimestamp(request.Header.Get("Date"), convert.RFC822),
stringToSign,
))
return stringToSign, nil
}
func (qss *QingStorSigner) buildCanonicalizedHeaders(request *http.Request) string {
keys := []string{}
for key := range request.Header {
if strings.HasPrefix(strings.ToLower(key), "x-qs-") {
keys = append(keys, strings.TrimSpace(strings.ToLower(key)))
}
}
sort.Strings(keys)
canonicalizedHeaders := ""
for _, key := range keys {
canonicalizedHeaders += key + ":" + strings.TrimSpace(request.Header.Get(key)) + "\n"
}
return canonicalizedHeaders
}
func (qss *QingStorSigner) buildCanonicalizedResource(request *http.Request) (string, error) {
path := utils.URLQueryEscape(request.URL.Path)
query := request.URL.Query()
keys := []string{}
for key := range query {
keys = append(keys, key)
}
sort.Strings(keys)
parts := []string{}
for _, key := range keys {
values := query[key]
if qss.paramsToSign(key) {
if len(values) > 0 {
if values[0] != "" {
value := strings.TrimSpace(strings.Join(values, ""))
value, err := utils.URLQueryUnescape(value)
if err != nil {
return "", err
}
parts = append(parts, key+"="+value)
} else {
parts = append(parts, key)
}
} else {
parts = append(parts, key)
}
}
}
joinedParts := strings.Join(parts, "&")
if joinedParts != "" {
path = path + "?" + joinedParts
}
logger.Debug(fmt.Sprintf(
"QingStor canonicalized resource: [%d] %s",
convert.StringToUnixTimestamp(request.Header.Get("Date"), convert.RFC822),
path,
))
return path, nil
}
func (qss *QingStorSigner) paramsToSign(key string) bool {
keysMap := map[string]bool{
"acl": true,
"cors": true,
"delete": true,
"mirror": true,
"part_number": true,
"policy": true,
"stats": true,
"upload_id": true,
"uploads": true,
"response-expires": true,
"response-cache-control": true,
"response-content-type": true,
"response-content-language": true,
"response-content-encoding": true,
"response-content-disposition": true,
}
return keysMap[key]
}

View file

@ -0,0 +1,85 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package signer
import (
"net/http"
"testing"
"time"
"github.com/pengsrc/go-shared/convert"
"github.com/stretchr/testify/assert"
)
func TestQingStorSignerWriteSignature(t *testing.T) {
url := "https://qingstor.com/?acl&upload_id=fde133b5f6d932cd9c79bac3c7318da1&part_number=0&other=abc"
httpRequest, err := http.NewRequest("GET", url, nil)
httpRequest.Header.Set("Date", convert.TimeToString(time.Time{}, convert.RFC822))
httpRequest.Header.Set("X-QS-Test-2", "Test 2")
httpRequest.Header.Set("X-QS-Test-1", "Test 1")
assert.Nil(t, err)
s := QingStorSigner{
AccessKeyID: "ENV_ACCESS_KEY_ID",
SecretAccessKey: "ENV_SECRET_ACCESS_KEY",
}
err = s.WriteSignature(httpRequest)
assert.Nil(t, err)
signature := "QS ENV_ACCESS_KEY_ID:bvglZF9iMOv1RaCTxPYWxexmt1UN2m5WKngYnhDEp2c="
assert.Equal(t, signature, httpRequest.Header.Get("Authorization"))
}
func TestQingStorSignerWriteSignatureChinese(t *testing.T) {
url := "https://zone.qingstor.com/bucket-name/中文"
httpRequest, err := http.NewRequest("GET", url, nil)
httpRequest.Header.Set("Date", convert.TimeToString(time.Time{}, convert.RFC822))
assert.Nil(t, err)
s := QingStorSigner{
AccessKeyID: "ENV_ACCESS_KEY_ID",
SecretAccessKey: "ENV_SECRET_ACCESS_KEY",
}
err = s.WriteSignature(httpRequest)
assert.Nil(t, err)
signature := "QS ENV_ACCESS_KEY_ID:XsTXX50kzqBf92zLG1aIUIJmZ0hqIHoaHgkumwnV3fs="
assert.Equal(t, signature, httpRequest.Header.Get("Authorization"))
}
func TestQingStorSignerWriteQuerySignature(t *testing.T) {
url := "https://qingstor.com/?acl&upload_id=fde133b5f6d932cd9c79bac3c7318da1&part_number=0"
httpRequest, err := http.NewRequest("GET", url, nil)
httpRequest.Header.Set("Date", convert.TimeToString(time.Time{}, convert.RFC822))
httpRequest.Header.Set("X-QS-Test-2", "Test 2")
httpRequest.Header.Set("X-QS-Test-1", "Test 1")
assert.Nil(t, err)
s := QingStorSigner{
AccessKeyID: "ENV_ACCESS_KEY_ID",
SecretAccessKey: "ENV_SECRET_ACCESS_KEY",
}
err = s.WriteQuerySignature(httpRequest, 3600)
assert.Nil(t, err)
targetURL := "https://qingstor.com/?acl&upload_id=fde133b5f6d932cd9c79bac3c7318da1&part_number=0&access_key_id=ENV_ACCESS_KEY_ID&expires=3600&signature=GRL3p3NOgHR9CQygASvyo344vdnO1hFke6ZvQ5mDVHM="
assert.Equal(t, httpRequest.URL.String(), targetURL)
}

View file

@ -0,0 +1,201 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package unpacker
import (
"bytes"
"fmt"
"net/http"
"reflect"
"strconv"
"time"
"github.com/pengsrc/go-shared/convert"
"github.com/pengsrc/go-shared/json"
"github.com/yunify/qingstor-sdk-go/logger"
"github.com/yunify/qingstor-sdk-go/request/data"
)
// BaseUnpacker is the base unpacker for all services.
type BaseUnpacker struct {
operation *data.Operation
httpResponse *http.Response
output *reflect.Value
}
// UnpackHTTPRequest unpacks http response with an operation and an output.
func (b *BaseUnpacker) UnpackHTTPRequest(o *data.Operation, r *http.Response, x *reflect.Value) error {
b.operation = o
b.httpResponse = r
b.output = x
err := b.exposeStatusCode()
if err != nil {
return err
}
err = b.parseResponseHeaders()
if err != nil {
return err
}
err = b.parseResponseBody()
if err != nil {
return err
}
err = b.parseResponseElements()
if err != nil {
return err
}
return nil
}
func (b *BaseUnpacker) exposeStatusCode() error {
value := b.output.Elem().FieldByName("StatusCode")
if value.IsValid() {
switch value.Interface().(type) {
case *int:
logger.Info(fmt.Sprintf(
"QingStor response status code: [%d] %d",
convert.StringToUnixTimestamp(b.httpResponse.Header.Get("Date"), convert.RFC822),
b.httpResponse.StatusCode,
))
value.Set(reflect.ValueOf(&b.httpResponse.StatusCode))
}
}
return nil
}
func (b *BaseUnpacker) parseResponseHeaders() error {
logger.Info(fmt.Sprintf(
"QingStor response headers: [%d] %s",
convert.StringToUnixTimestamp(b.httpResponse.Header.Get("Date"), convert.RFC822),
fmt.Sprint(b.httpResponse.Header),
))
if b.isResponseRight() {
fields := b.output.Elem()
for i := 0; i < fields.NumField(); i++ {
field := fields.Field(i)
fieldTagName := fields.Type().Field(i).Tag.Get("name")
fieldTagLocation := fields.Type().Field(i).Tag.Get("location")
fieldStringValue := b.httpResponse.Header.Get(fieldTagName)
if fieldTagName != "" && fieldTagLocation == "headers" {
switch field.Interface().(type) {
case *string:
field.Set(reflect.ValueOf(&fieldStringValue))
case *int:
intValue, err := strconv.Atoi(fieldStringValue)
if err != nil {
return err
}
field.Set(reflect.ValueOf(&intValue))
case *int64:
int64Value, err := strconv.ParseInt(fieldStringValue, 10, 64)
if err != nil {
return err
}
field.Set(reflect.ValueOf(&int64Value))
case *bool:
case *time.Time:
formatString := fields.Type().Field(i).Tag.Get("format")
format := ""
switch formatString {
case "RFC 822":
format = convert.RFC822
case "ISO 8601":
format = convert.ISO8601
}
timeValue, err := convert.StringToTime(fieldStringValue, format)
if err != nil {
return err
}
field.Set(reflect.ValueOf(&timeValue))
}
}
}
}
return nil
}
func (b *BaseUnpacker) parseResponseBody() error {
if b.isResponseRight() {
value := b.output.Elem().FieldByName("Body")
if value.IsValid() {
switch value.Type().String() {
case "string":
buffer := &bytes.Buffer{}
buffer.ReadFrom(b.httpResponse.Body)
b.httpResponse.Body.Close()
logger.Info(fmt.Sprintf(
"QingStor response body string: [%d] %s",
convert.StringToUnixTimestamp(b.httpResponse.Header.Get("Date"), convert.RFC822),
string(buffer.Bytes()),
))
value.SetString(string(buffer.Bytes()))
case "io.ReadCloser":
value.Set(reflect.ValueOf(b.httpResponse.Body))
}
}
}
return nil
}
func (b *BaseUnpacker) parseResponseElements() error {
if b.isResponseRight() {
if b.httpResponse.Header.Get("Content-Type") == "application/json" {
buffer := &bytes.Buffer{}
buffer.ReadFrom(b.httpResponse.Body)
b.httpResponse.Body.Close()
logger.Info(fmt.Sprintf(
"QingStor response body string: [%d] %s",
convert.StringToUnixTimestamp(b.httpResponse.Header.Get("Date"), convert.RFC822),
string(buffer.Bytes()),
))
_, err := json.Decode(buffer.Bytes(), b.output.Interface())
if err != nil {
return err
}
}
}
return nil
}
func (b *BaseUnpacker) isResponseRight() bool {
rightStatusCodes := b.operation.StatusCodes
if len(rightStatusCodes) == 0 {
rightStatusCodes = append(rightStatusCodes, 200)
}
flag := false
for _, statusCode := range rightStatusCodes {
if statusCode == b.httpResponse.StatusCode {
flag = true
}
}
return flag
}

View file

@ -0,0 +1,85 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package unpacker
import (
"bytes"
"io/ioutil"
"net/http"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/yunify/qingstor-sdk-go/request/data"
)
func StringValue(v *string) string {
if v != nil {
return *v
}
return ""
}
func IntValue(v *int) int {
if v != nil {
return *v
}
return 0
}
func Int64Value(v *int64) int64 {
if v != nil {
return *v
}
return 0
}
func TimeValue(v *time.Time) time.Time {
if v != nil {
return *v
}
return time.Time{}
}
func TestBaseUnpacker_UnpackHTTPRequest(t *testing.T) {
type FakeOutput struct {
StatusCode *int
A *string `location:"elements" json:"a" name:"a"`
B *string `location:"elements" json:"b" name:"b"`
CD *int `location:"elements" json:"cd" name:"cd"`
EF *int64 `location:"elements" json:"ef" name:"ef"`
}
httpResponse := &http.Response{Header: http.Header{}}
httpResponse.StatusCode = 200
httpResponse.Header.Set("Content-Type", "application/json")
responseString := `{"a": "el_a", "b": "el_b", "cd": 1024, "ef": 2048}`
httpResponse.Body = ioutil.NopCloser(bytes.NewReader([]byte(responseString)))
output := &FakeOutput{}
outputValue := reflect.ValueOf(output)
unpacker := BaseUnpacker{}
err := unpacker.UnpackHTTPRequest(&data.Operation{}, httpResponse, &outputValue)
assert.Nil(t, err)
assert.Equal(t, 200, IntValue(output.StatusCode))
assert.Equal(t, "el_a", StringValue(output.A))
assert.Equal(t, "el_b", StringValue(output.B))
assert.Equal(t, 1024, IntValue(output.CD))
assert.Equal(t, int64(2048), Int64Value(output.EF))
}

View file

@ -0,0 +1,72 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package unpacker
import (
"bytes"
"net/http"
"reflect"
"github.com/pengsrc/go-shared/json"
"github.com/yunify/qingstor-sdk-go/request/data"
"github.com/yunify/qingstor-sdk-go/request/errors"
)
// QingStorUnpacker is the response unpacker for QingStor service.
type QingStorUnpacker struct {
baseUnpacker *BaseUnpacker
}
// UnpackHTTPRequest unpack the http response with an operation, http response and an output.
func (qu *QingStorUnpacker) UnpackHTTPRequest(o *data.Operation, r *http.Response, x *reflect.Value) error {
qu.baseUnpacker = &BaseUnpacker{}
err := qu.baseUnpacker.UnpackHTTPRequest(o, r, x)
if err != nil {
return err
}
err = qu.parseError()
if err != nil {
return err
}
return nil
}
func (qu *QingStorUnpacker) parseError() error {
if !qu.baseUnpacker.isResponseRight() {
if qu.baseUnpacker.httpResponse.Header.Get("Content-Type") == "application/json" {
buffer := &bytes.Buffer{}
buffer.ReadFrom(qu.baseUnpacker.httpResponse.Body)
qu.baseUnpacker.httpResponse.Body.Close()
qsError := &errors.QingStorError{}
if buffer.Len() > 0 {
_, err := json.Decode(buffer.Bytes(), qsError)
if err != nil {
return err
}
}
qsError.StatusCode = qu.baseUnpacker.httpResponse.StatusCode
return qsError
}
}
return nil
}

View file

@ -0,0 +1,124 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package unpacker
import (
"bytes"
"io/ioutil"
"net/http"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/yunify/qingstor-sdk-go/request/data"
"github.com/yunify/qingstor-sdk-go/request/errors"
)
func TestQingStorUnpacker_UnpackHTTPRequest(t *testing.T) {
type Bucket struct {
// Created time of the Bucket
Created *time.Time `json:"created" name:"created" format:"RFC 822"`
// QingCloud Zone ID
Location *string `json:"location" name:"location"`
// Bucket name
Name *string `json:"name" name:"name"`
// URL to access the Bucket
URL *string `json:"url" name:"url"`
}
type ListBucketsOutput struct {
StatusCode *int `location:"statusCode"`
Error *errors.QingStorError
RequestID *string `location:"requestID"`
XTestHeader *string `json:"X-Test-Header" name:"X-Test-Header" location:"headers"`
XTestTime *time.Time `json:"X-Test-Time" name:"X-Test-Time" format:"RFC 822" location:"headers"`
// Buckets information
Buckets []*Bucket `json:"buckets" name:"buckets"`
// Bucket count
Count *int `json:"count" name:"count"`
}
httpResponse := &http.Response{Header: http.Header{
"X-Test-Header": []string{"test-header"},
"X-Test-Time": []string{"Thu, 01 Sep 2016 07:30:00 GMT"},
}}
httpResponse.StatusCode = 200
httpResponse.Header.Set("Content-Type", "application/json")
responseString := `{
"count": 2,
"buckets": [
{
"name": "test-bucket",
"location": "pek3a",
"url": "https://test-bucket.pek3a.qingstor.com",
"created": "2015-07-11T04:45:57Z"
},
{
"name": "test-photos",
"location": "pek3a",
"url": "https://test-photos.pek3a.qingstor.com",
"created": "2015-07-12T09:40:32Z"
}
]
}`
httpResponse.Body = ioutil.NopCloser(bytes.NewReader([]byte(responseString)))
output := &ListBucketsOutput{}
outputValue := reflect.ValueOf(output)
unpacker := QingStorUnpacker{}
err := unpacker.UnpackHTTPRequest(&data.Operation{}, httpResponse, &outputValue)
assert.Nil(t, err)
assert.Equal(t, "test-header", StringValue(output.XTestHeader))
assert.Equal(t, time.Date(2016, 9, 1, 7, 30, 0, 0, time.UTC), TimeValue(output.XTestTime))
assert.Equal(t, 2, IntValue(output.Count))
assert.Equal(t, "test-bucket", StringValue(output.Buckets[0].Name))
assert.Equal(t, "pek3a", StringValue(output.Buckets[0].Location))
assert.Equal(t, time.Date(2015, 7, 12, 9, 40, 32, 0, time.UTC), TimeValue(output.Buckets[1].Created))
}
func TestQingStorUnpacker_UnpackHTTPRequestWithError(t *testing.T) {
type ListBucketsOutput struct {
StatusCode *int `location:"statusCode"`
Error *errors.QingStorError
RequestID *string `location:"requestID"`
}
httpResponse := &http.Response{Header: http.Header{}}
httpResponse.StatusCode = 400
httpResponse.Header.Set("Content-Type", "application/json")
responseString := `{
"code": "bad_request",
"message": "Invalid argument(s) or invalid argument value(s)",
"request_id": "aa08cf7a43f611e5886952542e6ce14b",
"url": "http://docs.qingcloud.com/object_storage/api/bucket/get.html"
}`
httpResponse.Body = ioutil.NopCloser(bytes.NewReader([]byte(responseString)))
output := &ListBucketsOutput{}
outputValue := reflect.ValueOf(output)
unpacker := QingStorUnpacker{}
err := unpacker.UnpackHTTPRequest(&data.Operation{}, httpResponse, &outputValue)
assert.NotNil(t, err)
switch e := err.(type) {
case *errors.QingStorError:
assert.Equal(t, "bad_request", e.Code)
assert.Equal(t, "aa08cf7a43f611e5886952542e6ce14b", e.RequestID)
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,266 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package service
import (
"time"
)
// String returns a pointer to the given string value.
func String(v string) *string {
return &v
}
// StringValue returns the value of the given string pointer or
// "" if the pointer is nil.
func StringValue(v *string) string {
if v != nil {
return *v
}
return ""
}
// StringSlice converts a slice of string values into a slice of
// string pointers
func StringSlice(src []string) []*string {
dst := make([]*string, len(src))
for i := 0; i < len(src); i++ {
dst[i] = &(src[i])
}
return dst
}
// StringValueSlice converts a slice of string pointers into a slice of
// string values
func StringValueSlice(src []*string) []string {
dst := make([]string, len(src))
for i := 0; i < len(src); i++ {
if src[i] != nil {
dst[i] = *(src[i])
}
}
return dst
}
// StringMap converts a string map of string values into a string
// map of string pointers
func StringMap(src map[string]string) map[string]*string {
dst := make(map[string]*string)
for k, val := range src {
v := val
dst[k] = &v
}
return dst
}
// StringValueMap converts a string map of string pointers into a string
// map of string values
func StringValueMap(src map[string]*string) map[string]string {
dst := make(map[string]string)
for k, val := range src {
if val != nil {
dst[k] = *val
}
}
return dst
}
// Bool returns a pointer to the given bool value.
func Bool(v bool) *bool {
return &v
}
// BoolValue returns the value of the given bool pointer or
// false if the pointer is nil.
func BoolValue(v *bool) bool {
if v != nil {
return *v
}
return false
}
// BoolSlice converts a slice of bool values into a slice of
// bool pointers
func BoolSlice(src []bool) []*bool {
dst := make([]*bool, len(src))
for i := 0; i < len(src); i++ {
dst[i] = &(src[i])
}
return dst
}
// BoolValueSlice converts a slice of bool pointers into a slice of
// bool values
func BoolValueSlice(src []*bool) []bool {
dst := make([]bool, len(src))
for i := 0; i < len(src); i++ {
if src[i] != nil {
dst[i] = *(src[i])
}
}
return dst
}
// BoolMap converts a string map of bool values into a string
// map of bool pointers
func BoolMap(src map[string]bool) map[string]*bool {
dst := make(map[string]*bool)
for k, val := range src {
v := val
dst[k] = &v
}
return dst
}
// BoolValueMap converts a string map of bool pointers into a string
// map of bool values
func BoolValueMap(src map[string]*bool) map[string]bool {
dst := make(map[string]bool)
for k, val := range src {
if val != nil {
dst[k] = *val
}
}
return dst
}
// Int returns a pointer to the given int value.
func Int(v int) *int {
return &v
}
// IntValue returns the value of the given int pointer or
// 0 if the pointer is nil.
func IntValue(v *int) int {
if v != nil {
return *v
}
return 0
}
// IntSlice converts a slice of int values into a slice of
// int pointers
func IntSlice(src []int) []*int {
dst := make([]*int, len(src))
for i := 0; i < len(src); i++ {
dst[i] = &(src[i])
}
return dst
}
// IntValueSlice converts a slice of int pointers into a slice of
// int values
func IntValueSlice(src []*int) []int {
dst := make([]int, len(src))
for i := 0; i < len(src); i++ {
if src[i] != nil {
dst[i] = *(src[i])
}
}
return dst
}
// IntMap converts a string map of int values into a string
// map of int pointers
func IntMap(src map[string]int) map[string]*int {
dst := make(map[string]*int)
for k, val := range src {
v := val
dst[k] = &v
}
return dst
}
// IntValueMap converts a string map of int pointers into a string
// map of int values
func IntValueMap(src map[string]*int) map[string]int {
dst := make(map[string]int)
for k, val := range src {
if val != nil {
dst[k] = *val
}
}
return dst
}
// Time returns a pointer to the given time.Time value.
func Time(v time.Time) *time.Time {
return &v
}
// TimeValue returns the value of the given time.Time pointer or
// time.Time{} if the pointer is nil.
func TimeValue(v *time.Time) time.Time {
if v != nil {
return *v
}
return time.Time{}
}
// TimeUnixMilli returns a Unix timestamp in milliseconds from "January 1, 1970 UTC".
// The result is undefined if the Unix time cannot be represented by an int64.
// Which includes calling TimeUnixMilli on a zero Time is undefined.
//
// See Go stdlib https://golang.org/pkg/time/#Time.UnixNano for more information.
func TimeUnixMilli(t time.Time) int64 {
return t.UnixNano() / int64(time.Millisecond/time.Nanosecond)
}
// TimeSlice converts a slice of time.Time values into a slice of
// time.Time pointers
func TimeSlice(src []time.Time) []*time.Time {
dst := make([]*time.Time, len(src))
for i := 0; i < len(src); i++ {
dst[i] = &(src[i])
}
return dst
}
// TimeValueSlice converts a slice of time.Time pointers into a slice of
// time.Time values
func TimeValueSlice(src []*time.Time) []time.Time {
dst := make([]time.Time, len(src))
for i := 0; i < len(src); i++ {
if src[i] != nil {
dst[i] = *(src[i])
}
}
return dst
}
// TimeMap converts a string map of time.Time values into a string
// map of time.Time pointers
func TimeMap(src map[string]time.Time) map[string]*time.Time {
dst := make(map[string]*time.Time)
for k, val := range src {
v := val
dst[k] = &v
}
return dst
}
// TimeValueMap converts a string map of time.Time pointers into a string
// map of time.Time values
func TimeValueMap(src map[string]*time.Time) map[string]time.Time {
dst := make(map[string]time.Time)
for k, val := range src {
if val != nil {
dst[k] = *val
}
}
return dst
}

View file

@ -0,0 +1,311 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package service
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var testCasesStringSlice = [][]string{
{"a", "b", "c", "d", "e"},
{"a", "b", "", "", "e"},
}
func TestStringSlice(t *testing.T) {
for idx, in := range testCasesStringSlice {
if in == nil {
continue
}
out := StringSlice(in)
assert.Len(t, out, len(in), "Unexpected len at idx %d", idx)
for i := range out {
assert.Equal(t, in[i], *(out[i]), "Unexpected value at idx %d", idx)
}
out2 := StringValueSlice(out)
assert.Len(t, out2, len(in), "Unexpected len at idx %d", idx)
assert.Equal(t, in, out2, "Unexpected value at idx %d", idx)
}
}
var testCasesStringValueSlice = [][]*string{
{String("a"), String("b"), nil, String("c")},
}
func TestStringValueSlice(t *testing.T) {
for idx, in := range testCasesStringValueSlice {
if in == nil {
continue
}
out := StringValueSlice(in)
assert.Len(t, out, len(in), "Unexpected len at idx %d", idx)
for i := range out {
if in[i] == nil {
assert.Empty(t, out[i], "Unexpected value at idx %d", idx)
} else {
assert.Equal(t, *(in[i]), out[i], "Unexpected value at idx %d", idx)
}
}
out2 := StringSlice(out)
assert.Len(t, out2, len(in), "Unexpected len at idx %d", idx)
for i := range out2 {
if in[i] == nil {
assert.Empty(t, *(out2[i]), "Unexpected value at idx %d", idx)
} else {
assert.Equal(t, in[i], out2[i], "Unexpected value at idx %d", idx)
}
}
}
}
var testCasesStringMap = []map[string]string{
{"a": "1", "b": "2", "c": "3"},
}
func TestStringMap(t *testing.T) {
for idx, in := range testCasesStringMap {
if in == nil {
continue
}
out := StringMap(in)
assert.Len(t, out, len(in), "Unexpected len at idx %d", idx)
for i := range out {
assert.Equal(t, in[i], *(out[i]), "Unexpected value at idx %d", idx)
}
out2 := StringValueMap(out)
assert.Len(t, out2, len(in), "Unexpected len at idx %d", idx)
assert.Equal(t, in, out2, "Unexpected value at idx %d", idx)
}
}
var testCasesBoolSlice = [][]bool{
{true, true, false, false},
}
func TestBoolSlice(t *testing.T) {
for idx, in := range testCasesBoolSlice {
if in == nil {
continue
}
out := BoolSlice(in)
assert.Len(t, out, len(in), "Unexpected len at idx %d", idx)
for i := range out {
assert.Equal(t, in[i], *(out[i]), "Unexpected value at idx %d", idx)
}
out2 := BoolValueSlice(out)
assert.Len(t, out2, len(in), "Unexpected len at idx %d", idx)
assert.Equal(t, in, out2, "Unexpected value at idx %d", idx)
}
}
var testCasesBoolValueSlice = [][]*bool{}
func TestBoolValueSlice(t *testing.T) {
for idx, in := range testCasesBoolValueSlice {
if in == nil {
continue
}
out := BoolValueSlice(in)
assert.Len(t, out, len(in), "Unexpected len at idx %d", idx)
for i := range out {
if in[i] == nil {
assert.Empty(t, out[i], "Unexpected value at idx %d", idx)
} else {
assert.Equal(t, *(in[i]), out[i], "Unexpected value at idx %d", idx)
}
}
out2 := BoolSlice(out)
assert.Len(t, out2, len(in), "Unexpected len at idx %d", idx)
for i := range out2 {
if in[i] == nil {
assert.Empty(t, *(out2[i]), "Unexpected value at idx %d", idx)
} else {
assert.Equal(t, in[i], out2[i], "Unexpected value at idx %d", idx)
}
}
}
}
var testCasesBoolMap = []map[string]bool{
{"a": true, "b": false, "c": true},
}
func TestBoolMap(t *testing.T) {
for idx, in := range testCasesBoolMap {
if in == nil {
continue
}
out := BoolMap(in)
assert.Len(t, out, len(in), "Unexpected len at idx %d", idx)
for i := range out {
assert.Equal(t, in[i], *(out[i]), "Unexpected value at idx %d", idx)
}
out2 := BoolValueMap(out)
assert.Len(t, out2, len(in), "Unexpected len at idx %d", idx)
assert.Equal(t, in, out2, "Unexpected value at idx %d", idx)
}
}
var testCasesIntSlice = [][]int{
{1, 2, 3, 4},
}
func TestIntSlice(t *testing.T) {
for idx, in := range testCasesIntSlice {
if in == nil {
continue
}
out := IntSlice(in)
assert.Len(t, out, len(in), "Unexpected len at idx %d", idx)
for i := range out {
assert.Equal(t, in[i], *(out[i]), "Unexpected value at idx %d", idx)
}
out2 := IntValueSlice(out)
assert.Len(t, out2, len(in), "Unexpected len at idx %d", idx)
assert.Equal(t, in, out2, "Unexpected value at idx %d", idx)
}
}
var testCasesIntValueSlice = [][]*int{}
func TestIntValueSlice(t *testing.T) {
for idx, in := range testCasesIntValueSlice {
if in == nil {
continue
}
out := IntValueSlice(in)
assert.Len(t, out, len(in), "Unexpected len at idx %d", idx)
for i := range out {
if in[i] == nil {
assert.Empty(t, out[i], "Unexpected value at idx %d", idx)
} else {
assert.Equal(t, *(in[i]), out[i], "Unexpected value at idx %d", idx)
}
}
out2 := IntSlice(out)
assert.Len(t, out2, len(in), "Unexpected len at idx %d", idx)
for i := range out2 {
if in[i] == nil {
assert.Empty(t, *(out2[i]), "Unexpected value at idx %d", idx)
} else {
assert.Equal(t, in[i], out2[i], "Unexpected value at idx %d", idx)
}
}
}
}
var testCasesIntMap = []map[string]int{
{"a": 3, "b": 2, "c": 1},
}
func TestIntMap(t *testing.T) {
for idx, in := range testCasesIntMap {
if in == nil {
continue
}
out := IntMap(in)
assert.Len(t, out, len(in), "Unexpected len at idx %d", idx)
for i := range out {
assert.Equal(t, in[i], *(out[i]), "Unexpected value at idx %d", idx)
}
out2 := IntValueMap(out)
assert.Len(t, out2, len(in), "Unexpected len at idx %d", idx)
assert.Equal(t, in, out2, "Unexpected value at idx %d", idx)
}
}
var testCasesTimeSlice = [][]time.Time{
{time.Now(), time.Now().AddDate(100, 0, 0)},
}
func TestTimeSlice(t *testing.T) {
for idx, in := range testCasesTimeSlice {
if in == nil {
continue
}
out := TimeSlice(in)
assert.Len(t, out, len(in), "Unexpected len at idx %d", idx)
for i := range out {
assert.Equal(t, in[i], *(out[i]), "Unexpected value at idx %d", idx)
}
out2 := TimeValueSlice(out)
assert.Len(t, out2, len(in), "Unexpected len at idx %d", idx)
assert.Equal(t, in, out2, "Unexpected value at idx %d", idx)
}
}
var testCasesTimeValueSlice = [][]*time.Time{}
func TestTimeValueSlice(t *testing.T) {
for idx, in := range testCasesTimeValueSlice {
if in == nil {
continue
}
out := TimeValueSlice(in)
assert.Len(t, out, len(in), "Unexpected len at idx %d", idx)
for i := range out {
if in[i] == nil {
assert.Empty(t, out[i], "Unexpected value at idx %d", idx)
} else {
assert.Equal(t, *(in[i]), out[i], "Unexpected value at idx %d", idx)
}
}
out2 := TimeSlice(out)
assert.Len(t, out2, len(in), "Unexpected len at idx %d", idx)
for i := range out2 {
if in[i] == nil {
assert.Empty(t, *(out2[i]), "Unexpected value at idx %d", idx)
} else {
assert.Equal(t, in[i], out2[i], "Unexpected value at idx %d", idx)
}
}
}
}
var testCasesTimeMap = []map[string]time.Time{
{"a": time.Now().AddDate(-100, 0, 0), "b": time.Now()},
}
func TestTimeMap(t *testing.T) {
for idx, in := range testCasesTimeMap {
if in == nil {
continue
}
out := TimeMap(in)
assert.Len(t, out, len(in), "Unexpected len at idx %d", idx)
for i := range out {
assert.Equal(t, in[i], *(out[i]), "Unexpected value at idx %d", idx)
}
out2 := TimeValueMap(out)
assert.Len(t, out2, len(in), "Unexpected len at idx %d", idx)
assert.Equal(t, in, out2, "Unexpected value at idx %d", idx)
}
}

View file

@ -0,0 +1,926 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package service
import (
"fmt"
"io"
"time"
"github.com/yunify/qingstor-sdk-go/config"
"github.com/yunify/qingstor-sdk-go/request"
"github.com/yunify/qingstor-sdk-go/request/data"
"github.com/yunify/qingstor-sdk-go/request/errors"
)
var _ fmt.State
var _ io.Reader
var _ time.Time
var _ config.Config
// AbortMultipartUpload does Abort multipart upload.
// Documentation URL: https://docs.qingcloud.com/qingstor/api/object/abort_multipart_upload.html
func (s *Bucket) AbortMultipartUpload(objectKey string, input *AbortMultipartUploadInput) (*AbortMultipartUploadOutput, error) {
r, x, err := s.AbortMultipartUploadRequest(objectKey, input)
if err != nil {
return x, err
}
err = r.Send()
if err != nil {
return nil, err
}
requestID := r.HTTPResponse.Header.Get("X-Qs-Request-Id")
x.RequestID = &requestID
return x, err
}
// AbortMultipartUploadRequest creates request and output object of AbortMultipartUpload.
func (s *Bucket) AbortMultipartUploadRequest(objectKey string, input *AbortMultipartUploadInput) (*request.Request, *AbortMultipartUploadOutput, error) {
if input == nil {
input = &AbortMultipartUploadInput{}
}
o := &data.Operation{
Config: s.Config,
Properties: s.Properties,
APIName: "Abort Multipart Upload",
RequestMethod: "DELETE",
RequestURI: "/<bucket-name>/<object-key>",
StatusCodes: []int{
204, // Object multipart deleted
},
}
s.Properties.ObjectKey = &objectKey
x := &AbortMultipartUploadOutput{}
r, err := request.New(o, input, x)
if err != nil {
return nil, nil, err
}
return r, x, nil
}
// AbortMultipartUploadInput presents input for AbortMultipartUpload.
type AbortMultipartUploadInput struct {
// Object multipart upload ID
UploadID *string `json:"upload_id" name:"upload_id" location:"params"` // Required
}
// Validate validates the input for AbortMultipartUpload.
func (v *AbortMultipartUploadInput) Validate() error {
if v.UploadID == nil {
return errors.ParameterRequiredError{
ParameterName: "UploadID",
ParentName: "AbortMultipartUploadInput",
}
}
return nil
}
// AbortMultipartUploadOutput presents output for AbortMultipartUpload.
type AbortMultipartUploadOutput struct {
StatusCode *int `location:"statusCode"`
RequestID *string `location:"requestID"`
}
// CompleteMultipartUpload does Complete multipart upload.
// Documentation URL: https://docs.qingcloud.com/qingstor/api/object/complete_multipart_upload.html
func (s *Bucket) CompleteMultipartUpload(objectKey string, input *CompleteMultipartUploadInput) (*CompleteMultipartUploadOutput, error) {
r, x, err := s.CompleteMultipartUploadRequest(objectKey, input)
if err != nil {
return x, err
}
err = r.Send()
if err != nil {
return nil, err
}
requestID := r.HTTPResponse.Header.Get("X-Qs-Request-Id")
x.RequestID = &requestID
return x, err
}
// CompleteMultipartUploadRequest creates request and output object of CompleteMultipartUpload.
func (s *Bucket) CompleteMultipartUploadRequest(objectKey string, input *CompleteMultipartUploadInput) (*request.Request, *CompleteMultipartUploadOutput, error) {
if input == nil {
input = &CompleteMultipartUploadInput{}
}
o := &data.Operation{
Config: s.Config,
Properties: s.Properties,
APIName: "Complete multipart upload",
RequestMethod: "POST",
RequestURI: "/<bucket-name>/<object-key>",
StatusCodes: []int{
201, // Object created
},
}
s.Properties.ObjectKey = &objectKey
x := &CompleteMultipartUploadOutput{}
r, err := request.New(o, input, x)
if err != nil {
return nil, nil, err
}
return r, x, nil
}
// CompleteMultipartUploadInput presents input for CompleteMultipartUpload.
type CompleteMultipartUploadInput struct {
// Object multipart upload ID
UploadID *string `json:"upload_id" name:"upload_id" location:"params"` // Required
// MD5sum of the object part
ETag *string `json:"ETag,omitempty" name:"ETag" location:"headers"`
// Encryption algorithm of the object
XQSEncryptionCustomerAlgorithm *string `json:"X-QS-Encryption-Customer-Algorithm,omitempty" name:"X-QS-Encryption-Customer-Algorithm" location:"headers"`
// Encryption key of the object
XQSEncryptionCustomerKey *string `json:"X-QS-Encryption-Customer-Key,omitempty" name:"X-QS-Encryption-Customer-Key" location:"headers"`
// MD5sum of encryption key
XQSEncryptionCustomerKeyMD5 *string `json:"X-QS-Encryption-Customer-Key-MD5,omitempty" name:"X-QS-Encryption-Customer-Key-MD5" location:"headers"`
// Object parts
ObjectParts []*ObjectPartType `json:"object_parts,omitempty" name:"object_parts" location:"elements"`
}
// Validate validates the input for CompleteMultipartUpload.
func (v *CompleteMultipartUploadInput) Validate() error {
if v.UploadID == nil {
return errors.ParameterRequiredError{
ParameterName: "UploadID",
ParentName: "CompleteMultipartUploadInput",
}
}
if len(v.ObjectParts) > 0 {
for _, property := range v.ObjectParts {
if err := property.Validate(); err != nil {
return err
}
}
}
return nil
}
// CompleteMultipartUploadOutput presents output for CompleteMultipartUpload.
type CompleteMultipartUploadOutput struct {
StatusCode *int `location:"statusCode"`
RequestID *string `location:"requestID"`
}
// DeleteObject does Delete the object.
// Documentation URL: https://docs.qingcloud.com/qingstor/api/object/delete.html
func (s *Bucket) DeleteObject(objectKey string) (*DeleteObjectOutput, error) {
r, x, err := s.DeleteObjectRequest(objectKey)
if err != nil {
return x, err
}
err = r.Send()
if err != nil {
return nil, err
}
requestID := r.HTTPResponse.Header.Get("X-Qs-Request-Id")
x.RequestID = &requestID
return x, err
}
// DeleteObjectRequest creates request and output object of DeleteObject.
func (s *Bucket) DeleteObjectRequest(objectKey string) (*request.Request, *DeleteObjectOutput, error) {
o := &data.Operation{
Config: s.Config,
Properties: s.Properties,
APIName: "DELETE Object",
RequestMethod: "DELETE",
RequestURI: "/<bucket-name>/<object-key>",
StatusCodes: []int{
204, // Object deleted
},
}
s.Properties.ObjectKey = &objectKey
x := &DeleteObjectOutput{}
r, err := request.New(o, nil, x)
if err != nil {
return nil, nil, err
}
return r, x, nil
}
// DeleteObjectOutput presents output for DeleteObject.
type DeleteObjectOutput struct {
StatusCode *int `location:"statusCode"`
RequestID *string `location:"requestID"`
}
// GetObject does Retrieve the object.
// Documentation URL: https://docs.qingcloud.com/qingstor/api/object/get.html
func (s *Bucket) GetObject(objectKey string, input *GetObjectInput) (*GetObjectOutput, error) {
r, x, err := s.GetObjectRequest(objectKey, input)
if err != nil {
return x, err
}
err = r.Send()
if err != nil {
return nil, err
}
requestID := r.HTTPResponse.Header.Get("X-Qs-Request-Id")
x.RequestID = &requestID
return x, err
}
// GetObjectRequest creates request and output object of GetObject.
func (s *Bucket) GetObjectRequest(objectKey string, input *GetObjectInput) (*request.Request, *GetObjectOutput, error) {
if input == nil {
input = &GetObjectInput{}
}
o := &data.Operation{
Config: s.Config,
Properties: s.Properties,
APIName: "GET Object",
RequestMethod: "GET",
RequestURI: "/<bucket-name>/<object-key>",
StatusCodes: []int{
200, // OK
206, // Partial content
304, // Not modified
412, // Precondition failed
},
}
s.Properties.ObjectKey = &objectKey
x := &GetObjectOutput{}
r, err := request.New(o, input, x)
if err != nil {
return nil, nil, err
}
return r, x, nil
}
// GetObjectInput presents input for GetObject.
type GetObjectInput struct {
// Specified the Cache-Control response header
ResponseCacheControl *string `json:"response-cache-control,omitempty" name:"response-cache-control" location:"params"`
// Specified the Content-Disposition response header
ResponseContentDisposition *string `json:"response-content-disposition,omitempty" name:"response-content-disposition" location:"params"`
// Specified the Content-Encoding response header
ResponseContentEncoding *string `json:"response-content-encoding,omitempty" name:"response-content-encoding" location:"params"`
// Specified the Content-Language response header
ResponseContentLanguage *string `json:"response-content-language,omitempty" name:"response-content-language" location:"params"`
// Specified the Content-Type response header
ResponseContentType *string `json:"response-content-type,omitempty" name:"response-content-type" location:"params"`
// Specified the Expires response header
ResponseExpires *string `json:"response-expires,omitempty" name:"response-expires" location:"params"`
// Check whether the ETag matches
IfMatch *string `json:"If-Match,omitempty" name:"If-Match" location:"headers"`
// Check whether the object has been modified
IfModifiedSince *time.Time `json:"If-Modified-Since,omitempty" name:"If-Modified-Since" format:"RFC 822" location:"headers"`
// Check whether the ETag does not match
IfNoneMatch *string `json:"If-None-Match,omitempty" name:"If-None-Match" location:"headers"`
// Check whether the object has not been modified
IfUnmodifiedSince *time.Time `json:"If-Unmodified-Since,omitempty" name:"If-Unmodified-Since" format:"RFC 822" location:"headers"`
// Specified range of the object
Range *string `json:"Range,omitempty" name:"Range" location:"headers"`
// Encryption algorithm of the object
XQSEncryptionCustomerAlgorithm *string `json:"X-QS-Encryption-Customer-Algorithm,omitempty" name:"X-QS-Encryption-Customer-Algorithm" location:"headers"`
// Encryption key of the object
XQSEncryptionCustomerKey *string `json:"X-QS-Encryption-Customer-Key,omitempty" name:"X-QS-Encryption-Customer-Key" location:"headers"`
// MD5sum of encryption key
XQSEncryptionCustomerKeyMD5 *string `json:"X-QS-Encryption-Customer-Key-MD5,omitempty" name:"X-QS-Encryption-Customer-Key-MD5" location:"headers"`
}
// Validate validates the input for GetObject.
func (v *GetObjectInput) Validate() error {
return nil
}
// GetObjectOutput presents output for GetObject.
type GetObjectOutput struct {
StatusCode *int `location:"statusCode"`
RequestID *string `location:"requestID"`
// The response body
Body io.ReadCloser `location:"body"`
// Object content length
ContentLength *int64 `json:"Content-Length,omitempty" name:"Content-Length" location:"headers"`
// Range of response data content
ContentRange *string `json:"Content-Range,omitempty" name:"Content-Range" location:"headers"`
// MD5sum of the object
ETag *string `json:"ETag,omitempty" name:"ETag" location:"headers"`
// Encryption algorithm of the object
XQSEncryptionCustomerAlgorithm *string `json:"X-QS-Encryption-Customer-Algorithm,omitempty" name:"X-QS-Encryption-Customer-Algorithm" location:"headers"`
}
// HeadObject does Check whether the object exists and available.
// Documentation URL: https://docs.qingcloud.com/qingstor/api/object/head.html
func (s *Bucket) HeadObject(objectKey string, input *HeadObjectInput) (*HeadObjectOutput, error) {
r, x, err := s.HeadObjectRequest(objectKey, input)
if err != nil {
return x, err
}
err = r.Send()
if err != nil {
return nil, err
}
requestID := r.HTTPResponse.Header.Get("X-Qs-Request-Id")
x.RequestID = &requestID
return x, err
}
// HeadObjectRequest creates request and output object of HeadObject.
func (s *Bucket) HeadObjectRequest(objectKey string, input *HeadObjectInput) (*request.Request, *HeadObjectOutput, error) {
if input == nil {
input = &HeadObjectInput{}
}
o := &data.Operation{
Config: s.Config,
Properties: s.Properties,
APIName: "HEAD Object",
RequestMethod: "HEAD",
RequestURI: "/<bucket-name>/<object-key>",
StatusCodes: []int{
200, // OK
},
}
s.Properties.ObjectKey = &objectKey
x := &HeadObjectOutput{}
r, err := request.New(o, input, x)
if err != nil {
return nil, nil, err
}
return r, x, nil
}
// HeadObjectInput presents input for HeadObject.
type HeadObjectInput struct {
// Check whether the ETag matches
IfMatch *string `json:"If-Match,omitempty" name:"If-Match" location:"headers"`
// Check whether the object has been modified
IfModifiedSince *time.Time `json:"If-Modified-Since,omitempty" name:"If-Modified-Since" format:"RFC 822" location:"headers"`
// Check whether the ETag does not match
IfNoneMatch *string `json:"If-None-Match,omitempty" name:"If-None-Match" location:"headers"`
// Check whether the object has not been modified
IfUnmodifiedSince *time.Time `json:"If-Unmodified-Since,omitempty" name:"If-Unmodified-Since" format:"RFC 822" location:"headers"`
// Encryption algorithm of the object
XQSEncryptionCustomerAlgorithm *string `json:"X-QS-Encryption-Customer-Algorithm,omitempty" name:"X-QS-Encryption-Customer-Algorithm" location:"headers"`
// Encryption key of the object
XQSEncryptionCustomerKey *string `json:"X-QS-Encryption-Customer-Key,omitempty" name:"X-QS-Encryption-Customer-Key" location:"headers"`
// MD5sum of encryption key
XQSEncryptionCustomerKeyMD5 *string `json:"X-QS-Encryption-Customer-Key-MD5,omitempty" name:"X-QS-Encryption-Customer-Key-MD5" location:"headers"`
}
// Validate validates the input for HeadObject.
func (v *HeadObjectInput) Validate() error {
return nil
}
// HeadObjectOutput presents output for HeadObject.
type HeadObjectOutput struct {
StatusCode *int `location:"statusCode"`
RequestID *string `location:"requestID"`
// Object content length
ContentLength *int64 `json:"Content-Length,omitempty" name:"Content-Length" location:"headers"`
// Object content type
ContentType *string `json:"Content-Type,omitempty" name:"Content-Type" location:"headers"`
// MD5sum of the object
ETag *string `json:"ETag,omitempty" name:"ETag" location:"headers"`
LastModified *time.Time `json:"Last-Modified,omitempty" name:"Last-Modified" format:"RFC 822" location:"headers"`
// Encryption algorithm of the object
XQSEncryptionCustomerAlgorithm *string `json:"X-QS-Encryption-Customer-Algorithm,omitempty" name:"X-QS-Encryption-Customer-Algorithm" location:"headers"`
}
// InitiateMultipartUpload does Initial multipart upload on the object.
// Documentation URL: https://docs.qingcloud.com/qingstor/api/object/initiate_multipart_upload.html
func (s *Bucket) InitiateMultipartUpload(objectKey string, input *InitiateMultipartUploadInput) (*InitiateMultipartUploadOutput, error) {
r, x, err := s.InitiateMultipartUploadRequest(objectKey, input)
if err != nil {
return x, err
}
err = r.Send()
if err != nil {
return nil, err
}
requestID := r.HTTPResponse.Header.Get("X-Qs-Request-Id")
x.RequestID = &requestID
return x, err
}
// InitiateMultipartUploadRequest creates request and output object of InitiateMultipartUpload.
func (s *Bucket) InitiateMultipartUploadRequest(objectKey string, input *InitiateMultipartUploadInput) (*request.Request, *InitiateMultipartUploadOutput, error) {
if input == nil {
input = &InitiateMultipartUploadInput{}
}
o := &data.Operation{
Config: s.Config,
Properties: s.Properties,
APIName: "Initiate Multipart Upload",
RequestMethod: "POST",
RequestURI: "/<bucket-name>/<object-key>?uploads",
StatusCodes: []int{
200, // OK
},
}
s.Properties.ObjectKey = &objectKey
x := &InitiateMultipartUploadOutput{}
r, err := request.New(o, input, x)
if err != nil {
return nil, nil, err
}
return r, x, nil
}
// InitiateMultipartUploadInput presents input for InitiateMultipartUpload.
type InitiateMultipartUploadInput struct {
// Object content type
ContentType *string `json:"Content-Type,omitempty" name:"Content-Type" location:"headers"`
// Encryption algorithm of the object
XQSEncryptionCustomerAlgorithm *string `json:"X-QS-Encryption-Customer-Algorithm,omitempty" name:"X-QS-Encryption-Customer-Algorithm" location:"headers"`
// Encryption key of the object
XQSEncryptionCustomerKey *string `json:"X-QS-Encryption-Customer-Key,omitempty" name:"X-QS-Encryption-Customer-Key" location:"headers"`
// MD5sum of encryption key
XQSEncryptionCustomerKeyMD5 *string `json:"X-QS-Encryption-Customer-Key-MD5,omitempty" name:"X-QS-Encryption-Customer-Key-MD5" location:"headers"`
}
// Validate validates the input for InitiateMultipartUpload.
func (v *InitiateMultipartUploadInput) Validate() error {
return nil
}
// InitiateMultipartUploadOutput presents output for InitiateMultipartUpload.
type InitiateMultipartUploadOutput struct {
StatusCode *int `location:"statusCode"`
RequestID *string `location:"requestID"`
// Bucket name
Bucket *string `json:"bucket,omitempty" name:"bucket" location:"elements"`
// Object key
Key *string `json:"key,omitempty" name:"key" location:"elements"`
// Object multipart upload ID
UploadID *string `json:"upload_id,omitempty" name:"upload_id" location:"elements"`
// Encryption algorithm of the object
XQSEncryptionCustomerAlgorithm *string `json:"X-QS-Encryption-Customer-Algorithm,omitempty" name:"X-QS-Encryption-Customer-Algorithm" location:"headers"`
}
// ListMultipart does List object parts.
// Documentation URL: https://docs.qingcloud.com/qingstor/api/object/list_multipart.html
func (s *Bucket) ListMultipart(objectKey string, input *ListMultipartInput) (*ListMultipartOutput, error) {
r, x, err := s.ListMultipartRequest(objectKey, input)
if err != nil {
return x, err
}
err = r.Send()
if err != nil {
return nil, err
}
requestID := r.HTTPResponse.Header.Get("X-Qs-Request-Id")
x.RequestID = &requestID
return x, err
}
// ListMultipartRequest creates request and output object of ListMultipart.
func (s *Bucket) ListMultipartRequest(objectKey string, input *ListMultipartInput) (*request.Request, *ListMultipartOutput, error) {
if input == nil {
input = &ListMultipartInput{}
}
o := &data.Operation{
Config: s.Config,
Properties: s.Properties,
APIName: "List Multipart",
RequestMethod: "GET",
RequestURI: "/<bucket-name>/<object-key>",
StatusCodes: []int{
200, // OK
},
}
s.Properties.ObjectKey = &objectKey
x := &ListMultipartOutput{}
r, err := request.New(o, input, x)
if err != nil {
return nil, nil, err
}
return r, x, nil
}
// ListMultipartInput presents input for ListMultipart.
type ListMultipartInput struct {
// Limit results count
Limit *int `json:"limit,omitempty" name:"limit" location:"params"`
// Object multipart upload part number
PartNumberMarker *int `json:"part_number_marker,omitempty" name:"part_number_marker" location:"params"`
// Object multipart upload ID
UploadID *string `json:"upload_id" name:"upload_id" location:"params"` // Required
}
// Validate validates the input for ListMultipart.
func (v *ListMultipartInput) Validate() error {
if v.UploadID == nil {
return errors.ParameterRequiredError{
ParameterName: "UploadID",
ParentName: "ListMultipartInput",
}
}
return nil
}
// ListMultipartOutput presents output for ListMultipart.
type ListMultipartOutput struct {
StatusCode *int `location:"statusCode"`
RequestID *string `location:"requestID"`
// Object multipart count
Count *int `json:"count,omitempty" name:"count" location:"elements"`
// Object parts
ObjectParts []*ObjectPartType `json:"object_parts,omitempty" name:"object_parts" location:"elements"`
}
// OptionsObject does Check whether the object accepts a origin with method and header.
// Documentation URL: https://docs.qingcloud.com/qingstor/api/object/options.html
func (s *Bucket) OptionsObject(objectKey string, input *OptionsObjectInput) (*OptionsObjectOutput, error) {
r, x, err := s.OptionsObjectRequest(objectKey, input)
if err != nil {
return x, err
}
err = r.Send()
if err != nil {
return nil, err
}
requestID := r.HTTPResponse.Header.Get("X-Qs-Request-Id")
x.RequestID = &requestID
return x, err
}
// OptionsObjectRequest creates request and output object of OptionsObject.
func (s *Bucket) OptionsObjectRequest(objectKey string, input *OptionsObjectInput) (*request.Request, *OptionsObjectOutput, error) {
if input == nil {
input = &OptionsObjectInput{}
}
o := &data.Operation{
Config: s.Config,
Properties: s.Properties,
APIName: "OPTIONS Object",
RequestMethod: "OPTIONS",
RequestURI: "/<bucket-name>/<object-key>",
StatusCodes: []int{
200, // OK
},
}
s.Properties.ObjectKey = &objectKey
x := &OptionsObjectOutput{}
r, err := request.New(o, input, x)
if err != nil {
return nil, nil, err
}
return r, x, nil
}
// OptionsObjectInput presents input for OptionsObject.
type OptionsObjectInput struct {
// Request headers
AccessControlRequestHeaders *string `json:"Access-Control-Request-Headers,omitempty" name:"Access-Control-Request-Headers" location:"headers"`
// Request method
AccessControlRequestMethod *string `json:"Access-Control-Request-Method" name:"Access-Control-Request-Method" location:"headers"` // Required
// Request origin
Origin *string `json:"Origin" name:"Origin" location:"headers"` // Required
}
// Validate validates the input for OptionsObject.
func (v *OptionsObjectInput) Validate() error {
if v.AccessControlRequestMethod == nil {
return errors.ParameterRequiredError{
ParameterName: "AccessControlRequestMethod",
ParentName: "OptionsObjectInput",
}
}
if v.Origin == nil {
return errors.ParameterRequiredError{
ParameterName: "Origin",
ParentName: "OptionsObjectInput",
}
}
return nil
}
// OptionsObjectOutput presents output for OptionsObject.
type OptionsObjectOutput struct {
StatusCode *int `location:"statusCode"`
RequestID *string `location:"requestID"`
// Allowed headers
AccessControlAllowHeaders *string `json:"Access-Control-Allow-Headers,omitempty" name:"Access-Control-Allow-Headers" location:"headers"`
// Allowed methods
AccessControlAllowMethods *string `json:"Access-Control-Allow-Methods,omitempty" name:"Access-Control-Allow-Methods" location:"headers"`
// Allowed origin
AccessControlAllowOrigin *string `json:"Access-Control-Allow-Origin,omitempty" name:"Access-Control-Allow-Origin" location:"headers"`
// Expose headers
AccessControlExposeHeaders *string `json:"Access-Control-Expose-Headers,omitempty" name:"Access-Control-Expose-Headers" location:"headers"`
// Max age
AccessControlMaxAge *string `json:"Access-Control-Max-Age,omitempty" name:"Access-Control-Max-Age" location:"headers"`
}
// PutObject does Upload the object.
// Documentation URL: https://docs.qingcloud.com/qingstor/api/object/put.html
func (s *Bucket) PutObject(objectKey string, input *PutObjectInput) (*PutObjectOutput, error) {
r, x, err := s.PutObjectRequest(objectKey, input)
if err != nil {
return x, err
}
err = r.Send()
if err != nil {
return nil, err
}
requestID := r.HTTPResponse.Header.Get("X-Qs-Request-Id")
x.RequestID = &requestID
return x, err
}
// PutObjectRequest creates request and output object of PutObject.
func (s *Bucket) PutObjectRequest(objectKey string, input *PutObjectInput) (*request.Request, *PutObjectOutput, error) {
if input == nil {
input = &PutObjectInput{}
}
o := &data.Operation{
Config: s.Config,
Properties: s.Properties,
APIName: "PUT Object",
RequestMethod: "PUT",
RequestURI: "/<bucket-name>/<object-key>",
StatusCodes: []int{
201, // Object created
},
}
s.Properties.ObjectKey = &objectKey
x := &PutObjectOutput{}
r, err := request.New(o, input, x)
if err != nil {
return nil, nil, err
}
return r, x, nil
}
// PutObjectInput presents input for PutObject.
type PutObjectInput struct {
// Object content size
ContentLength *int64 `json:"Content-Length" name:"Content-Length" location:"headers"` // Required
// Object MD5sum
ContentMD5 *string `json:"Content-MD5,omitempty" name:"Content-MD5" location:"headers"`
// Object content type
ContentType *string `json:"Content-Type,omitempty" name:"Content-Type" location:"headers"`
// Used to indicate that particular server behaviors are required by the client
Expect *string `json:"Expect,omitempty" name:"Expect" location:"headers"`
// Copy source, format (/<bucket-name>/<object-key>)
XQSCopySource *string `json:"X-QS-Copy-Source,omitempty" name:"X-QS-Copy-Source" location:"headers"`
// Encryption algorithm of the object
XQSCopySourceEncryptionCustomerAlgorithm *string `json:"X-QS-Copy-Source-Encryption-Customer-Algorithm,omitempty" name:"X-QS-Copy-Source-Encryption-Customer-Algorithm" location:"headers"`
// Encryption key of the object
XQSCopySourceEncryptionCustomerKey *string `json:"X-QS-Copy-Source-Encryption-Customer-Key,omitempty" name:"X-QS-Copy-Source-Encryption-Customer-Key" location:"headers"`
// MD5sum of encryption key
XQSCopySourceEncryptionCustomerKeyMD5 *string `json:"X-QS-Copy-Source-Encryption-Customer-Key-MD5,omitempty" name:"X-QS-Copy-Source-Encryption-Customer-Key-MD5" location:"headers"`
// Check whether the copy source matches
XQSCopySourceIfMatch *string `json:"X-QS-Copy-Source-If-Match,omitempty" name:"X-QS-Copy-Source-If-Match" location:"headers"`
// Check whether the copy source has been modified
XQSCopySourceIfModifiedSince *time.Time `json:"X-QS-Copy-Source-If-Modified-Since,omitempty" name:"X-QS-Copy-Source-If-Modified-Since" format:"RFC 822" location:"headers"`
// Check whether the copy source does not match
XQSCopySourceIfNoneMatch *string `json:"X-QS-Copy-Source-If-None-Match,omitempty" name:"X-QS-Copy-Source-If-None-Match" location:"headers"`
// Check whether the copy source has not been modified
XQSCopySourceIfUnmodifiedSince *time.Time `json:"X-QS-Copy-Source-If-Unmodified-Since,omitempty" name:"X-QS-Copy-Source-If-Unmodified-Since" format:"RFC 822" location:"headers"`
// Encryption algorithm of the object
XQSEncryptionCustomerAlgorithm *string `json:"X-QS-Encryption-Customer-Algorithm,omitempty" name:"X-QS-Encryption-Customer-Algorithm" location:"headers"`
// Encryption key of the object
XQSEncryptionCustomerKey *string `json:"X-QS-Encryption-Customer-Key,omitempty" name:"X-QS-Encryption-Customer-Key" location:"headers"`
// MD5sum of encryption key
XQSEncryptionCustomerKeyMD5 *string `json:"X-QS-Encryption-Customer-Key-MD5,omitempty" name:"X-QS-Encryption-Customer-Key-MD5" location:"headers"`
// Check whether fetch target object has not been modified
XQSFetchIfUnmodifiedSince *time.Time `json:"X-QS-Fetch-If-Unmodified-Since,omitempty" name:"X-QS-Fetch-If-Unmodified-Since" format:"RFC 822" location:"headers"`
// Fetch source, should be a valid url
XQSFetchSource *string `json:"X-QS-Fetch-Source,omitempty" name:"X-QS-Fetch-Source" location:"headers"`
// Move source, format (/<bucket-name>/<object-key>)
XQSMoveSource *string `json:"X-QS-Move-Source,omitempty" name:"X-QS-Move-Source" location:"headers"`
// The request body
Body io.Reader `location:"body"`
}
// Validate validates the input for PutObject.
func (v *PutObjectInput) Validate() error {
return nil
}
// PutObjectOutput presents output for PutObject.
type PutObjectOutput struct {
StatusCode *int `location:"statusCode"`
RequestID *string `location:"requestID"`
}
// UploadMultipart does Upload object multipart.
// Documentation URL: https://docs.qingcloud.com/qingstor/api/object/multipart/upload_multipart.html
func (s *Bucket) UploadMultipart(objectKey string, input *UploadMultipartInput) (*UploadMultipartOutput, error) {
r, x, err := s.UploadMultipartRequest(objectKey, input)
if err != nil {
return x, err
}
err = r.Send()
if err != nil {
return nil, err
}
requestID := r.HTTPResponse.Header.Get("X-Qs-Request-Id")
x.RequestID = &requestID
return x, err
}
// UploadMultipartRequest creates request and output object of UploadMultipart.
func (s *Bucket) UploadMultipartRequest(objectKey string, input *UploadMultipartInput) (*request.Request, *UploadMultipartOutput, error) {
if input == nil {
input = &UploadMultipartInput{}
}
o := &data.Operation{
Config: s.Config,
Properties: s.Properties,
APIName: "Upload Multipart",
RequestMethod: "PUT",
RequestURI: "/<bucket-name>/<object-key>",
StatusCodes: []int{
201, // Object multipart created
},
}
s.Properties.ObjectKey = &objectKey
x := &UploadMultipartOutput{}
r, err := request.New(o, input, x)
if err != nil {
return nil, nil, err
}
return r, x, nil
}
// UploadMultipartInput presents input for UploadMultipart.
type UploadMultipartInput struct {
// Object multipart upload part number
PartNumber *int `json:"part_number" name:"part_number" default:"0" location:"params"` // Required
// Object multipart upload ID
UploadID *string `json:"upload_id" name:"upload_id" location:"params"` // Required
// Object multipart content length
ContentLength *int64 `json:"Content-Length,omitempty" name:"Content-Length" location:"headers"`
// Object multipart content MD5sum
ContentMD5 *string `json:"Content-MD5,omitempty" name:"Content-MD5" location:"headers"`
// Encryption algorithm of the object
XQSEncryptionCustomerAlgorithm *string `json:"X-QS-Encryption-Customer-Algorithm,omitempty" name:"X-QS-Encryption-Customer-Algorithm" location:"headers"`
// Encryption key of the object
XQSEncryptionCustomerKey *string `json:"X-QS-Encryption-Customer-Key,omitempty" name:"X-QS-Encryption-Customer-Key" location:"headers"`
// MD5sum of encryption key
XQSEncryptionCustomerKeyMD5 *string `json:"X-QS-Encryption-Customer-Key-MD5,omitempty" name:"X-QS-Encryption-Customer-Key-MD5" location:"headers"`
// The request body
Body io.Reader `location:"body"`
}
// Validate validates the input for UploadMultipart.
func (v *UploadMultipartInput) Validate() error {
if v.PartNumber == nil {
return errors.ParameterRequiredError{
ParameterName: "PartNumber",
ParentName: "UploadMultipartInput",
}
}
if v.UploadID == nil {
return errors.ParameterRequiredError{
ParameterName: "UploadID",
ParentName: "UploadMultipartInput",
}
}
return nil
}
// UploadMultipartOutput presents output for UploadMultipart.
type UploadMultipartOutput struct {
StatusCode *int `location:"statusCode"`
RequestID *string `location:"requestID"`
}

View file

@ -0,0 +1,104 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
// Package service provides QingStor Service API (API Version 2016-01-06)
package service
import (
"github.com/yunify/qingstor-sdk-go/config"
"github.com/yunify/qingstor-sdk-go/request"
"github.com/yunify/qingstor-sdk-go/request/data"
)
// Service QingStor provides low-cost and reliable online storage service with unlimited storage space, high read and write performance, high reliability and data safety, fine-grained access control, and easy to use API.
type Service struct {
Config *config.Config
}
// Init initializes a new service.
func Init(c *config.Config) (*Service, error) {
return &Service{Config: c}, nil
}
// ListBuckets does Retrieve the bucket list.
// Documentation URL: https://docs.qingcloud.com/qingstor/api/service/get.html
func (s *Service) ListBuckets(input *ListBucketsInput) (*ListBucketsOutput, error) {
r, x, err := s.ListBucketsRequest(input)
if err != nil {
return x, err
}
err = r.Send()
if err != nil {
return nil, err
}
requestID := r.HTTPResponse.Header.Get("X-Qs-Request-Id")
x.RequestID = &requestID
return x, err
}
// ListBucketsRequest creates request and output object of ListBuckets.
func (s *Service) ListBucketsRequest(input *ListBucketsInput) (*request.Request, *ListBucketsOutput, error) {
if input == nil {
input = &ListBucketsInput{}
}
o := &data.Operation{
Config: s.Config,
APIName: "Get Service",
RequestMethod: "GET",
RequestURI: "/",
StatusCodes: []int{
200, // OK
},
}
x := &ListBucketsOutput{}
r, err := request.New(o, input, x)
if err != nil {
return nil, nil, err
}
return r, x, nil
}
// ListBucketsInput presents input for ListBuckets.
type ListBucketsInput struct {
// Limits results to buckets that in the location
Location *string `json:"Location,omitempty" name:"Location" location:"headers"`
}
// Validate validates the input for ListBuckets.
func (v *ListBucketsInput) Validate() error {
return nil
}
// ListBucketsOutput presents output for ListBuckets.
type ListBucketsOutput struct {
StatusCode *int `location:"statusCode"`
RequestID *string `location:"requestID"`
// Buckets information
Buckets []*BucketType `json:"buckets,omitempty" name:"buckets" location:"elements"`
// Bucket count
Count *int `json:"count,omitempty" name:"count" location:"elements"`
}

View file

@ -0,0 +1,463 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package service
import (
"fmt"
"time"
"github.com/yunify/qingstor-sdk-go/request/errors"
)
// Properties presents the service properties.
type Properties struct {
// Bucket name
BucketName *string `json:"bucket-name" name:"bucket-name"` // Required
// Object key
ObjectKey *string `json:"object-key" name:"object-key"` // Required
// QingCloud Zone ID
Zone *string `json:"zone" name:"zone"`
}
// ACLType presents ACL.
type ACLType struct {
Grantee *GranteeType `json:"grantee" name:"grantee"` // Required
// Permission for this grantee
// Permission's available values: READ, WRITE, FULL_CONTROL
Permission *string `json:"permission" name:"permission"` // Required
}
// Validate validates the ACL.
func (v *ACLType) Validate() error {
if v.Grantee != nil {
if err := v.Grantee.Validate(); err != nil {
return err
}
}
if v.Grantee == nil {
return errors.ParameterRequiredError{
ParameterName: "Grantee",
ParentName: "ACL",
}
}
if v.Permission == nil {
return errors.ParameterRequiredError{
ParameterName: "Permission",
ParentName: "ACL",
}
}
if v.Permission != nil {
permissionValidValues := []string{"READ", "WRITE", "FULL_CONTROL"}
permissionParameterValue := fmt.Sprint(*v.Permission)
permissionIsValid := false
for _, value := range permissionValidValues {
if value == permissionParameterValue {
permissionIsValid = true
}
}
if !permissionIsValid {
return errors.ParameterValueNotAllowedError{
ParameterName: "Permission",
ParameterValue: permissionParameterValue,
AllowedValues: permissionValidValues,
}
}
}
return nil
}
// BucketType presents Bucket.
type BucketType struct {
// Created time of the bucket
Created *time.Time `json:"created,omitempty" name:"created" format:"ISO 8601"`
// QingCloud Zone ID
Location *string `json:"location,omitempty" name:"location"`
// Bucket name
Name *string `json:"name,omitempty" name:"name"`
// URL to access the bucket
URL *string `json:"url,omitempty" name:"url"`
}
// Validate validates the Bucket.
func (v *BucketType) Validate() error {
return nil
}
// ConditionType presents Condition.
type ConditionType struct {
IPAddress *IPAddressType `json:"ip_address,omitempty" name:"ip_address"`
IsNull *IsNullType `json:"is_null,omitempty" name:"is_null"`
NotIPAddress *NotIPAddressType `json:"not_ip_address,omitempty" name:"not_ip_address"`
StringLike *StringLikeType `json:"string_like,omitempty" name:"string_like"`
StringNotLike *StringNotLikeType `json:"string_not_like,omitempty" name:"string_not_like"`
}
// Validate validates the Condition.
func (v *ConditionType) Validate() error {
if v.IPAddress != nil {
if err := v.IPAddress.Validate(); err != nil {
return err
}
}
if v.IsNull != nil {
if err := v.IsNull.Validate(); err != nil {
return err
}
}
if v.NotIPAddress != nil {
if err := v.NotIPAddress.Validate(); err != nil {
return err
}
}
if v.StringLike != nil {
if err := v.StringLike.Validate(); err != nil {
return err
}
}
if v.StringNotLike != nil {
if err := v.StringNotLike.Validate(); err != nil {
return err
}
}
return nil
}
// CORSRuleType presents CORSRule.
type CORSRuleType struct {
// Allowed headers
AllowedHeaders []*string `json:"allowed_headers,omitempty" name:"allowed_headers"`
// Allowed methods
AllowedMethods []*string `json:"allowed_methods" name:"allowed_methods"` // Required
// Allowed origin
AllowedOrigin *string `json:"allowed_origin" name:"allowed_origin"` // Required
// Expose headers
ExposeHeaders []*string `json:"expose_headers,omitempty" name:"expose_headers"`
// Max age seconds
MaxAgeSeconds *int `json:"max_age_seconds,omitempty" name:"max_age_seconds"`
}
// Validate validates the CORSRule.
func (v *CORSRuleType) Validate() error {
if len(v.AllowedMethods) == 0 {
return errors.ParameterRequiredError{
ParameterName: "AllowedMethods",
ParentName: "CORSRule",
}
}
if v.AllowedOrigin == nil {
return errors.ParameterRequiredError{
ParameterName: "AllowedOrigin",
ParentName: "CORSRule",
}
}
return nil
}
// GranteeType presents Grantee.
type GranteeType struct {
// Grantee user ID
ID *string `json:"id,omitempty" name:"id"`
// Grantee group name
Name *string `json:"name,omitempty" name:"name"`
// Grantee type
// Type's available values: user, group
Type *string `json:"type" name:"type"` // Required
}
// Validate validates the Grantee.
func (v *GranteeType) Validate() error {
if v.Type == nil {
return errors.ParameterRequiredError{
ParameterName: "Type",
ParentName: "Grantee",
}
}
if v.Type != nil {
typeValidValues := []string{"user", "group"}
typeParameterValue := fmt.Sprint(*v.Type)
typeIsValid := false
for _, value := range typeValidValues {
if value == typeParameterValue {
typeIsValid = true
}
}
if !typeIsValid {
return errors.ParameterValueNotAllowedError{
ParameterName: "Type",
ParameterValue: typeParameterValue,
AllowedValues: typeValidValues,
}
}
}
return nil
}
// IPAddressType presents IPAddress.
type IPAddressType struct {
// Source IP
SourceIP []*string `json:"source_ip,omitempty" name:"source_ip"`
}
// Validate validates the IPAddress.
func (v *IPAddressType) Validate() error {
return nil
}
// IsNullType presents IsNull.
type IsNullType struct {
// Refer url
Referer *bool `json:"Referer,omitempty" name:"Referer"`
}
// Validate validates the IsNull.
func (v *IsNullType) Validate() error {
return nil
}
// KeyType presents Key.
type KeyType struct {
// Object created time
Created *time.Time `json:"created,omitempty" name:"created" format:"ISO 8601"`
// Whether this key is encrypted
Encrypted *bool `json:"encrypted,omitempty" name:"encrypted"`
// MD5sum of the object
Etag *string `json:"etag,omitempty" name:"etag"`
// Object key
Key *string `json:"key,omitempty" name:"key"`
// MIME type of the object
MimeType *string `json:"mime_type,omitempty" name:"mime_type"`
// Last modified time in unix time format
Modified *int `json:"modified,omitempty" name:"modified"`
// Object content size
Size *int64 `json:"size,omitempty" name:"size"`
}
// Validate validates the Key.
func (v *KeyType) Validate() error {
return nil
}
// KeyDeleteErrorType presents KeyDeleteError.
type KeyDeleteErrorType struct {
// Error code
Code *string `json:"code,omitempty" name:"code"`
// Object key
Key *string `json:"key,omitempty" name:"key"`
// Error message
Message *string `json:"message,omitempty" name:"message"`
}
// Validate validates the KeyDeleteError.
func (v *KeyDeleteErrorType) Validate() error {
return nil
}
// NotIPAddressType presents NotIPAddress.
type NotIPAddressType struct {
// Source IP
SourceIP []*string `json:"source_ip,omitempty" name:"source_ip"`
}
// Validate validates the NotIPAddress.
func (v *NotIPAddressType) Validate() error {
return nil
}
// ObjectPartType presents ObjectPart.
type ObjectPartType struct {
// Object part created time
Created *time.Time `json:"created,omitempty" name:"created" format:"ISO 8601"`
// MD5sum of the object part
Etag *string `json:"etag,omitempty" name:"etag"`
// Object part number
PartNumber *int `json:"part_number" name:"part_number" default:"0"` // Required
// Object part size
Size *int64 `json:"size,omitempty" name:"size"`
}
// Validate validates the ObjectPart.
func (v *ObjectPartType) Validate() error {
if v.PartNumber == nil {
return errors.ParameterRequiredError{
ParameterName: "PartNumber",
ParentName: "ObjectPart",
}
}
return nil
}
// OwnerType presents Owner.
type OwnerType struct {
// User ID
ID *string `json:"id,omitempty" name:"id"`
// Username
Name *string `json:"name,omitempty" name:"name"`
}
// Validate validates the Owner.
func (v *OwnerType) Validate() error {
return nil
}
// StatementType presents Statement.
type StatementType struct {
// QingStor API methods
Action []*string `json:"action" name:"action"` // Required
Condition *ConditionType `json:"condition,omitempty" name:"condition"`
// Statement effect
// Effect's available values: allow, deny
Effect *string `json:"effect" name:"effect"` // Required
// Bucket policy id, must be unique
ID *string `json:"id" name:"id"` // Required
// The resources to apply bucket policy
Resource []*string `json:"resource,omitempty" name:"resource"`
// The user to apply bucket policy
User []*string `json:"user" name:"user"` // Required
}
// Validate validates the Statement.
func (v *StatementType) Validate() error {
if len(v.Action) == 0 {
return errors.ParameterRequiredError{
ParameterName: "Action",
ParentName: "Statement",
}
}
if v.Condition != nil {
if err := v.Condition.Validate(); err != nil {
return err
}
}
if v.Effect == nil {
return errors.ParameterRequiredError{
ParameterName: "Effect",
ParentName: "Statement",
}
}
if v.Effect != nil {
effectValidValues := []string{"allow", "deny"}
effectParameterValue := fmt.Sprint(*v.Effect)
effectIsValid := false
for _, value := range effectValidValues {
if value == effectParameterValue {
effectIsValid = true
}
}
if !effectIsValid {
return errors.ParameterValueNotAllowedError{
ParameterName: "Effect",
ParameterValue: effectParameterValue,
AllowedValues: effectValidValues,
}
}
}
if v.ID == nil {
return errors.ParameterRequiredError{
ParameterName: "ID",
ParentName: "Statement",
}
}
if len(v.User) == 0 {
return errors.ParameterRequiredError{
ParameterName: "User",
ParentName: "Statement",
}
}
return nil
}
// StringLikeType presents StringLike.
type StringLikeType struct {
// Refer url
Referer []*string `json:"Referer,omitempty" name:"Referer"`
}
// Validate validates the StringLike.
func (v *StringLikeType) Validate() error {
return nil
}
// StringNotLikeType presents StringNotLike.
type StringNotLikeType struct {
// Refer url
Referer []*string `json:"Referer,omitempty" name:"Referer"`
}
// Validate validates the StringNotLike.
func (v *StringNotLikeType) Validate() error {
return nil
}
// UploadsType presents Uploads.
type UploadsType struct {
// Object part created time
Created *time.Time `json:"created,omitempty" name:"created" format:"ISO 8601"`
// Object key
Key *string `json:"key,omitempty" name:"key"`
// Object upload id
UploadID *string `json:"upload_id,omitempty" name:"upload_id"`
}
// Validate validates the Uploads.
func (v *UploadsType) Validate() error {
return nil
}

View file

@ -0,0 +1,8 @@
{
"output": {
"file_naming": {
"style": "snake_case",
"extension": ".go"
}
}
}

View file

@ -0,0 +1,40 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
{{$service := .Data.Service}}
// Package service provides {{$service.Name}} Service API (API Version {{$service.APIVersion}})
package service
import (
"github.com/yunify/qingstor-sdk-go/config"
"github.com/yunify/qingstor-sdk-go/request"
"github.com/yunify/qingstor-sdk-go/request/data"
)
{{if $service.Description}}// Service {{$service.Description}}{{end}}
type Service struct {
Config *config.Config
}
// Init initializes a new service.
func Init(c *config.Config) (*Service, error) {
return &Service{Config: c}, nil
}
{{range $_, $operation := $service.Operations}}
{{template "RenderOperation" passThrough $service $operation}}
{{end}}

View file

@ -0,0 +1,390 @@
{{define "Type"}}
{{- $typeName := index . 0 -}}
{{- $disablePointer := index . 1 -}}
{{- if eq $typeName "string" -}}
{{- if not $disablePointer -}}*{{- end -}}string
{{- else if eq $typeName "boolean" -}}
{{- if not $disablePointer -}}*{{- end -}}bool
{{- else if eq $typeName "integer" -}}
{{- if not $disablePointer -}}*{{- end -}}int
{{- else if eq $typeName "long" -}}
{{- if not $disablePointer -}}*{{- end -}}int64
{{- else if eq $typeName "timestamp" -}}
{{- if not $disablePointer -}}*{{- end -}}time.Time
{{- else if eq $typeName "binary" -}}
io.Reader
{{- else if eq $typeName "array" -}}
interface{}
{{- else if eq $typeName "object" -}}
interface{}
{{- else if eq $typeName "map" -}}
interface{}
{{- else if eq $typeName "any" -}}
interface{}
{{- else -}}
*{{$typeName | camelCase}}Type
{{- end -}}
{{end}}
{{define "PropertyType"}}
{{- $property := index . 0 -}}
{{- $disablePointer := index . 1 -}}
{{- if eq $property.Type "object" -}}
{{template "Type" passThrough $property.ExtraType $disablePointer}}
{{- else if eq $property.Type "array" -}}
[]{{template "Type" passThrough $property.ExtraType $disablePointer}}
{{- else if eq $property.Type "map" -}}
map[string]{{template "Type" passThrough $property.ExtraType $disablePointer}}
{{- else if eq $property.Type "any" -}}
{{template "Type" passThrough $property.Type $disablePointer}}
{{- else -}}
{{template "Type" passThrough $property.Type $disablePointer}}
{{- end -}}
{{end}}
{{define "PropertyTags"}}
{{- $property := . -}}
{{- if $property.IsRequired -}}
{{- printf `json:"%s"` ($property.Name | normalized) -}}
{{- else -}}
{{- printf `json:"%s,omitempty"` ($property.Name | normalized) -}}
{{- end -}}
{{- printf ` name:"%s"` ($property.Name | normalized) -}}
{{- if $property.Format}}
{{- printf ` format:"%s"` $property.Format -}}
{{- end -}}
{{- if $property.Default -}}
{{- printf ` default:"%s"` $property.Default -}}
{{- end -}}
{{end}}
{{define "PropertyTagsDashConnected"}}
{{- $property := . -}}
{{- printf `json:"%s"` ($property.Name | dashConnected) -}}
{{- printf ` name:"%s"` ($property.Name | dashConnected) -}}
{{end}}
{{define "PropertyExtraTags"}}
{{- $propertyExtraTags := . -}}
{{- if $propertyExtraTags -}}
{{- printf " %s" $propertyExtraTags -}}
{{- end -}}
{{end}}
{{define "RenderProperties"}}
{{- $customizedType := index . 0 -}}
{{- $propertyExtraTags := index . 1 -}}
{{- $operationName := index . 2 -}}
{{range $_, $property := $customizedType.Properties -}}
{{if or (ne $operationName "Delete Multiple Objects") (ne $property.ID "Content-MD5") -}}
{{if $property.Description -}}
// {{$property.Description}}
{{end -}}
{{if $property.Enum -}}
// {{$property.ID | camelCase}}'s available values: {{$property.Enum | commaConnected}}
{{end -}}
{{$property.ID | camelCase | upperFirst}}{{" " -}}
{{template "PropertyType" passThrough $property false}}{{" " -}}
`{{template "PropertyTags" $property}}{{template "PropertyExtraTags" $propertyExtraTags}}`{{" " -}}
{{if $property.IsRequired -}}
// Required
{{- end}}
{{- end}}
{{end}}
{{end}}
{{define "RenderOperation"}}
{{$service := index . 0}}
{{$operation := index . 1}}
{{$belongs := replace $service.Name "QingStor" "Service" -1}}
{{$belongs := replace $belongs "Object" "Bucket" -1}}
{{$opID := $operation.ID | camelCase}}
{{$isBucket := eq $service.Name "Bucket"}}
{{$isObject := eq $service.Name "Object"}}
{{$hasParams := gt (len $operation.Request.Params.Properties) 0}}
{{$hasHeaders := gt (len $operation.Request.Headers.Properties) 0}}
{{$hasElements := gt (len $operation.Request.Elements.Properties) 0}}
{{$hasStringBody := eq $operation.Request.Body.Type "string"}}
{{$hasBinaryBody := eq $operation.Request.Body.Type "binary"}}
{{$hasInput := or $hasParams $hasHeaders $hasElements $hasStringBody $hasBinaryBody}}
{{if $operation.Description -}}
{{if eq $belongs "Bucket" -}}
// {{replace $opID "Bucket" "" -1}} does {{$operation.Description}}
{{else -}}
// {{$opID}} does {{$operation.Description}}
{{end -}}
{{end -}}
{{if $operation.DocumentationURL -}}
// Documentation URL: {{$operation.DocumentationURL}}
{{- end}}
{{if eq $belongs "Bucket" -}}
func (s *{{$belongs}}) {{replace $opID "Bucket" "" -1 -}}(
{{- if $isObject}}objectKey string,{{end -}}
{{- if $hasInput}}input *{{$opID}}Input{{end -}}
) (*{{$opID}}Output, error) {
{{else -}}
func (s *{{$belongs}}) {{$opID}}(
{{- if $hasInput}}input *{{$opID}}Input{{end -}}
) (*{{$opID}}Output, error) {
{{end -}}
{{if eq $belongs "Bucket" -}}
r, x, err := s.{{replace $opID "Bucket" "" -1}}Request(
{{- if $isObject}}objectKey,{{end -}}
{{- if $hasInput}}input{{end -}}
)
{{else -}}
r, x, err := s.{{$opID}}Request(
{{- if $hasInput}}input{{end -}}
)
{{end}}
if err != nil {
return x, err
}
err = r.Send()
if err != nil {
return nil, err
}
requestID := r.HTTPResponse.Header.Get("X-Qs-Request-Id")
x.RequestID = &requestID
return x, err
}
{{if $operation.Description -}}
{{if eq $belongs "Bucket" -}}
// {{replace $opID "Bucket" "" -1}}Request creates request and output object of {{$opID}}.
{{else -}}
// {{$opID}}Request creates request and output object of {{$opID}}.
{{end -}}
{{end -}}
{{if eq $belongs "Bucket" -}}
func (s *{{$belongs}}) {{replace $opID "Bucket" "" -1 -}}Request(
{{- if $isObject}}objectKey string,{{end -}}
{{- if $hasInput}}input *{{$opID}}Input{{end -}}
) (*request.Request, *{{$opID}}Output, error) {
{{else -}}
func (s *{{$belongs}}) {{$opID}}Request(
{{- if $hasInput}}input *{{$opID}}Input{{end -}}
) (*request.Request, *{{$opID}}Output, error) {
{{end -}}
{{if $hasInput}}
if input == nil {
input = &{{$opID}}Input{}
}
{{end}}
{{$uri := $operation.Request.URI}}
{{$uri := replace $uri "{" "<" -1}}
{{$uri := replace $uri "}" ">" -1}}
{{$uri := dashConnected $uri}}
o := &data.Operation{
Config: s.Config,
{{- if ne $belongs "Service"}}
Properties: s.Properties,
{{- end}}
APIName: "{{$operation.Name}}",
RequestMethod: "{{$operation.Request.Method}}",
RequestURI: "{{$uri}}",
{{if $operation.Response.StatusCodes -}}
StatusCodes: []int{
{{range $statusCodeNumber, $statusCode := $operation.Response.StatusCodes -}}
{{$statusCodeNumber}},
{{- if $statusCode.Description -}}
// {{$statusCode.Description}}
{{- end}}
{{end -}}
},
{{else}}
StatusCodes: []int{
200, // OK
},
{{end -}}
}
{{if eq $service.Name "Object"}}
s.Properties.ObjectKey = &objectKey
{{end}}
x := &{{$opID}}Output{}
r, err := request.New(o, {{if $hasInput}}input{{else}}nil{{end}}, x)
if err != nil {
return nil, nil, err
}
return r, x, nil
}
{{if $hasInput}}
// {{$opID}}Input presents input for {{$opID}}.
type {{$opID}}Input struct {
{{- if $operation.Request.Params.Properties | len}}
{{$data := $operation.Request.Params -}}
{{template "RenderProperties" passThrough $data `location:"params"` $operation.Name}}
{{end}}
{{- if $operation.Request.Headers.Properties | len}}
{{$data := $operation.Request.Headers -}}
{{template "RenderProperties" passThrough $data `location:"headers"` $operation.Name}}
{{end}}
{{- if $operation.Request.Elements.Properties | len}}
{{$data := $operation.Request.Elements -}}
{{template "RenderProperties" passThrough $data `location:"elements"` $operation.Name}}
{{end}}
{{- if eq $operation.Request.Body.Type "string"}}
{{if $operation.Request.Body.Description -}}
// {{$operation.Request.Body.Description}}
{{- end}}
Body string `location:"body"`
{{else if eq $operation.Request.Body.Type "binary"}}
{{if $operation.Request.Body.Description -}}
// {{$operation.Request.Body.Description}}
{{- end}}
Body io.Reader `location:"body"`
{{end}}
}
// Validate validates the input for {{$opID}}.
func (v *{{$opID}}Input) Validate() error {
{{template "ValidateCustomizedType" passThrough $operation.Request.Params $operation.Name}}
{{template "ValidateCustomizedType" passThrough $operation.Request.Headers $operation.Name}}
{{template "ValidateCustomizedType" passThrough $operation.Request.Elements $operation.Name}}
return nil
}
{{end}}
// {{$opID}}Output presents output for {{$opID}}.
type {{$opID}}Output struct {
StatusCode *int `location:"statusCode"`
RequestID *string `location:"requestID"`
{{if eq $operation.Response.Body.Type "string"}}
{{if $operation.Response.Body.Description -}}
// {{$operation.Response.Body.Description}}
{{- end}}
Body string `location:"body"`
{{else if eq $operation.Response.Body.Type "binary"}}
{{if $operation.Response.Body.Description -}}
// {{$operation.Response.Body.Description}}
{{- end}}
Body io.ReadCloser `location:"body"`
{{end}}
{{if $operation.Response.Elements.Properties | len}}
{{$data := $operation.Response.Elements}}
{{template "RenderProperties" passThrough $data `location:"elements"` $operation.Name}}
{{end}}
{{if $operation.Response.Headers.Properties | len}}
{{$data := $operation.Response.Headers}}
{{template "RenderProperties" passThrough $data `location:"headers"` $operation.Name}}
{{end}}
}
{{end}}
{{define "SubServiceInitParams"}}
{{- $customizedType := index . 0 -}}
{{- $disablePointer := index . 1 -}}
{{- range $_, $property := $customizedType.Properties -}}
{{$property.ID | camelCase | lowerFirstWord}}{{" " -}}
{{template "PropertyType" passThrough $property $disablePointer}},
{{- end -}}
{{end}}
{{define "ValidateCustomizedType"}}
{{$customizedType := index . 0}}
{{$operationName := index . 1}}
{{range $_, $property := $customizedType.Properties}}
{{if or (ne $operationName "Delete Multiple Objects") (ne $property.ID "Content-MD5") -}}
{{$isNormalType := or (eq $property.Type "string") (eq $property.Type "integer")}}
{{$isContentLength := eq $property.ID "Content-Length"}}
{{if and $isNormalType (not $isContentLength) }}
{{if $property.IsRequired }}
if v.{{$property.ID | camelCase}} == nil {
return errors.ParameterRequiredError{
ParameterName: "{{$property.ID | camelCase}}",
ParentName: "{{$customizedType.ID | camelCase}}",
}
}
{{end}}
{{$parameterName := $property.ID | camelCase | lowerFirstWord}}
{{if gt ($property.Enum | len) 0}}
if v.{{$property.ID | camelCase}} != nil {
{{$parameterName}}ValidValues := []string{
{{- $property.Enum | commaConnectedWithQuote -}}
}
{{$parameterName}}ParameterValue := fmt.Sprint(*v.{{$property.ID | camelCase}})
{{$parameterName}}IsValid := false
for _, value := range {{$parameterName}}ValidValues {
if value == {{$parameterName}}ParameterValue {
{{$parameterName}}IsValid = true
}
}
if !{{$parameterName}}IsValid {
return errors.ParameterValueNotAllowedError{
ParameterName: "{{$property.ID | camelCase}}",
ParameterValue: {{$parameterName}}ParameterValue,
AllowedValues: {{$parameterName}}ValidValues,
}
}
}
{{end}}
{{end}}
{{if eq $property.Type "object"}}
if v.{{$property.ID | camelCase}} != nil {
if err := v.{{$property.ID | camelCase}}.Validate(); err != nil {
return err
}
}
{{if $property.IsRequired }}
if v.{{$property.ID | camelCase}} == nil {
return errors.ParameterRequiredError{
ParameterName: "{{$property.ID | camelCase}}",
ParentName: "{{$customizedType.ID | camelCase}}",
}
}
{{end}}
{{end}}
{{if eq $property.Type "array"}}
{{if $property.IsRequired}}
if len(v.{{$property.ID | camelCase}}) == 0 {
return errors.ParameterRequiredError{
ParameterName: "{{$property.ID | camelCase}}",
ParentName: "{{$customizedType.ID | camelCase}}",
}
}
{{end}}
{{$isNotString := ne $property.ExtraType "string"}}
{{$isNotInteger := ne $property.ExtraType "integer"}}
{{$isNotTimestamp := ne $property.ExtraType "timestamp"}}
{{if and $isNotString $isNotInteger $isNotTimestamp}}
if len(v.{{$property.ID | camelCase}}) > 0 {
for _, property := range v.{{$property.ID | camelCase}} {
if err := property.Validate(); err != nil {
return err
}
}
}
{{end}}
{{end}}
{{end}}
{{end}}
{{end}}

View file

@ -0,0 +1,61 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
{{$service := .Data.Service}}
{{$subService := index .Data.SubServices .CurrentSubServiceID}}
package service
import (
"fmt"
"io"
"time"
"github.com/yunify/qingstor-sdk-go/config"
"github.com/yunify/qingstor-sdk-go/request"
"github.com/yunify/qingstor-sdk-go/request/data"
"github.com/yunify/qingstor-sdk-go/request/errors"
)
var _ fmt.State
var _ io.Reader
var _ time.Time
var _ config.Config
{{if ne $subService.Name "Object"}}
// {{$subService.ID | camelCase}} presents {{$subService.ID | snakeCase}}.
type {{$subService.ID | camelCase}} struct {
Config *config.Config
Properties *Properties
}
// {{$subService.ID | camelCase}} initializes a new {{$subService.ID | snakeCase}}.
func (s *Service) {{$subService.ID | camelCase}}(
{{- template "SubServiceInitParams" passThrough $subService.Properties true -}}
) (*{{$subService.ID | camelCase}}, error) {
properties := &Properties{
{{range $_, $property := $subService.Properties.Properties -}}
{{$property.ID | upperFirst}}: &{{$property.ID}},
{{end -}}
}
return &{{$subService.ID | camelCase}}{Config: s.Config, Properties: properties}, nil
}
{{end}}
{{range $_, $operation := $subService.Operations}}
{{template "RenderOperation" passThrough $subService $operation}}
{{end}}

View file

@ -0,0 +1,62 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
{{$service := .Data.Service}}
{{$objectSubService := index .Data.SubServices "Object"}}
{{$customizedTypes := .Data.CustomizedTypes}}
package service
import (
"fmt"
"time"
"github.com/yunify/qingstor-sdk-go/request/errors"
)
// Properties presents the service properties.
type Properties struct {
{{- template "RenderProperties" passThrough $service.Properties "" "" -}}
{{range $_, $p := $objectSubService.Properties.Properties -}}
{{- if $p.Description -}}
// {{$p.Description}}
{{end -}}
{{if $p.Enum -}}
// {{camelCase $p.ID}}'s available values: {{commaConnected $p.Enum}}
{{end -}}
{{$p.ID | camelCase | upperFirst}}{{" " -}}
{{template "PropertyType" passThrough $p false}}{{" " -}}
`{{template "PropertyTagsDashConnected" $p}}`{{" " -}}
{{if $p.IsRequired -}}
// Required
{{- end}}
{{end}}
}
{{range $_, $customizedType := $customizedTypes}}
// {{$customizedType.ID | camelCase}}Type presents {{$customizedType.ID | camelCase}}.
type {{$customizedType.ID | camelCase}}Type struct {
{{template "RenderProperties" passThrough $customizedType "" ""}}
}
// Validate validates the {{$customizedType.ID | camelCase}}.
func (v *{{$customizedType.ID | camelCase}}Type) Validate() error {
{{template "ValidateCustomizedType" passThrough $customizedType ""}}
return nil
}
{{end}}

View file

@ -0,0 +1,3 @@
# Ignore local config
config.yaml
test_config.yaml

269
vendor/github.com/yunify/qingstor-sdk-go/test/bucket.go generated vendored Normal file
View file

@ -0,0 +1,269 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/DATA-DOG/godog"
"github.com/DATA-DOG/godog/gherkin"
qsErrors "github.com/yunify/qingstor-sdk-go/request/errors"
qs "github.com/yunify/qingstor-sdk-go/service"
)
// BucketFeatureContext provides feature context for bucket.
func BucketFeatureContext(s *godog.Suite) {
s.Step(`^initialize the bucket$`, initializeTheBucket)
s.Step(`^the bucket is initialized$`, theBucketIsInitialized)
s.Step(`^put bucket$`, putBucketFake)
s.Step(`^put bucket status code is (\d+)$`, putBucketStatusCodeIsFake)
s.Step(`^put same bucket again$`, putSameBucketAgain)
s.Step(`^put same bucket again status code is (\d+)$$`, putSameBucketAgainStatusCodeIs)
s.Step(`^list objects$`, listObjects)
s.Step(`^list objects status code is (\d+)$`, listObjectsStatusCodeIs)
s.Step(`^list objects keys count is (\d+)$`, listObjectsKeysCountIs)
s.Step(`^head bucket$`, headBucket)
s.Step(`^head bucket status code is (\d+)$`, headBucketStatusCodeIs)
s.Step(`^delete bucket$`, deleteBucketFake)
s.Step(`^delete bucket status code is (\d+)$`, deleteBucketStatusCodeIsFake)
s.Step(`^delete multiple objects:$`, deleteMultipleObjects)
s.Step(`^delete multiple objects code is (\d+)$`, deleteMultipleObjectsCodeIs)
s.Step(`^get bucket statistics$`, getBucketStatistics)
s.Step(`^get bucket statistics status code is (\d+)$`, getBucketStatisticsStatusCodeIs)
s.Step(`^get bucket statistics status is "([^"]*)"$`, getBucketStatisticsStatusIs)
s.Step(`^an object created by initiate multipart upload$`, anObjectCreatedByInitiateMultipartUpload)
s.Step(`^list multipart uploads$`, listMultipartUploads)
s.Step(`^list multipart uploads count is (\d+)$`, listMultipartUploadsCountIs)
s.Step(`^list multipart uploads with prefix$`, listMultipartUploadsWithPrefix)
s.Step(`^list multipart uploads with prefix count is (\d+)$`, listMultipartUploadsWithPrefixCountIs)
}
// --------------------------------------------------------------------------
var bucket *qs.Bucket
func initializeTheBucket() error {
bucket, err = qsService.Bucket(tc.BucketName, tc.Zone)
return err
}
func theBucketIsInitialized() error {
if bucket == nil {
return errors.New("Bucket is not initialized")
}
return nil
}
// --------------------------------------------------------------------------
var putBucketOutput *qs.PutBucketOutput
func putBucket() error {
putBucketOutput, err = bucket.Put()
return err
}
func putBucketFake() error {
return nil
}
func putBucketStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(putBucketOutput.StatusCode), statusCode)
}
func putBucketStatusCodeIsFake(_ int) error {
return nil
}
// --------------------------------------------------------------------------
func putSameBucketAgain() error {
_, err = bucket.Put()
return nil
}
func putSameBucketAgainStatusCodeIs(statusCode int) error {
switch e := err.(type) {
case *qsErrors.QingStorError:
return checkEqual(e.StatusCode, statusCode)
}
return fmt.Errorf("put same bucket again should get \"%d\"", statusCode)
}
// --------------------------------------------------------------------------
var listObjectsOutput *qs.ListObjectsOutput
func listObjects() error {
listObjectsOutput, err = bucket.ListObjects(&qs.ListObjectsInput{
Delimiter: qs.String("/"),
Limit: qs.Int(1000),
Prefix: qs.String("Test/"),
Marker: qs.String("Next"),
})
return err
}
func listObjectsStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(listObjectsOutput.StatusCode), statusCode)
}
func listObjectsKeysCountIs(count int) error {
return checkEqual(len(listObjectsOutput.Keys), count)
}
// --------------------------------------------------------------------------
var headBucketOutput *qs.HeadBucketOutput
func headBucket() error {
headBucketOutput, err = bucket.Head()
return err
}
func headBucketStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(headBucketOutput.StatusCode), statusCode)
}
// --------------------------------------------------------------------------
var deleteBucketOutput *qs.DeleteBucketOutput
func deleteBucket() error {
deleteBucketOutput, err = bucket.Delete()
return err
}
func deleteBucketFake() error {
return nil
}
func deleteBucketStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(deleteBucketOutput.StatusCode), statusCode)
}
func deleteBucketStatusCodeIsFake(_ int) error {
return nil
}
// --------------------------------------------------------------------------
var deleteMultipleObjectsOutput *qs.DeleteMultipleObjectsOutput
func deleteMultipleObjects(requestJSON *gherkin.DocString) error {
_, err := bucket.PutObject("object_0", nil)
if err != nil {
return err
}
_, err = bucket.PutObject("object_1", nil)
if err != nil {
return err
}
_, err = bucket.PutObject("object_2", nil)
if err != nil {
return err
}
deleteMultipleObjectsInput := &qs.DeleteMultipleObjectsInput{}
err = json.Unmarshal([]byte(requestJSON.Content), deleteMultipleObjectsInput)
if err != nil {
return err
}
deleteMultipleObjectsOutput, err = bucket.DeleteMultipleObjects(
&qs.DeleteMultipleObjectsInput{
Objects: deleteMultipleObjectsInput.Objects,
Quiet: deleteMultipleObjectsInput.Quiet,
},
)
return err
}
func deleteMultipleObjectsCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(deleteMultipleObjectsOutput.StatusCode), statusCode)
}
// --------------------------------------------------------------------------
var getBucketStatisticsOutput *qs.GetBucketStatisticsOutput
func getBucketStatistics() error {
getBucketStatisticsOutput, err = bucket.GetStatistics()
return err
}
func getBucketStatisticsStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(getBucketStatisticsOutput.StatusCode), statusCode)
}
func getBucketStatisticsStatusIs(status string) error {
return checkEqual(qs.StringValue(getBucketStatisticsOutput.Status), status)
}
// --------------------------------------------------------------------------
var listMultipartUploadsOutputObjectKey = "list_multipart_uploads_object_key"
var listMultipartUploadsInitiateOutput *qs.InitiateMultipartUploadOutput
var listMultipartUploadsOutput *qs.ListMultipartUploadsOutput
func anObjectCreatedByInitiateMultipartUpload() error {
listMultipartUploadsInitiateOutput, err = bucket.InitiateMultipartUpload(
listMultipartUploadsOutputObjectKey, nil,
)
return err
}
func listMultipartUploads() error {
listMultipartUploadsOutput, err = bucket.ListMultipartUploads(nil)
return err
}
func listMultipartUploadsCountIs(count int) error {
return checkEqual(len(listMultipartUploadsOutput.Uploads), count)
}
func listMultipartUploadsWithPrefix() error {
listMultipartUploadsOutput, err = bucket.ListMultipartUploads(
&qs.ListMultipartUploadsInput{
Prefix: qs.String(listMultipartUploadsOutputObjectKey),
},
)
return err
}
func listMultipartUploadsWithPrefixCountIs(count int) error {
_, err = bucket.AbortMultipartUpload(
listMultipartUploadsOutputObjectKey, &qs.AbortMultipartUploadInput{
UploadID: listMultipartUploadsInitiateOutput.UploadID,
},
)
if err != nil {
return err
}
return checkEqual(len(listMultipartUploadsOutput.Uploads), count)
}

View file

@ -0,0 +1,79 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package main
import (
"encoding/json"
"fmt"
"github.com/DATA-DOG/godog"
"github.com/DATA-DOG/godog/gherkin"
qs "github.com/yunify/qingstor-sdk-go/service"
)
// BucketACLFeatureContext provides feature context for bucket ACL.
func BucketACLFeatureContext(s *godog.Suite) {
s.Step(`^put bucket ACL:$`, putBucketACL)
s.Step(`^put bucket ACL status code is (\d+)$`, putBucketACLStatusCodeIs)
s.Step(`^get bucket ACL$`, getBucketACL)
s.Step(`^get bucket ACL status code is (\d+)$`, getBucketACLStatusCodeIs)
s.Step(`^get bucket ACL should have grantee name "([^"]*)"$`, getBucketACLShouldHaveGranteeName)
}
// --------------------------------------------------------------------------
var putBucketACLOutput *qs.PutBucketACLOutput
func putBucketACL(ACLJSONText *gherkin.DocString) error {
putBucketACLInput := &qs.PutBucketACLInput{}
err = json.Unmarshal([]byte(ACLJSONText.Content), putBucketACLInput)
if err != nil {
return err
}
putBucketACLOutput, err = bucket.PutACL(putBucketACLInput)
return err
}
func putBucketACLStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(putBucketACLOutput.StatusCode), statusCode)
}
// --------------------------------------------------------------------------
var getBucketACLOutput *qs.GetBucketACLOutput
func getBucketACL() error {
getBucketACLOutput, err = bucket.GetACL()
return err
}
func getBucketACLStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(getBucketACLOutput.StatusCode), statusCode)
}
func getBucketACLShouldHaveGranteeName(name string) error {
for _, ACL := range getBucketACLOutput.ACL {
if qs.StringValue(ACL.Grantee.Name) == name {
return nil
}
}
return fmt.Errorf("Grantee name \"%s\" not found in bucket ACLs", name)
}

View file

@ -0,0 +1,95 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package main
import (
"encoding/json"
"fmt"
"github.com/DATA-DOG/godog"
"github.com/DATA-DOG/godog/gherkin"
qs "github.com/yunify/qingstor-sdk-go/service"
)
// BucketCORSFeatureContext provides feature context for bucket CORS.
func BucketCORSFeatureContext(s *godog.Suite) {
s.Step(`^put bucket CORS:$`, putBucketCORS)
s.Step(`^put bucket CORS status code is (\d+)$`, putBucketCORSStatusCodeIs)
s.Step(`^get bucket CORS$`, getBucketCORS)
s.Step(`^get bucket CORS status code is (\d+)$`, getBucketCORSStatusCodeIs)
s.Step(`^get bucket CORS should have allowed origin "([^"]*)"$`, getBucketCORSShouldHaveAllowedOrigin)
s.Step(`^delete bucket CORS`, deleteBucketCORS)
s.Step(`^delete bucket CORS status code is (\d+)$`, deleteBucketCORSStatusCodeIs)
}
// --------------------------------------------------------------------------
var putBucketCORSOutput *qs.PutBucketCORSOutput
func putBucketCORS(CORSJSONText *gherkin.DocString) error {
putBucketCORSInput := &qs.PutBucketCORSInput{}
err = json.Unmarshal([]byte(CORSJSONText.Content), putBucketCORSInput)
if err != nil {
return err
}
putBucketCORSOutput, err = bucket.PutCORS(putBucketCORSInput)
return err
}
func putBucketCORSStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(putBucketCORSOutput.StatusCode), statusCode)
}
// --------------------------------------------------------------------------
var getBucketCORSOutput *qs.GetBucketCORSOutput
func getBucketCORS() error {
getBucketCORSOutput, err = bucket.GetCORS()
return err
}
func getBucketCORSStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(getBucketCORSOutput.StatusCode), statusCode)
}
func getBucketCORSShouldHaveAllowedOrigin(origin string) error {
for _, CORSRule := range getBucketCORSOutput.CORSRules {
if qs.StringValue(CORSRule.AllowedOrigin) == origin {
return nil
}
}
return fmt.Errorf("Allowed origin \"%s\" not found in bucket CORS rules", origin)
}
// --------------------------------------------------------------------------
var deleteBucketCORSOutput *qs.DeleteBucketCORSOutput
func deleteBucketCORS() error {
deleteBucketCORSOutput, err = bucket.DeleteCORS()
return err
}
func deleteBucketCORSStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(deleteBucketCORSOutput.StatusCode), statusCode)
}

View file

@ -0,0 +1,88 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package main
import (
"encoding/json"
"github.com/DATA-DOG/godog"
"github.com/DATA-DOG/godog/gherkin"
qs "github.com/yunify/qingstor-sdk-go/service"
)
// BucketExternalMirrorFeatureContext provides feature context for bucket external mirror.
func BucketExternalMirrorFeatureContext(s *godog.Suite) {
s.Step(`^put bucket external mirror:$`, putBucketExternalMirror)
s.Step(`^put bucket external mirror status code is (\d+)$`, putBucketExternalMirrorStatusCodeIs)
s.Step(`^get bucket external mirror$`, getBucketExternalMirror)
s.Step(`^get bucket external mirror status code is (\d+)$`, getBucketExternalMirrorStatusCodeIs)
s.Step(`^get bucket external mirror should have source_site "([^"]*)"$`, getBucketExternalMirrorShouldHaveSourceSite)
s.Step(`^delete bucket external mirror$`, deleteBucketExternalMirror)
s.Step(`^delete bucket external mirror status code is (\d+)$`, deleteBucketExternalMirrorStatusCodeIs)
}
// --------------------------------------------------------------------------
var putBucketExternalMirrorOutput *qs.PutBucketExternalMirrorOutput
func putBucketExternalMirror(ExternalMirrorJSONText *gherkin.DocString) error {
putBucketExternalMirrorInput := &qs.PutBucketExternalMirrorInput{}
err = json.Unmarshal([]byte(ExternalMirrorJSONText.Content), putBucketExternalMirrorInput)
if err != nil {
return err
}
putBucketExternalMirrorOutput, err = bucket.PutExternalMirror(putBucketExternalMirrorInput)
return err
}
func putBucketExternalMirrorStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(putBucketExternalMirrorOutput.StatusCode), statusCode)
}
// --------------------------------------------------------------------------
var getBucketExternalMirrorOutput *qs.GetBucketExternalMirrorOutput
func getBucketExternalMirror() error {
getBucketExternalMirrorOutput, err = bucket.GetExternalMirror()
return err
}
func getBucketExternalMirrorStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(getBucketExternalMirrorOutput.StatusCode), statusCode)
}
func getBucketExternalMirrorShouldHaveSourceSite(sourceSite string) error {
return checkEqual(qs.StringValue(getBucketExternalMirrorOutput.SourceSite), sourceSite)
}
// --------------------------------------------------------------------------
var deleteBucketExternalMirrorOutput *qs.DeleteBucketExternalMirrorOutput
func deleteBucketExternalMirror() error {
deleteBucketExternalMirrorOutput, err = bucket.DeleteExternalMirror()
return err
}
func deleteBucketExternalMirrorStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(deleteBucketExternalMirrorOutput.StatusCode), statusCode)
}

View file

@ -0,0 +1,105 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package main
import (
"encoding/json"
"fmt"
"github.com/DATA-DOG/godog"
"github.com/DATA-DOG/godog/gherkin"
qs "github.com/yunify/qingstor-sdk-go/service"
)
// BucketPolicyFeatureContext provides feature context for bucket policy.
func BucketPolicyFeatureContext(s *godog.Suite) {
s.Step(`^put bucket policy:$`, putBucketPolicy)
s.Step(`^put bucket policy status code is (\d+)$`, putBucketPolicyStatusCodeIs)
s.Step(`^get bucket policy$`, getBucketPolicy)
s.Step(`^get bucket policy status code is (\d+)$`, getBucketPolicyStatusCodeIs)
s.Step(`^get bucket policy should have Referer "([^"]*)"$`, getBucketPolicyShouldHaveReferer)
s.Step(`^delete bucket policy$`, deleteBucketPolicy)
s.Step(`^delete bucket policy status code is (\d+)$`, deleteBucketPolicyStatusCodeIs)
}
// --------------------------------------------------------------------------
var putBucketPolicyOutput *qs.PutBucketPolicyOutput
func putBucketPolicy(PolicyJSONText *gherkin.DocString) error {
putBucketPolicyInput := &qs.PutBucketPolicyInput{}
err = json.Unmarshal([]byte(PolicyJSONText.Content), putBucketPolicyInput)
if err != nil {
return err
}
if len(putBucketPolicyInput.Statement) == 1 {
putBucketPolicyInput.Statement[0].Resource = qs.StringSlice([]string{tc.BucketName + "/*"})
}
putBucketPolicyOutput, err = bucket.PutPolicy(putBucketPolicyInput)
return err
}
func putBucketPolicyStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(putBucketPolicyOutput.StatusCode), statusCode)
}
// --------------------------------------------------------------------------
var getBucketPolicyOutput *qs.GetBucketPolicyOutput
func getBucketPolicy() error {
getBucketPolicyOutput, err = bucket.GetPolicy()
return err
}
func getBucketPolicyStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(getBucketPolicyOutput.StatusCode), statusCode)
}
func getBucketPolicyShouldHaveReferer(compare string) error {
for _, statement := range getBucketPolicyOutput.Statement {
if statement.Condition != nil &&
statement.Condition.StringLike != nil {
for _, referer := range statement.Condition.StringLike.Referer {
if qs.StringValue(referer) == compare {
return nil
}
}
}
}
return fmt.Errorf("Referer \"%s\" not found in bucket policy statement", compare)
}
// --------------------------------------------------------------------------
var deleteBucketPolicyOutput *qs.DeleteBucketPolicyOutput
func deleteBucketPolicy() error {
deleteBucketPolicyOutput, err = bucket.DeletePolicy()
return err
}
func deleteBucketPolicyStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(deleteBucketPolicyOutput.StatusCode), statusCode)
}

View file

@ -0,0 +1,15 @@
# QingStor services configuration
access_key_id: ACCESS_KEY_ID
secret_access_key: SECRET_ACCESS_KEY
host: qingstor.com
port: 443
protocol: https
connection_retries: 3
# Additional User-Agent
additional_user_agent: "test/integration"
# Valid log levels are "debug", "info", "warn", "error", and "fatal".
log_level: debug

View file

@ -0,0 +1,9 @@
hash: a498153ecdb880e9d2a81849ffae311e03bf11570e30d28f64d396d532a066c6
updated: 2017-05-22T13:59:32.427609656+08:00
imports:
- name: github.com/DATA-DOG/godog
version: 623ff9946e5c72834c19a019a153b8e3a338a52c
subpackages:
- colors
- gherkin
testImports: []

View file

@ -0,0 +1,4 @@
package: github.com/yunify/qingstor-sdk-go/test
import:
- package: github.com/DATA-DOG/godog
version: v0.6.0

138
vendor/github.com/yunify/qingstor-sdk-go/test/main.go generated vendored Normal file
View file

@ -0,0 +1,138 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package main
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"time"
"github.com/DATA-DOG/godog"
"github.com/yunify/qingstor-sdk-go/config"
qsErrors "github.com/yunify/qingstor-sdk-go/request/errors"
qs "github.com/yunify/qingstor-sdk-go/service"
)
func main() {
setUp()
context := func(s *godog.Suite) {
ServiceFeatureContext(s)
BucketFeatureContext(s)
BucketACLFeatureContext(s)
BucketCORSFeatureContext(s)
BucketPolicyFeatureContext(s)
BucketExternalMirrorFeatureContext(s)
ObjectFeatureContext(s)
ObjectMultipartFeatureContext(s)
}
options := godog.Options{
Format: "pretty",
Paths: []string{"./features"},
Tags: "",
}
status := godog.RunWithOptions("*", context, options)
//tearDown()
os.Exit(status)
}
func setUp() {
loadTestConfig()
loadConfig()
initQingStorService()
err := initializeTheBucket()
checkErrorForExit(err)
err = theBucketIsInitialized()
checkErrorForExit(err)
//err = putBucket()
//checkError(err)
//err = putBucketStatusCodeIs(201)
//checkError(err)
}
func tearDown() {
err := deleteBucket()
checkError(err)
err = deleteBucketStatusCodeIs(204)
checkError(err)
retries := 0
for retries < tc.MaxRetries {
deleteBucketOutput, err = bucket.Delete()
checkError(err)
if err != nil {
switch e := err.(type) {
case *qsErrors.QingStorError:
if e.Code == "bucket_not_exists" {
return
}
}
}
retries++
time.Sleep(time.Second * time.Duration(tc.RetryWaitTime))
}
}
var err error
var tc *testConfig
var c *config.Config
var qsService *qs.Service
type testConfig struct {
Zone string `json:"zone" yaml:"zone"`
BucketName string `json:"bucket_name" yaml:"bucket_name"`
RetryWaitTime int `json:"retry_wait_time" yaml:"retry_wait_time"`
MaxRetries int `json:"max_retries" yaml:"max_retries"`
}
func loadTestConfig() {
if tc == nil {
configYAML, err := ioutil.ReadFile("./test_config.yaml")
checkErrorForExit(err)
tc = &testConfig{}
err = yaml.Unmarshal(configYAML, tc)
checkErrorForExit(err)
}
}
func loadConfig() {
if c == nil {
c, err = config.NewDefault()
checkErrorForExit(err)
err = c.LoadConfigFromFilePath("./config.yaml")
checkErrorForExit(err)
}
}
func initQingStorService() {
if qsService == nil {
qsService, err = qs.Init(c)
checkErrorForExit(err)
}
}

263
vendor/github.com/yunify/qingstor-sdk-go/test/object.go generated vendored Normal file
View file

@ -0,0 +1,263 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package main
import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"github.com/DATA-DOG/godog"
"github.com/yunify/qingstor-sdk-go/request"
qs "github.com/yunify/qingstor-sdk-go/service"
)
// ObjectFeatureContext provides feature context for object.
func ObjectFeatureContext(s *godog.Suite) {
s.Step(`^put object with key "(.{1,})"$`, putObjectWithKey)
s.Step(`^put object status code is (\d+)$`, putObjectStatusCodeIs)
s.Step(`^copy object with key "(.{1,})"$`, copyObjectWithKey)
s.Step(`^copy object status code is (\d+)$`, copyObjectStatusCodeIs)
s.Step(`^move object with key "(.{1,})"$`, moveObjectWithKey)
s.Step(`^move object status code is (\d+)$`, moveObjectStatusCodeIs)
s.Step(`^get object with key "(.{1,})"$`, getObjectWithKey)
s.Step(`^get object status code is (\d+)$`, getObjectStatusCodeIs)
s.Step(`^get object content length is (\d+)$`, getObjectContentLengthIs)
s.Step(`^get object "(.{1,})" with content type "(.{1,})"$`, getObjectWithContentType)
s.Step(`^get object content type is "(.{1,})"$`, getObjectContentTypeIs)
s.Step(`^get object "(.{1,})" with query signature$`, getObjectWithQuerySignature)
s.Step(`^get object with query signature content length is (\d+)$`, getObjectWithQuerySignatureContentLengthIs)
s.Step(`^head object with key "(.{1,})"$`, headObjectWithKey)
s.Step(`^head object status code is (\d+)$`, headObjectStatusCodeIs)
s.Step(`^options object "(.{1,})" with method "([^"]*)" and origin "([^"]*)"$`, optionsObjectWithMethodAndOrigin)
s.Step(`^options object status code is (\d+)$`, optionsObjectStatusCodeIs)
s.Step(`^delete object with key "(.{1,})"$`, deleteObjectWithKey)
s.Step(`^delete object status code is (\d+)$`, deleteObjectStatusCodeIs)
s.Step(`^delete the move object with key "(.{1,})"$`, deleteTheMoveObjectWithKey)
s.Step(`^delete the move object status code is (\d+)$`, deleteTheMoveObjectStatusCodeIs)
}
// --------------------------------------------------------------------------
var putObjectOutput *qs.PutObjectOutput
func putObjectWithKey(objectKey string) error {
_, err = exec.Command("dd", "if=/dev/zero", "of=/tmp/sdk_bin", "bs=1024", "count=1").Output()
if err != nil {
return err
}
defer os.Remove("/tmp/sdk_bin")
file, err := os.Open("/tmp/sdk_bin")
if err != nil {
return err
}
defer file.Close()
hash := md5.New()
_, err = io.Copy(hash, file)
hashInBytes := hash.Sum(nil)[:16]
md5String := hex.EncodeToString(hashInBytes)
//file.Seek(0, io.SeekStart)
file.Seek(0, 0)
putObjectOutput, err = bucket.PutObject(objectKey, &qs.PutObjectInput{
ContentType: qs.String("text/plain"),
ContentMD5: qs.String(md5String),
Body: file,
})
return err
}
func putObjectStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(putObjectOutput.StatusCode), statusCode)
}
// --------------------------------------------------------------------------
var copyObjectOutput *qs.PutObjectOutput
func copyObjectWithKey(objectKey string) error {
copyObjectKey := fmt.Sprintf(`%s_copy`, objectKey)
copyObjectOutput, err = bucket.PutObject(copyObjectKey, &qs.PutObjectInput{
XQSCopySource: qs.String(fmt.Sprintf(`/%s/%s`, tc.BucketName, objectKey)),
})
return err
}
func copyObjectStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(copyObjectOutput.StatusCode), statusCode)
}
// --------------------------------------------------------------------------
var moveObjectOutput *qs.PutObjectOutput
func moveObjectWithKey(objectKey string) error {
copyObjectKey := fmt.Sprintf(`%s_copy`, objectKey)
moveObjectKey := fmt.Sprintf(`%s_move`, objectKey)
moveObjectOutput, err = bucket.PutObject(moveObjectKey, &qs.PutObjectInput{
XQSMoveSource: qs.String(fmt.Sprintf(`/%s/%s`, tc.BucketName, copyObjectKey)),
})
return err
}
func moveObjectStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(moveObjectOutput.StatusCode), statusCode)
}
// --------------------------------------------------------------------------
var getObjectOutput *qs.GetObjectOutput
func getObjectWithKey(objectKey string) error {
getObjectOutput, err = bucket.GetObject(objectKey, nil)
return err
}
func getObjectStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(getObjectOutput.StatusCode), statusCode)
}
func getObjectContentLengthIs(length int) error {
buffer := &bytes.Buffer{}
buffer.ReadFrom(getObjectOutput.Body)
getObjectOutput.Body.Close()
return checkEqual(len(buffer.Bytes())*1024, length)
}
// --------------------------------------------------------------------------
var getObjectWithContentTypeRequest *request.Request
func getObjectWithContentType(objectKey, contentType string) error {
getObjectWithContentTypeRequest, _, err = bucket.GetObjectRequest(
objectKey,
&qs.GetObjectInput{
ResponseContentType: qs.String(contentType),
},
)
if err != nil {
return err
}
err = getObjectWithContentTypeRequest.Send()
if err != nil {
return err
}
return nil
}
func getObjectContentTypeIs(contentType string) error {
return checkEqual(getObjectWithContentTypeRequest.HTTPResponse.Header.Get("Content-Type"), contentType)
}
// --------------------------------------------------------------------------
var getObjectWithQuerySignatureURL string
func getObjectWithQuerySignature(objectKey string) error {
r, _, err := bucket.GetObjectRequest(objectKey, nil)
if err != nil {
return err
}
err = r.SignQuery(10)
if err != nil {
return err
}
getObjectWithQuerySignatureURL = r.HTTPRequest.URL.String()
return nil
}
func getObjectWithQuerySignatureContentLengthIs(length int) error {
out, err := http.Get(getObjectWithQuerySignatureURL)
if err != nil {
return err
}
buffer := &bytes.Buffer{}
buffer.ReadFrom(out.Body)
out.Body.Close()
return checkEqual(len(buffer.Bytes())*1024, length)
}
// --------------------------------------------------------------------------
var headObjectOutput *qs.HeadObjectOutput
func headObjectWithKey(objectKey string) error {
headObjectOutput, err = bucket.HeadObject(objectKey, nil)
return err
}
func headObjectStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(headObjectOutput.StatusCode), statusCode)
}
// --------------------------------------------------------------------------
var optionsObjectOutput *qs.OptionsObjectOutput
func optionsObjectWithMethodAndOrigin(objectKey, method, origin string) error {
optionsObjectOutput, err = bucket.OptionsObject(
objectKey,
&qs.OptionsObjectInput{
AccessControlRequestMethod: qs.String(method),
Origin: qs.String(origin),
},
)
return err
}
func optionsObjectStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(optionsObjectOutput.StatusCode), statusCode)
}
// --------------------------------------------------------------------------
var deleteObjectOutput *qs.DeleteObjectOutput
var deleteTheMoveObjectOutput *qs.DeleteObjectOutput
func deleteObjectWithKey(objectKey string) error {
deleteObjectOutput, err = bucket.DeleteObject(objectKey)
return err
}
func deleteObjectStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(deleteObjectOutput.StatusCode), statusCode)
}
func deleteTheMoveObjectWithKey(objectKey string) error {
deleteTheMoveObjectOutput, err = bucket.DeleteObject(fmt.Sprintf(`%s_move`, objectKey))
return err
}
func deleteTheMoveObjectStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(deleteTheMoveObjectOutput.StatusCode), statusCode)
}

View file

@ -0,0 +1,238 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package main
import (
"os"
"os/exec"
"github.com/DATA-DOG/godog"
"fmt"
"github.com/yunify/qingstor-sdk-go/request/errors"
qs "github.com/yunify/qingstor-sdk-go/service"
)
// ObjectMultipartFeatureContext provides feature context for object multipart.
func ObjectMultipartFeatureContext(s *godog.Suite) {
s.Step(`^initiate multipart upload with key "(.{1,})"$`, initiateMultipartUploadWithKey)
s.Step(`^initiate multipart upload status code is (\d+)$`, initiateMultipartUploadStatusCodeIs)
s.Step(`^upload the first part with key "(.{1,})"$`, uploadTheFirstPartWithKey)
s.Step(`^upload the first part status code is (\d+)$`, uploadTheFirstPartStatusCodeIs)
s.Step(`^upload the second part with key "(.{1,})"$`, uploadTheSecondPartWithKey)
s.Step(`^upload the second part status code is (\d+)$`, uploadTheSecondPartStatusCodeIs)
s.Step(`^upload the third part with key "(.{1,})"$`, uploadTheThirdPartWithKey)
s.Step(`^upload the third part status code is (\d+)$`, uploadTheThirdPartStatusCodeIs)
s.Step(`^list multipart with key "(.{1,})"$`, listMultipartWithKey)
s.Step(`^list multipart status code is (\d+)$`, listMultipartStatusCodeIs)
s.Step(`^list multipart object parts count is (\d+)$`, listMultipartObjectPartsCountIs)
s.Step(`^complete multipart upload with key "(.{1,})"$`, completeMultipartUploadWithKey)
s.Step(`^complete multipart upload status code is (\d+)$`, completeMultipartUploadStatusCodeIs)
s.Step(`^abort multipart upload with key "(.{1,})"$`, abortMultipartUploadWithKey)
s.Step(`^abort multipart upload status code is (\d+)$`, abortMultipartUploadStatusCodeIs)
s.Step(`^delete the multipart object with key "(.{1,})"$`, deleteTheMultipartObjectWithKey)
s.Step(`^delete the multipart object status code is (\d+)$`, deleteTheMultipartObjectStatusCodeIs)
}
// --------------------------------------------------------------------------
var initiateMultipartUploadOutput *qs.InitiateMultipartUploadOutput
func initiateMultipartUploadWithKey(objectKey string) error {
initiateMultipartUploadOutput, err = bucket.InitiateMultipartUpload(
objectKey,
&qs.InitiateMultipartUploadInput{
ContentType: qs.String("text/plain"),
},
)
return err
}
func initiateMultipartUploadStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(initiateMultipartUploadOutput.StatusCode), statusCode)
}
// --------------------------------------------------------------------------
var uploadTheFirstPartOutput *qs.UploadMultipartOutput
var uploadTheSecondPartOutput *qs.UploadMultipartOutput
var uploadTheThirdPartOutput *qs.UploadMultipartOutput
func uploadTheFirstPartWithKey(objectKey string) error {
_, err = exec.Command("dd", "if=/dev/zero", "of=/tmp/sdk_bin_part_0", "bs=1048576", "count=5").Output()
if err != nil {
return err
}
defer os.Remove("/tmp/sdk_bin_part_0")
file, err := os.Open("/tmp/sdk_bin_part_0")
if err != nil {
return err
}
defer file.Close()
uploadTheFirstPartOutput, err = bucket.UploadMultipart(
objectKey,
&qs.UploadMultipartInput{
UploadID: initiateMultipartUploadOutput.UploadID,
PartNumber: qs.Int(0),
Body: file,
},
)
return err
}
func uploadTheFirstPartStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(uploadTheFirstPartOutput.StatusCode), statusCode)
}
func uploadTheSecondPartWithKey(objectKey string) error {
_, err = exec.Command("dd", "if=/dev/zero", "of=/tmp/sdk_bin_part_1", "bs=1048576", "count=4").Output()
if err != nil {
return err
}
defer os.Remove("/tmp/sdk_bin_part_1")
file, err := os.Open("/tmp/sdk_bin_part_1")
if err != nil {
return err
}
defer file.Close()
uploadTheSecondPartOutput, err = bucket.UploadMultipart(
objectKey,
&qs.UploadMultipartInput{
UploadID: initiateMultipartUploadOutput.UploadID,
PartNumber: qs.Int(1),
Body: file,
},
)
return err
}
func uploadTheSecondPartStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(uploadTheSecondPartOutput.StatusCode), statusCode)
}
func uploadTheThirdPartWithKey(objectKey string) error {
_, err = exec.Command("dd", "if=/dev/zero", "of=/tmp/sdk_bin_part_2", "bs=1048576", "count=3").Output()
if err != nil {
return err
}
defer os.Remove("/tmp/sdk_bin_part_2")
file, err := os.Open("/tmp/sdk_bin_part_2")
if err != nil {
return err
}
defer file.Close()
uploadTheThirdPartOutput, err = bucket.UploadMultipart(
objectKey,
&qs.UploadMultipartInput{
UploadID: initiateMultipartUploadOutput.UploadID,
PartNumber: qs.Int(2),
Body: file,
},
)
return err
}
func uploadTheThirdPartStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(uploadTheThirdPartOutput.StatusCode), statusCode)
}
// --------------------------------------------------------------------------
var listMultipartOutput *qs.ListMultipartOutput
func listMultipartWithKey(objectKey string) error {
listMultipartOutput, err = bucket.ListMultipart(
objectKey,
&qs.ListMultipartInput{
UploadID: initiateMultipartUploadOutput.UploadID,
},
)
return err
}
func listMultipartStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(listMultipartOutput.StatusCode), statusCode)
}
func listMultipartObjectPartsCountIs(count int) error {
return checkEqual(len(listMultipartOutput.ObjectParts), count)
}
// --------------------------------------------------------------------------
var completeMultipartUploadOutput *qs.CompleteMultipartUploadOutput
func completeMultipartUploadWithKey(objectKey string) error {
completeMultipartUploadOutput, err = bucket.CompleteMultipartUpload(
objectKey,
&qs.CompleteMultipartUploadInput{
UploadID: initiateMultipartUploadOutput.UploadID,
ETag: qs.String(`"4072783b8efb99a9e5817067d68f61c6"`),
ObjectParts: listMultipartOutput.ObjectParts,
},
)
return err
}
func completeMultipartUploadStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(completeMultipartUploadOutput.StatusCode), statusCode)
}
// --------------------------------------------------------------------------
func abortMultipartUploadWithKey(objectKey string) error {
_, err = bucket.AbortMultipartUpload(
objectKey,
&qs.AbortMultipartUploadInput{
UploadID: initiateMultipartUploadOutput.UploadID,
},
)
return nil
}
func abortMultipartUploadStatusCodeIs(statusCode int) error {
switch e := err.(type) {
case *errors.QingStorError:
return checkEqual(e.StatusCode, statusCode)
}
return fmt.Errorf("abort multipart upload should get \"%d\"", statusCode)
}
// --------------------------------------------------------------------------
var deleteTheMultipartObjectOutput *qs.DeleteObjectOutput
func deleteTheMultipartObjectWithKey(objectKey string) error {
deleteTheMultipartObjectOutput, err = bucket.DeleteObject(objectKey)
return err
}
func deleteTheMultipartObjectStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(deleteTheMultipartObjectOutput.StatusCode), statusCode)
}

View file

@ -0,0 +1,60 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package main
import (
"errors"
"github.com/DATA-DOG/godog"
qs "github.com/yunify/qingstor-sdk-go/service"
)
// ServiceFeatureContext provides feature context for service.
func ServiceFeatureContext(s *godog.Suite) {
s.Step(`^initialize QingStor service$`, initializeQingStorService)
s.Step(`^the QingStor service is initialized$`, theQingStorServiceIsInitialized)
s.Step(`^list buckets$`, listBuckets)
s.Step(`^list buckets status code is (\d+)$`, listBucketsStatusCodeIs)
}
// --------------------------------------------------------------------------
func initializeQingStorService() error {
return nil
}
func theQingStorServiceIsInitialized() error {
if qsService == nil {
return errors.New("QingStor service is not initialized")
}
return nil
}
// --------------------------------------------------------------------------
var listBucketsOutput *qs.ListBucketsOutput
func listBuckets() error {
listBucketsOutput, err = qsService.ListBuckets(nil)
return err
}
func listBucketsStatusCodeIs(statusCode int) error {
return checkEqual(qs.IntValue(listBucketsOutput.StatusCode), statusCode)
}

View file

@ -0,0 +1,7 @@
# Test configurations
zone: pek3a
bucket_name: access-key-id
retry_wait_time: 3 # seconds
max_retries: 60

54
vendor/github.com/yunify/qingstor-sdk-go/test/utils.go generated vendored Normal file
View file

@ -0,0 +1,54 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package main
import (
"fmt"
"os"
"reflect"
)
func checkError(err error) {
if err != nil {
fmt.Println(err.Error())
}
}
func checkErrorForExit(err error, code ...int) {
if err != nil {
exitCode := 1
if len(code) > 0 {
exitCode = code[0]
}
fmt.Println(err.Error(), exitCode)
os.Exit(exitCode)
}
}
func checkEqual(value, shouldBe interface{}) error {
if value == nil || shouldBe == nil {
if value == shouldBe {
return nil
}
} else {
if reflect.DeepEqual(value, shouldBe) {
return nil
}
}
return fmt.Errorf("Value \"%v\" should be \"%v\"", value, shouldBe)
}

View file

@ -0,0 +1,23 @@
package utils
import (
"net/url"
"strings"
)
// URLQueryEscape escapes the original string.
func URLQueryEscape(origin string) string {
escaped := url.QueryEscape(origin)
escaped = strings.Replace(escaped, "%2F", "/", -1)
escaped = strings.Replace(escaped, "%3D", "=", -1)
escaped = strings.Replace(escaped, "+", "%20", -1)
return escaped
}
// URLQueryUnescape unescapes the escaped string.
func URLQueryUnescape(escaped string) (string, error) {
escaped = strings.Replace(escaped, "/", "%2F", -1)
escaped = strings.Replace(escaped, "=", "%3D", -1)
escaped = strings.Replace(escaped, "%20", " ", -1)
return url.QueryUnescape(escaped)
}

View file

@ -0,0 +1,30 @@
package utils
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestURLQueryEscape(t *testing.T) {
assert.Equal(t, "/", URLQueryEscape("/"))
assert.Equal(t, "%20", URLQueryEscape(" "))
assert.Equal(t,
"%21%40%23%24%25%5E%26%2A%28%29_%2B%7B%7D%7C",
URLQueryEscape("!@#$%^&*()_+{}|"),
)
}
func TestURLQueryUnescape(t *testing.T) {
x, err := URLQueryUnescape("/")
assert.Nil(t, err)
assert.Equal(t, "/", x)
x, err = URLQueryUnescape("%20")
assert.Nil(t, err)
assert.Equal(t, " ", x)
x, err = URLQueryUnescape("%21%40%23%24%25%5E%26%2A%28%29_%2B%7B%7D%7C")
assert.Nil(t, err)
assert.Equal(t, "!@#$%^&*()_+{}|", x)
}

23
vendor/github.com/yunify/qingstor-sdk-go/version.go generated vendored Normal file
View file

@ -0,0 +1,23 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
// Package sdk is the official QingStor SDK for the Go programming language.
//
// https://github.com/yunify/qingstor-sdk-go
package sdk
// Version number.
const Version = "2.2.5"

View file

@ -0,0 +1,28 @@
// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------
package sdk
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestVersion(t *testing.T) {
assert.Equal(t, "string", reflect.ValueOf(Version).Type().String())
}