oss

package module
v0.0.0-...-2127a14 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2015 License: Apache-2.0 Imports: 21 Imported by: 6

README

oss-go-sdk

Build Status Coveralls

aliyun OSS(open storage service) golang client.

Install

go get -v github.com/Lupino/oss-go-sdk

License

Apache2.0

OSS Usage

OSS, Open Storage Service. Equal to well known Amazon S3.

Create Account

Go to OSS website, create a new account for new user.

After account created, you can create the OSS instance and get the accessKeyId and accessKeySecret.

Initial OSS API

import (
    "github.com/Lupino/oss-go-sdk"
)
var APIOptions = oss.GetDefaultAPIOptioins()
APIOptions.AccessID = AccessKeyID
APIOptions.SecretAccessKey = AccessKeySecret
var OSSAPI, err = oss.NewAPI(APIOptions)

Get Service

var result oss.ListAllMyBucketsResult
var headers = make(map[string]string)
var err error
err = OSSAPI.GetService(&result, headers)

Parse the error

oss-go-sdk implement error return by OSS into error interface by oss.Error, so you can get error return by OSS server from oss.Error, just like:

var realErr = err.(*oss.Error)

Tutorial

API docs

API docs

Documentation

Overview

Package oss aliyun OSS(open storage service) golang client.

## Create Account

Go to [OSS website](http://www.aliyun.com/product/oss/?lang=en), create a new account for new user.

After account created, you can create the OSS instance and get the `accessKeyId` and `accessKeySecret`.

## Initial OSS API

import (
    "github.com/Lupino/oss-go-sdk"
)
var APIOptions = oss.GetDefaultAPIOptioins()
APIOptions.AccessID = AccessKeyID
APIOptions.SecretAccessKey = AccessKeySecret
var OSSAPI, err = oss.NewAPI(APIOptions)

## Get Service

var result oss.ListAllMyBucketsResult
var headers = make(map[string]string)
var err error
err = OSSAPI.GetService(&result, headers)

## Parse the error

`oss-go-sdk` implement error return by OSS into `error` interface by `oss.Error`, so you can get error return by OSS server from `oss.Error`, just like:

var realErr = err.(*oss.Error)

Index

Constants

View Source
const PROVIDER = "OSS"

PROVIDER defined provider

View Source
const SelfDefineHeaderPrefix = "x-oss-"

SelfDefineHeaderPrefix defined oss header prefix

View Source
const Version = "0.0.1"

Version OSS package version

Variables

View Source
var AGENT string

AGENT http requset agent

View Source
var OSSHostList = []string{"aliyun-inc.com", "aliyuncs.com", "alibaba.net", "s3.amazonaws.com"}

OSSHostList defined OSS host list

Functions

This section is empty.

Types

type ACLGrant

type ACLGrant string

ACLGrant defined AccessControlPolicy grant type

const (
	// ACLPublicReadWrite defined public read write AccessControlPolicy
	ACLPublicReadWrite ACLGrant = "public-read-write"
	// ACLPublicRead defined public read AccessControlPolicy
	ACLPublicRead ACLGrant = "public-read"
	// ACLPrivate defined private AccessControlPolicy
	ACLPrivate ACLGrant = "private"
)

type API

type API struct {
	// contains filtered or unexported fields
}

API A simple OSS API

func NewAPI

func NewAPI(options *APIOptions) (*API, error)

NewAPI initial simple OSS API

func (*API) AppendObject

func (api *API) AppendObject(bucket, object string, position int, body io.Reader,
	headers map[string]string) (result http.Header, err error)

AppendObject append data to an appendable object

func (*API) CopyObject

func (api *API) CopyObject(sourceBucket, sourceObject, targetBucket, targetObject string,
	headers map[string]string) (result CopyObjectResult, err error)

CopyObject copy an object from sourceName to name.

func (*API) CreateBucket

func (api *API) CreateBucket(bucket string, acl ACLGrant, headers map[string]string) error

CreateBucket create bucket

func (*API) DeleteBucket

func (api *API) DeleteBucket(bucket string) error

DeleteBucket delete an empty bucket. If bucket is not empty, will throw BucketNotEmptyError. If bucket is not exists, will throw NoSuchBucketError.

func (*API) DeleteBucketCORS

func (api *API) DeleteBucketCORS(bucket string) error

DeleteBucketCORS Delete bucket cors

func (*API) DeleteBucketLifecycle

func (api *API) DeleteBucketLifecycle(bucket string) error

DeleteBucketLifecycle delete bucket object lifecycle

func (*API) DeleteBucketLogging

func (api *API) DeleteBucketLogging(bucket string) error

DeleteBucketLogging delete bucket logging settings

func (*API) DeleteBucketReferer

func (api *API) DeleteBucketReferer(bucket string) error

DeleteBucketReferer delete the bucket request Referer white list.

func (*API) DeleteBucketWebsite

func (api *API) DeleteBucketWebsite(bucket string) error

DeleteBucketWebsite delete bucket website config

func (*API) DeleteObject

func (api *API) DeleteObject(bucket, object string) error

DeleteObject delete an object from the bucket.

func (*API) DeleteObjects

func (api *API) DeleteObjects(bucket string, objects []string, result *DeleteResult) error

DeleteObjects delete multi objects in one request.

func (*API) GetBucket

func (api *API) GetBucket(bucket string, result *ListBucketResult, headers map[string]string) error

GetBucket list object that in bucket

func (*API) GetBucketACL

func (api *API) GetBucketACL(bucket string, result *AccessControlPolicy) error

GetBucketACL get the bucket ACL.

func (*API) GetBucketCORS

func (api *API) GetBucketCORS(bucket string, result *CORSConfiguration) error

GetBucketCORS Get bucket cors

func (*API) GetBucketLifecycle

func (api *API) GetBucketLifecycle(bucket string, result *LifecycleConfiguration) error

GetBucketLifecycle get bucket lifecycle

func (*API) GetBucketLocation

func (api *API) GetBucketLocation(bucket string, result *LocationConstraint) error

GetBucketLocation get Location of bucket

func (*API) GetBucketLogging

func (api *API) GetBucketLogging(bucket string, result *BucketLoggingStatus) error

GetBucketLogging get bucket logging settings

func (*API) GetBucketReferer

func (api *API) GetBucketReferer(bucket string, result *RefererConfiguration) error

GetBucketReferer get the bucket request Referer white list.

func (*API) GetBucketWebsite

func (api *API) GetBucketWebsite(bucket string, result *WebsiteConfiguration) error

GetBucketWebsite get bucket website config.

func (*API) GetMultiPartUpload

func (api *API) GetMultiPartUpload(bucket, object, uploadID string) (*MultipartUpload, error)

GetMultiPartUpload get multipart upload

func (*API) GetObject

func (api *API) GetObject(bucket, object string, headers, params map[string]string) (io.ReadCloser, error)

GetObject get an object from the bucket.

func (*API) GetObjectACL

func (api *API) GetObjectACL(bucket, object string, result *AccessControlPolicy) error

GetObjectACL get object acl

func (*API) GetService

func (api *API) GetService(result *ListAllMyBucketsResult, headers map[string]string) error

GetService list all buckets of user

func (*API) HeadObject

func (api *API) HeadObject(bucket, object string, headers map[string]string) (result http.Header, err error)

HeadObject head an object and get the meta info.

func (*API) ListAllMyBuckets

func (api *API) ListAllMyBuckets(result *ListAllMyBucketsResult, headers map[string]string) error

ListAllMyBuckets list all buckets of user

func (*API) ListBucket

func (api *API) ListBucket(bucket string, result *ListBucketResult, headers map[string]string) error

ListBucket list object that in bucket

func (*API) ListMultipartUpload

func (api *API) ListMultipartUpload(bucket string, opts *ListMultipartUploadOptions) ([]*MultipartUpload, error)

ListMultipartUpload list all multipart uploads and their parts

func (*API) NewMultipartUpload

func (api *API) NewMultipartUpload(bucket, object string, headers map[string]string) (*MultipartUpload, error)

NewMultipartUpload initial multipart upload

func (*API) OptionObject

func (api *API) OptionObject(bucket, object string, headers map[string]string) (http.Header, error)

OptionObject options object to determine if user can send the actual HTTP request

func (*API) PostObject

func (api *API) PostObject(bucket, object string, body io.Reader, headers map[string]string) error

PostObject is same to PutObject, but use POST method, so just alisa to PutObject

func (*API) PutBucket

func (api *API) PutBucket(bucket string, acl ACLGrant, location string, headers map[string]string) error

PutBucket create bucket

  • bucket: bucket name If bucket exists and not belong to current account, will throw BucketAlreadyExistsError. If bucket not exists, will create a new bucket and set it's ACL
  • acl: one of private public-read public-read-write
  • location: the bucket data region location, Current available: oss-cn-hangzhou, oss-cn-qingdao, oss-cn-beijing, oss-cn-hongkong and oss-cn-shenzhen If change exists bucket region, will throw BucketAlreadyExistsError. If region value invalid, will throw InvalidLocationConstraintError.
  • headers: HTTP header

func (*API) PutBucketACL

func (api *API) PutBucketACL(bucket string, acl ACLGrant, headers map[string]string) error

PutBucketACL create bucket with acl or update bucket acl when bucket is exists

func (*API) PutBucketCORS

func (api *API) PutBucketCORS(bucket string, config CORSConfiguration) error

PutBucketCORS put bucket cors

func (*API) PutBucketLifecycle

func (api *API) PutBucketLifecycle(bucket string, rule LifecycleRule) error

PutBucketLifecycle set the bucket object lifecycle.

func (*API) PutBucketLogging

func (api *API) PutBucketLogging(sourcebucket, targetbucket, prefix string) error

PutBucketLogging update the bucket logging settings. Log file will create every one hour and name format: <prefix><bucket>-YYYY-mm-DD-HH-MM-SS-UniqueString.

func (*API) PutBucketReferer

func (api *API) PutBucketReferer(bucket string, config RefererConfiguration) error

PutBucketReferer set the bucket request Referer white list.

func (*API) PutBucketWebsite

func (api *API) PutBucketWebsite(bucket, indexfile, errorfile string) error

PutBucketWebsite set the bucket as a static website.

  • indexfile: the object that contain index page
  • errorfile: the object taht contain error page

func (*API) PutObject

func (api *API) PutObject(bucket, object string, body io.Reader, headers map[string]string) error

PutObject add an object to the bucket.

  • bucket: an exists bucket
  • object: object name store on OSS
  • body: readable object
  • headers: HTTP Header

func (*API) PutObjectACL

func (api *API) PutObjectACL(bucket, object, acl string) error

PutObjectACL update object acl

func (*API) SetDebug

func (api *API) SetDebug()

SetDebug set debug for OSS API

func (*API) SetIsOSSHost

func (api *API) SetIsOSSHost(isOSSHost bool)

SetIsOSSHost set is oss host for OSS API

func (*API) SetRetryTimes

func (api *API) SetRetryTimes(retryTimes int)

SetRetryTimes set retry times for OSS API

func (*API) SetTimeout

func (api *API) SetTimeout(timeout time.Duration)

SetTimeout set timeout for OSS API

func (*API) SignURL

func (api *API) SignURL(options *SignURLOptions) string

SignURL create the authorization for OSS based on the input method, url, body and headers

Returns:

signature url.

func (*API) SignURLAuthWithExpireTime

func (api *API) SignURLAuthWithExpireTime(options *SignURLOptions) string

SignURLAuthWithExpireTime create the authorization for OSS based on the input method, url, body and headers

Returns:

signature url.

func (*API) UploadLargeFile

func (api *API) UploadLargeFile(bucket, object, fileName string, bufSize int64,
	headers map[string]string) (result CompleteMultipartUploadResult, err error)

UploadLargeFile upload large file, the content is read from filename. The large file is splitted into many parts. It will put the many parts into bucket and then merge all the parts into one object.

type APIOptions

type APIOptions struct {
	// The OSS server host
	Host string
	Port int
	// access key you create on aliyun console website
	AccessID string
	// access secret you create
	SecretAccessKey string
	IsSecurity      bool
	StsToken        string
}

APIOptions the options of OSS API

func GetDefaultAPIOptioins

func GetDefaultAPIOptioins() *APIOptions

GetDefaultAPIOptioins get default api options for OSS API

type AccessControlPolicy

type AccessControlPolicy struct {
	XMLName           xml.Name `xml:"AccessControlPolicy"`
	Owner             Owner
	AccessControlList []string `xml:"AccessControlList>Grant"`
}

AccessControlPolicy defined access control policy

type BucketLoggingStatus

type BucketLoggingStatus struct {
	XMLName xml.Name `xml:"BucketLoggingStatus"`
	Bucket  string   `xml:"LoggingEnabled>TargetBucket"`
	// prefix path name to store the log files
	Prefix string `xml:"LoggingEnabled>TargetPrefix"`
}

BucketLoggingStatus defined bucket logging status

type BucketMeta

type BucketMeta struct {
	XMLName xml.Name `xml:"Bucket"`
	// bucket store data region, e.g.: oss-cn-hangzhou-a
	Location string
	// bucket name
	Name string
	//  bucket create GMT date, e.g.: 2015-02-19T08:39:44.000Z
	CreationDate time.Time
}

BucketMeta defined bucket meta

type CORSConfiguration

type CORSConfiguration struct {
	XMLName xml.Name `xml:"CORSConfiguration"`
	Rules   []CORSRule
}

CORSConfiguration defined cors configuration

type CORSRule

type CORSRule struct {
	XMLName       xml.Name `xml:"CORSRule"`
	AllowedOrigin []string
	AllowedMethod []string
	AllowedHeader []string
	ExposeHeader  []string
	MaxAgeSeconds int
}

CORSRule defined cors rule

type CompleteMultipartUpload

type CompleteMultipartUpload struct {
	XMLName xml.Name `xml:"CompleteMultipartUpload"`
	Parts   []Part
}

CompleteMultipartUpload defined complete multipart upload xml

type CompleteMultipartUploadResult

type CompleteMultipartUploadResult struct {
	XMLName  xml.Name `xml:"CompleteMultipartUploadResult"`
	Location string
	Bucket   string
	Key      string
	ETag     string
}

CompleteMultipartUploadResult defined complete multipart upload result xml

type Content

type Content struct {
	XMLName      xml.Name `xml:"Contents"`
	Key          string
	LastModified time.Time
	ETag         string
	Type         string
	Size         int
	StorageClass string
	Owner        Owner
}

Content defined get bucket content

type CopyObjectResult

type CopyObjectResult struct {
	XMLName      xml.Name `xml:"CopyObjectResult"`
	LastModified time.Time
	ETag         string
}

CopyObjectResult defined copy object result

type CopyPartResult

type CopyPartResult struct {
	XMLName      xml.Name `xml:"CopyPartResult"`
	LastModified string
	ETag         string
}

CopyPartResult defined copy part result

type CreateBucketConfiguration

type CreateBucketConfiguration struct {
	XMLName xml.Name `xml:"CreateBucketConfiguration"`
	// the bucket data region location, Current available: oss-cn-hangzhou, oss-cn-qingdao, oss-cn-beijing, oss-cn-hongkong and oss-cn-shenzhen If change exists bucket region, will throw BucketAlreadyExistsError. If region value invalid, will throw InvalidLocationConstraintError.
	LocationConstraint string
}

CreateBucketConfiguration defined create bucket configuration

type DeleteResult

type DeleteResult struct {
	XMLName xml.Name `xml:"DeleteResult"`
	Objects []string `xml:"Deleted>Key"`
}

DeleteResult defined delete result

type DeleteXML

type DeleteXML struct {
	XMLName xml.Name `xml:"Delete"`
	Quiet   bool
	Objects []ObjectKey `xml:"--"`
}

DeleteXML defined delete multiple objects xml

type Error

type Error struct {
	XMLName xml.Name `xml:"Error"`
	// error name
	Code string
	// error message
	Message string
	// uuid for this request,
	// if you meet some unhandled problem,
	// you can send this request id to OSS engineer to find out what's happend.
	RequestID string `xml:"RequestId"`
	// OSS cluster name for this request
	HostID string `xml:"HostId"`
	// error xml return by OSS server
	Raw []byte `xml:"-"`
}

Error Each error return by OSS server

func (*Error) Error

func (e *Error) Error() string

Error returns the underlying error's message.

type InitiateMultipartUploadResult

type InitiateMultipartUploadResult struct {
	XMLName  xml.Name `xml:"InitiateMultipartUploadResult"`
	Bucket   string
	Key      string
	UploadID string `xml:"UploadId"`
}

InitiateMultipartUploadResult defined initiate multipart upload result

type LifecycleConfiguration

type LifecycleConfiguration struct {
	XMLName xml.Name `xml:"LifecycleConfiguration"`
	Rule    LifecycleRule
}

LifecycleConfiguration defined lifecycle configuration

type LifecycleRule

type LifecycleRule struct {
	XMLName xml.Name `xml:"Rule"`
	// rule id, if not set, OSS will auto create it with random string.
	ID string
	// store prefix
	Prefix string
	// rule status, allow values: Enabled or Disabled
	Status string
	// expire days, date and days only set one.
	ExpirationDays int `xml:"Expiration>Days,omitempty"`
	// expire date, e.g.: 2022-10-11T00:00:00.000Z date and days only set one.
	ExpirationDate time.Time `xml:"Expiration>Date,omitempty"`
}

LifecycleRule defined lifecycle configuration rule

type ListAllMyBucketsResult

type ListAllMyBucketsResult struct {
	XMLName xml.Name `xml:"ListAllMyBucketsResult"`
	// search buckets using prefix key
	Prefix string
	// search start from marker, including marker key
	Marker string
	// max buckets, default is 100, limit to 1000
	MaxKeys string
	// truncate or not
	IsTruncated bool
	// next marker string
	NextMarker string
	// object owner, including id and displayName
	Owner   Owner
	Buckets []BucketMeta `xml:"Buckets>Bucket"`
}

ListAllMyBucketsResult defined list all my buckets result

type ListBucketResult

type ListBucketResult struct {
	XMLName        xml.Name `xml:"ListBucketResult"`
	Contents       []Content
	CommonPrefixes string
	Delimiter      string
	IsTruncated    bool
	Marker         string
	MaxKeys        string
	Name           string
	Owner          Owner
	Prefix         string
	EncodingType   string `xml:"encoding-type"`
}

ListBucketResult defined list bucket result

type ListMultipartUploadOptions

type ListMultipartUploadOptions struct {
	Params             map[string]string
	KeyMarker          string
	UploadIDMarker     string
	NextKeyMarker      string
	NextUploadIDMarker string
	Delimiter          string
	Prefix             string
	MaxUploads         string
	EncodingType       string
}

ListMultipartUploadOptions list multipart upload options

func GetDefaultListMultipartUploadOptions

func GetDefaultListMultipartUploadOptions() *ListMultipartUploadOptions

GetDefaultListMultipartUploadOptions get default list multipart upload options

type ListMultipartUploadsResult

type ListMultipartUploadsResult struct {
	XMLName            xml.Name `xml:"ListMultipartUploadsResult"`
	Bucket             string
	KeyMarker          string
	UploadIDMarker     string `xml:"UploadIdMarker"`
	NextKeyMarker      string
	NextUploadIDMarker string `xml:"NextUploadIdMarker"`
	Delimiter          string
	Prefix             string
	MaxUploads         string
	IsTruncated        bool
	Uploads            []Upload `xml:"Upload"`
}

ListMultipartUploadsResult defined list multipart uploads result

type ListPartsResult

type ListPartsResult struct {
	XMLName              xml.Name `xml:"ListPartsResult"`
	Bucket               string
	Key                  string
	UploadID             string `xml:"UploadId"`
	NextPartNumberMarker int
	MaxParts             int
	IsTruncated          bool
	Parts                []Part
}

ListPartsResult defined list parts result

type LocationConstraint

type LocationConstraint string

LocationConstraint the bucket data region location, Current available: oss-cn-hangzhou, oss-cn-qingdao, oss-cn-beijing, oss-cn-hongkong and oss-cn-shenzhen If change exists bucket region, will throw BucketAlreadyExistsError. If region value invalid, will throw InvalidLocationConstraintError.

type MultipartUpload

type MultipartUpload struct {
	Bucket    string
	Key       string
	UploadID  string
	Initiated time.Time
	// contains filtered or unexported fields
}

MultipartUpload defined multipart upload struct

func (*MultipartUpload) AbortUpload

func (multi *MultipartUpload) AbortUpload() error

AbortUpload cancel multiupload and delete all parts

func (*MultipartUpload) CompleteUpload

func (multi *MultipartUpload) CompleteUpload(parts []Part, result *CompleteMultipartUploadResult) error

CompleteUpload finish multiupload and merge all the parts as a object.

func (*MultipartUpload) CopyPart

func (multi *MultipartUpload) CopyPart(sourceBucket, sourceObject string, partNumber int,
	sourceRange string, headers map[string]string) (string, error)

CopyPart upload a part with data copy from srouce object in source bucket

func (*MultipartUpload) ListParts

func (multi *MultipartUpload) ListParts(maxParts, partNumberMarker int,
	result *ListPartsResult) error

ListParts list all upload parts of current upload_id

func (*MultipartUpload) UploadPart

func (multi *MultipartUpload) UploadPart(partNumber int, body io.Reader) (string, error)

UploadPart upload the content of io.Reader as one part.

type ObjectKey

type ObjectKey struct {
	XMLName xml.Name `xml:"Object"`
	Key     string
}

ObjectKey defined delete multiple objects object key

type Owner

type Owner struct {
	XMLName     xml.Name `xml:"Owner"`
	ID          string
	DisplayName string
}

Owner defined owner

type Part

type Part struct {
	XMLName      xml.Name `xml:"Part"`
	PartNumber   int
	ETag         string
	LastModified time.Time
	Size         int
}

Part defined part xml

type RefererConfiguration

type RefererConfiguration struct {
	XMLName           xml.Name `xml:"RefererConfiguration"`
	AllowEmptyReferer bool
	RefererList       []string `xml:"RefererList>Referer"`
}

RefererConfiguration defined referer configuration

type SignURLOptions

type SignURLOptions struct {
	// one of PUT, GET, DELETE, HEAD
	Method string
	// HTTP address of bucket or object, eg: http://HOST/bucket/object
	URL string
	// HTTP header
	Headers map[string]string
	// path of bucket or bbject, eg: /bucket/ or /bucket/object
	Resource string
	Timeout  time.Duration
	Params   map[string]string
	// only for SignURL
	Object string
	// only for SignURL
	Bucket string
}

SignURLOptions defined sign url options

func GetDefaultSignURLOptions

func GetDefaultSignURLOptions() *SignURLOptions

GetDefaultSignURLOptions get default sign url options

type Upload

type Upload struct {
	XMLName   xml.Name `xml:"Upload"`
	Key       string
	UploadID  string `xml:"UploadId"`
	Initiated time.Time
}

Upload defined upload xml

type WebsiteConfiguration

type WebsiteConfiguration struct {
	XMLName     xml.Name `xml:"WebsiteConfiguration"`
	IndexSuffix string   `xml:"IndexDocument>Suffix"`
	ErrorKey    string   `xml:"ErrorDocument>Key"`
}

WebsiteConfiguration defined website configuration

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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