12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825 |
- // client test
- // use gocheck, install gocheck to execute "go get gopkg.in/check.v1",
- // see https://labix.org/gocheck
- package oss
- import (
- "encoding/json"
- "io/ioutil"
- "log"
- "math/rand"
- "net"
- "net/http"
- "os"
- "reflect"
- "runtime"
- "strconv"
- "strings"
- "testing"
- "time"
- . "gopkg.in/check.v1"
- )
- // Test hooks up gocheck into the "go test" runner.
- func Test(t *testing.T) {
- TestingT(t)
- }
- type OssClientSuite struct{}
- var _ = Suite(&OssClientSuite{})
- var (
- // Endpoint/ID/Key
- endpoint = os.Getenv("OSS_TEST_ENDPOINT")
- accessID = os.Getenv("OSS_TEST_ACCESS_KEY_ID")
- accessKey = os.Getenv("OSS_TEST_ACCESS_KEY_SECRET")
- // Proxy
- proxyHost = os.Getenv("OSS_TEST_PROXY_HOST")
- proxyUser = os.Getenv("OSS_TEST_PROXY_USER")
- proxyPasswd = os.Getenv("OSS_TEST_PROXY_PASSWORD")
- // STS
- stsaccessID = os.Getenv("OSS_TEST_STS_ID")
- stsaccessKey = os.Getenv("OSS_TEST_STS_KEY")
- stsARN = os.Getenv("OSS_TEST_STS_ARN")
- // Credential
- credentialAccessID = os.Getenv("OSS_CREDENTIAL_KEY_ID")
- credentialAccessKey = os.Getenv("OSS_CREDENTIAL_KEY_SECRET")
- credentialUID = os.Getenv("OSS_CREDENTIAL_UID")
- )
- var (
- // prefix of bucket name for bucket ops test
- bucketNamePrefix = "go-sdk-test-bucket-"
- // bucket name for object ops test
- bucketName = bucketNamePrefix + RandLowStr(6)
- archiveBucketName = bucketNamePrefix + "arch-" + RandLowStr(6)
- // object name for object ops test
- objectNamePrefix = "go-sdk-test-object-"
- // sts region is one and only hangzhou
- stsRegion = "cn-hangzhou"
- // Credentials
- credentialBucketName = bucketNamePrefix + RandLowStr(6)
- )
- var (
- logPath = "go_sdk_test_" + time.Now().Format("20060102_150405") + ".log"
- testLogFile, _ = os.OpenFile(logPath, os.O_RDWR|os.O_CREATE, 0664)
- testLogger = log.New(testLogFile, "", log.Ldate|log.Ltime|log.Lshortfile)
- letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
- timeoutInOperation = 3 * time.Second
- )
- func RandStr(n int) string {
- b := make([]rune, n)
- randMarker := rand.New(rand.NewSource(time.Now().UnixNano()))
- for i := range b {
- b[i] = letters[randMarker.Intn(len(letters))]
- }
- return string(b)
- }
- func CreateFile(fileName, content string, c *C) {
- fout, err := os.Create(fileName)
- defer fout.Close()
- c.Assert(err, IsNil)
- _, err = fout.WriteString(content)
- c.Assert(err, IsNil)
- }
- func RandLowStr(n int) string {
- return strings.ToLower(RandStr(n))
- }
- func ForceDeleteBucket(client *Client, bucketName string, c *C) {
- bucket, err := client.Bucket(bucketName)
- c.Assert(err, IsNil)
- // Delete Object
- marker := Marker("")
- for {
- lor, err := bucket.ListObjects(marker)
- c.Assert(err, IsNil)
- for _, object := range lor.Objects {
- err = bucket.DeleteObject(object.Key)
- c.Assert(err, IsNil)
- }
- marker = Marker(lor.NextMarker)
- if !lor.IsTruncated {
- break
- }
- }
- // Delete Object Versions and DeleteMarks
- keyMarker := KeyMarker("")
- versionIdMarker := VersionIdMarker("")
- options := []Option{keyMarker, versionIdMarker}
- for {
- lor, err := bucket.ListObjectVersions(options...)
- if err != nil {
- break
- }
- for _, object := range lor.ObjectDeleteMarkers {
- err = bucket.DeleteObject(object.Key, VersionId(object.VersionId))
- c.Assert(err, IsNil)
- }
- for _, object := range lor.ObjectVersions {
- err = bucket.DeleteObject(object.Key, VersionId(object.VersionId))
- c.Assert(err, IsNil)
- }
- keyMarker = KeyMarker(lor.NextKeyMarker)
- versionIdMarker := VersionIdMarker(lor.NextVersionIdMarker)
- options = []Option{keyMarker, versionIdMarker}
- if !lor.IsTruncated {
- break
- }
- }
- // Delete Part
- keyMarker = KeyMarker("")
- uploadIDMarker := UploadIDMarker("")
- for {
- lmur, err := bucket.ListMultipartUploads(keyMarker, uploadIDMarker)
- c.Assert(err, IsNil)
- for _, upload := range lmur.Uploads {
- var imur = InitiateMultipartUploadResult{Bucket: bucketName,
- Key: upload.Key, UploadID: upload.UploadID}
- err = bucket.AbortMultipartUpload(imur)
- c.Assert(err, IsNil)
- }
- keyMarker = KeyMarker(lmur.NextKeyMarker)
- uploadIDMarker = UploadIDMarker(lmur.NextUploadIDMarker)
- if !lmur.IsTruncated {
- break
- }
- }
- // delete live channel
- strMarker := ""
- for {
- result, err := bucket.ListLiveChannel(Marker(strMarker))
- c.Assert(err, IsNil)
- for _, channel := range result.LiveChannel {
- err := bucket.DeleteLiveChannel(channel.Name)
- c.Assert(err, IsNil)
- }
- if result.IsTruncated {
- strMarker = result.NextMarker
- } else {
- break
- }
- }
- // Delete Bucket
- err = client.DeleteBucket(bucketName)
- c.Assert(err, IsNil)
- }
- // SetUpSuite runs once when the suite starts running
- func (s *OssClientSuite) SetUpSuite(c *C) {
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- lbr, err := client.ListBuckets(Prefix(bucketNamePrefix), MaxKeys(1000))
- c.Assert(err, IsNil)
- for _, bucket := range lbr.Buckets {
- ForceDeleteBucket(client, bucket.Name, c)
- }
- testLogger.Println("test client started")
- }
- // TearDownSuite runs before each test or benchmark starts running
- func (s *OssClientSuite) TearDownSuite(c *C) {
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- lbr, err := client.ListBuckets(Prefix(bucketNamePrefix), MaxKeys(1000))
- c.Assert(err, IsNil)
- for _, bucket := range lbr.Buckets {
- s.deleteBucket(client, bucket.Name, c)
- }
- testLogger.Println("test client completed")
- }
- func (s *OssClientSuite) deleteBucket(client *Client, bucketName string, c *C) {
- ForceDeleteBucket(client, bucketName, c)
- }
- // SetUpTest runs after each test or benchmark runs
- func (s *OssClientSuite) SetUpTest(c *C) {
- }
- // TearDownTest runs once after all tests or benchmarks have finished running
- func (s *OssClientSuite) TearDownTest(c *C) {
- }
- // TestCreateBucket
- func (s *OssClientSuite) TestCreateBucket(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- // Create
- client.DeleteBucket(bucketNameTest)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- //sleep 3 seconds after create bucket
- time.Sleep(timeoutInOperation)
- // verify bucket is exist
- found, err := client.IsBucketExist(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(found, Equals, true)
- res, err := client.GetBucketACL(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.ACL, Equals, string(ACLPrivate))
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- // CreateBucket creates with ACLPublicRead
- err = client.CreateBucket(bucketNameTest, ACL(ACLPublicRead))
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- res, err = client.GetBucketACL(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.ACL, Equals, string(ACLPublicRead))
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- // ACLPublicReadWrite
- err = client.CreateBucket(bucketNameTest, ACL(ACLPublicReadWrite))
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- res, err = client.GetBucketACL(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- // ACLPrivate
- err = client.CreateBucket(bucketNameTest, ACL(ACLPrivate))
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- res, err = client.GetBucketACL(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.ACL, Equals, string(ACLPrivate))
- // Delete
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- // Create bucket with configuration and test GetBucketInfo
- for _, storage := range []StorageClassType{StorageStandard, StorageIA, StorageArchive} {
- bucketNameTest := bucketNamePrefix + RandLowStr(6)
- err = client.CreateBucket(bucketNameTest, StorageClass(storage), ACL(ACLPublicRead))
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- res, err := client.GetBucketInfo(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.BucketInfo.Name, Equals, bucketNameTest)
- c.Assert(res.BucketInfo.StorageClass, Equals, string(storage))
- c.Assert(res.BucketInfo.ACL, Equals, string(ACLPublicRead))
- // Delete
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // Error put bucket with configuration
- err = client.CreateBucket("ERRORBUCKETNAME", StorageClass(StorageArchive))
- c.Assert(err, NotNil)
- // Create bucket with configuration and test ListBuckets
- for _, storage := range []StorageClassType{StorageStandard, StorageIA, StorageArchive} {
- bucketNameTest := bucketNamePrefix + RandLowStr(6)
- err = client.CreateBucket(bucketNameTest, StorageClass(storage))
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- res, err := client.GetBucketInfo(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.BucketInfo.Name, Equals, bucketNameTest)
- c.Assert(res.BucketInfo.StorageClass, Equals, string(storage))
- // Delete
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- }
- // TestCreateBucketNegative
- func (s *OssClientSuite) TestCreateBucketNegative(c *C) {
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- // Bucket name invalid
- err = client.CreateBucket("xx")
- c.Assert(err, NotNil)
- err = client.CreateBucket("XXXX")
- c.Assert(err, NotNil)
- testLogger.Println(err)
- err = client.CreateBucket("_bucket")
- c.Assert(err, NotNil)
- testLogger.Println(err)
- // ACL invalid
- err = client.CreateBucket(bucketNamePrefix+RandLowStr(6), ACL("InvaldAcl"))
- c.Assert(err, NotNil)
- testLogger.Println(err)
- }
- // TestDeleteBucket
- func (s *OssClientSuite) TestDeleteBucket(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- // Create
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- // Check
- found, err := client.IsBucketExist(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(found, Equals, true)
- // Delete
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- // Check
- found, err = client.IsBucketExist(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(found, Equals, false)
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, NotNil)
- }
- // TestDeleteBucketNegative
- func (s *OssClientSuite) TestDeleteBucketNegative(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- // Bucket name invalid
- err = client.DeleteBucket("xx")
- c.Assert(err, NotNil)
- err = client.DeleteBucket("XXXX")
- c.Assert(err, NotNil)
- err = client.DeleteBucket("_bucket")
- c.Assert(err, NotNil)
- // Delete no exist bucket
- err = client.DeleteBucket("notexist")
- c.Assert(err, NotNil)
- // No permission to delete, this ak/sk for js sdk
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- accessID := "<accessKeyId>"
- accessKey := "<accessKeySecret>"
- clientOtherUser, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- err = clientOtherUser.DeleteBucket(bucketNameTest)
- c.Assert(err, NotNil)
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestListBucket
- func (s *OssClientSuite) TestListBucket(c *C) {
- var prefix = bucketNamePrefix + RandLowStr(6)
- var bucketNameLbOne = prefix + "tlb1"
- var bucketNameLbTwo = prefix + "tlb2"
- var bucketNameLbThree = prefix + "tlb3"
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- // CreateBucket
- err = client.CreateBucket(bucketNameLbOne)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameLbTwo)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameLbThree)
- c.Assert(err, IsNil)
- // ListBuckets, specified prefix
- var respHeader http.Header
- lbr, err := client.ListBuckets(Prefix(prefix), MaxKeys(2), GetResponseHeader(&respHeader))
- c.Assert(GetRequestId(respHeader) != "", Equals, true)
- c.Assert(err, IsNil)
- c.Assert(len(lbr.Buckets), Equals, 2)
- // ListBuckets, specified max keys
- lbr, err = client.ListBuckets(MaxKeys(2))
- c.Assert(err, IsNil)
- c.Assert(len(lbr.Buckets), Equals, 2)
- // ListBuckets, specified max keys
- lbr, err = client.ListBuckets(Marker(bucketNameLbOne), MaxKeys(1))
- c.Assert(err, IsNil)
- c.Assert(len(lbr.Buckets), Equals, 1)
- // ListBuckets, specified max keys
- lbr, err = client.ListBuckets(Marker(bucketNameLbOne))
- c.Assert(err, IsNil)
- c.Assert(len(lbr.Buckets) >= 2, Equals, true)
- // DeleteBucket
- err = client.DeleteBucket(bucketNameLbOne)
- c.Assert(err, IsNil)
- err = client.DeleteBucket(bucketNameLbTwo)
- c.Assert(err, IsNil)
- err = client.DeleteBucket(bucketNameLbThree)
- c.Assert(err, IsNil)
- }
- // TestListBucket
- func (s *OssClientSuite) TestIsBucketExist(c *C) {
- var prefix = bucketNamePrefix + RandLowStr(6)
- var bucketNameLbOne = prefix + "tibe1"
- var bucketNameLbTwo = prefix + "tibe11"
- var bucketNameLbThree = prefix + "tibe111"
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- // CreateBucket
- err = client.CreateBucket(bucketNameLbOne)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameLbTwo)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameLbThree)
- c.Assert(err, IsNil)
- // Exist
- exist, err := client.IsBucketExist(bucketNameLbTwo)
- c.Assert(err, IsNil)
- c.Assert(exist, Equals, true)
- exist, err = client.IsBucketExist(bucketNameLbThree)
- c.Assert(err, IsNil)
- c.Assert(exist, Equals, true)
- exist, err = client.IsBucketExist(bucketNameLbOne)
- c.Assert(err, IsNil)
- c.Assert(exist, Equals, true)
- // Not exist
- exist, err = client.IsBucketExist(prefix + "tibe")
- c.Assert(err, IsNil)
- c.Assert(exist, Equals, false)
- exist, err = client.IsBucketExist(prefix + "tibe1111")
- c.Assert(err, IsNil)
- c.Assert(exist, Equals, false)
- // Negative
- exist, err = client.IsBucketExist("BucketNameInvalid")
- c.Assert(err, NotNil)
- // DeleteBucket
- err = client.DeleteBucket(bucketNameLbOne)
- c.Assert(err, IsNil)
- err = client.DeleteBucket(bucketNameLbTwo)
- c.Assert(err, IsNil)
- err = client.DeleteBucket(bucketNameLbThree)
- c.Assert(err, IsNil)
- }
- // TestSetBucketAcl
- func (s *OssClientSuite) TestSetBucketAcl(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- // Private
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- res, err := client.GetBucketACL(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.ACL, Equals, string(ACLPrivate))
- // Set ACL_PUBLIC_R
- err = client.SetBucketACL(bucketNameTest, ACLPublicRead)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- res, err = client.GetBucketACL(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.ACL, Equals, string(ACLPublicRead))
- // Set ACL_PUBLIC_RW
- err = client.SetBucketACL(bucketNameTest, ACLPublicReadWrite)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- res, err = client.GetBucketACL(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
- // Set ACL_PUBLIC_RW
- err = client.SetBucketACL(bucketNameTest, ACLPrivate)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- res, err = client.GetBucketACL(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.ACL, Equals, string(ACLPrivate))
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestSetBucketAclNegative
- func (s *OssClientSuite) TestBucketAclNegative(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- err = client.SetBucketACL(bucketNameTest, "InvalidACL")
- c.Assert(err, NotNil)
- testLogger.Println(err)
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestGetBucketAcl
- func (s *OssClientSuite) TestGetBucketAcl(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- // Private
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- res, err := client.GetBucketACL(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.ACL, Equals, string(ACLPrivate))
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- // PublicRead
- err = client.CreateBucket(bucketNameTest, ACL(ACLPublicRead))
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- res, err = client.GetBucketACL(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.ACL, Equals, string(ACLPublicRead))
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- // PublicReadWrite
- err = client.CreateBucket(bucketNameTest, ACL(ACLPublicReadWrite))
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- res, err = client.GetBucketACL(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestGetBucketAcl
- func (s *OssClientSuite) TestGetBucketLocation(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- // Private
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- loc, err := client.GetBucketLocation(bucketNameTest)
- c.Assert(strings.HasPrefix(loc, "oss-"), Equals, true)
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestGetBucketLocationNegative
- func (s *OssClientSuite) TestGetBucketLocationNegative(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- // Not exist
- _, err = client.GetBucketLocation(bucketNameTest)
- c.Assert(err, NotNil)
- // Not exist
- _, err = client.GetBucketLocation("InvalidBucketName_")
- c.Assert(err, NotNil)
- }
- // TestSetBucketLifecycle
- func (s *OssClientSuite) TestSetBucketLifecycle(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- var rule1 = BuildLifecycleRuleByDate("rule1", "one", true, 2015, 11, 11)
- var rule2 = BuildLifecycleRuleByDays("rule2", "two", true, 3)
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- // Set single rule
- var rules = []LifecycleRule{rule1}
- err = client.SetBucketLifecycle(bucketNameTest, rules)
- c.Assert(err, IsNil)
- // Double set rule
- err = client.SetBucketLifecycle(bucketNameTest, rules)
- c.Assert(err, IsNil)
- res, err := client.GetBucketLifecycle(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(len(res.Rules), Equals, 1)
- c.Assert(res.Rules[0].ID, Equals, "rule1")
- err = client.DeleteBucketLifecycle(bucketNameTest)
- c.Assert(err, IsNil)
- // Set two rules
- rules = []LifecycleRule{rule1, rule2}
- err = client.SetBucketLifecycle(bucketNameTest, rules)
- c.Assert(err, IsNil)
- // Eliminate effect of cache
- time.Sleep(timeoutInOperation)
- res, err = client.GetBucketLifecycle(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(len(res.Rules), Equals, 2)
- c.Assert(res.Rules[0].ID, Equals, "rule1")
- c.Assert(res.Rules[1].ID, Equals, "rule2")
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestSetBucketLifecycleNew
- func (s *OssClientSuite) TestSetBucketLifecycleNew(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- //invalid status of lifecyclerule
- expiration := LifecycleExpiration{
- Days: 30,
- }
- rule := LifecycleRule{
- ID: "rule1",
- Prefix: "one",
- Status: "Invalid",
- Expiration: &expiration,
- }
- rules := []LifecycleRule{rule}
- err = client.SetBucketLifecycle(bucketNameTest, rules)
- c.Assert(err, NotNil)
- //invalid value of CreatedBeforeDate
- expiration = LifecycleExpiration{
- CreatedBeforeDate: RandStr(10),
- }
- rule = LifecycleRule{
- ID: "rule1",
- Prefix: "one",
- Status: "Enabled",
- Expiration: &expiration,
- }
- rules = []LifecycleRule{rule}
- err = client.SetBucketLifecycle(bucketNameTest, rules)
- c.Assert(err, NotNil)
- //invalid value of Days
- abortMPU := LifecycleAbortMultipartUpload{
- Days: -30,
- }
- rule = LifecycleRule{
- ID: "rule1",
- Prefix: "one",
- Status: "Enabled",
- AbortMultipartUpload: &abortMPU,
- }
- rules = []LifecycleRule{rule}
- err = client.SetBucketLifecycle(bucketNameTest, rules)
- c.Assert(err, NotNil)
- expiration = LifecycleExpiration{
- CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
- }
- rule1 := LifecycleRule{
- ID: "rule1",
- Prefix: "one",
- Status: "Enabled",
- Expiration: &expiration,
- }
- abortMPU = LifecycleAbortMultipartUpload{
- Days: 30,
- }
- rule2 := LifecycleRule{
- ID: "rule2",
- Prefix: "two",
- Status: "Enabled",
- Expiration: &expiration,
- AbortMultipartUpload: &abortMPU,
- }
- transition1 := LifecycleTransition{
- Days: 3,
- StorageClass: StorageIA,
- }
- transition2 := LifecycleTransition{
- Days: 30,
- StorageClass: StorageArchive,
- }
- transitions := []LifecycleTransition{transition1, transition2}
- rule3 := LifecycleRule{
- ID: "rule3",
- Prefix: "three",
- Status: "Enabled",
- AbortMultipartUpload: &abortMPU,
- Transitions: transitions,
- }
- // Set single rule
- rules = []LifecycleRule{rule1}
- err = client.SetBucketLifecycle(bucketNameTest, rules)
- c.Assert(err, IsNil)
- res, err := client.GetBucketLifecycle(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(len(res.Rules), Equals, 1)
- c.Assert(res.Rules[0].ID, Equals, "rule1")
- c.Assert(res.Rules[0].Expiration, NotNil)
- c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
- err = client.DeleteBucketLifecycle(bucketNameTest)
- c.Assert(err, IsNil)
- // Set two rule: rule1 and rule2
- rules = []LifecycleRule{rule1, rule2}
- err = client.SetBucketLifecycle(bucketNameTest, rules)
- c.Assert(err, IsNil)
- res, err = client.GetBucketLifecycle(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(len(res.Rules), Equals, 2)
- c.Assert(res.Rules[0].ID, Equals, "rule1")
- c.Assert(res.Rules[0].Expiration, NotNil)
- c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
- c.Assert(res.Rules[1].ID, Equals, "rule2")
- c.Assert(res.Rules[1].Expiration, NotNil)
- c.Assert(res.Rules[1].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
- c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
- c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
- err = client.DeleteBucketLifecycle(bucketNameTest)
- c.Assert(err, IsNil)
- // Set two rule: rule2 and rule3
- rules = []LifecycleRule{rule2, rule3}
- err = client.SetBucketLifecycle(bucketNameTest, rules)
- c.Assert(err, IsNil)
- res, err = client.GetBucketLifecycle(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(len(res.Rules), Equals, 2)
- c.Assert(res.Rules[0].ID, Equals, "rule2")
- c.Assert(res.Rules[0].Expiration, NotNil)
- c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
- c.Assert(res.Rules[0].AbortMultipartUpload, NotNil)
- c.Assert(res.Rules[0].AbortMultipartUpload.Days, Equals, 30)
- c.Assert(res.Rules[1].ID, Equals, "rule3")
- c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
- c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
- c.Assert(len(res.Rules[1].Transitions), Equals, 2)
- c.Assert(res.Rules[1].Transitions[0].StorageClass, Equals, StorageIA)
- c.Assert(res.Rules[1].Transitions[0].Days, Equals, 3)
- c.Assert(res.Rules[1].Transitions[1].StorageClass, Equals, StorageArchive)
- c.Assert(res.Rules[1].Transitions[1].Days, Equals, 30)
- err = client.DeleteBucketLifecycle(bucketNameTest)
- c.Assert(err, IsNil)
- // Set two rule: rule1 and rule3
- rules = []LifecycleRule{rule1, rule3}
- err = client.SetBucketLifecycle(bucketNameTest, rules)
- c.Assert(err, IsNil)
- res, err = client.GetBucketLifecycle(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(len(res.Rules), Equals, 2)
- c.Assert(res.Rules[0].ID, Equals, "rule1")
- c.Assert(res.Rules[0].Expiration, NotNil)
- c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
- c.Assert(res.Rules[1].ID, Equals, "rule3")
- c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
- c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
- c.Assert(len(res.Rules[1].Transitions), Equals, 2)
- c.Assert(res.Rules[1].Transitions[0].StorageClass, Equals, StorageIA)
- c.Assert(res.Rules[1].Transitions[0].Days, Equals, 3)
- c.Assert(res.Rules[1].Transitions[1].StorageClass, Equals, StorageArchive)
- c.Assert(res.Rules[1].Transitions[1].Days, Equals, 30)
- err = client.DeleteBucketLifecycle(bucketNameTest)
- c.Assert(err, IsNil)
- // Set three rules
- rules = []LifecycleRule{rule1, rule2, rule3}
- err = client.SetBucketLifecycle(bucketNameTest, rules)
- c.Assert(err, IsNil)
- res, err = client.GetBucketLifecycle(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(len(res.Rules), Equals, 3)
- c.Assert(res.Rules[0].ID, Equals, "rule1")
- c.Assert(res.Rules[0].Expiration, NotNil)
- c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
- c.Assert(res.Rules[1].ID, Equals, "rule2")
- c.Assert(res.Rules[1].Expiration, NotNil)
- c.Assert(res.Rules[1].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
- c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
- c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
- c.Assert(res.Rules[2].ID, Equals, "rule3")
- c.Assert(res.Rules[2].AbortMultipartUpload, NotNil)
- c.Assert(res.Rules[2].AbortMultipartUpload.Days, Equals, 30)
- c.Assert(len(res.Rules[2].Transitions), Equals, 2)
- c.Assert(res.Rules[2].Transitions[0].StorageClass, Equals, StorageIA)
- c.Assert(res.Rules[2].Transitions[0].Days, Equals, 3)
- c.Assert(res.Rules[2].Transitions[1].StorageClass, Equals, StorageArchive)
- c.Assert(res.Rules[2].Transitions[1].Days, Equals, 30)
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestDeleteBucketLifecycle
- func (s *OssClientSuite) TestDeleteBucketLifecycle(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- var rule1 = BuildLifecycleRuleByDate("rule1", "one", true, 2015, 11, 11)
- var rule2 = BuildLifecycleRuleByDays("rule2", "two", true, 3)
- var rules = []LifecycleRule{rule1, rule2}
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- //time.Sleep(timeoutInOperation)
- err = client.DeleteBucketLifecycle(bucketNameTest)
- c.Assert(err, IsNil)
- err = client.SetBucketLifecycle(bucketNameTest, rules)
- c.Assert(err, IsNil)
- //time.Sleep(timeoutInOperation)
- res, err := client.GetBucketLifecycle(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(len(res.Rules), Equals, 2)
- // Delete
- err = client.DeleteBucketLifecycle(bucketNameTest)
- c.Assert(err, IsNil)
- //time.Sleep(timeoutInOperation)
- res, err = client.GetBucketLifecycle(bucketNameTest)
- c.Assert(err, NotNil)
- // Eliminate effect of cache
- //time.Sleep(timeoutInOperation)
- // Delete when not set
- err = client.DeleteBucketLifecycle(bucketNameTest)
- c.Assert(err, IsNil)
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestSetBucketLifecycleNegative
- func (s *OssClientSuite) TestBucketLifecycleNegative(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- var rules = []LifecycleRule{}
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- // Set with no rule
- err = client.SetBucketLifecycle(bucketNameTest, rules)
- c.Assert(err, NotNil)
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- // Not exist
- err = client.SetBucketLifecycle(bucketNameTest, rules)
- c.Assert(err, NotNil)
- // Not exist
- _, err = client.GetBucketLifecycle(bucketNameTest)
- c.Assert(err, NotNil)
- // Not exist
- err = client.DeleteBucketLifecycle(bucketNameTest)
- c.Assert(err, NotNil)
- }
- // TestSetBucketReferer
- func (s *OssClientSuite) TestSetBucketReferer(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- var referers = []string{"http://www.aliyun.com", "https://www.aliyun.com"}
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- res, err := client.GetBucketReferer(bucketNameTest)
- c.Assert(res.AllowEmptyReferer, Equals, true)
- c.Assert(len(res.RefererList), Equals, 0)
- // Set referers
- err = client.SetBucketReferer(bucketNameTest, referers, false)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- res, err = client.GetBucketReferer(bucketNameTest)
- c.Assert(res.AllowEmptyReferer, Equals, false)
- c.Assert(len(res.RefererList), Equals, 2)
- c.Assert(res.RefererList[0], Equals, "http://www.aliyun.com")
- c.Assert(res.RefererList[1], Equals, "https://www.aliyun.com")
- // Reset referer, referers empty
- referers = []string{""}
- err = client.SetBucketReferer(bucketNameTest, referers, true)
- c.Assert(err, IsNil)
- referers = []string{}
- err = client.SetBucketReferer(bucketNameTest, referers, true)
- c.Assert(err, IsNil)
- res, err = client.GetBucketReferer(bucketNameTest)
- c.Assert(res.AllowEmptyReferer, Equals, true)
- c.Assert(len(res.RefererList), Equals, 0)
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestSetBucketRefererNegative
- func (s *OssClientSuite) TestBucketRefererNegative(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- var referers = []string{""}
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- // Not exist
- _, err = client.GetBucketReferer(bucketNameTest)
- c.Assert(err, NotNil)
- testLogger.Println(err)
- // Not exist
- err = client.SetBucketReferer(bucketNameTest, referers, true)
- c.Assert(err, NotNil)
- testLogger.Println(err)
- }
- // TestSetBucketLogging
- func (s *OssClientSuite) TestSetBucketLogging(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- var bucketNameTarget = bucketNameTest + "-target"
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTarget)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- // Set logging
- err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
- c.Assert(err, IsNil)
- // Reset
- err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", false)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- res, err := client.GetBucketLogging(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
- c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
- err = client.DeleteBucketLogging(bucketNameTest)
- c.Assert(err, IsNil)
- // Set to self
- err = client.SetBucketLogging(bucketNameTest, bucketNameTest, "prefix", true)
- c.Assert(err, IsNil)
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- err = client.DeleteBucket(bucketNameTarget)
- c.Assert(err, IsNil)
- }
- // TestDeleteBucketLogging
- func (s *OssClientSuite) TestDeleteBucketLogging(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- var bucketNameTarget = bucketNameTest + "-target"
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTarget)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- // Get when not set
- res, err := client.GetBucketLogging(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
- c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
- // Set
- err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
- c.Assert(err, IsNil)
- // Get
- time.Sleep(timeoutInOperation)
- res, err = client.GetBucketLogging(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.LoggingEnabled.TargetBucket, Equals, bucketNameTarget)
- c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "prefix")
- // Set
- err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", false)
- c.Assert(err, IsNil)
- // Get
- time.Sleep(timeoutInOperation)
- res, err = client.GetBucketLogging(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
- c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
- // Delete
- err = client.DeleteBucketLogging(bucketNameTest)
- c.Assert(err, IsNil)
- // Get after delete
- time.Sleep(timeoutInOperation)
- res, err = client.GetBucketLogging(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
- c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- err = client.DeleteBucket(bucketNameTarget)
- c.Assert(err, IsNil)
- }
- // TestSetBucketLoggingNegative
- func (s *OssClientSuite) TestSetBucketLoggingNegative(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- var bucketNameTarget = bucketNameTest + "-target"
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- // Not exist
- _, err = client.GetBucketLogging(bucketNameTest)
- c.Assert(err, NotNil)
- // Not exist
- err = client.SetBucketLogging(bucketNameTest, "targetbucket", "prefix", true)
- c.Assert(err, NotNil)
- // Not exist
- err = client.DeleteBucketLogging(bucketNameTest)
- c.Assert(err, NotNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- // Target bucket not exist
- err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
- c.Assert(err, NotNil)
- // Parameter invalid
- err = client.SetBucketLogging(bucketNameTest, "XXXX", "prefix", true)
- c.Assert(err, NotNil)
- err = client.SetBucketLogging(bucketNameTest, "xx", "prefix", true)
- c.Assert(err, NotNil)
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestSetBucketWebsite
- func (s *OssClientSuite) TestSetBucketWebsite(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- var indexWebsite = "myindex.html"
- var errorWebsite = "myerror.html"
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- // Set
- err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
- c.Assert(err, IsNil)
- // Double set
- err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
- c.Assert(err, IsNil)
- res, err := client.GetBucketWebsite(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
- c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
- // Reset
- err = client.SetBucketWebsite(bucketNameTest, "your"+indexWebsite, "your"+errorWebsite)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- res, err = client.GetBucketWebsite(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.IndexDocument.Suffix, Equals, "your"+indexWebsite)
- c.Assert(res.ErrorDocument.Key, Equals, "your"+errorWebsite)
- err = client.DeleteBucketWebsite(bucketNameTest)
- c.Assert(err, IsNil)
- // Set after delete
- err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
- c.Assert(err, IsNil)
- // Eliminate effect of cache
- time.Sleep(timeoutInOperation)
- res, err = client.GetBucketWebsite(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
- c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestDeleteBucketWebsite
- func (s *OssClientSuite) TestDeleteBucketWebsite(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- var indexWebsite = "myindex.html"
- var errorWebsite = "myerror.html"
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- // Get
- res, err := client.GetBucketWebsite(bucketNameTest)
- c.Assert(err, NotNil)
- // Detele without set
- err = client.DeleteBucketWebsite(bucketNameTest)
- c.Assert(err, IsNil)
- // Set
- err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- res, err = client.GetBucketWebsite(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
- c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
- // Detele
- time.Sleep(timeoutInOperation)
- err = client.DeleteBucketWebsite(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- res, err = client.GetBucketWebsite(bucketNameTest)
- c.Assert(err, NotNil)
- // Detele after delete
- err = client.DeleteBucketWebsite(bucketNameTest)
- c.Assert(err, IsNil)
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestSetBucketWebsiteNegative
- func (s *OssClientSuite) TestSetBucketWebsiteNegative(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- var indexWebsite = "myindex.html"
- var errorWebsite = "myerror.html"
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- err = client.DeleteBucket(bucketNameTest)
- // Not exist
- _, err = client.GetBucketWebsite(bucketNameTest)
- c.Assert(err, NotNil)
- err = client.DeleteBucketWebsite(bucketNameTest)
- c.Assert(err, NotNil)
- err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
- c.Assert(err, NotNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- // Set
- time.Sleep(timeoutInOperation)
- err = client.SetBucketWebsite(bucketNameTest, "myindex", "myerror")
- c.Assert(err, IsNil)
- res, err := client.GetBucketWebsite(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.IndexDocument.Suffix, Equals, "myindex")
- c.Assert(res.ErrorDocument.Key, Equals, "myerror")
- // Detele
- err = client.DeleteBucketWebsite(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- _, err = client.GetBucketWebsite(bucketNameTest)
- c.Assert(err, NotNil)
- // Detele after delete
- err = client.DeleteBucketWebsite(bucketNameTest)
- c.Assert(err, IsNil)
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestSetBucketWebsiteDetail
- func (s *OssClientSuite) TestSetBucketWebsiteDetail(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- var indexWebsite = "myindex.html"
- var errorWebsite = "myerror.html"
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- btrue := true
- bfalse := false
- // Define one routing rule
- ruleOk := RoutingRule{
- RuleNumber: 1,
- Condition: Condition{
- KeyPrefixEquals: "",
- HTTPErrorCodeReturnedEquals: 404,
- },
- Redirect: Redirect{
- RedirectType: "Mirror",
- // PassQueryString: &btrue, // set default value
- MirrorURL: "http://www.test.com/",
- // MirrorPassQueryString:&btrue, // set default value
- // MirrorFollowRedirect:&bfalse, // set default value
- // MirrorCheckMd5:&bfalse, // set default value
- MirrorHeaders: MirrorHeaders{
- // PassAll:&bfalse, // set default value
- Pass: []string{"myheader-key1", "myheader-key2"},
- Remove: []string{"myheader-key3", "myheader-key4"},
- Set: []MirrorHeaderSet{
- MirrorHeaderSet{
- Key: "myheader-key5",
- Value: "myheader-value5",
- },
- },
- },
- },
- }
- // Define array routing rule
- ruleArrOk := []RoutingRule{
- RoutingRule{
- RuleNumber: 2,
- Condition: Condition{
- KeyPrefixEquals: "abc/",
- HTTPErrorCodeReturnedEquals: 404,
- IncludeHeader: []IncludeHeader{
- IncludeHeader{
- Key: "host",
- Equals: "test.oss-cn-beijing-internal.aliyuncs.com",
- },
- },
- },
- Redirect: Redirect{
- RedirectType: "AliCDN",
- Protocol: "http",
- HostName: "www.test.com",
- PassQueryString: &bfalse,
- ReplaceKeyWith: "prefix/${key}.suffix",
- HttpRedirectCode: 301,
- },
- },
- RoutingRule{
- RuleNumber: 3,
- Condition: Condition{
- KeyPrefixEquals: "",
- HTTPErrorCodeReturnedEquals: 404,
- },
- Redirect: Redirect{
- RedirectType: "Mirror",
- PassQueryString: &btrue,
- MirrorURL: "http://www.test.com/",
- MirrorPassQueryString: &btrue,
- MirrorFollowRedirect: &bfalse,
- MirrorCheckMd5: &bfalse,
- MirrorHeaders: MirrorHeaders{
- PassAll: &btrue,
- Pass: []string{"myheader-key1", "myheader-key2"},
- Remove: []string{"myheader-key3", "myheader-key4"},
- Set: []MirrorHeaderSet{
- MirrorHeaderSet{
- Key: "myheader-key5",
- Value: "myheader-value5",
- },
- },
- },
- },
- },
- }
- // Set one routing rule
- wxmlOne := WebsiteXML{}
- wxmlOne.RoutingRules = append(wxmlOne.RoutingRules, ruleOk)
- var responseHeader http.Header
- err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlOne, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- requestId := GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- res, err := client.GetBucketWebsite(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.RoutingRules[0].Redirect.RedirectType, Equals, "Mirror")
- c.Assert(*res.RoutingRules[0].Redirect.PassQueryString, Equals, false)
- c.Assert(*res.RoutingRules[0].Redirect.MirrorPassQueryString, Equals, false)
- c.Assert(*res.RoutingRules[0].Redirect.MirrorFollowRedirect, Equals, true)
- c.Assert(*res.RoutingRules[0].Redirect.MirrorCheckMd5, Equals, false)
- c.Assert(*res.RoutingRules[0].Redirect.MirrorHeaders.PassAll, Equals, false)
- // Set one routing rule and IndexDocument, IndexDocument
- wxml := WebsiteXML{
- IndexDocument: IndexDocument{Suffix: indexWebsite},
- ErrorDocument: ErrorDocument{Key: errorWebsite},
- }
- wxml.RoutingRules = append(wxml.RoutingRules, ruleOk)
- err = client.SetBucketWebsiteDetail(bucketNameTest, wxml, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- requestId = GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- res, err = client.GetBucketWebsite(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
- c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
- c.Assert(res.RoutingRules[0].Redirect.RedirectType, Equals, "Mirror")
- // Set array routing rule
- wxml.RoutingRules = append(wxml.RoutingRules, ruleArrOk...)
- err = client.SetBucketWebsiteDetail(bucketNameTest, wxml, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- requestId = GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- res, err = client.GetBucketWebsite(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
- c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
- c.Assert(len(res.RoutingRules), Equals, 3)
- c.Assert(res.RoutingRules[1].Redirect.RedirectType, Equals, "AliCDN")
- c.Assert(*res.RoutingRules[2].Redirect.MirrorPassQueryString, Equals, true)
- c.Assert(*res.RoutingRules[2].Redirect.MirrorFollowRedirect, Equals, false)
- // Define one error routing rule
- ruleErr := RoutingRule{
- RuleNumber: 1,
- Redirect: Redirect{
- RedirectType: "Mirror",
- PassQueryString: &btrue,
- },
- }
- // Define array error routing rule
- rulesErrArr := []RoutingRule{
- RoutingRule{
- RuleNumber: 1,
- Redirect: Redirect{
- RedirectType: "Mirror",
- PassQueryString: &btrue,
- },
- },
- RoutingRule{
- RuleNumber: 2,
- Redirect: Redirect{
- RedirectType: "Mirror",
- PassQueryString: &btrue,
- },
- },
- }
- ruleIntErr := RoutingRule{
- // RuleNumber:0, // set NULL value
- Condition: Condition{
- KeyPrefixEquals: "",
- HTTPErrorCodeReturnedEquals: 404,
- },
- Redirect: Redirect{
- RedirectType: "Mirror",
- // PassQueryString: &btrue, // set default value
- MirrorURL: "http://www.test.com/",
- // MirrorPassQueryString:&btrue, // set default value
- // MirrorFollowRedirect:&bfalse, // set default value
- // MirrorCheckMd5:&bfalse, // set default value
- MirrorHeaders: MirrorHeaders{
- // PassAll:&bfalse, // set default value
- Pass: []string{"myheader-key1", "myheader-key2"},
- Remove: []string{"myheader-key3", "myheader-key4"},
- Set: []MirrorHeaderSet{
- MirrorHeaderSet{
- Key: "myheader-key5",
- Value: "myheader-value5",
- },
- },
- },
- },
- }
- // Set one int type error rule
- wxmlIntErr := WebsiteXML{}
- wxmlIntErr.RoutingRules = append(wxmlIntErr.RoutingRules, ruleIntErr)
- err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlIntErr)
- c.Assert(err, NotNil)
- // Set one error rule
- wxmlErr := WebsiteXML{}
- wxmlErr.RoutingRules = append(wxmlErr.RoutingRules, ruleErr)
- err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlErr)
- c.Assert(err, NotNil)
- // Set one error rule and one correct rule
- wxmlErr.RoutingRules = append(wxmlErr.RoutingRules, ruleOk)
- err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlErr)
- c.Assert(err, NotNil)
- wxmlErrRuleArr := WebsiteXML{}
- wxmlErrRuleArr.RoutingRules = append(wxmlErrRuleArr.RoutingRules, rulesErrArr...)
- // Set array error routing rule
- err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlErrRuleArr)
- c.Assert(err, NotNil)
- err = client.DeleteBucketWebsite(bucketNameTest)
- c.Assert(err, IsNil)
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestSetBucketCORS
- func (s *OssClientSuite) TestSetBucketCORS(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- var rule1 = CORSRule{
- AllowedOrigin: []string{"*"},
- AllowedMethod: []string{"PUT", "GET", "POST"},
- AllowedHeader: []string{},
- ExposeHeader: []string{},
- MaxAgeSeconds: 100,
- }
- var rule2 = CORSRule{
- AllowedOrigin: []string{"http://www.a.com", "http://www.b.com"},
- AllowedMethod: []string{"GET"},
- AllowedHeader: []string{"Authorization"},
- ExposeHeader: []string{"x-oss-test", "x-oss-test1"},
- MaxAgeSeconds: 200,
- }
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- // Set
- err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1})
- c.Assert(err, IsNil)
- gbcr, err := client.GetBucketCORS(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(len(gbcr.CORSRules), Equals, 1)
- c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 1)
- c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 3)
- c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 0)
- c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 0)
- c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 100)
- // Double set
- err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1})
- c.Assert(err, IsNil)
- gbcr, err = client.GetBucketCORS(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(len(gbcr.CORSRules), Equals, 1)
- c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 1)
- c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 3)
- c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 0)
- c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 0)
- c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 100)
- // Set rule2
- err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule2})
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- gbcr, err = client.GetBucketCORS(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(len(gbcr.CORSRules), Equals, 1)
- c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 2)
- c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 1)
- c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 1)
- c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 2)
- c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 200)
- // Reset
- err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1, rule2})
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- gbcr, err = client.GetBucketCORS(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(len(gbcr.CORSRules), Equals, 2)
- // Set after delete
- err = client.DeleteBucketCORS(bucketNameTest)
- c.Assert(err, IsNil)
- err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1, rule2})
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- gbcr, err = client.GetBucketCORS(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(len(gbcr.CORSRules), Equals, 2)
- err = client.DeleteBucketCORS(bucketNameTest)
- c.Assert(err, IsNil)
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestDeleteBucketCORS
- func (s *OssClientSuite) TestDeleteBucketCORS(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- var rule = CORSRule{
- AllowedOrigin: []string{"*"},
- AllowedMethod: []string{"PUT", "GET", "POST"},
- AllowedHeader: []string{},
- ExposeHeader: []string{},
- MaxAgeSeconds: 100,
- }
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- // Delete not set
- err = client.DeleteBucketCORS(bucketNameTest)
- c.Assert(err, IsNil)
- // Set
- err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- _, err = client.GetBucketCORS(bucketNameTest)
- c.Assert(err, IsNil)
- // Detele
- err = client.DeleteBucketCORS(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- _, err = client.GetBucketCORS(bucketNameTest)
- c.Assert(err, NotNil)
- // Detele after deleting
- err = client.DeleteBucketCORS(bucketNameTest)
- c.Assert(err, IsNil)
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestSetBucketCORSNegative
- func (s *OssClientSuite) TestSetBucketCORSNegative(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- var rule = CORSRule{
- AllowedOrigin: []string{"*"},
- AllowedMethod: []string{"PUT", "GET", "POST"},
- AllowedHeader: []string{},
- ExposeHeader: []string{},
- MaxAgeSeconds: 100,
- }
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- err = client.DeleteBucket(bucketNameTest)
- // Not exist
- _, err = client.GetBucketCORS(bucketNameTest)
- c.Assert(err, NotNil)
- err = client.DeleteBucketCORS(bucketNameTest)
- c.Assert(err, NotNil)
- err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
- c.Assert(err, NotNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- _, err = client.GetBucketCORS(bucketNameTest)
- c.Assert(err, NotNil)
- // Set
- err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- _, err = client.GetBucketCORS(bucketNameTest)
- c.Assert(err, IsNil)
- // Delete
- err = client.DeleteBucketCORS(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- _, err = client.GetBucketCORS(bucketNameTest)
- c.Assert(err, NotNil)
- // Delete after deleting
- err = client.DeleteBucketCORS(bucketNameTest)
- c.Assert(err, IsNil)
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestGetBucketInfo
- func (s *OssClientSuite) TestGetBucketInfo(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- res, err := client.GetBucketInfo(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.BucketInfo.Name, Equals, bucketNameTest)
- c.Assert(strings.HasPrefix(res.BucketInfo.Location, "oss-"), Equals, true)
- c.Assert(res.BucketInfo.ACL, Equals, "private")
- c.Assert(strings.HasSuffix(res.BucketInfo.ExtranetEndpoint, ".com"), Equals, true)
- c.Assert(strings.HasSuffix(res.BucketInfo.IntranetEndpoint, ".com"), Equals, true)
- c.Assert(res.BucketInfo.CreationDate, NotNil)
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestGetBucketInfoNegative
- func (s *OssClientSuite) TestGetBucketInfoNegative(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- // Not exist
- _, err = client.GetBucketInfo(bucketNameTest)
- c.Assert(err, NotNil)
- // Bucket name invalid
- _, err = client.GetBucketInfo("InvalidBucketName_")
- c.Assert(err, NotNil)
- }
- // TestEndpointFormat
- func (s *OssClientSuite) TestEndpointFormat(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- // http://host
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- res, err := client.GetBucketACL(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.ACL, Equals, string(ACLPrivate))
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- // http://host:port
- client, err = New(endpoint+":80", accessID, accessKey)
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- time.Sleep(timeoutInOperation)
- res, err = client.GetBucketACL(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.ACL, Equals, string(ACLPrivate))
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestCname
- func (s *OssClientSuite) _TestCname(c *C) {
- var bucketNameTest = "<my-bucket-cname>"
- client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>", UseCname(true))
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- _, err = client.ListBuckets()
- c.Assert(err, NotNil)
- res, err := client.GetBucketACL(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.ACL, Equals, string(ACLPrivate))
- }
- // TestCnameNegative
- func (s *OssClientSuite) _TestCnameNegative(c *C) {
- var bucketNameTest = "<my-bucket-cname>"
- client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>", UseCname(true))
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, NotNil)
- _, err = client.ListBuckets()
- c.Assert(err, NotNil)
- _, err = client.GetBucketACL(bucketNameTest)
- c.Assert(err, NotNil)
- }
- // _TestHTTPS
- func (s *OssClientSuite) _TestHTTPS(c *C) {
- var bucketNameTest = "<my-bucket-https>"
- client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>")
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- res, err := client.GetBucketACL(bucketNameTest)
- c.Assert(err, IsNil)
- c.Assert(res.ACL, Equals, string(ACLPrivate))
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestClientOption
- func (s *OssClientSuite) TestClientOption(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- client, err := New(endpoint, accessID, accessKey, UseCname(true),
- Timeout(11, 12), SecurityToken("token"), Proxy(proxyHost))
- c.Assert(err, IsNil)
- // CreateBucket timeout
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, NotNil)
- c.Assert(client.Conn.config.HTTPTimeout.ConnectTimeout, Equals, time.Second*11)
- c.Assert(client.Conn.config.HTTPTimeout.ReadWriteTimeout, Equals, time.Second*12)
- c.Assert(client.Conn.config.HTTPTimeout.HeaderTimeout, Equals, time.Second*12)
- c.Assert(client.Conn.config.HTTPTimeout.IdleConnTimeout, Equals, time.Second*12)
- c.Assert(client.Conn.config.HTTPTimeout.LongTimeout, Equals, time.Second*12*10)
- c.Assert(client.Conn.config.SecurityToken, Equals, "token")
- c.Assert(client.Conn.config.IsCname, Equals, true)
- c.Assert(client.Conn.config.IsUseProxy, Equals, true)
- c.Assert(client.Config.ProxyHost, Equals, proxyHost)
- client, err = New(endpoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
- c.Assert(client.Conn.config.IsUseProxy, Equals, true)
- c.Assert(client.Config.ProxyHost, Equals, proxyHost)
- c.Assert(client.Conn.config.IsAuthProxy, Equals, true)
- c.Assert(client.Conn.config.ProxyUser, Equals, proxyUser)
- c.Assert(client.Conn.config.ProxyPassword, Equals, proxyPasswd)
- client, err = New(endpoint, accessID, accessKey, UserAgent("go sdk user agent"))
- c.Assert(client.Conn.config.UserAgent, Equals, "go sdk user agent")
- // Check we can overide the http.Client
- httpClient := new(http.Client)
- client, err = New(endpoint, accessID, accessKey, HTTPClient(httpClient))
- c.Assert(client.HTTPClient, Equals, httpClient)
- c.Assert(client.Conn.client, Equals, httpClient)
- client, err = New(endpoint, accessID, accessKey)
- c.Assert(client.HTTPClient, IsNil)
- }
- // TestProxy
- func (s *OssClientSuite) TestProxy(c *C) {
- bucketNameTest := bucketNamePrefix + RandLowStr(6)
- objectName := "体育/奥运/首金"
- objectValue := "大江东去,浪淘尽,千古风流人物。 故垒西边,人道是、三国周郎赤壁。"
- client, err := New(endpoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
- // Create bucket
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- // Get bucket info
- _, err = client.GetBucketInfo(bucketNameTest)
- c.Assert(err, IsNil)
- bucket, err := client.Bucket(bucketNameTest)
- // Sign URL
- str, err := bucket.SignURL(objectName, HTTPPut, 60)
- c.Assert(err, IsNil)
- c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
- c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
- c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
- // Put object with URL
- err = bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
- c.Assert(err, IsNil)
- // Sign URL for get object
- str, err = bucket.SignURL(objectName, HTTPGet, 60)
- c.Assert(err, IsNil)
- c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
- c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
- c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
- // Get object with URL
- body, err := bucket.GetObjectWithURL(str)
- c.Assert(err, IsNil)
- str, err = readBody(body)
- c.Assert(err, IsNil)
- c.Assert(str, Equals, objectValue)
- // Put object
- err = bucket.PutObject(objectName, strings.NewReader(objectValue))
- c.Assert(err, IsNil)
- // Get object
- _, err = bucket.GetObject(objectName)
- c.Assert(err, IsNil)
- // List objects
- _, err = bucket.ListObjects()
- c.Assert(err, IsNil)
- // Delete object
- err = bucket.DeleteObject(objectName)
- c.Assert(err, IsNil)
- // Delete bucket
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // TestProxy for https endpoint
- func (s *OssClientSuite) TestHttpsEndpointProxy(c *C) {
- bucketNameTest := bucketNamePrefix + RandLowStr(6)
- objectName := objectNamePrefix + RandLowStr(6)
- objectValue := RandLowStr(100)
- httpsEndPoint := ""
- if strings.HasPrefix(endpoint, "http://") {
- httpsEndPoint = strings.Replace(endpoint, "http://", "https://", 1)
- } else if !strings.HasPrefix(endpoint, "https://") {
- httpsEndPoint = "https://" + endpoint
- } else {
- httpsEndPoint = endpoint
- }
- client, err := New(httpsEndPoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
- // Create bucket
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- bucket, err := client.Bucket(bucketNameTest)
- // Put object
- err = bucket.PutObject(objectName, strings.NewReader(objectValue))
- c.Assert(err, IsNil)
- // Get object
- _, err = bucket.GetObject(objectName)
- c.Assert(err, IsNil)
- // List objects
- _, err = bucket.ListObjects()
- c.Assert(err, IsNil)
- // Delete object
- err = bucket.DeleteObject(objectName)
- c.Assert(err, IsNil)
- // Delete bucket
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- // Private
- func (s *OssClientSuite) checkBucket(buckets []BucketProperties, bucket string) bool {
- for _, v := range buckets {
- if v.Name == bucket {
- return true
- }
- }
- return false
- }
- func (s *OssClientSuite) getBucket(buckets []BucketProperties, bucket string) (bool, BucketProperties) {
- for _, v := range buckets {
- if v.Name == bucket {
- return true, v
- }
- }
- return false, BucketProperties{}
- }
- func (s *OssClientSuite) TestHttpLogNotSignUrl(c *C) {
- logName := "." + string(os.PathSeparator) + "test-go-sdk-httpdebug.log" + RandStr(5)
- f, err := os.OpenFile(logName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660)
- c.Assert(err, IsNil)
- client, err := New(endpoint, accessID, accessKey)
- client.Config.LogLevel = Debug
- client.Config.Logger = log.New(f, "", log.LstdFlags)
- var testBucketName = bucketNamePrefix + RandLowStr(6)
- // CreateBucket
- err = client.CreateBucket(testBucketName)
- f.Close()
- // read log file,get http info
- contents, err := ioutil.ReadFile(logName)
- c.Assert(err, IsNil)
- httpContent := string(contents)
- //fmt.Println(httpContent)
- c.Assert(strings.Contains(httpContent, "signStr"), Equals, true)
- c.Assert(strings.Contains(httpContent, "Method:"), Equals, true)
- // delete test bucket and log
- os.Remove(logName)
- client.DeleteBucket(testBucketName)
- }
- func (s *OssClientSuite) TestHttpLogSignUrl(c *C) {
- logName := "." + string(os.PathSeparator) + "test-go-sdk-httpdebug-signurl.log" + RandStr(5)
- f, err := os.OpenFile(logName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660)
- c.Assert(err, IsNil)
- client, err := New(endpoint, accessID, accessKey)
- client.Config.LogLevel = Debug
- client.Config.Logger = log.New(f, "", log.LstdFlags)
- var testBucketName = bucketNamePrefix + RandLowStr(6)
- // CreateBucket
- err = client.CreateBucket(testBucketName)
- f.Close()
- // clear log
- f, err = os.OpenFile(logName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660)
- client.Config.Logger = log.New(f, "", log.LstdFlags)
- bucket, _ := client.Bucket(testBucketName)
- objectName := objectNamePrefix + RandStr(8)
- objectValue := RandStr(20)
- // Sign URL for put
- str, err := bucket.SignURL(objectName, HTTPPut, 60)
- c.Assert(err, IsNil)
- c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
- c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
- c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
- // Error put object with URL
- err = bucket.PutObjectWithURL(str, strings.NewReader(objectValue), ContentType("image/tiff"))
- f.Close()
- // read log file,get http info
- contents, err := ioutil.ReadFile(logName)
- c.Assert(err, IsNil)
- httpContent := string(contents)
- //fmt.Println(httpContent)
- c.Assert(strings.Contains(httpContent, "signStr"), Equals, true)
- c.Assert(strings.Contains(httpContent, "Method:"), Equals, true)
- // delete test bucket and log
- os.Remove(logName)
- client.DeleteBucket(testBucketName)
- }
- func (s *OssClientSuite) TestSetLimitUploadSpeed(c *C) {
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- err = client.LimitUploadSpeed(100)
- goVersion := runtime.Version()
- pSlice := strings.Split(strings.ToLower(goVersion), ".")
- // compare with go1.7
- if len(pSlice) >= 2 {
- if pSlice[0] > "go1" {
- c.Assert(err, IsNil)
- } else if pSlice[0] == "go1" {
- subVersion, _ := strconv.Atoi(pSlice[1])
- if subVersion >= 7 {
- c.Assert(err, IsNil)
- } else {
- c.Assert(err, NotNil)
- }
- } else {
- c.Assert(err, NotNil)
- }
- }
- }
- func (s *OssClientSuite) TestBucketEncyptionError(c *C) {
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(5)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- // SetBucketEncryption:AES256 ,"123"
- encryptionRule := ServerEncryptionRule{}
- encryptionRule.SSEDefault.SSEAlgorithm = string(AESAlgorithm)
- encryptionRule.SSEDefault.KMSMasterKeyID = "123"
- var responseHeader http.Header
- err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
- c.Assert(err, NotNil)
- requestId := GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- // GetBucketEncryption
- _, err = client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
- c.Assert(err, NotNil)
- requestId = GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- // Get default bucket info
- bucketResult, err := client.GetBucketInfo(bucketName)
- c.Assert(err, IsNil)
- c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "")
- c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
- c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
- err = client.DeleteBucket(bucketName)
- c.Assert(err, IsNil)
- }
- func (s *OssClientSuite) TestBucketEncyptionPutAndGetAndDelete(c *C) {
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(5)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- // SetBucketEncryption:KMS ,""
- encryptionRule := ServerEncryptionRule{}
- encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
- var responseHeader http.Header
- err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- requestId := GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- // GetBucketEncryption
- getResult, err := client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- requestId = GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- // check encryption value
- c.Assert(encryptionRule.SSEDefault.SSEAlgorithm, Equals, getResult.SSEDefault.SSEAlgorithm)
- c.Assert(encryptionRule.SSEDefault.KMSMasterKeyID, Equals, getResult.SSEDefault.KMSMasterKeyID)
- // delete bucket encyption
- err = client.DeleteBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- requestId = GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- // GetBucketEncryption failure
- _, err = client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
- c.Assert(err, NotNil)
- requestId = GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- // Get default bucket info
- bucketResult, err := client.GetBucketInfo(bucketName)
- c.Assert(err, IsNil)
- c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "")
- c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
- c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
- err = client.DeleteBucket(bucketName)
- c.Assert(err, IsNil)
- }
- func (s *OssClientSuite) TestBucketEncyptionPutObjectSuccess(c *C) {
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(5)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- // SetBucketEncryption:KMS ,""
- encryptionRule := ServerEncryptionRule{}
- encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
- var responseHeader http.Header
- err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- requestId := GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- // GetBucketEncryption
- getResult, err := client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- requestId = GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- // check encryption value
- c.Assert(encryptionRule.SSEDefault.SSEAlgorithm, Equals, getResult.SSEDefault.SSEAlgorithm)
- c.Assert(encryptionRule.SSEDefault.KMSMasterKeyID, Equals, getResult.SSEDefault.KMSMasterKeyID)
- // Get default bucket info
- bucketResult, err := client.GetBucketInfo(bucketName)
- c.Assert(err, IsNil)
- c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "KMS")
- c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
- c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
- // put and get object success
- //bucket, err := client.Bucket(bucketName)
- //c.Assert(err, IsNil)
- // put object success
- //objectName := objectNamePrefix + RandStr(8)
- //context := RandStr(100)
- //err = bucket.PutObject(objectName, strings.NewReader(context))
- //c.Assert(err, IsNil)
- // get object success
- //body, err := bucket.GetObject(objectName)
- //c.Assert(err, IsNil)
- //str, err := readBody(body)
- //c.Assert(err, IsNil)
- //body.Close()
- //c.Assert(str, Equals, context)
- //bucket.DeleteObject(objectName)
- err = client.DeleteBucket(bucketName)
- c.Assert(err, IsNil)
- }
- func (s *OssClientSuite) TestBucketEncyptionPutObjectError(c *C) {
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(5)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- // SetBucketEncryption:KMS ,""
- encryptionRule := ServerEncryptionRule{}
- encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
- var responseHeader http.Header
- err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- requestId := GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- // GetBucketEncryption
- getResult, err := client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- requestId = GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- // check encryption value
- c.Assert(encryptionRule.SSEDefault.SSEAlgorithm, Equals, getResult.SSEDefault.SSEAlgorithm)
- c.Assert(encryptionRule.SSEDefault.KMSMasterKeyID, Equals, getResult.SSEDefault.KMSMasterKeyID)
- // Get default bucket info
- bucketResult, err := client.GetBucketInfo(bucketName)
- c.Assert(err, IsNil)
- c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "KMS")
- c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
- c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
- // put and get object failure
- bucket, err := client.Bucket(bucketName)
- c.Assert(err, IsNil)
- // put object failure
- objectName := objectNamePrefix + RandStr(8)
- context := RandStr(100)
- err = bucket.PutObject(objectName, strings.NewReader(context))
- c.Assert(err, NotNil)
- err = client.DeleteBucket(bucketName)
- c.Assert(err, IsNil)
- }
- func (s *OssClientSuite) TestBucketTaggingOperation(c *C) {
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(5)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- var respHeader http.Header
- // Bucket Tagging
- var tagging Tagging
- tagging.Tags = []Tag{Tag{Key: "testkey2", Value: "testvalue2"}}
- err = client.SetBucketTagging(bucketName, tagging, GetResponseHeader(&respHeader))
- c.Assert(err, IsNil)
- c.Assert(GetRequestId(respHeader) != "", Equals, true)
- getResult, err := client.GetBucketTagging(bucketName)
- c.Assert(err, IsNil)
- c.Assert(getResult.Tags[0].Key, Equals, tagging.Tags[0].Key)
- c.Assert(getResult.Tags[0].Value, Equals, tagging.Tags[0].Value)
- // delete BucketTagging
- err = client.DeleteBucketTagging(bucketName, GetResponseHeader(&respHeader))
- c.Assert(err, IsNil)
- c.Assert(GetRequestId(respHeader) != "", Equals, true)
- getResult, err = client.GetBucketTagging(bucketName, GetResponseHeader(&respHeader))
- c.Assert(err, IsNil)
- c.Assert(GetRequestId(respHeader) != "", Equals, true)
- c.Assert(len(getResult.Tags), Equals, 0)
- err = client.DeleteBucket(bucketName)
- c.Assert(err, IsNil)
- }
- func (s *OssClientSuite) TestListBucketsTagging(c *C) {
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName1 := bucketNamePrefix + RandLowStr(5)
- err = client.CreateBucket(bucketName1)
- c.Assert(err, IsNil)
- bucketName2 := bucketNamePrefix + RandLowStr(5)
- err = client.CreateBucket(bucketName2)
- c.Assert(err, IsNil)
- // Bucket Tagging
- var tagging Tagging
- tagging.Tags = []Tag{Tag{Key: "testkey", Value: "testvalue"}}
- err = client.SetBucketTagging(bucketName1, tagging)
- c.Assert(err, IsNil)
- // list bucket
- listResult, err := client.ListBuckets(TagKey("testkey"))
- c.Assert(err, IsNil)
- c.Assert(len(listResult.Buckets), Equals, 1)
- c.Assert(listResult.Buckets[0].Name, Equals, bucketName1)
- client.DeleteBucket(bucketName1)
- client.DeleteBucket(bucketName2)
- }
- func (s *OssClientSuite) TestGetBucketStat(c *C) {
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(5)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- bucket, err := client.Bucket(bucketName)
- c.Assert(err, IsNil)
- // put object
- objectName := objectNamePrefix + RandLowStr(5)
- err = bucket.PutObject(objectName, strings.NewReader(RandStr(10)))
- c.Assert(err, IsNil)
- bucket.DeleteObject(objectName)
- err = bucket.PutObject(objectName, strings.NewReader(RandStr(10)))
- c.Assert(err, IsNil)
- bucket.DeleteObject(objectName)
- _, err = client.GetBucketStat(bucketName)
- c.Assert(err, IsNil)
- client.DeleteBucket(bucketName)
- }
- func (s *OssBucketSuite) TestGetBucketVersioning(c *C) {
- // create a bucket with default proprety
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(6)
- var respHeader http.Header
- err = client.CreateBucket(bucketName, GetResponseHeader(&respHeader))
- c.Assert(err, IsNil)
- c.Assert(GetRequestId(respHeader) != "", Equals, true)
- // put bucket version:enabled
- var versioningConfig VersioningConfig
- versioningConfig.Status = string(VersionEnabled)
- err = client.SetBucketVersioning(bucketName, versioningConfig)
- c.Assert(err, IsNil)
- // get bucket version success
- versioningResult, err := client.GetBucketVersioning(bucketName, GetResponseHeader(&respHeader))
- c.Assert(versioningResult.Status, Equals, "Enabled")
- c.Assert(GetRequestId(respHeader) != "", Equals, true)
- ForceDeleteBucket(client, bucketName, c)
- }
- func (s *OssClientSuite) TestBucketPolicy(c *C) {
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(5)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- var responseHeader http.Header
- ret, err := client.GetBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
- c.Assert(err, NotNil)
- requestId := GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- policyInfo := `
- {
- "Version":"1",
- "Statement":[
- {
- "Action":[
- "oss:GetObject",
- "oss:PutObject"
- ],
- "Effect":"Deny",
- "Principal":"[123456790]",
- "Resource":["acs:oss:*:1234567890:*/*"]
- }
- ]
- }`
- err = client.SetBucketPolicy(bucketName, policyInfo, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- requestId = GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- ret, err = client.GetBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- testLogger.Println("policy:", ret)
- c.Assert(ret, Equals, policyInfo)
- requestId = GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- err = client.DeleteBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- requestId = GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- client.DeleteBucket(bucketName)
- }
- func (s *OssClientSuite) TestBucketPolicyNegative(c *C) {
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(5)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- var responseHeader http.Header
- _, err = client.GetBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
- c.Assert(err, NotNil)
- requestId := GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- // Setting the Version is 2, this is error policy
- errPolicy := `
- {
- "Version":"2",
- "Statement":[
- {
- "Action":[
- "oss:GetObject",
- "oss:PutObject"
- ],
- "Effect":"Deny",
- "Principal":"[123456790]",
- "Resource":["acs:oss:*:1234567890:*/*"]
- }
- ]
- }`
- err = client.SetBucketPolicy(bucketName, errPolicy, GetResponseHeader(&responseHeader))
- c.Assert(err, NotNil)
- testLogger.Println("err:", err)
- requestId = GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- err = client.DeleteBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- bucketNameEmpty := bucketNamePrefix + RandLowStr(5)
- client.DeleteBucket(bucketNameEmpty)
- err = client.DeleteBucketPolicy(bucketNameEmpty, GetResponseHeader(&responseHeader))
- c.Assert(err, NotNil)
- requestId = GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- client.DeleteBucket(bucketName)
- }
- func (s *OssClientSuite) TestSetBucketRequestPayment(c *C) {
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(5)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- reqPayConf := RequestPaymentConfiguration{
- Payer: "Requester",
- }
- err = client.SetBucketRequestPayment(bucketName, reqPayConf)
- c.Assert(err, IsNil)
- ret, err := client.GetBucketRequestPayment(bucketName)
- c.Assert(err, IsNil)
- c.Assert(ret.Payer, Equals, "Requester")
- client.DeleteBucket(bucketName)
- c.Assert(err, IsNil)
- }
- func (s *OssClientSuite) TestSetBucketRequestPaymentNegative(c *C) {
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(5)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- reqPayConf := RequestPaymentConfiguration{
- Payer: "Requesterttttt", // this is a error configuration
- }
- err = client.SetBucketRequestPayment(bucketName, reqPayConf)
- c.Assert(err, NotNil)
- ret, err := client.GetBucketRequestPayment(bucketName)
- c.Assert(err, IsNil)
- c.Assert(ret.Payer, Equals, "BucketOwner")
- client.DeleteBucket(bucketName)
- c.Assert(err, IsNil)
- }
- func (s *OssClientSuite) TestBucketQos(c *C) {
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- ret, err := client.GetUserQoSInfo()
- c.Assert(err, IsNil)
- testLogger.Println("QosInfo:", ret)
- bucketName := bucketNamePrefix + RandLowStr(5)
- _ = client.DeleteBucket(bucketName)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- _, err = client.GetBucketQosInfo(bucketName)
- c.Assert(err, NotNil)
- // case 1 set BucketQoSConfiguration every member
- five := 5
- four := 4
- totalQps := 200
- qosConf := BucketQoSConfiguration{
- TotalUploadBandwidth: &five,
- IntranetUploadBandwidth: &four,
- ExtranetUploadBandwidth: &four,
- TotalDownloadBandwidth: &four,
- IntranetDownloadBandwidth: &four,
- ExtranetDownloadBandwidth: &four,
- TotalQPS: &totalQps,
- IntranetQPS: &totalQps,
- ExtranetQPS: &totalQps,
- }
- var responseHeader http.Header
- err = client.SetBucketQoSInfo(bucketName, qosConf, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- requestId := GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- // wait a moment for configuration effect
- time.Sleep(time.Second)
- retQos, err := client.GetBucketQosInfo(bucketName)
- c.Assert(err, IsNil)
- // set qosConf default value
- qosConf.XMLName.Local = "QoSConfiguration"
- c.Assert(struct2string(retQos, c), Equals, struct2string(qosConf, c))
- // case 2 set BucketQoSConfiguration not every member
- qosConfNo := BucketQoSConfiguration{
- TotalUploadBandwidth: &five,
- IntranetUploadBandwidth: &four,
- ExtranetUploadBandwidth: &four,
- TotalDownloadBandwidth: &four,
- IntranetDownloadBandwidth: &four,
- ExtranetDownloadBandwidth: &four,
- TotalQPS: &totalQps,
- }
- err = client.SetBucketQoSInfo(bucketName, qosConfNo)
- c.Assert(err, IsNil)
- // wait a moment for configuration effect
- time.Sleep(time.Second)
- retQos, err = client.GetBucketQosInfo(bucketName)
- c.Assert(err, IsNil)
- // set qosConfNo default value
- qosConfNo.XMLName.Local = "QoSConfiguration"
- defNum := -1
- qosConfNo.IntranetQPS = &defNum
- qosConfNo.ExtranetQPS = &defNum
- c.Assert(struct2string(retQos, c), Equals, struct2string(qosConfNo, c))
- err = client.DeleteBucketQosInfo(bucketName)
- c.Assert(err, IsNil)
- // wait a moment for configuration effect
- time.Sleep(time.Second)
- _, err = client.GetBucketQosInfo(bucketName)
- c.Assert(err, NotNil)
- // this is a error qos configuration
- to := *ret.TotalUploadBandwidth + 2
- qosErrConf := BucketQoSConfiguration{
- TotalUploadBandwidth: &to, // this exceed user TotalUploadBandwidth
- IntranetUploadBandwidth: &four,
- ExtranetUploadBandwidth: &four,
- TotalDownloadBandwidth: &four,
- IntranetDownloadBandwidth: &four,
- ExtranetDownloadBandwidth: &four,
- TotalQPS: &totalQps,
- IntranetQPS: &totalQps,
- ExtranetQPS: &totalQps,
- }
- err = client.SetBucketQoSInfo(bucketName, qosErrConf)
- c.Assert(err, NotNil)
- err = client.DeleteBucketQosInfo(bucketName)
- c.Assert(err, IsNil)
- err = client.DeleteBucket(bucketName)
- c.Assert(err, IsNil)
- }
- // struct to string
- func struct2string(obj interface{}, c *C) string {
- t := reflect.TypeOf(obj)
- v := reflect.ValueOf(obj)
- var data = make(map[string]interface{})
- for i := 0; i < t.NumField(); i++ {
- data[t.Field(i).Name] = v.Field(i).Interface()
- }
- str, err := json.Marshal(data)
- c.Assert(err, IsNil)
- return string(str)
- }
- type TestCredentials struct {
- }
- func (testCreInf *TestCredentials) GetAccessKeyID() string {
- return os.Getenv("OSS_TEST_ACCESS_KEY_ID")
- }
- func (testCreInf *TestCredentials) GetAccessKeySecret() string {
- return os.Getenv("OSS_TEST_ACCESS_KEY_SECRET")
- }
- func (testCreInf *TestCredentials) GetSecurityToken() string {
- return ""
- }
- type TestCredentialsProvider struct {
- }
- func (testInfBuild *TestCredentialsProvider) GetCredentials() Credentials {
- return &TestCredentials{}
- }
- func (s *OssClientSuite) TestClientCredentialInfBuild(c *C) {
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- var defaultBuild TestCredentialsProvider
- client, err := New(endpoint, "", "", SetCredentialsProvider(&defaultBuild))
- c.Assert(err, IsNil)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
- func (s *OssClientSuite) TestClientSetLocalIpError(c *C) {
- // create client and bucket
- ipAddr, err := net.ResolveIPAddr("ip", "127.0.0.1")
- c.Assert(err, IsNil)
- localTCPAddr := &(net.TCPAddr{IP: ipAddr.IP})
- client, err := New(endpoint, accessID, accessKey, SetLocalAddr(localTCPAddr))
- c.Assert(err, IsNil)
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, NotNil)
- }
- func (s *OssClientSuite) TestClientSetLocalIpSuccess(c *C) {
- //get local ip
- conn, err := net.Dial("udp", "8.8.8.8:80")
- c.Assert(err, IsNil)
- localAddr := conn.LocalAddr().(*net.UDPAddr)
- localIp := localAddr.IP.String()
- conn.Close()
- ipAddr, err := net.ResolveIPAddr("ip", localIp)
- c.Assert(err, IsNil)
- localTCPAddr := &(net.TCPAddr{IP: ipAddr.IP})
- client, err := New(endpoint, accessID, accessKey, SetLocalAddr(localTCPAddr))
- c.Assert(err, IsNil)
- var bucketNameTest = bucketNamePrefix + RandLowStr(6)
- err = client.CreateBucket(bucketNameTest)
- c.Assert(err, IsNil)
- err = client.DeleteBucket(bucketNameTest)
- c.Assert(err, IsNil)
- }
|