tcos

package
v0.0.0-...-20aadb4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2018 License: MIT Imports: 14 Imported by: 0

README

Usage

import "github.com/TechCatsLab/apix/cloud/tencent/tcos"

AuthorizationConfig

Get APPID, SecretID, SecretKey from https://console.cloud.tencent.com/cam/capi

var authorizationConfig = &tcos.AuthorizationConfig{
    AppID:     YourAPPID,
    SecretID:  YourSecretID,
    SecretKey: YourSecretKey,
}

GetServiceClient

authorizationClient, err := tcos.CreateAuthorizationClient(authorizationConfig)

buckets, err := authorizationClient.ListBuckets()
for _, bucket := range buckets {
    fmt.Printf("Bucket: %+v\n", bucket)
}

GetBucketClient

  • put new bucket
bucketConfig := &tcos.BucketConfig{{
    AuthorizationConfig: authorizationConfig,
    Name:                NewBucketName,
    Region:              Region(e.g. "ap-shanghai"),
}

bucketClient, err := tcos.PutBucket(config, nil)
  • from authorizationClient
buckets, err := authorizationClient.ListBuckets()

bucketClient, err := authorizationClient.CreateBucketClient(&buckets[0])
  • from CreateBucketClient
bucketConfig := &tcos.BucketConfig{{
    AuthorizationConfig: authorizationConfig,
    Name:                BucketName,
    Region:              Region(e.g. "ap-shanghai"),
}

bucketClient, err := tcos.CreateBucketClient(bucketConfig)

BucketClient Operations

  • PutCORS(opt *tcos.BucketPutCORSOptions)
opt := &tcos.BucketPutCORSOptions{
    Rules: []tcos.BucketCORSRule{
        {
            AllowedOrigins: []string{"http://www.qq.com"},
            AllowedMethods: []string{"PUT", "GET"},
            AllowedHeaders: []string{"x-cos-meta-test", "x-cos-xx"},
            MaxAgeSeconds:  500,
            ExposeHeaders:  []string{"x-cos-meta-test1"},
        },
        {
            ID:             "1234",
            AllowedOrigins: []string{"http://www.google.com", "twitter.com"},
            AllowedMethods: []string{"PUT", "GET"},
            MaxAgeSeconds:  500,
        },
    },
}
err = bucketClient.PutCORS(opt)
  • GetCORS()
rules, err := bucketClient.GetCORS()
for _, r := range rules {
    fmt.Printf("%+v\n", r)
}
  • PutObject(objectKey string, reader io.Reader, force bool, opt *ObjectPutOptions)
f, err := os.Open(filename)
s, err := f.Stat()

opt := &ObjectPutOptions{
    ObjectPutHeaderOptions: &ObjectPutHeaderOptions{
        ContentLength: int(s.Size()),
    },
}
resp, err = bucketClient.PutObject(s.Name(), f, false, opt)
  • GetObject(objectKey string, opt *ObjectGetOptions)
resp, err := bucketClient.GetObject(objectKey, nil)
data, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
err = ioutil.WriteFile(filepath.Join(localPath, filename), data, 0666)
  • Copy(sourceKey, destKey string, force bool, opt *ObjectCopyOptions)
res, resp, err = bucketClient.Copy("filename_1", "files/filename_1", true, nil)
  • Move(sourceKey, destKey string, force bool, opt *ObjectCopyOptions)
res, resp, err = bucketClient.Move("filename_1", "files/filename_1", true, nil)
  • Rename(sourceKey, fileName string, opt *ObjectCopyOptions)
res, resp, err = bucketClient.Rename("filename_1", "filename_2", true, nil)
  • DeleteObject(objectKey string)
err = bucketClient.DeleteObject(filename)
  • ListObjects(opt *BucketGetOptions)
list, err := bucketClient.ListObjects(nil)
for _, obj := range list {
    t.Logf("Object: %+v\n", obj)
}
  • ObjectDownloadURL(objectKey string)
url, err := bucketClient.ObjectDownloadURL(objectKey)
  • ObjectStaticURL(objectKey string) - NOTICE: This action need enable static website in basicconfig of bucket
url, err := bucketClient.ObjectStaticURL(objectKey)
  • HeadObject(objectKey string, opt *ObjectHeadOptions)
resp, err = bucketClient.HeadObject(filename, nil)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckAuthorizationConfig

func CheckAuthorizationConfig(config AuthorizationConfig) error

CheckAuthorizationConfig ...

func ConfirmAuthorization

func ConfirmAuthorization(client *cos.Client) error

ConfirmAuthorization is used to confirm authorization info

func DownloadToLocal

func DownloadToLocal(client *BucketClient, objectKey, localPath, filename string) error

DownloadToLocal object to local

func HeadBucket

func HeadBucket(client *cos.Client) error

HeadBucket tests bucket is available or not Status: 200 - ok, 403 - Forbidden, 404 - Not Found

Types

type ACLHeaderOptions

type ACLHeaderOptions = cos.ACLHeaderOptions

ACLHeaderOptions see Permission-related headers in https://intl.cloud.tencent.com/document/product/436/7749#non-common-header

type AuthorizationClient

type AuthorizationClient struct {
	*AuthorizationConfig
	Client *cos.Client
}

AuthorizationClient is used for services.

func CreateAuthorizationClient

func CreateAuthorizationClient(config AuthorizationConfig) (*AuthorizationClient, error)

CreateAuthorizationClient creates a service client. Client contains BaseURL, common service, *ServiceService, *BucketService, *ObjectService

func (*AuthorizationClient) CreateBucketClient

func (c *AuthorizationClient) CreateBucketClient(bucket *Bucket) (*BucketClient, error)

CreateBucketClient is used to get a bucket client by AuthorizationClient

func (*AuthorizationClient) ListBuckets

func (c *AuthorizationClient) ListBuckets() ([]Bucket, error)

ListBuckets is used to get all buckets of authorized user

func (*AuthorizationClient) Service

func (c *AuthorizationClient) Service() (*cos.ServiceGetResult, error)

Service returns a service, which contains Owner and Buckets.

type AuthorizationConfig

type AuthorizationConfig struct {
	AppID     string // mandatory
	SecretID  string // mandatory
	SecretKey string // mandatory
}

AuthorizationConfig contains AppID, SecretID, SecretKey. AppID can be used to create a new bucket. SecretID & SecretKey are used for AuthorizationTransport. Get values from https://console.cloud.tencent.com/cam/capi Set policy for permission in https://console.cloud.tencent.com/cam/policy

type Bucket

type Bucket = cos.Bucket

Bucket contains Name, AppID, Region, CreateDate of created bucket

type BucketCORSRule

type BucketCORSRule = cos.BucketCORSRule

BucketCORSRule : ID string `xml:"ID,omitempty"` AllowedMethods []string `xml:"AllowedMethod"` AllowedOrigins []string `xml:"AllowedOrigin"` AllowedHeaders []string `xml:"AllowedHeader,omitempty"` MaxAgeSeconds int `xml:"MaxAgeSeconds,omitempty"` ExposeHeaders []string `xml:"ExposeHeader,omitempty"`

type BucketClient

type BucketClient struct {
	*BucketConfig
	Client *cos.Client
}

BucketClient is used for bucket operation

func CreateBucketClient

func CreateBucketClient(config BucketConfig) (*BucketClient, error)

CreateBucketClient creates a bucket client, which can request bucket operations

func PutBucket

func PutBucket(config BucketConfig, options *BucketPutOptions) (*BucketClient, error)

PutBucket is used to create a new bucket. options: https://intl.cloud.tencent.com/document/product/436/7738#request-header

func (*BucketClient) Copy

func (c *BucketClient) Copy(sourceKey, destKey string, force bool, opt *ObjectCopyOptions) (*ObjectCopyResult, *http.Response, error)

Copy sourceKey to destKey. Enable force will overwrite file if destKey exists. This action can be used to copy, move, rename and reset object

func (*BucketClient) Delete

func (c *BucketClient) Delete() error

Delete the bucket. The bucket must be empty before deleting.

func (*BucketClient) DeleteObject

func (c *BucketClient) DeleteObject(objectKey string) error

DeleteObject is used to delete one file (Object) in Bucket. This action requires WRITE permission for the Bucket. if the named object doesn't exist, delete operation is ok and return 204 - No Content

func (*BucketClient) DownloadObject

func (c *BucketClient) DownloadObject(objectKey string, writer io.Writer) error

DownloadObject ...

func (*BucketClient) GetCORS

func (c *BucketClient) GetCORS() ([]BucketCORSRule, error)

GetCORS output CORS of basic-config in bucket

func (*BucketClient) GetObject

func (c *BucketClient) GetObject(objectKey string, opt *ObjectGetOptions) (*http.Response, error)

GetObject ...

func (*BucketClient) HeadObject

func (c *BucketClient) HeadObject(objectKey string, opt *ObjectHeadOptions) (*http.Response, error)

HeadObject requests object meta info. if opt specified "IfModifiedSince" Header and object is not modified, response 304

func (*BucketClient) ListObjects

func (c *BucketClient) ListObjects(opt *BucketGetOptions) ([]Object, error)

ListObjects is used to get all objects in bucket. This action requires READ permission for the Bucket. see options details in https://intl.cloud.tencent.com/document/product/436/7734#request-parameters

func (*BucketClient) Move

func (c *BucketClient) Move(sourceKey, destKey string, force bool, opt *ObjectCopyOptions) (*ObjectCopyResult, *http.Response, error)

Move object

func (*BucketClient) ObjectDownloadURL

func (c *BucketClient) ObjectDownloadURL(objectKey string) (string, error)

ObjectDownloadURL ...

func (*BucketClient) ObjectStaticURL

func (c *BucketClient) ObjectStaticURL(objectKey string) (string, error)

ObjectStaticURL return static url, which can be embedded in website NOTICE: This action need enable static website in basicconfig of bucket

func (*BucketClient) PutCORS

func (c *BucketClient) PutCORS(opt *BucketPutCORSOptions) error

PutCORS config

func (*BucketClient) PutObject

func (c *BucketClient) PutObject(objectKey string, reader io.Reader, force bool, opt *ObjectPutOptions) (*http.Response, error)

PutObject to bucket. This action requires WRITE permission for the Bucket. enable force will overwrite object if ObjectAlreadyExists

func (*BucketClient) Rename

func (c *BucketClient) Rename(sourceKey, fileName string, opt *ObjectCopyOptions) (*ObjectCopyResult, *http.Response, error)

Rename object TODO: Rename directory

type BucketConfig

type BucketConfig struct {
	*AuthorizationConfig        // mandatory
	Name                 string // mandatory
	Region               string // mandatory, see: https://intl.cloud.tencent.com/document/product/436/6224
}

BucketConfig contains AuthorizationConfig, Name, Region. AuthorizationConfig is needed, for more information see service.go When creating new bucket, Name & Region is mandatory.

type BucketGetOptions

type BucketGetOptions = cos.BucketGetOptions

BucketGetOptions contains request parameters of ListObjects() see options details in https://intl.cloud.tencent.com/document/product/436/7734#request-parameters

type BucketPutCORSOptions

type BucketPutCORSOptions = cos.BucketPutCORSOptions

BucketPutCORSOptions contains XMLName and BucketCORSRule details see https://intl.cloud.tencent.com/document/product/436/8279#request-body

type BucketPutOptions

type BucketPutOptions = cos.BucketPutOptions

BucketPutOptions in https://intl.cloud.tencent.com/document/product/436/7738#request-header

type Object

type Object = cos.Object

Object contains Key, ETag, Size, PartNumber, LastModified, StorageClass, Owner of created object e.g. Object{Key:"text.txt", ETag:"\"847f4281d3e9ad10844ef37da835cfc0\"", Size:4545, PartNumber:0, LastModified:"2018-05-22T14:39:23.000Z", StorageClass:"STANDARD", Owner:(*cos.Owner)(0xc42018bb00)}

type ObjectCopyHeaderOptions

type ObjectCopyHeaderOptions = cos.ObjectCopyHeaderOptions

ObjectCopyHeaderOptions see https://cloud.tencent.com/document/product/436/10881#.E9.9D.9E.E5.85.AC.E5.85.B1.E5.A4.B4.E9.83.A8

type ObjectCopyOptions

type ObjectCopyOptions = cos.ObjectCopyOptions

ObjectCopyOptions contains ObjectCopyHeaderOptions and ACLHeaderOptions

type ObjectCopyResult

type ObjectCopyResult = cos.ObjectCopyResult

ObjectCopyResult ...

func Copy

func Copy(client *BucketClient, sourceKey, destKey string, force bool, opt *ObjectCopyOptions) (*ObjectCopyResult, *http.Response, error)

Copy ...

type ObjectGetOptions

type ObjectGetOptions = cos.ObjectGetOptions

ObjectGetOptions is used for GetObject() details see https://intl.cloud.tencent.com/document/product/436/7753

type ObjectHeadOptions

type ObjectHeadOptions = cos.ObjectHeadOptions

ObjectHeadOptions specified "IfModifiedSince" Header

type ObjectPutHeaderOptions

type ObjectPutHeaderOptions = cos.ObjectPutHeaderOptions

ObjectPutHeaderOptions see Recommended Header in https://intl.cloud.tencent.com/document/product/436/7749#non-common-header

type ObjectPutOptions

type ObjectPutOptions = cos.ObjectPutOptions

ObjectPutOptions contains ACLHeaderOptions and ObjectPutHeaderOptions details see https://intl.cloud.tencent.com/document/product/436/7749#request-header

type OpError

type OpError struct {
	Code    string
	Message string
	Err     error
}

OpError contains code & message, which describes the Err

func ErrConvert

func ErrConvert(err error) (*OpError, bool)

ErrConvert to OpError

func (*OpError) Error

func (err *OpError) Error() string

Error return string format

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL