client_test.go 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431
  1. // client test
  2. // use gocheck, install gocheck to execute "go get gopkg.in/check.v1",
  3. // see https://labix.org/gocheck
  4. package oss
  5. import (
  6. "encoding/json"
  7. "encoding/xml"
  8. "io/ioutil"
  9. "log"
  10. "math/rand"
  11. "net"
  12. "net/http"
  13. "os"
  14. "reflect"
  15. "runtime"
  16. "strconv"
  17. "strings"
  18. "testing"
  19. "time"
  20. . "gopkg.in/check.v1"
  21. )
  22. // Test hooks up gocheck into the "go test" runner.
  23. func Test(t *testing.T) {
  24. TestingT(t)
  25. }
  26. type OssClientSuite struct{}
  27. var _ = Suite(&OssClientSuite{})
  28. var (
  29. // Endpoint/ID/Key
  30. endpoint = os.Getenv("OSS_TEST_ENDPOINT")
  31. accessID = os.Getenv("OSS_TEST_ACCESS_KEY_ID")
  32. accessKey = os.Getenv("OSS_TEST_ACCESS_KEY_SECRET")
  33. accountID = os.Getenv("OSS_TEST_ACCOUNT_ID")
  34. // Proxy
  35. proxyHost = os.Getenv("OSS_TEST_PROXY_HOST")
  36. proxyUser = os.Getenv("OSS_TEST_PROXY_USER")
  37. proxyPasswd = os.Getenv("OSS_TEST_PROXY_PASSWORD")
  38. // STS
  39. stsaccessID = os.Getenv("OSS_TEST_STS_ID")
  40. stsaccessKey = os.Getenv("OSS_TEST_STS_KEY")
  41. stsARN = os.Getenv("OSS_TEST_STS_ARN")
  42. // Credential
  43. credentialAccessID = os.Getenv("OSS_CREDENTIAL_KEY_ID")
  44. credentialAccessKey = os.Getenv("OSS_CREDENTIAL_KEY_SECRET")
  45. credentialUID = os.Getenv("OSS_CREDENTIAL_UID")
  46. )
  47. var (
  48. // prefix of bucket name for bucket ops test
  49. bucketNamePrefix = "go-sdk-test-bucket-"
  50. // bucket name for object ops test
  51. bucketName = bucketNamePrefix + RandLowStr(6)
  52. archiveBucketName = bucketNamePrefix + "arch-" + RandLowStr(6)
  53. // object name for object ops test
  54. objectNamePrefix = "go-sdk-test-object-"
  55. // sts region is one and only hangzhou
  56. stsRegion = "cn-hangzhou"
  57. // Credentials
  58. credentialBucketName = bucketNamePrefix + RandLowStr(6)
  59. )
  60. var (
  61. logPath = "go_sdk_test_" + time.Now().Format("20060102_150405") + ".log"
  62. testLogFile, _ = os.OpenFile(logPath, os.O_RDWR|os.O_CREATE, 0664)
  63. testLogger = log.New(testLogFile, "", log.Ldate|log.Ltime|log.Lshortfile)
  64. letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
  65. timeoutInOperation = 3 * time.Second
  66. )
  67. func RandStr(n int) string {
  68. b := make([]rune, n)
  69. randMarker := rand.New(rand.NewSource(time.Now().UnixNano()))
  70. for i := range b {
  71. b[i] = letters[randMarker.Intn(len(letters))]
  72. }
  73. return string(b)
  74. }
  75. func CreateFile(fileName, content string, c *C) {
  76. fout, err := os.Create(fileName)
  77. defer fout.Close()
  78. c.Assert(err, IsNil)
  79. _, err = fout.WriteString(content)
  80. c.Assert(err, IsNil)
  81. }
  82. func RandLowStr(n int) string {
  83. return strings.ToLower(RandStr(n))
  84. }
  85. func ForceDeleteBucket(client *Client, bucketName string, c *C) {
  86. bucket, err := client.Bucket(bucketName)
  87. c.Assert(err, IsNil)
  88. // Delete Object
  89. marker := Marker("")
  90. for {
  91. lor, err := bucket.ListObjects(marker)
  92. c.Assert(err, IsNil)
  93. for _, object := range lor.Objects {
  94. err = bucket.DeleteObject(object.Key)
  95. c.Assert(err, IsNil)
  96. }
  97. marker = Marker(lor.NextMarker)
  98. if !lor.IsTruncated {
  99. break
  100. }
  101. }
  102. // Delete Object Versions and DeleteMarks
  103. keyMarker := KeyMarker("")
  104. versionIdMarker := VersionIdMarker("")
  105. options := []Option{keyMarker, versionIdMarker}
  106. for {
  107. lor, err := bucket.ListObjectVersions(options...)
  108. if err != nil {
  109. break
  110. }
  111. for _, object := range lor.ObjectDeleteMarkers {
  112. err = bucket.DeleteObject(object.Key, VersionId(object.VersionId))
  113. c.Assert(err, IsNil)
  114. }
  115. for _, object := range lor.ObjectVersions {
  116. err = bucket.DeleteObject(object.Key, VersionId(object.VersionId))
  117. c.Assert(err, IsNil)
  118. }
  119. keyMarker = KeyMarker(lor.NextKeyMarker)
  120. versionIdMarker := VersionIdMarker(lor.NextVersionIdMarker)
  121. options = []Option{keyMarker, versionIdMarker}
  122. if !lor.IsTruncated {
  123. break
  124. }
  125. }
  126. // Delete Part
  127. keyMarker = KeyMarker("")
  128. uploadIDMarker := UploadIDMarker("")
  129. for {
  130. lmur, err := bucket.ListMultipartUploads(keyMarker, uploadIDMarker)
  131. c.Assert(err, IsNil)
  132. for _, upload := range lmur.Uploads {
  133. var imur = InitiateMultipartUploadResult{Bucket: bucketName,
  134. Key: upload.Key, UploadID: upload.UploadID}
  135. err = bucket.AbortMultipartUpload(imur)
  136. c.Assert(err, IsNil)
  137. }
  138. keyMarker = KeyMarker(lmur.NextKeyMarker)
  139. uploadIDMarker = UploadIDMarker(lmur.NextUploadIDMarker)
  140. if !lmur.IsTruncated {
  141. break
  142. }
  143. }
  144. // delete live channel
  145. strMarker := ""
  146. for {
  147. result, err := bucket.ListLiveChannel(Marker(strMarker))
  148. c.Assert(err, IsNil)
  149. for _, channel := range result.LiveChannel {
  150. err := bucket.DeleteLiveChannel(channel.Name)
  151. c.Assert(err, IsNil)
  152. }
  153. if result.IsTruncated {
  154. strMarker = result.NextMarker
  155. } else {
  156. break
  157. }
  158. }
  159. // Delete Bucket
  160. err = client.DeleteBucket(bucketName)
  161. c.Assert(err, IsNil)
  162. }
  163. // SetUpSuite runs once when the suite starts running
  164. func (s *OssClientSuite) SetUpSuite(c *C) {
  165. client, err := New(endpoint, accessID, accessKey)
  166. c.Assert(err, IsNil)
  167. lbr, err := client.ListBuckets(Prefix(bucketNamePrefix), MaxKeys(1000))
  168. c.Assert(err, IsNil)
  169. for _, bucket := range lbr.Buckets {
  170. ForceDeleteBucket(client, bucket.Name, c)
  171. }
  172. testLogger.Println("test client started")
  173. }
  174. // TearDownSuite runs before each test or benchmark starts running
  175. func (s *OssClientSuite) TearDownSuite(c *C) {
  176. client, err := New(endpoint, accessID, accessKey)
  177. c.Assert(err, IsNil)
  178. lbr, err := client.ListBuckets(Prefix(bucketNamePrefix), MaxKeys(1000))
  179. c.Assert(err, IsNil)
  180. for _, bucket := range lbr.Buckets {
  181. s.deleteBucket(client, bucket.Name, c)
  182. }
  183. testLogger.Println("test client completed")
  184. }
  185. func (s *OssClientSuite) deleteBucket(client *Client, bucketName string, c *C) {
  186. ForceDeleteBucket(client, bucketName, c)
  187. }
  188. // SetUpTest runs after each test or benchmark runs
  189. func (s *OssClientSuite) SetUpTest(c *C) {
  190. }
  191. // TearDownTest runs once after all tests or benchmarks have finished running
  192. func (s *OssClientSuite) TearDownTest(c *C) {
  193. }
  194. // TestCreateBucket
  195. func (s *OssClientSuite) TestCreateBucket(c *C) {
  196. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  197. client, err := New(endpoint, accessID, accessKey)
  198. c.Assert(err, IsNil)
  199. // Create
  200. client.DeleteBucket(bucketNameTest)
  201. err = client.CreateBucket(bucketNameTest)
  202. c.Assert(err, IsNil)
  203. //sleep 3 seconds after create bucket
  204. time.Sleep(timeoutInOperation)
  205. // verify bucket is exist
  206. found, err := client.IsBucketExist(bucketNameTest)
  207. c.Assert(err, IsNil)
  208. c.Assert(found, Equals, true)
  209. res, err := client.GetBucketACL(bucketNameTest)
  210. c.Assert(err, IsNil)
  211. c.Assert(res.ACL, Equals, string(ACLPrivate))
  212. err = client.DeleteBucket(bucketNameTest)
  213. c.Assert(err, IsNil)
  214. time.Sleep(timeoutInOperation)
  215. // CreateBucket creates with ACLPublicRead
  216. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicRead))
  217. c.Assert(err, IsNil)
  218. time.Sleep(timeoutInOperation)
  219. res, err = client.GetBucketACL(bucketNameTest)
  220. c.Assert(err, IsNil)
  221. c.Assert(res.ACL, Equals, string(ACLPublicRead))
  222. err = client.DeleteBucket(bucketNameTest)
  223. c.Assert(err, IsNil)
  224. time.Sleep(timeoutInOperation)
  225. // ACLPublicReadWrite
  226. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicReadWrite))
  227. c.Assert(err, IsNil)
  228. time.Sleep(timeoutInOperation)
  229. res, err = client.GetBucketACL(bucketNameTest)
  230. c.Assert(err, IsNil)
  231. c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
  232. err = client.DeleteBucket(bucketNameTest)
  233. c.Assert(err, IsNil)
  234. time.Sleep(timeoutInOperation)
  235. // ACLPrivate
  236. err = client.CreateBucket(bucketNameTest, ACL(ACLPrivate))
  237. c.Assert(err, IsNil)
  238. time.Sleep(timeoutInOperation)
  239. res, err = client.GetBucketACL(bucketNameTest)
  240. c.Assert(err, IsNil)
  241. c.Assert(res.ACL, Equals, string(ACLPrivate))
  242. // Delete
  243. err = client.DeleteBucket(bucketNameTest)
  244. c.Assert(err, IsNil)
  245. // Create bucket with configuration and test GetBucketInfo
  246. for _, storage := range []StorageClassType{StorageStandard, StorageIA, StorageArchive, StorageColdArchive} {
  247. bucketNameTest := bucketNamePrefix + RandLowStr(6)
  248. err = client.CreateBucket(bucketNameTest, StorageClass(storage), ACL(ACLPublicRead))
  249. c.Assert(err, IsNil)
  250. time.Sleep(timeoutInOperation)
  251. res, err := client.GetBucketInfo(bucketNameTest)
  252. c.Assert(err, IsNil)
  253. c.Assert(res.BucketInfo.Name, Equals, bucketNameTest)
  254. c.Assert(res.BucketInfo.StorageClass, Equals, string(storage))
  255. c.Assert(res.BucketInfo.ACL, Equals, string(ACLPublicRead))
  256. // Delete
  257. err = client.DeleteBucket(bucketNameTest)
  258. c.Assert(err, IsNil)
  259. }
  260. // Error put bucket with configuration
  261. err = client.CreateBucket("ERRORBUCKETNAME", StorageClass(StorageArchive))
  262. c.Assert(err, NotNil)
  263. // Create bucket with configuration and test ListBuckets
  264. for _, storage := range []StorageClassType{StorageStandard, StorageIA, StorageArchive, StorageColdArchive} {
  265. bucketNameTest := bucketNamePrefix + RandLowStr(6)
  266. err = client.CreateBucket(bucketNameTest, StorageClass(storage))
  267. c.Assert(err, IsNil)
  268. time.Sleep(timeoutInOperation)
  269. res, err := client.GetBucketInfo(bucketNameTest)
  270. c.Assert(err, IsNil)
  271. c.Assert(res.BucketInfo.Name, Equals, bucketNameTest)
  272. c.Assert(res.BucketInfo.StorageClass, Equals, string(storage))
  273. // Delete
  274. err = client.DeleteBucket(bucketNameTest)
  275. c.Assert(err, IsNil)
  276. }
  277. }
  278. func (s *OssClientSuite) TestCreateBucketRedundancyType(c *C) {
  279. bucketNameTest := bucketNamePrefix + RandLowStr(6)
  280. client, err := New(endpoint, accessID, accessKey)
  281. c.Assert(err, IsNil)
  282. // CreateBucket creates without property
  283. err = client.CreateBucket(bucketNameTest)
  284. c.Assert(err, IsNil)
  285. client.DeleteBucket(bucketNameTest)
  286. time.Sleep(timeoutInOperation)
  287. // CreateBucket creates with RedundancyZRS
  288. err = client.CreateBucket(bucketNameTest, RedundancyType(RedundancyZRS))
  289. c.Assert(err, IsNil)
  290. res, err := client.GetBucketInfo(bucketNameTest)
  291. c.Assert(err, IsNil)
  292. c.Assert(res.BucketInfo.RedundancyType, Equals, string(RedundancyZRS))
  293. client.DeleteBucket(bucketNameTest)
  294. time.Sleep(timeoutInOperation)
  295. // CreateBucket creates with RedundancyLRS
  296. err = client.CreateBucket(bucketNameTest, RedundancyType(RedundancyLRS))
  297. c.Assert(err, IsNil)
  298. res, err = client.GetBucketInfo(bucketNameTest)
  299. c.Assert(err, IsNil)
  300. c.Assert(res.BucketInfo.RedundancyType, Equals, string(RedundancyLRS))
  301. c.Assert(res.BucketInfo.StorageClass, Equals, string(StorageStandard))
  302. client.DeleteBucket(bucketNameTest)
  303. time.Sleep(timeoutInOperation)
  304. // CreateBucket creates with ACLPublicRead RedundancyZRS
  305. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicRead), RedundancyType(RedundancyZRS))
  306. c.Assert(err, IsNil)
  307. res, err = client.GetBucketInfo(bucketNameTest)
  308. c.Assert(err, IsNil)
  309. c.Assert(res.BucketInfo.RedundancyType, Equals, string(RedundancyZRS))
  310. c.Assert(res.BucketInfo.ACL, Equals, string(ACLPublicRead))
  311. client.DeleteBucket(bucketNameTest)
  312. }
  313. // TestCreateBucketNegative
  314. func (s *OssClientSuite) TestCreateBucketNegative(c *C) {
  315. client, err := New(endpoint, accessID, accessKey)
  316. c.Assert(err, IsNil)
  317. // Bucket name invalid
  318. err = client.CreateBucket("xx")
  319. c.Assert(err, NotNil)
  320. err = client.CreateBucket("XXXX")
  321. c.Assert(err, NotNil)
  322. testLogger.Println(err)
  323. err = client.CreateBucket("_bucket")
  324. c.Assert(err, NotNil)
  325. testLogger.Println(err)
  326. // ACL invalid
  327. err = client.CreateBucket(bucketNamePrefix+RandLowStr(6), ACL("InvaldAcl"))
  328. c.Assert(err, NotNil)
  329. testLogger.Println(err)
  330. }
  331. // TestDeleteBucket
  332. func (s *OssClientSuite) TestDeleteBucket(c *C) {
  333. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  334. client, err := New(endpoint, accessID, accessKey)
  335. c.Assert(err, IsNil)
  336. // Create
  337. err = client.CreateBucket(bucketNameTest)
  338. c.Assert(err, IsNil)
  339. time.Sleep(timeoutInOperation)
  340. // Check
  341. found, err := client.IsBucketExist(bucketNameTest)
  342. c.Assert(err, IsNil)
  343. c.Assert(found, Equals, true)
  344. // Delete
  345. err = client.DeleteBucket(bucketNameTest)
  346. c.Assert(err, IsNil)
  347. time.Sleep(timeoutInOperation)
  348. // Check
  349. found, err = client.IsBucketExist(bucketNameTest)
  350. c.Assert(err, IsNil)
  351. c.Assert(found, Equals, false)
  352. err = client.DeleteBucket(bucketNameTest)
  353. c.Assert(err, NotNil)
  354. }
  355. // TestDeleteBucketNegative
  356. func (s *OssClientSuite) TestDeleteBucketNegative(c *C) {
  357. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  358. client, err := New(endpoint, accessID, accessKey)
  359. c.Assert(err, IsNil)
  360. // Bucket name invalid
  361. err = client.DeleteBucket("xx")
  362. c.Assert(err, NotNil)
  363. err = client.DeleteBucket("XXXX")
  364. c.Assert(err, NotNil)
  365. err = client.DeleteBucket("_bucket")
  366. c.Assert(err, NotNil)
  367. // Delete no exist bucket
  368. err = client.DeleteBucket("notexist")
  369. c.Assert(err, NotNil)
  370. // No permission to delete, this ak/sk for js sdk
  371. err = client.CreateBucket(bucketNameTest)
  372. c.Assert(err, IsNil)
  373. accessID := "<accessKeyId>"
  374. accessKey := "<accessKeySecret>"
  375. clientOtherUser, err := New(endpoint, accessID, accessKey)
  376. c.Assert(err, IsNil)
  377. err = clientOtherUser.DeleteBucket(bucketNameTest)
  378. c.Assert(err, NotNil)
  379. err = client.DeleteBucket(bucketNameTest)
  380. c.Assert(err, IsNil)
  381. }
  382. // TestListBucket
  383. func (s *OssClientSuite) TestListBucket(c *C) {
  384. var prefix = bucketNamePrefix + RandLowStr(6)
  385. var bucketNameLbOne = prefix + "tlb1"
  386. var bucketNameLbTwo = prefix + "tlb2"
  387. var bucketNameLbThree = prefix + "tlb3"
  388. client, err := New(endpoint, accessID, accessKey)
  389. c.Assert(err, IsNil)
  390. // CreateBucket
  391. err = client.CreateBucket(bucketNameLbOne)
  392. c.Assert(err, IsNil)
  393. err = client.CreateBucket(bucketNameLbTwo)
  394. c.Assert(err, IsNil)
  395. err = client.CreateBucket(bucketNameLbThree)
  396. c.Assert(err, IsNil)
  397. // ListBuckets, specified prefix
  398. var respHeader http.Header
  399. lbr, err := client.ListBuckets(Prefix(prefix), MaxKeys(2), GetResponseHeader(&respHeader))
  400. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  401. c.Assert(err, IsNil)
  402. c.Assert(len(lbr.Buckets), Equals, 2)
  403. // ListBuckets, specified max keys
  404. lbr, err = client.ListBuckets(MaxKeys(2))
  405. c.Assert(err, IsNil)
  406. c.Assert(len(lbr.Buckets), Equals, 2)
  407. // ListBuckets, specified max keys
  408. lbr, err = client.ListBuckets(Marker(bucketNameLbOne), MaxKeys(1))
  409. c.Assert(err, IsNil)
  410. c.Assert(len(lbr.Buckets), Equals, 1)
  411. // ListBuckets, specified max keys
  412. lbr, err = client.ListBuckets(Marker(bucketNameLbOne))
  413. c.Assert(err, IsNil)
  414. c.Assert(len(lbr.Buckets) >= 2, Equals, true)
  415. // DeleteBucket
  416. err = client.DeleteBucket(bucketNameLbOne)
  417. c.Assert(err, IsNil)
  418. err = client.DeleteBucket(bucketNameLbTwo)
  419. c.Assert(err, IsNil)
  420. err = client.DeleteBucket(bucketNameLbThree)
  421. c.Assert(err, IsNil)
  422. }
  423. // TestListBucket
  424. func (s *OssClientSuite) TestIsBucketExist(c *C) {
  425. var prefix = bucketNamePrefix + RandLowStr(6)
  426. var bucketNameLbOne = prefix + "tibe1"
  427. var bucketNameLbTwo = prefix + "tibe11"
  428. var bucketNameLbThree = prefix + "tibe111"
  429. client, err := New(endpoint, accessID, accessKey)
  430. c.Assert(err, IsNil)
  431. // CreateBucket
  432. err = client.CreateBucket(bucketNameLbOne)
  433. c.Assert(err, IsNil)
  434. err = client.CreateBucket(bucketNameLbTwo)
  435. c.Assert(err, IsNil)
  436. err = client.CreateBucket(bucketNameLbThree)
  437. c.Assert(err, IsNil)
  438. // Exist
  439. exist, err := client.IsBucketExist(bucketNameLbTwo)
  440. c.Assert(err, IsNil)
  441. c.Assert(exist, Equals, true)
  442. exist, err = client.IsBucketExist(bucketNameLbThree)
  443. c.Assert(err, IsNil)
  444. c.Assert(exist, Equals, true)
  445. exist, err = client.IsBucketExist(bucketNameLbOne)
  446. c.Assert(err, IsNil)
  447. c.Assert(exist, Equals, true)
  448. // Not exist
  449. exist, err = client.IsBucketExist(prefix + "tibe")
  450. c.Assert(err, IsNil)
  451. c.Assert(exist, Equals, false)
  452. exist, err = client.IsBucketExist(prefix + "tibe1111")
  453. c.Assert(err, IsNil)
  454. c.Assert(exist, Equals, false)
  455. // Negative
  456. exist, err = client.IsBucketExist("BucketNameInvalid")
  457. c.Assert(err, NotNil)
  458. // DeleteBucket
  459. err = client.DeleteBucket(bucketNameLbOne)
  460. c.Assert(err, IsNil)
  461. err = client.DeleteBucket(bucketNameLbTwo)
  462. c.Assert(err, IsNil)
  463. err = client.DeleteBucket(bucketNameLbThree)
  464. c.Assert(err, IsNil)
  465. }
  466. // TestSetBucketAcl
  467. func (s *OssClientSuite) TestSetBucketAcl(c *C) {
  468. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  469. client, err := New(endpoint, accessID, accessKey)
  470. c.Assert(err, IsNil)
  471. // Private
  472. err = client.CreateBucket(bucketNameTest)
  473. c.Assert(err, IsNil)
  474. res, err := client.GetBucketACL(bucketNameTest)
  475. c.Assert(err, IsNil)
  476. c.Assert(res.ACL, Equals, string(ACLPrivate))
  477. // Set ACL_PUBLIC_R
  478. err = client.SetBucketACL(bucketNameTest, ACLPublicRead)
  479. c.Assert(err, IsNil)
  480. time.Sleep(timeoutInOperation)
  481. res, err = client.GetBucketACL(bucketNameTest)
  482. c.Assert(err, IsNil)
  483. c.Assert(res.ACL, Equals, string(ACLPublicRead))
  484. // Set ACL_PUBLIC_RW
  485. err = client.SetBucketACL(bucketNameTest, ACLPublicReadWrite)
  486. c.Assert(err, IsNil)
  487. time.Sleep(timeoutInOperation)
  488. res, err = client.GetBucketACL(bucketNameTest)
  489. c.Assert(err, IsNil)
  490. c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
  491. // Set ACL_PUBLIC_RW
  492. err = client.SetBucketACL(bucketNameTest, ACLPrivate)
  493. c.Assert(err, IsNil)
  494. time.Sleep(timeoutInOperation)
  495. res, err = client.GetBucketACL(bucketNameTest)
  496. c.Assert(err, IsNil)
  497. c.Assert(res.ACL, Equals, string(ACLPrivate))
  498. err = client.DeleteBucket(bucketNameTest)
  499. c.Assert(err, IsNil)
  500. }
  501. // TestSetBucketAclNegative
  502. func (s *OssClientSuite) TestBucketAclNegative(c *C) {
  503. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  504. client, err := New(endpoint, accessID, accessKey)
  505. c.Assert(err, IsNil)
  506. err = client.CreateBucket(bucketNameTest)
  507. c.Assert(err, IsNil)
  508. err = client.SetBucketACL(bucketNameTest, "InvalidACL")
  509. c.Assert(err, NotNil)
  510. testLogger.Println(err)
  511. err = client.DeleteBucket(bucketNameTest)
  512. c.Assert(err, IsNil)
  513. }
  514. // TestGetBucketAcl
  515. func (s *OssClientSuite) TestGetBucketAcl(c *C) {
  516. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  517. client, err := New(endpoint, accessID, accessKey)
  518. c.Assert(err, IsNil)
  519. // Private
  520. err = client.CreateBucket(bucketNameTest)
  521. c.Assert(err, IsNil)
  522. time.Sleep(timeoutInOperation)
  523. res, err := client.GetBucketACL(bucketNameTest)
  524. c.Assert(err, IsNil)
  525. c.Assert(res.ACL, Equals, string(ACLPrivate))
  526. err = client.DeleteBucket(bucketNameTest)
  527. c.Assert(err, IsNil)
  528. time.Sleep(timeoutInOperation)
  529. // PublicRead
  530. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicRead))
  531. c.Assert(err, IsNil)
  532. time.Sleep(timeoutInOperation)
  533. res, err = client.GetBucketACL(bucketNameTest)
  534. c.Assert(err, IsNil)
  535. c.Assert(res.ACL, Equals, string(ACLPublicRead))
  536. err = client.DeleteBucket(bucketNameTest)
  537. c.Assert(err, IsNil)
  538. time.Sleep(timeoutInOperation)
  539. // PublicReadWrite
  540. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicReadWrite))
  541. c.Assert(err, IsNil)
  542. time.Sleep(timeoutInOperation)
  543. res, err = client.GetBucketACL(bucketNameTest)
  544. c.Assert(err, IsNil)
  545. c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
  546. err = client.DeleteBucket(bucketNameTest)
  547. c.Assert(err, IsNil)
  548. }
  549. // TestGetBucketAcl
  550. func (s *OssClientSuite) TestGetBucketLocation(c *C) {
  551. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  552. client, err := New(endpoint, accessID, accessKey)
  553. c.Assert(err, IsNil)
  554. // Private
  555. err = client.CreateBucket(bucketNameTest)
  556. c.Assert(err, IsNil)
  557. loc, err := client.GetBucketLocation(bucketNameTest)
  558. c.Assert(strings.HasPrefix(loc, "oss-"), Equals, true)
  559. err = client.DeleteBucket(bucketNameTest)
  560. c.Assert(err, IsNil)
  561. }
  562. // TestGetBucketLocationNegative
  563. func (s *OssClientSuite) TestGetBucketLocationNegative(c *C) {
  564. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  565. client, err := New(endpoint, accessID, accessKey)
  566. c.Assert(err, IsNil)
  567. // Not exist
  568. _, err = client.GetBucketLocation(bucketNameTest)
  569. c.Assert(err, NotNil)
  570. // Not exist
  571. _, err = client.GetBucketLocation("InvalidBucketName_")
  572. c.Assert(err, NotNil)
  573. }
  574. // TestSetBucketLifecycle
  575. func (s *OssClientSuite) TestSetBucketLifecycle(c *C) {
  576. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  577. var rule1 = BuildLifecycleRuleByDate("rule1", "one", true, 2015, 11, 11)
  578. var rule2 = BuildLifecycleRuleByDays("rule2", "two", true, 3)
  579. client, err := New(endpoint, accessID, accessKey)
  580. c.Assert(err, IsNil)
  581. err = client.CreateBucket(bucketNameTest)
  582. c.Assert(err, IsNil)
  583. // Set single rule
  584. var rules = []LifecycleRule{rule1}
  585. err = client.SetBucketLifecycle(bucketNameTest, rules)
  586. c.Assert(err, IsNil)
  587. // Double set rule
  588. err = client.SetBucketLifecycle(bucketNameTest, rules)
  589. c.Assert(err, IsNil)
  590. res, err := client.GetBucketLifecycle(bucketNameTest)
  591. c.Assert(err, IsNil)
  592. c.Assert(len(res.Rules), Equals, 1)
  593. c.Assert(res.Rules[0].ID, Equals, "rule1")
  594. err = client.DeleteBucketLifecycle(bucketNameTest)
  595. c.Assert(err, IsNil)
  596. // Set two rules
  597. rules = []LifecycleRule{rule1, rule2}
  598. err = client.SetBucketLifecycle(bucketNameTest, rules)
  599. c.Assert(err, IsNil)
  600. // Eliminate effect of cache
  601. time.Sleep(timeoutInOperation)
  602. res, err = client.GetBucketLifecycle(bucketNameTest)
  603. c.Assert(err, IsNil)
  604. c.Assert(len(res.Rules), Equals, 2)
  605. c.Assert(res.Rules[0].ID, Equals, "rule1")
  606. c.Assert(res.Rules[1].ID, Equals, "rule2")
  607. err = client.DeleteBucket(bucketNameTest)
  608. c.Assert(err, IsNil)
  609. }
  610. // TestSetBucketLifecycleNew
  611. func (s *OssClientSuite) TestSetBucketLifecycleNew(c *C) {
  612. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  613. client, err := New(endpoint, accessID, accessKey)
  614. c.Assert(err, IsNil)
  615. err = client.CreateBucket(bucketNameTest)
  616. c.Assert(err, IsNil)
  617. //invalid status of lifecyclerule
  618. expiration := LifecycleExpiration{
  619. Days: 30,
  620. }
  621. rule := LifecycleRule{
  622. ID: "rule1",
  623. Prefix: "one",
  624. Status: "Invalid",
  625. Expiration: &expiration,
  626. }
  627. rules := []LifecycleRule{rule}
  628. err = client.SetBucketLifecycle(bucketNameTest, rules)
  629. c.Assert(err, NotNil)
  630. //invalid value of CreatedBeforeDate
  631. expiration = LifecycleExpiration{
  632. CreatedBeforeDate: RandStr(10),
  633. }
  634. rule = LifecycleRule{
  635. ID: "rule1",
  636. Prefix: "one",
  637. Status: "Enabled",
  638. Expiration: &expiration,
  639. }
  640. rules = []LifecycleRule{rule}
  641. err = client.SetBucketLifecycle(bucketNameTest, rules)
  642. c.Assert(err, NotNil)
  643. //invalid value of Days
  644. abortMPU := LifecycleAbortMultipartUpload{
  645. Days: -30,
  646. }
  647. rule = LifecycleRule{
  648. ID: "rule1",
  649. Prefix: "one",
  650. Status: "Enabled",
  651. AbortMultipartUpload: &abortMPU,
  652. }
  653. rules = []LifecycleRule{rule}
  654. err = client.SetBucketLifecycle(bucketNameTest, rules)
  655. c.Assert(err, NotNil)
  656. expiration = LifecycleExpiration{
  657. CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
  658. }
  659. rule1 := LifecycleRule{
  660. ID: "rule1",
  661. Prefix: "one",
  662. Status: "Enabled",
  663. Expiration: &expiration,
  664. }
  665. abortMPU = LifecycleAbortMultipartUpload{
  666. Days: 30,
  667. }
  668. rule2 := LifecycleRule{
  669. ID: "rule2",
  670. Prefix: "two",
  671. Status: "Enabled",
  672. Expiration: &expiration,
  673. AbortMultipartUpload: &abortMPU,
  674. }
  675. transition1 := LifecycleTransition{
  676. Days: 3,
  677. StorageClass: StorageIA,
  678. }
  679. transition2 := LifecycleTransition{
  680. Days: 30,
  681. StorageClass: StorageArchive,
  682. }
  683. transitions := []LifecycleTransition{transition1, transition2}
  684. rule3 := LifecycleRule{
  685. ID: "rule3",
  686. Prefix: "three",
  687. Status: "Enabled",
  688. AbortMultipartUpload: &abortMPU,
  689. Transitions: transitions,
  690. }
  691. // Set single rule
  692. rules = []LifecycleRule{rule1}
  693. err = client.SetBucketLifecycle(bucketNameTest, rules)
  694. c.Assert(err, IsNil)
  695. res, err := client.GetBucketLifecycle(bucketNameTest)
  696. c.Assert(err, IsNil)
  697. c.Assert(len(res.Rules), Equals, 1)
  698. c.Assert(res.Rules[0].ID, Equals, "rule1")
  699. c.Assert(res.Rules[0].Expiration, NotNil)
  700. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  701. err = client.DeleteBucketLifecycle(bucketNameTest)
  702. c.Assert(err, IsNil)
  703. // Set two rule: rule1 and rule2
  704. rules = []LifecycleRule{rule1, rule2}
  705. err = client.SetBucketLifecycle(bucketNameTest, rules)
  706. c.Assert(err, IsNil)
  707. res, err = client.GetBucketLifecycle(bucketNameTest)
  708. c.Assert(err, IsNil)
  709. c.Assert(len(res.Rules), Equals, 2)
  710. c.Assert(res.Rules[0].ID, Equals, "rule1")
  711. c.Assert(res.Rules[0].Expiration, NotNil)
  712. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  713. c.Assert(res.Rules[1].ID, Equals, "rule2")
  714. c.Assert(res.Rules[1].Expiration, NotNil)
  715. c.Assert(res.Rules[1].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  716. c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
  717. c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
  718. err = client.DeleteBucketLifecycle(bucketNameTest)
  719. c.Assert(err, IsNil)
  720. // Set two rule: rule2 and rule3
  721. rules = []LifecycleRule{rule2, rule3}
  722. err = client.SetBucketLifecycle(bucketNameTest, rules)
  723. c.Assert(err, IsNil)
  724. res, err = client.GetBucketLifecycle(bucketNameTest)
  725. c.Assert(err, IsNil)
  726. c.Assert(len(res.Rules), Equals, 2)
  727. c.Assert(res.Rules[0].ID, Equals, "rule2")
  728. c.Assert(res.Rules[0].Expiration, NotNil)
  729. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  730. c.Assert(res.Rules[0].AbortMultipartUpload, NotNil)
  731. c.Assert(res.Rules[0].AbortMultipartUpload.Days, Equals, 30)
  732. c.Assert(res.Rules[1].ID, Equals, "rule3")
  733. c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
  734. c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
  735. c.Assert(len(res.Rules[1].Transitions), Equals, 2)
  736. c.Assert(res.Rules[1].Transitions[0].StorageClass, Equals, StorageIA)
  737. c.Assert(res.Rules[1].Transitions[0].Days, Equals, 3)
  738. c.Assert(res.Rules[1].Transitions[1].StorageClass, Equals, StorageArchive)
  739. c.Assert(res.Rules[1].Transitions[1].Days, Equals, 30)
  740. err = client.DeleteBucketLifecycle(bucketNameTest)
  741. c.Assert(err, IsNil)
  742. // Set two rule: rule1 and rule3
  743. rules = []LifecycleRule{rule1, rule3}
  744. err = client.SetBucketLifecycle(bucketNameTest, rules)
  745. c.Assert(err, IsNil)
  746. res, err = client.GetBucketLifecycle(bucketNameTest)
  747. c.Assert(err, IsNil)
  748. c.Assert(len(res.Rules), Equals, 2)
  749. c.Assert(res.Rules[0].ID, Equals, "rule1")
  750. c.Assert(res.Rules[0].Expiration, NotNil)
  751. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  752. c.Assert(res.Rules[1].ID, Equals, "rule3")
  753. c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
  754. c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
  755. c.Assert(len(res.Rules[1].Transitions), Equals, 2)
  756. c.Assert(res.Rules[1].Transitions[0].StorageClass, Equals, StorageIA)
  757. c.Assert(res.Rules[1].Transitions[0].Days, Equals, 3)
  758. c.Assert(res.Rules[1].Transitions[1].StorageClass, Equals, StorageArchive)
  759. c.Assert(res.Rules[1].Transitions[1].Days, Equals, 30)
  760. err = client.DeleteBucketLifecycle(bucketNameTest)
  761. c.Assert(err, IsNil)
  762. // Set three rules
  763. rules = []LifecycleRule{rule1, rule2, rule3}
  764. err = client.SetBucketLifecycle(bucketNameTest, rules)
  765. c.Assert(err, IsNil)
  766. res, err = client.GetBucketLifecycle(bucketNameTest)
  767. c.Assert(err, IsNil)
  768. c.Assert(len(res.Rules), Equals, 3)
  769. c.Assert(res.Rules[0].ID, Equals, "rule1")
  770. c.Assert(res.Rules[0].Expiration, NotNil)
  771. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  772. c.Assert(res.Rules[1].ID, Equals, "rule2")
  773. c.Assert(res.Rules[1].Expiration, NotNil)
  774. c.Assert(res.Rules[1].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  775. c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
  776. c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
  777. c.Assert(res.Rules[2].ID, Equals, "rule3")
  778. c.Assert(res.Rules[2].AbortMultipartUpload, NotNil)
  779. c.Assert(res.Rules[2].AbortMultipartUpload.Days, Equals, 30)
  780. c.Assert(len(res.Rules[2].Transitions), Equals, 2)
  781. c.Assert(res.Rules[2].Transitions[0].StorageClass, Equals, StorageIA)
  782. c.Assert(res.Rules[2].Transitions[0].Days, Equals, 3)
  783. c.Assert(res.Rules[2].Transitions[1].StorageClass, Equals, StorageArchive)
  784. c.Assert(res.Rules[2].Transitions[1].Days, Equals, 30)
  785. err = client.DeleteBucket(bucketNameTest)
  786. c.Assert(err, IsNil)
  787. }
  788. // TestSetBucketLifecycleAboutVersionObject
  789. func (s *OssClientSuite) TestSetBucketLifecycleAboutVersionObject(c *C) {
  790. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  791. client, err := New(endpoint, accessID, accessKey)
  792. c.Assert(err, IsNil)
  793. err = client.CreateBucket(bucketNameTest)
  794. c.Assert(err, IsNil)
  795. deleteMark := true
  796. expiration := LifecycleExpiration{
  797. ExpiredObjectDeleteMarker: &deleteMark,
  798. }
  799. versionExpiration := LifecycleVersionExpiration{
  800. NoncurrentDays: 20,
  801. }
  802. versionTransition := LifecycleVersionTransition{
  803. NoncurrentDays: 10,
  804. StorageClass: "IA",
  805. }
  806. rule := LifecycleRule{
  807. Status: "Enabled",
  808. Expiration: &expiration,
  809. NonVersionExpiration: &versionExpiration,
  810. NonVersionTransition: &versionTransition,
  811. }
  812. rules := []LifecycleRule{rule}
  813. err = client.SetBucketLifecycle(bucketNameTest, rules)
  814. c.Assert(err, IsNil)
  815. res, err := client.GetBucketLifecycle(bucketNameTest)
  816. c.Assert(err, IsNil)
  817. c.Assert(res.Rules[0].Expiration, NotNil)
  818. c.Assert(res.Rules[0].Expiration.Days, Equals, 0)
  819. c.Assert(res.Rules[0].Expiration.Date, Equals, "")
  820. c.Assert(*(res.Rules[0].Expiration.ExpiredObjectDeleteMarker), Equals, true)
  821. c.Assert(res.Rules[0].NonVersionExpiration.NoncurrentDays, Equals, 20)
  822. c.Assert(res.Rules[0].NonVersionTransition.NoncurrentDays, Equals, 10)
  823. c.Assert(res.Rules[0].NonVersionTransition.StorageClass, Equals, StorageClassType("IA"))
  824. err = client.DeleteBucket(bucketNameTest)
  825. c.Assert(err, IsNil)
  826. }
  827. // TestDeleteBucketLifecycle
  828. func (s *OssClientSuite) TestDeleteBucketLifecycle(c *C) {
  829. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  830. var rule1 = BuildLifecycleRuleByDate("rule1", "one", true, 2015, 11, 11)
  831. var rule2 = BuildLifecycleRuleByDays("rule2", "two", true, 3)
  832. var rules = []LifecycleRule{rule1, rule2}
  833. client, err := New(endpoint, accessID, accessKey)
  834. c.Assert(err, IsNil)
  835. err = client.CreateBucket(bucketNameTest)
  836. c.Assert(err, IsNil)
  837. //time.Sleep(timeoutInOperation)
  838. err = client.DeleteBucketLifecycle(bucketNameTest)
  839. c.Assert(err, IsNil)
  840. err = client.SetBucketLifecycle(bucketNameTest, rules)
  841. c.Assert(err, IsNil)
  842. //time.Sleep(timeoutInOperation)
  843. res, err := client.GetBucketLifecycle(bucketNameTest)
  844. c.Assert(err, IsNil)
  845. c.Assert(len(res.Rules), Equals, 2)
  846. // Delete
  847. err = client.DeleteBucketLifecycle(bucketNameTest)
  848. c.Assert(err, IsNil)
  849. //time.Sleep(timeoutInOperation)
  850. res, err = client.GetBucketLifecycle(bucketNameTest)
  851. c.Assert(err, NotNil)
  852. // Eliminate effect of cache
  853. //time.Sleep(timeoutInOperation)
  854. // Delete when not set
  855. err = client.DeleteBucketLifecycle(bucketNameTest)
  856. c.Assert(err, IsNil)
  857. err = client.DeleteBucket(bucketNameTest)
  858. c.Assert(err, IsNil)
  859. }
  860. // TestSetBucketLifecycleNegative
  861. func (s *OssClientSuite) TestBucketLifecycleNegative(c *C) {
  862. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  863. var rules = []LifecycleRule{}
  864. client, err := New(endpoint, accessID, accessKey)
  865. c.Assert(err, IsNil)
  866. err = client.CreateBucket(bucketNameTest)
  867. c.Assert(err, IsNil)
  868. // Set with no rule
  869. err = client.SetBucketLifecycle(bucketNameTest, rules)
  870. c.Assert(err, NotNil)
  871. err = client.DeleteBucket(bucketNameTest)
  872. c.Assert(err, IsNil)
  873. // Not exist
  874. err = client.SetBucketLifecycle(bucketNameTest, rules)
  875. c.Assert(err, NotNil)
  876. // Not exist
  877. _, err = client.GetBucketLifecycle(bucketNameTest)
  878. c.Assert(err, NotNil)
  879. // Not exist
  880. err = client.DeleteBucketLifecycle(bucketNameTest)
  881. c.Assert(err, NotNil)
  882. }
  883. // TestSetBucketReferer
  884. func (s *OssClientSuite) TestSetBucketReferer(c *C) {
  885. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  886. var referers = []string{"http://www.aliyun.com", "https://www.aliyun.com"}
  887. client, err := New(endpoint, accessID, accessKey)
  888. c.Assert(err, IsNil)
  889. err = client.CreateBucket(bucketNameTest)
  890. c.Assert(err, IsNil)
  891. time.Sleep(timeoutInOperation)
  892. res, err := client.GetBucketReferer(bucketNameTest)
  893. c.Assert(res.AllowEmptyReferer, Equals, true)
  894. c.Assert(len(res.RefererList), Equals, 0)
  895. // Set referers
  896. err = client.SetBucketReferer(bucketNameTest, referers, false)
  897. c.Assert(err, IsNil)
  898. time.Sleep(timeoutInOperation)
  899. res, err = client.GetBucketReferer(bucketNameTest)
  900. c.Assert(res.AllowEmptyReferer, Equals, false)
  901. c.Assert(len(res.RefererList), Equals, 2)
  902. c.Assert(res.RefererList[0], Equals, "http://www.aliyun.com")
  903. c.Assert(res.RefererList[1], Equals, "https://www.aliyun.com")
  904. // Reset referer, referers empty
  905. referers = []string{""}
  906. err = client.SetBucketReferer(bucketNameTest, referers, true)
  907. c.Assert(err, IsNil)
  908. referers = []string{}
  909. err = client.SetBucketReferer(bucketNameTest, referers, true)
  910. c.Assert(err, IsNil)
  911. res, err = client.GetBucketReferer(bucketNameTest)
  912. c.Assert(res.AllowEmptyReferer, Equals, true)
  913. c.Assert(len(res.RefererList), Equals, 0)
  914. err = client.DeleteBucket(bucketNameTest)
  915. c.Assert(err, IsNil)
  916. }
  917. // TestSetBucketRefererNegative
  918. func (s *OssClientSuite) TestBucketRefererNegative(c *C) {
  919. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  920. var referers = []string{""}
  921. client, err := New(endpoint, accessID, accessKey)
  922. c.Assert(err, IsNil)
  923. // Not exist
  924. _, err = client.GetBucketReferer(bucketNameTest)
  925. c.Assert(err, NotNil)
  926. testLogger.Println(err)
  927. // Not exist
  928. err = client.SetBucketReferer(bucketNameTest, referers, true)
  929. c.Assert(err, NotNil)
  930. testLogger.Println(err)
  931. }
  932. // TestSetBucketLogging
  933. func (s *OssClientSuite) TestSetBucketLogging(c *C) {
  934. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  935. var bucketNameTarget = bucketNameTest + "-target"
  936. client, err := New(endpoint, accessID, accessKey)
  937. c.Assert(err, IsNil)
  938. err = client.CreateBucket(bucketNameTest)
  939. c.Assert(err, IsNil)
  940. err = client.CreateBucket(bucketNameTarget)
  941. c.Assert(err, IsNil)
  942. time.Sleep(timeoutInOperation)
  943. // Set logging
  944. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
  945. c.Assert(err, IsNil)
  946. // Reset
  947. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", false)
  948. c.Assert(err, IsNil)
  949. time.Sleep(timeoutInOperation)
  950. res, err := client.GetBucketLogging(bucketNameTest)
  951. c.Assert(err, IsNil)
  952. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  953. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  954. err = client.DeleteBucketLogging(bucketNameTest)
  955. c.Assert(err, IsNil)
  956. // Set to self
  957. err = client.SetBucketLogging(bucketNameTest, bucketNameTest, "prefix", true)
  958. c.Assert(err, IsNil)
  959. err = client.DeleteBucket(bucketNameTest)
  960. c.Assert(err, IsNil)
  961. err = client.DeleteBucket(bucketNameTarget)
  962. c.Assert(err, IsNil)
  963. }
  964. // TestDeleteBucketLogging
  965. func (s *OssClientSuite) TestDeleteBucketLogging(c *C) {
  966. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  967. var bucketNameTarget = bucketNameTest + "-target"
  968. client, err := New(endpoint, accessID, accessKey)
  969. c.Assert(err, IsNil)
  970. err = client.CreateBucket(bucketNameTest)
  971. c.Assert(err, IsNil)
  972. err = client.CreateBucket(bucketNameTarget)
  973. c.Assert(err, IsNil)
  974. time.Sleep(timeoutInOperation)
  975. // Get when not set
  976. res, err := client.GetBucketLogging(bucketNameTest)
  977. c.Assert(err, IsNil)
  978. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  979. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  980. // Set
  981. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
  982. c.Assert(err, IsNil)
  983. // Get
  984. time.Sleep(timeoutInOperation)
  985. res, err = client.GetBucketLogging(bucketNameTest)
  986. c.Assert(err, IsNil)
  987. c.Assert(res.LoggingEnabled.TargetBucket, Equals, bucketNameTarget)
  988. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "prefix")
  989. // Set
  990. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", false)
  991. c.Assert(err, IsNil)
  992. // Get
  993. time.Sleep(timeoutInOperation)
  994. res, err = client.GetBucketLogging(bucketNameTest)
  995. c.Assert(err, IsNil)
  996. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  997. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  998. // Delete
  999. err = client.DeleteBucketLogging(bucketNameTest)
  1000. c.Assert(err, IsNil)
  1001. // Get after delete
  1002. time.Sleep(timeoutInOperation)
  1003. res, err = client.GetBucketLogging(bucketNameTest)
  1004. c.Assert(err, IsNil)
  1005. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  1006. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  1007. err = client.DeleteBucket(bucketNameTest)
  1008. c.Assert(err, IsNil)
  1009. err = client.DeleteBucket(bucketNameTarget)
  1010. c.Assert(err, IsNil)
  1011. }
  1012. // TestSetBucketLoggingNegative
  1013. func (s *OssClientSuite) TestSetBucketLoggingNegative(c *C) {
  1014. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1015. var bucketNameTarget = bucketNameTest + "-target"
  1016. client, err := New(endpoint, accessID, accessKey)
  1017. c.Assert(err, IsNil)
  1018. // Not exist
  1019. _, err = client.GetBucketLogging(bucketNameTest)
  1020. c.Assert(err, NotNil)
  1021. // Not exist
  1022. err = client.SetBucketLogging(bucketNameTest, "targetbucket", "prefix", true)
  1023. c.Assert(err, NotNil)
  1024. // Not exist
  1025. err = client.DeleteBucketLogging(bucketNameTest)
  1026. c.Assert(err, NotNil)
  1027. err = client.CreateBucket(bucketNameTest)
  1028. c.Assert(err, IsNil)
  1029. time.Sleep(timeoutInOperation)
  1030. // Target bucket not exist
  1031. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
  1032. c.Assert(err, NotNil)
  1033. // Parameter invalid
  1034. err = client.SetBucketLogging(bucketNameTest, "XXXX", "prefix", true)
  1035. c.Assert(err, NotNil)
  1036. err = client.SetBucketLogging(bucketNameTest, "xx", "prefix", true)
  1037. c.Assert(err, NotNil)
  1038. err = client.DeleteBucket(bucketNameTest)
  1039. c.Assert(err, IsNil)
  1040. }
  1041. // TestSetBucketWebsite
  1042. func (s *OssClientSuite) TestSetBucketWebsite(c *C) {
  1043. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1044. var indexWebsite = "myindex.html"
  1045. var errorWebsite = "myerror.html"
  1046. client, err := New(endpoint, accessID, accessKey)
  1047. c.Assert(err, IsNil)
  1048. time.Sleep(timeoutInOperation)
  1049. err = client.CreateBucket(bucketNameTest)
  1050. c.Assert(err, IsNil)
  1051. // Set
  1052. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  1053. c.Assert(err, IsNil)
  1054. // Double set
  1055. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  1056. c.Assert(err, IsNil)
  1057. res, err := client.GetBucketWebsite(bucketNameTest)
  1058. c.Assert(err, IsNil)
  1059. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  1060. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  1061. // Reset
  1062. err = client.SetBucketWebsite(bucketNameTest, "your"+indexWebsite, "your"+errorWebsite)
  1063. c.Assert(err, IsNil)
  1064. time.Sleep(timeoutInOperation)
  1065. res, err = client.GetBucketWebsite(bucketNameTest)
  1066. c.Assert(err, IsNil)
  1067. c.Assert(res.IndexDocument.Suffix, Equals, "your"+indexWebsite)
  1068. c.Assert(res.ErrorDocument.Key, Equals, "your"+errorWebsite)
  1069. err = client.DeleteBucketWebsite(bucketNameTest)
  1070. c.Assert(err, IsNil)
  1071. // Set after delete
  1072. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  1073. c.Assert(err, IsNil)
  1074. // Eliminate effect of cache
  1075. time.Sleep(timeoutInOperation)
  1076. res, err = client.GetBucketWebsite(bucketNameTest)
  1077. c.Assert(err, IsNil)
  1078. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  1079. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  1080. err = client.DeleteBucket(bucketNameTest)
  1081. c.Assert(err, IsNil)
  1082. }
  1083. // TestDeleteBucketWebsite
  1084. func (s *OssClientSuite) TestDeleteBucketWebsite(c *C) {
  1085. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1086. var indexWebsite = "myindex.html"
  1087. var errorWebsite = "myerror.html"
  1088. client, err := New(endpoint, accessID, accessKey)
  1089. c.Assert(err, IsNil)
  1090. err = client.CreateBucket(bucketNameTest)
  1091. c.Assert(err, IsNil)
  1092. time.Sleep(timeoutInOperation)
  1093. // Get
  1094. res, err := client.GetBucketWebsite(bucketNameTest)
  1095. c.Assert(err, NotNil)
  1096. // Detele without set
  1097. err = client.DeleteBucketWebsite(bucketNameTest)
  1098. c.Assert(err, IsNil)
  1099. // Set
  1100. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  1101. c.Assert(err, IsNil)
  1102. time.Sleep(timeoutInOperation)
  1103. res, err = client.GetBucketWebsite(bucketNameTest)
  1104. c.Assert(err, IsNil)
  1105. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  1106. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  1107. // Detele
  1108. time.Sleep(timeoutInOperation)
  1109. err = client.DeleteBucketWebsite(bucketNameTest)
  1110. c.Assert(err, IsNil)
  1111. time.Sleep(timeoutInOperation)
  1112. res, err = client.GetBucketWebsite(bucketNameTest)
  1113. c.Assert(err, NotNil)
  1114. // Detele after delete
  1115. err = client.DeleteBucketWebsite(bucketNameTest)
  1116. c.Assert(err, IsNil)
  1117. err = client.DeleteBucket(bucketNameTest)
  1118. c.Assert(err, IsNil)
  1119. }
  1120. // TestSetBucketWebsiteNegative
  1121. func (s *OssClientSuite) TestSetBucketWebsiteNegative(c *C) {
  1122. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1123. var indexWebsite = "myindex.html"
  1124. var errorWebsite = "myerror.html"
  1125. client, err := New(endpoint, accessID, accessKey)
  1126. c.Assert(err, IsNil)
  1127. err = client.DeleteBucket(bucketNameTest)
  1128. // Not exist
  1129. _, err = client.GetBucketWebsite(bucketNameTest)
  1130. c.Assert(err, NotNil)
  1131. err = client.DeleteBucketWebsite(bucketNameTest)
  1132. c.Assert(err, NotNil)
  1133. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  1134. c.Assert(err, NotNil)
  1135. err = client.CreateBucket(bucketNameTest)
  1136. c.Assert(err, IsNil)
  1137. // Set
  1138. time.Sleep(timeoutInOperation)
  1139. err = client.SetBucketWebsite(bucketNameTest, "myindex", "myerror")
  1140. c.Assert(err, IsNil)
  1141. res, err := client.GetBucketWebsite(bucketNameTest)
  1142. c.Assert(err, IsNil)
  1143. c.Assert(res.IndexDocument.Suffix, Equals, "myindex")
  1144. c.Assert(res.ErrorDocument.Key, Equals, "myerror")
  1145. // Detele
  1146. err = client.DeleteBucketWebsite(bucketNameTest)
  1147. c.Assert(err, IsNil)
  1148. time.Sleep(timeoutInOperation)
  1149. _, err = client.GetBucketWebsite(bucketNameTest)
  1150. c.Assert(err, NotNil)
  1151. // Detele after delete
  1152. err = client.DeleteBucketWebsite(bucketNameTest)
  1153. c.Assert(err, IsNil)
  1154. err = client.DeleteBucket(bucketNameTest)
  1155. c.Assert(err, IsNil)
  1156. }
  1157. // TestSetBucketWebsiteDetail
  1158. func (s *OssClientSuite) TestSetBucketWebsiteDetail(c *C) {
  1159. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1160. var indexWebsite = "myindex.html"
  1161. var errorWebsite = "myerror.html"
  1162. client, err := New(endpoint, accessID, accessKey)
  1163. c.Assert(err, IsNil)
  1164. err = client.CreateBucket(bucketNameTest)
  1165. c.Assert(err, IsNil)
  1166. time.Sleep(timeoutInOperation)
  1167. btrue := true
  1168. bfalse := false
  1169. // Define one routing rule
  1170. ruleOk := RoutingRule{
  1171. RuleNumber: 1,
  1172. Condition: Condition{
  1173. KeyPrefixEquals: "",
  1174. HTTPErrorCodeReturnedEquals: 404,
  1175. },
  1176. Redirect: Redirect{
  1177. RedirectType: "Mirror",
  1178. // PassQueryString: &btrue, // set default value
  1179. MirrorURL: "http://www.test.com/",
  1180. // MirrorPassQueryString:&btrue, // set default value
  1181. // MirrorFollowRedirect:&bfalse, // set default value
  1182. // MirrorCheckMd5:&bfalse, // set default value
  1183. MirrorHeaders: MirrorHeaders{
  1184. // PassAll:&bfalse, // set default value
  1185. Pass: []string{"myheader-key1", "myheader-key2"},
  1186. Remove: []string{"myheader-key3", "myheader-key4"},
  1187. Set: []MirrorHeaderSet{
  1188. MirrorHeaderSet{
  1189. Key: "myheader-key5",
  1190. Value: "myheader-value5",
  1191. },
  1192. },
  1193. },
  1194. },
  1195. }
  1196. // Define array routing rule
  1197. ruleArrOk := []RoutingRule{
  1198. RoutingRule{
  1199. RuleNumber: 2,
  1200. Condition: Condition{
  1201. KeyPrefixEquals: "abc/",
  1202. HTTPErrorCodeReturnedEquals: 404,
  1203. IncludeHeader: []IncludeHeader{
  1204. IncludeHeader{
  1205. Key: "host",
  1206. Equals: "test.oss-cn-beijing-internal.aliyuncs.com",
  1207. },
  1208. },
  1209. },
  1210. Redirect: Redirect{
  1211. RedirectType: "AliCDN",
  1212. Protocol: "http",
  1213. HostName: "www.test.com",
  1214. PassQueryString: &bfalse,
  1215. ReplaceKeyWith: "prefix/${key}.suffix",
  1216. HttpRedirectCode: 301,
  1217. },
  1218. },
  1219. RoutingRule{
  1220. RuleNumber: 3,
  1221. Condition: Condition{
  1222. KeyPrefixEquals: "",
  1223. HTTPErrorCodeReturnedEquals: 404,
  1224. },
  1225. Redirect: Redirect{
  1226. RedirectType: "Mirror",
  1227. PassQueryString: &btrue,
  1228. MirrorURL: "http://www.test.com/",
  1229. MirrorPassQueryString: &btrue,
  1230. MirrorFollowRedirect: &bfalse,
  1231. MirrorCheckMd5: &bfalse,
  1232. MirrorHeaders: MirrorHeaders{
  1233. PassAll: &btrue,
  1234. Pass: []string{"myheader-key1", "myheader-key2"},
  1235. Remove: []string{"myheader-key3", "myheader-key4"},
  1236. Set: []MirrorHeaderSet{
  1237. MirrorHeaderSet{
  1238. Key: "myheader-key5",
  1239. Value: "myheader-value5",
  1240. },
  1241. },
  1242. },
  1243. },
  1244. },
  1245. }
  1246. // Set one routing rule
  1247. wxmlOne := WebsiteXML{}
  1248. wxmlOne.RoutingRules = append(wxmlOne.RoutingRules, ruleOk)
  1249. var responseHeader http.Header
  1250. err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlOne, GetResponseHeader(&responseHeader))
  1251. c.Assert(err, IsNil)
  1252. requestId := GetRequestId(responseHeader)
  1253. c.Assert(len(requestId) > 0, Equals, true)
  1254. res, err := client.GetBucketWebsite(bucketNameTest)
  1255. c.Assert(err, IsNil)
  1256. c.Assert(res.RoutingRules[0].Redirect.RedirectType, Equals, "Mirror")
  1257. c.Assert(*res.RoutingRules[0].Redirect.PassQueryString, Equals, false)
  1258. c.Assert(*res.RoutingRules[0].Redirect.MirrorPassQueryString, Equals, false)
  1259. c.Assert(*res.RoutingRules[0].Redirect.MirrorFollowRedirect, Equals, true)
  1260. c.Assert(*res.RoutingRules[0].Redirect.MirrorCheckMd5, Equals, false)
  1261. c.Assert(*res.RoutingRules[0].Redirect.MirrorHeaders.PassAll, Equals, false)
  1262. // Set one routing rule and IndexDocument, IndexDocument
  1263. wxml := WebsiteXML{
  1264. IndexDocument: IndexDocument{Suffix: indexWebsite},
  1265. ErrorDocument: ErrorDocument{Key: errorWebsite},
  1266. }
  1267. wxml.RoutingRules = append(wxml.RoutingRules, ruleOk)
  1268. err = client.SetBucketWebsiteDetail(bucketNameTest, wxml, GetResponseHeader(&responseHeader))
  1269. c.Assert(err, IsNil)
  1270. requestId = GetRequestId(responseHeader)
  1271. c.Assert(len(requestId) > 0, Equals, true)
  1272. res, err = client.GetBucketWebsite(bucketNameTest)
  1273. c.Assert(err, IsNil)
  1274. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  1275. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  1276. c.Assert(res.RoutingRules[0].Redirect.RedirectType, Equals, "Mirror")
  1277. // Set array routing rule
  1278. wxml.RoutingRules = append(wxml.RoutingRules, ruleArrOk...)
  1279. err = client.SetBucketWebsiteDetail(bucketNameTest, wxml, GetResponseHeader(&responseHeader))
  1280. c.Assert(err, IsNil)
  1281. requestId = GetRequestId(responseHeader)
  1282. c.Assert(len(requestId) > 0, Equals, true)
  1283. res, err = client.GetBucketWebsite(bucketNameTest)
  1284. c.Assert(err, IsNil)
  1285. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  1286. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  1287. c.Assert(len(res.RoutingRules), Equals, 3)
  1288. c.Assert(res.RoutingRules[1].Redirect.RedirectType, Equals, "AliCDN")
  1289. c.Assert(*res.RoutingRules[2].Redirect.MirrorPassQueryString, Equals, true)
  1290. c.Assert(*res.RoutingRules[2].Redirect.MirrorFollowRedirect, Equals, false)
  1291. // Define one error routing rule
  1292. ruleErr := RoutingRule{
  1293. RuleNumber: 1,
  1294. Redirect: Redirect{
  1295. RedirectType: "Mirror",
  1296. PassQueryString: &btrue,
  1297. },
  1298. }
  1299. // Define array error routing rule
  1300. rulesErrArr := []RoutingRule{
  1301. RoutingRule{
  1302. RuleNumber: 1,
  1303. Redirect: Redirect{
  1304. RedirectType: "Mirror",
  1305. PassQueryString: &btrue,
  1306. },
  1307. },
  1308. RoutingRule{
  1309. RuleNumber: 2,
  1310. Redirect: Redirect{
  1311. RedirectType: "Mirror",
  1312. PassQueryString: &btrue,
  1313. },
  1314. },
  1315. }
  1316. ruleIntErr := RoutingRule{
  1317. // RuleNumber:0, // set NULL value
  1318. Condition: Condition{
  1319. KeyPrefixEquals: "",
  1320. HTTPErrorCodeReturnedEquals: 404,
  1321. },
  1322. Redirect: Redirect{
  1323. RedirectType: "Mirror",
  1324. // PassQueryString: &btrue, // set default value
  1325. MirrorURL: "http://www.test.com/",
  1326. // MirrorPassQueryString:&btrue, // set default value
  1327. // MirrorFollowRedirect:&bfalse, // set default value
  1328. // MirrorCheckMd5:&bfalse, // set default value
  1329. MirrorHeaders: MirrorHeaders{
  1330. // PassAll:&bfalse, // set default value
  1331. Pass: []string{"myheader-key1", "myheader-key2"},
  1332. Remove: []string{"myheader-key3", "myheader-key4"},
  1333. Set: []MirrorHeaderSet{
  1334. MirrorHeaderSet{
  1335. Key: "myheader-key5",
  1336. Value: "myheader-value5",
  1337. },
  1338. },
  1339. },
  1340. },
  1341. }
  1342. // Set one int type error rule
  1343. wxmlIntErr := WebsiteXML{}
  1344. wxmlIntErr.RoutingRules = append(wxmlIntErr.RoutingRules, ruleIntErr)
  1345. err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlIntErr)
  1346. c.Assert(err, NotNil)
  1347. // Set one error rule
  1348. wxmlErr := WebsiteXML{}
  1349. wxmlErr.RoutingRules = append(wxmlErr.RoutingRules, ruleErr)
  1350. err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlErr)
  1351. c.Assert(err, NotNil)
  1352. // Set one error rule and one correct rule
  1353. wxmlErr.RoutingRules = append(wxmlErr.RoutingRules, ruleOk)
  1354. err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlErr)
  1355. c.Assert(err, NotNil)
  1356. wxmlErrRuleArr := WebsiteXML{}
  1357. wxmlErrRuleArr.RoutingRules = append(wxmlErrRuleArr.RoutingRules, rulesErrArr...)
  1358. // Set array error routing rule
  1359. err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlErrRuleArr)
  1360. c.Assert(err, NotNil)
  1361. err = client.DeleteBucketWebsite(bucketNameTest)
  1362. c.Assert(err, IsNil)
  1363. err = client.DeleteBucket(bucketNameTest)
  1364. c.Assert(err, IsNil)
  1365. }
  1366. // TestSetBucketWebsiteXml
  1367. func (s *OssClientSuite) TestSetBucketWebsiteXml(c *C) {
  1368. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1369. client, err := New(endpoint, accessID, accessKey)
  1370. c.Assert(err, IsNil)
  1371. err = client.CreateBucket(bucketNameTest)
  1372. c.Assert(err, IsNil)
  1373. time.Sleep(timeoutInOperation)
  1374. // Define one routing rule
  1375. ruleOk := RoutingRule{
  1376. RuleNumber: 1,
  1377. Condition: Condition{
  1378. KeyPrefixEquals: "",
  1379. HTTPErrorCodeReturnedEquals: 404,
  1380. },
  1381. Redirect: Redirect{
  1382. RedirectType: "Mirror",
  1383. // PassQueryString: &btrue, // set default value
  1384. MirrorURL: "http://www.test.com/",
  1385. // MirrorPassQueryString:&btrue, // set default value
  1386. // MirrorFollowRedirect:&bfalse, // set default value
  1387. // MirrorCheckMd5:&bfalse, // set default value
  1388. MirrorHeaders: MirrorHeaders{
  1389. // PassAll:&bfalse, // set default value
  1390. Pass: []string{"myheader-key1", "myheader-key2"},
  1391. Remove: []string{"myheader-key3", "myheader-key4"},
  1392. Set: []MirrorHeaderSet{
  1393. MirrorHeaderSet{
  1394. Key: "myheader-key5",
  1395. Value: "myheader-value5",
  1396. },
  1397. },
  1398. },
  1399. },
  1400. }
  1401. // Set one routing rule
  1402. wxmlOne := WebsiteXML{}
  1403. wxmlOne.RoutingRules = append(wxmlOne.RoutingRules, ruleOk)
  1404. bs, err := xml.Marshal(wxmlOne)
  1405. c.Assert(err, IsNil)
  1406. var responseHeader http.Header
  1407. err = client.SetBucketWebsiteXml(bucketNameTest, string(bs), GetResponseHeader(&responseHeader))
  1408. c.Assert(err, IsNil)
  1409. requestId := GetRequestId(responseHeader)
  1410. c.Assert(len(requestId) > 0, Equals, true)
  1411. res, err := client.GetBucketWebsite(bucketNameTest)
  1412. c.Assert(err, IsNil)
  1413. c.Assert(res.RoutingRules[0].Redirect.RedirectType, Equals, "Mirror")
  1414. c.Assert(*res.RoutingRules[0].Redirect.PassQueryString, Equals, false)
  1415. c.Assert(*res.RoutingRules[0].Redirect.MirrorPassQueryString, Equals, false)
  1416. c.Assert(*res.RoutingRules[0].Redirect.MirrorFollowRedirect, Equals, true)
  1417. c.Assert(*res.RoutingRules[0].Redirect.MirrorCheckMd5, Equals, false)
  1418. c.Assert(*res.RoutingRules[0].Redirect.MirrorHeaders.PassAll, Equals, false)
  1419. }
  1420. // TestSetBucketCORS
  1421. func (s *OssClientSuite) TestSetBucketCORS(c *C) {
  1422. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1423. var rule1 = CORSRule{
  1424. AllowedOrigin: []string{"*"},
  1425. AllowedMethod: []string{"PUT", "GET", "POST"},
  1426. AllowedHeader: []string{},
  1427. ExposeHeader: []string{},
  1428. MaxAgeSeconds: 100,
  1429. }
  1430. var rule2 = CORSRule{
  1431. AllowedOrigin: []string{"http://www.a.com", "http://www.b.com"},
  1432. AllowedMethod: []string{"GET"},
  1433. AllowedHeader: []string{"Authorization"},
  1434. ExposeHeader: []string{"x-oss-test", "x-oss-test1"},
  1435. MaxAgeSeconds: 200,
  1436. }
  1437. client, err := New(endpoint, accessID, accessKey)
  1438. c.Assert(err, IsNil)
  1439. err = client.CreateBucket(bucketNameTest)
  1440. c.Assert(err, IsNil)
  1441. time.Sleep(timeoutInOperation)
  1442. // Set
  1443. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1})
  1444. c.Assert(err, IsNil)
  1445. gbcr, err := client.GetBucketCORS(bucketNameTest)
  1446. c.Assert(err, IsNil)
  1447. c.Assert(len(gbcr.CORSRules), Equals, 1)
  1448. c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 1)
  1449. c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 3)
  1450. c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 0)
  1451. c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 0)
  1452. c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 100)
  1453. // Double set
  1454. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1})
  1455. c.Assert(err, IsNil)
  1456. gbcr, err = client.GetBucketCORS(bucketNameTest)
  1457. c.Assert(err, IsNil)
  1458. c.Assert(len(gbcr.CORSRules), Equals, 1)
  1459. c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 1)
  1460. c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 3)
  1461. c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 0)
  1462. c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 0)
  1463. c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 100)
  1464. // Set rule2
  1465. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule2})
  1466. c.Assert(err, IsNil)
  1467. time.Sleep(timeoutInOperation)
  1468. gbcr, err = client.GetBucketCORS(bucketNameTest)
  1469. c.Assert(err, IsNil)
  1470. c.Assert(len(gbcr.CORSRules), Equals, 1)
  1471. c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 2)
  1472. c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 1)
  1473. c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 1)
  1474. c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 2)
  1475. c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 200)
  1476. // Reset
  1477. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1, rule2})
  1478. c.Assert(err, IsNil)
  1479. time.Sleep(timeoutInOperation)
  1480. gbcr, err = client.GetBucketCORS(bucketNameTest)
  1481. c.Assert(err, IsNil)
  1482. c.Assert(len(gbcr.CORSRules), Equals, 2)
  1483. // Set after delete
  1484. err = client.DeleteBucketCORS(bucketNameTest)
  1485. c.Assert(err, IsNil)
  1486. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1, rule2})
  1487. c.Assert(err, IsNil)
  1488. time.Sleep(timeoutInOperation)
  1489. gbcr, err = client.GetBucketCORS(bucketNameTest)
  1490. c.Assert(err, IsNil)
  1491. c.Assert(len(gbcr.CORSRules), Equals, 2)
  1492. err = client.DeleteBucketCORS(bucketNameTest)
  1493. c.Assert(err, IsNil)
  1494. err = client.DeleteBucket(bucketNameTest)
  1495. c.Assert(err, IsNil)
  1496. }
  1497. // TestDeleteBucketCORS
  1498. func (s *OssClientSuite) TestDeleteBucketCORS(c *C) {
  1499. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1500. var rule = CORSRule{
  1501. AllowedOrigin: []string{"*"},
  1502. AllowedMethod: []string{"PUT", "GET", "POST"},
  1503. AllowedHeader: []string{},
  1504. ExposeHeader: []string{},
  1505. MaxAgeSeconds: 100,
  1506. }
  1507. client, err := New(endpoint, accessID, accessKey)
  1508. c.Assert(err, IsNil)
  1509. err = client.CreateBucket(bucketNameTest)
  1510. c.Assert(err, IsNil)
  1511. time.Sleep(timeoutInOperation)
  1512. // Delete not set
  1513. err = client.DeleteBucketCORS(bucketNameTest)
  1514. c.Assert(err, IsNil)
  1515. // Set
  1516. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
  1517. c.Assert(err, IsNil)
  1518. time.Sleep(timeoutInOperation)
  1519. _, err = client.GetBucketCORS(bucketNameTest)
  1520. c.Assert(err, IsNil)
  1521. // Detele
  1522. err = client.DeleteBucketCORS(bucketNameTest)
  1523. c.Assert(err, IsNil)
  1524. time.Sleep(timeoutInOperation)
  1525. _, err = client.GetBucketCORS(bucketNameTest)
  1526. c.Assert(err, NotNil)
  1527. // Detele after deleting
  1528. err = client.DeleteBucketCORS(bucketNameTest)
  1529. c.Assert(err, IsNil)
  1530. err = client.DeleteBucket(bucketNameTest)
  1531. c.Assert(err, IsNil)
  1532. }
  1533. // TestSetBucketCORSNegative
  1534. func (s *OssClientSuite) TestSetBucketCORSNegative(c *C) {
  1535. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1536. var rule = CORSRule{
  1537. AllowedOrigin: []string{"*"},
  1538. AllowedMethod: []string{"PUT", "GET", "POST"},
  1539. AllowedHeader: []string{},
  1540. ExposeHeader: []string{},
  1541. MaxAgeSeconds: 100,
  1542. }
  1543. client, err := New(endpoint, accessID, accessKey)
  1544. c.Assert(err, IsNil)
  1545. err = client.DeleteBucket(bucketNameTest)
  1546. // Not exist
  1547. _, err = client.GetBucketCORS(bucketNameTest)
  1548. c.Assert(err, NotNil)
  1549. err = client.DeleteBucketCORS(bucketNameTest)
  1550. c.Assert(err, NotNil)
  1551. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
  1552. c.Assert(err, NotNil)
  1553. err = client.CreateBucket(bucketNameTest)
  1554. c.Assert(err, IsNil)
  1555. time.Sleep(timeoutInOperation)
  1556. _, err = client.GetBucketCORS(bucketNameTest)
  1557. c.Assert(err, NotNil)
  1558. // Set
  1559. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
  1560. c.Assert(err, IsNil)
  1561. time.Sleep(timeoutInOperation)
  1562. _, err = client.GetBucketCORS(bucketNameTest)
  1563. c.Assert(err, IsNil)
  1564. // Delete
  1565. err = client.DeleteBucketCORS(bucketNameTest)
  1566. c.Assert(err, IsNil)
  1567. time.Sleep(timeoutInOperation)
  1568. _, err = client.GetBucketCORS(bucketNameTest)
  1569. c.Assert(err, NotNil)
  1570. // Delete after deleting
  1571. err = client.DeleteBucketCORS(bucketNameTest)
  1572. c.Assert(err, IsNil)
  1573. err = client.DeleteBucket(bucketNameTest)
  1574. c.Assert(err, IsNil)
  1575. }
  1576. // TestGetBucketInfo
  1577. func (s *OssClientSuite) TestGetBucketInfo(c *C) {
  1578. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1579. client, err := New(endpoint, accessID, accessKey)
  1580. c.Assert(err, IsNil)
  1581. err = client.CreateBucket(bucketNameTest)
  1582. c.Assert(err, IsNil)
  1583. res, err := client.GetBucketInfo(bucketNameTest)
  1584. c.Assert(err, IsNil)
  1585. c.Assert(res.BucketInfo.Name, Equals, bucketNameTest)
  1586. c.Assert(strings.HasPrefix(res.BucketInfo.Location, "oss-"), Equals, true)
  1587. c.Assert(res.BucketInfo.ACL, Equals, "private")
  1588. c.Assert(strings.HasSuffix(res.BucketInfo.ExtranetEndpoint, ".com"), Equals, true)
  1589. c.Assert(strings.HasSuffix(res.BucketInfo.IntranetEndpoint, ".com"), Equals, true)
  1590. c.Assert(res.BucketInfo.CreationDate, NotNil)
  1591. err = client.DeleteBucket(bucketNameTest)
  1592. c.Assert(err, IsNil)
  1593. }
  1594. // TestGetBucketInfoNegative
  1595. func (s *OssClientSuite) TestGetBucketInfoNegative(c *C) {
  1596. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1597. client, err := New(endpoint, accessID, accessKey)
  1598. c.Assert(err, IsNil)
  1599. // Not exist
  1600. _, err = client.GetBucketInfo(bucketNameTest)
  1601. c.Assert(err, NotNil)
  1602. // Bucket name invalid
  1603. _, err = client.GetBucketInfo("InvalidBucketName_")
  1604. c.Assert(err, NotNil)
  1605. }
  1606. // TestEndpointFormat
  1607. func (s *OssClientSuite) TestEndpointFormat(c *C) {
  1608. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1609. // http://host
  1610. client, err := New(endpoint, accessID, accessKey)
  1611. c.Assert(err, IsNil)
  1612. err = client.CreateBucket(bucketNameTest)
  1613. c.Assert(err, IsNil)
  1614. time.Sleep(timeoutInOperation)
  1615. res, err := client.GetBucketACL(bucketNameTest)
  1616. c.Assert(err, IsNil)
  1617. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1618. err = client.DeleteBucket(bucketNameTest)
  1619. c.Assert(err, IsNil)
  1620. time.Sleep(timeoutInOperation)
  1621. // http://host:port
  1622. client, err = New(endpoint+":80", accessID, accessKey)
  1623. c.Assert(err, IsNil)
  1624. err = client.CreateBucket(bucketNameTest)
  1625. c.Assert(err, IsNil)
  1626. time.Sleep(timeoutInOperation)
  1627. res, err = client.GetBucketACL(bucketNameTest)
  1628. c.Assert(err, IsNil)
  1629. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1630. err = client.DeleteBucket(bucketNameTest)
  1631. c.Assert(err, IsNil)
  1632. }
  1633. // TestCname
  1634. func (s *OssClientSuite) _TestCname(c *C) {
  1635. var bucketNameTest = "<my-bucket-cname>"
  1636. client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>", UseCname(true))
  1637. c.Assert(err, IsNil)
  1638. err = client.CreateBucket(bucketNameTest)
  1639. c.Assert(err, IsNil)
  1640. _, err = client.ListBuckets()
  1641. c.Assert(err, NotNil)
  1642. res, err := client.GetBucketACL(bucketNameTest)
  1643. c.Assert(err, IsNil)
  1644. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1645. }
  1646. // TestCnameNegative
  1647. func (s *OssClientSuite) _TestCnameNegative(c *C) {
  1648. var bucketNameTest = "<my-bucket-cname>"
  1649. client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>", UseCname(true))
  1650. c.Assert(err, IsNil)
  1651. err = client.CreateBucket(bucketNameTest)
  1652. c.Assert(err, NotNil)
  1653. _, err = client.ListBuckets()
  1654. c.Assert(err, NotNil)
  1655. _, err = client.GetBucketACL(bucketNameTest)
  1656. c.Assert(err, NotNil)
  1657. }
  1658. // _TestHTTPS
  1659. func (s *OssClientSuite) _TestHTTPS(c *C) {
  1660. var bucketNameTest = "<my-bucket-https>"
  1661. client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>")
  1662. c.Assert(err, IsNil)
  1663. err = client.CreateBucket(bucketNameTest)
  1664. c.Assert(err, IsNil)
  1665. res, err := client.GetBucketACL(bucketNameTest)
  1666. c.Assert(err, IsNil)
  1667. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1668. err = client.DeleteBucket(bucketNameTest)
  1669. c.Assert(err, IsNil)
  1670. }
  1671. // TestClientOption
  1672. func (s *OssClientSuite) TestClientOption(c *C) {
  1673. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1674. client, err := New(endpoint, accessID, accessKey, UseCname(true),
  1675. Timeout(11, 12), SecurityToken("token"), Proxy(proxyHost))
  1676. c.Assert(err, IsNil)
  1677. // CreateBucket timeout
  1678. err = client.CreateBucket(bucketNameTest)
  1679. c.Assert(err, NotNil)
  1680. c.Assert(client.Conn.config.HTTPTimeout.ConnectTimeout, Equals, time.Second*11)
  1681. c.Assert(client.Conn.config.HTTPTimeout.ReadWriteTimeout, Equals, time.Second*12)
  1682. c.Assert(client.Conn.config.HTTPTimeout.HeaderTimeout, Equals, time.Second*12)
  1683. c.Assert(client.Conn.config.HTTPTimeout.IdleConnTimeout, Equals, time.Second*12)
  1684. c.Assert(client.Conn.config.HTTPTimeout.LongTimeout, Equals, time.Second*12*10)
  1685. c.Assert(client.Conn.config.SecurityToken, Equals, "token")
  1686. c.Assert(client.Conn.config.IsCname, Equals, true)
  1687. c.Assert(client.Conn.config.IsUseProxy, Equals, true)
  1688. c.Assert(client.Config.ProxyHost, Equals, proxyHost)
  1689. client, err = New(endpoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
  1690. c.Assert(client.Conn.config.IsUseProxy, Equals, true)
  1691. c.Assert(client.Config.ProxyHost, Equals, proxyHost)
  1692. c.Assert(client.Conn.config.IsAuthProxy, Equals, true)
  1693. c.Assert(client.Conn.config.ProxyUser, Equals, proxyUser)
  1694. c.Assert(client.Conn.config.ProxyPassword, Equals, proxyPasswd)
  1695. client, err = New(endpoint, accessID, accessKey, UserAgent("go sdk user agent"))
  1696. c.Assert(client.Conn.config.UserAgent, Equals, "go sdk user agent")
  1697. // Check we can overide the http.Client
  1698. httpClient := new(http.Client)
  1699. client, err = New(endpoint, accessID, accessKey, HTTPClient(httpClient))
  1700. c.Assert(client.HTTPClient, Equals, httpClient)
  1701. c.Assert(client.Conn.client, Equals, httpClient)
  1702. client, err = New(endpoint, accessID, accessKey)
  1703. c.Assert(client.HTTPClient, IsNil)
  1704. }
  1705. // TestProxy
  1706. func (s *OssClientSuite) ProxyTestFunc(c *C, authVersion AuthVersionType, extraHeaders []string) {
  1707. bucketNameTest := bucketNamePrefix + RandLowStr(6)
  1708. objectName := "体育/奥运/首金"
  1709. objectValue := "大江东去,浪淘尽,千古风流人物。 故垒西边,人道是、三国周郎赤壁。"
  1710. client, err := New(endpoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
  1711. oldType := client.Config.AuthVersion
  1712. oldHeaders := client.Config.AdditionalHeaders
  1713. client.Config.AuthVersion = authVersion
  1714. client.Config.AdditionalHeaders = extraHeaders
  1715. // Create bucket
  1716. err = client.CreateBucket(bucketNameTest)
  1717. c.Assert(err, IsNil)
  1718. // Get bucket info
  1719. _, err = client.GetBucketInfo(bucketNameTest)
  1720. c.Assert(err, IsNil)
  1721. bucket, err := client.Bucket(bucketNameTest)
  1722. // Sign URL
  1723. str, err := bucket.SignURL(objectName, HTTPPut, 60)
  1724. c.Assert(err, IsNil)
  1725. if bucket.Client.Config.AuthVersion == AuthV1 {
  1726. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  1727. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  1728. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  1729. } else {
  1730. c.Assert(strings.Contains(str, HTTPParamSignatureVersion+"=OSS2"), Equals, true)
  1731. c.Assert(strings.Contains(str, HTTPParamExpiresV2+"="), Equals, true)
  1732. c.Assert(strings.Contains(str, HTTPParamAccessKeyIDV2+"="), Equals, true)
  1733. c.Assert(strings.Contains(str, HTTPParamSignatureV2+"="), Equals, true)
  1734. }
  1735. // Put object with URL
  1736. err = bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  1737. c.Assert(err, IsNil)
  1738. // Sign URL for get object
  1739. str, err = bucket.SignURL(objectName, HTTPGet, 60)
  1740. c.Assert(err, IsNil)
  1741. if bucket.Client.Config.AuthVersion == AuthV1 {
  1742. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  1743. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  1744. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  1745. } else {
  1746. c.Assert(strings.Contains(str, HTTPParamSignatureVersion+"=OSS2"), Equals, true)
  1747. c.Assert(strings.Contains(str, HTTPParamExpiresV2+"="), Equals, true)
  1748. c.Assert(strings.Contains(str, HTTPParamAccessKeyIDV2+"="), Equals, true)
  1749. c.Assert(strings.Contains(str, HTTPParamSignatureV2+"="), Equals, true)
  1750. }
  1751. // Get object with URL
  1752. body, err := bucket.GetObjectWithURL(str)
  1753. c.Assert(err, IsNil)
  1754. str, err = readBody(body)
  1755. c.Assert(err, IsNil)
  1756. c.Assert(str, Equals, objectValue)
  1757. // Put object
  1758. err = bucket.PutObject(objectName, strings.NewReader(objectValue))
  1759. c.Assert(err, IsNil)
  1760. // Get object
  1761. _, err = bucket.GetObject(objectName)
  1762. c.Assert(err, IsNil)
  1763. // List objects
  1764. _, err = bucket.ListObjects()
  1765. c.Assert(err, IsNil)
  1766. // Delete object
  1767. err = bucket.DeleteObject(objectName)
  1768. c.Assert(err, IsNil)
  1769. // Delete bucket
  1770. err = client.DeleteBucket(bucketNameTest)
  1771. c.Assert(err, IsNil)
  1772. client.Config.AuthVersion = oldType
  1773. client.Config.AdditionalHeaders = oldHeaders
  1774. }
  1775. func (s *OssClientSuite) TestProxy(c *C) {
  1776. s.ProxyTestFunc(c, AuthV1, []string{})
  1777. s.ProxyTestFunc(c, AuthV2, []string{})
  1778. s.ProxyTestFunc(c, AuthV2, []string{"host", "range", "user-agent"})
  1779. }
  1780. // TestProxy for https endpoint
  1781. func (s *OssClientSuite) TestHttpsEndpointProxy(c *C) {
  1782. bucketNameTest := bucketNamePrefix + RandLowStr(6)
  1783. objectName := objectNamePrefix + RandLowStr(6)
  1784. objectValue := RandLowStr(100)
  1785. httpsEndPoint := ""
  1786. if strings.HasPrefix(endpoint, "http://") {
  1787. httpsEndPoint = strings.Replace(endpoint, "http://", "https://", 1)
  1788. } else if !strings.HasPrefix(endpoint, "https://") {
  1789. httpsEndPoint = "https://" + endpoint
  1790. } else {
  1791. httpsEndPoint = endpoint
  1792. }
  1793. client, err := New(httpsEndPoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
  1794. // Create bucket
  1795. err = client.CreateBucket(bucketNameTest)
  1796. c.Assert(err, IsNil)
  1797. bucket, err := client.Bucket(bucketNameTest)
  1798. // Put object
  1799. err = bucket.PutObject(objectName, strings.NewReader(objectValue))
  1800. c.Assert(err, IsNil)
  1801. // Get object
  1802. _, err = bucket.GetObject(objectName)
  1803. c.Assert(err, IsNil)
  1804. // List objects
  1805. _, err = bucket.ListObjects()
  1806. c.Assert(err, IsNil)
  1807. // Delete object
  1808. err = bucket.DeleteObject(objectName)
  1809. c.Assert(err, IsNil)
  1810. // Delete bucket
  1811. err = client.DeleteBucket(bucketNameTest)
  1812. c.Assert(err, IsNil)
  1813. }
  1814. // Private
  1815. func (s *OssClientSuite) checkBucket(buckets []BucketProperties, bucket string) bool {
  1816. for _, v := range buckets {
  1817. if v.Name == bucket {
  1818. return true
  1819. }
  1820. }
  1821. return false
  1822. }
  1823. func (s *OssClientSuite) getBucket(buckets []BucketProperties, bucket string) (bool, BucketProperties) {
  1824. for _, v := range buckets {
  1825. if v.Name == bucket {
  1826. return true, v
  1827. }
  1828. }
  1829. return false, BucketProperties{}
  1830. }
  1831. func (s *OssClientSuite) TestHttpLogNotSignUrl(c *C) {
  1832. logName := "." + string(os.PathSeparator) + "test-go-sdk-httpdebug.log" + RandStr(5)
  1833. f, err := os.OpenFile(logName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660)
  1834. c.Assert(err, IsNil)
  1835. client, err := New(endpoint, accessID, accessKey)
  1836. client.Config.LogLevel = Debug
  1837. client.Config.Logger = log.New(f, "", log.LstdFlags)
  1838. var testBucketName = bucketNamePrefix + RandLowStr(6)
  1839. // CreateBucket
  1840. err = client.CreateBucket(testBucketName)
  1841. f.Close()
  1842. // read log file,get http info
  1843. contents, err := ioutil.ReadFile(logName)
  1844. c.Assert(err, IsNil)
  1845. httpContent := string(contents)
  1846. //fmt.Println(httpContent)
  1847. c.Assert(strings.Contains(httpContent, "signStr"), Equals, true)
  1848. c.Assert(strings.Contains(httpContent, "Method:"), Equals, true)
  1849. // delete test bucket and log
  1850. os.Remove(logName)
  1851. client.DeleteBucket(testBucketName)
  1852. }
  1853. func (s *OssClientSuite) HttpLogSignUrlTestFunc(c *C, authVersion AuthVersionType, extraHeaders []string) {
  1854. logName := "." + string(os.PathSeparator) + "test-go-sdk-httpdebug-signurl.log" + RandStr(5)
  1855. f, err := os.OpenFile(logName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660)
  1856. c.Assert(err, IsNil)
  1857. client, err := New(endpoint, accessID, accessKey)
  1858. client.Config.LogLevel = Debug
  1859. client.Config.Logger = log.New(f, "", log.LstdFlags)
  1860. oldType := client.Config.AuthVersion
  1861. oldHeaders := client.Config.AdditionalHeaders
  1862. client.Config.AuthVersion = authVersion
  1863. client.Config.AdditionalHeaders = extraHeaders
  1864. var testBucketName = bucketNamePrefix + RandLowStr(6)
  1865. // CreateBucket
  1866. err = client.CreateBucket(testBucketName)
  1867. f.Close()
  1868. // clear log
  1869. f, err = os.OpenFile(logName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660)
  1870. client.Config.Logger = log.New(f, "", log.LstdFlags)
  1871. bucket, _ := client.Bucket(testBucketName)
  1872. objectName := objectNamePrefix + RandStr(8)
  1873. objectValue := RandStr(20)
  1874. // Sign URL for put
  1875. str, err := bucket.SignURL(objectName, HTTPPut, 60)
  1876. c.Assert(err, IsNil)
  1877. if bucket.Client.Config.AuthVersion == AuthV1 {
  1878. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  1879. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  1880. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  1881. } else {
  1882. c.Assert(strings.Contains(str, HTTPParamSignatureVersion+"=OSS2"), Equals, true)
  1883. c.Assert(strings.Contains(str, HTTPParamExpiresV2+"="), Equals, true)
  1884. c.Assert(strings.Contains(str, HTTPParamAccessKeyIDV2+"="), Equals, true)
  1885. c.Assert(strings.Contains(str, HTTPParamSignatureV2+"="), Equals, true)
  1886. }
  1887. // Error put object with URL
  1888. err = bucket.PutObjectWithURL(str, strings.NewReader(objectValue), ContentType("image/tiff"))
  1889. f.Close()
  1890. // read log file,get http info
  1891. contents, err := ioutil.ReadFile(logName)
  1892. c.Assert(err, IsNil)
  1893. httpContent := string(contents)
  1894. //fmt.Println(httpContent)
  1895. c.Assert(strings.Contains(httpContent, "signStr"), Equals, true)
  1896. c.Assert(strings.Contains(httpContent, "Method:"), Equals, true)
  1897. // delete test bucket and log
  1898. os.Remove(logName)
  1899. client.DeleteBucket(testBucketName)
  1900. client.Config.AuthVersion = oldType
  1901. client.Config.AdditionalHeaders = oldHeaders
  1902. }
  1903. func (s *OssClientSuite) TestHttpLogSignUrl(c *C) {
  1904. s.HttpLogSignUrlTestFunc(c, AuthV1, []string{})
  1905. s.HttpLogSignUrlTestFunc(c, AuthV2, []string{})
  1906. s.HttpLogSignUrlTestFunc(c, AuthV2, []string{"host", "range", "user-agent"})
  1907. }
  1908. func (s *OssClientSuite) TestSetLimitUploadSpeed(c *C) {
  1909. client, err := New(endpoint, accessID, accessKey)
  1910. c.Assert(err, IsNil)
  1911. err = client.LimitUploadSpeed(100)
  1912. goVersion := runtime.Version()
  1913. pSlice := strings.Split(strings.ToLower(goVersion), ".")
  1914. // compare with go1.7
  1915. if len(pSlice) >= 2 {
  1916. if pSlice[0] > "go1" {
  1917. c.Assert(err, IsNil)
  1918. } else if pSlice[0] == "go1" {
  1919. subVersion, _ := strconv.Atoi(pSlice[1])
  1920. if subVersion >= 7 {
  1921. c.Assert(err, IsNil)
  1922. } else {
  1923. c.Assert(err, NotNil)
  1924. }
  1925. } else {
  1926. c.Assert(err, NotNil)
  1927. }
  1928. }
  1929. }
  1930. func (s *OssClientSuite) TestBucketEncyptionError(c *C) {
  1931. client, err := New(endpoint, accessID, accessKey)
  1932. c.Assert(err, IsNil)
  1933. bucketName := bucketNamePrefix + RandLowStr(5)
  1934. err = client.CreateBucket(bucketName)
  1935. c.Assert(err, IsNil)
  1936. // SetBucketEncryption:AES256 ,"123"
  1937. encryptionRule := ServerEncryptionRule{}
  1938. encryptionRule.SSEDefault.SSEAlgorithm = string(AESAlgorithm)
  1939. encryptionRule.SSEDefault.KMSMasterKeyID = "123"
  1940. var responseHeader http.Header
  1941. err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
  1942. c.Assert(err, NotNil)
  1943. requestId := GetRequestId(responseHeader)
  1944. c.Assert(len(requestId) > 0, Equals, true)
  1945. // GetBucketEncryption
  1946. _, err = client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1947. c.Assert(err, NotNil)
  1948. requestId = GetRequestId(responseHeader)
  1949. c.Assert(len(requestId) > 0, Equals, true)
  1950. // Get default bucket info
  1951. bucketResult, err := client.GetBucketInfo(bucketName)
  1952. c.Assert(err, IsNil)
  1953. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "")
  1954. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
  1955. c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
  1956. err = client.DeleteBucket(bucketName)
  1957. c.Assert(err, IsNil)
  1958. }
  1959. func (s *OssClientSuite) TestBucketEncryptionPutAndGetAndDelete(c *C) {
  1960. client, err := New(endpoint, accessID, accessKey)
  1961. c.Assert(err, IsNil)
  1962. bucketName := bucketNamePrefix + RandLowStr(5)
  1963. err = client.CreateBucket(bucketName)
  1964. c.Assert(err, IsNil)
  1965. // SetBucketEncryption:KMS ,""
  1966. encryptionRule := ServerEncryptionRule{}
  1967. encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
  1968. var responseHeader http.Header
  1969. err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
  1970. c.Assert(err, IsNil)
  1971. requestId := GetRequestId(responseHeader)
  1972. c.Assert(len(requestId) > 0, Equals, true)
  1973. // GetBucketEncryption
  1974. getResult, err := client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1975. c.Assert(err, IsNil)
  1976. requestId = GetRequestId(responseHeader)
  1977. c.Assert(len(requestId) > 0, Equals, true)
  1978. // check encryption value
  1979. c.Assert(encryptionRule.SSEDefault.SSEAlgorithm, Equals, getResult.SSEDefault.SSEAlgorithm)
  1980. c.Assert(encryptionRule.SSEDefault.KMSMasterKeyID, Equals, getResult.SSEDefault.KMSMasterKeyID)
  1981. // delete bucket encyption
  1982. err = client.DeleteBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1983. c.Assert(err, IsNil)
  1984. requestId = GetRequestId(responseHeader)
  1985. c.Assert(len(requestId) > 0, Equals, true)
  1986. // GetBucketEncryption failure
  1987. _, err = client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1988. c.Assert(err, NotNil)
  1989. requestId = GetRequestId(responseHeader)
  1990. c.Assert(len(requestId) > 0, Equals, true)
  1991. // Get default bucket info
  1992. bucketResult, err := client.GetBucketInfo(bucketName)
  1993. c.Assert(err, IsNil)
  1994. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "")
  1995. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
  1996. c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
  1997. err = client.DeleteBucket(bucketName)
  1998. c.Assert(err, IsNil)
  1999. }
  2000. func (s *OssClientSuite) TestBucketEncryptionWithSm4(c *C) {
  2001. client, err := New(endpoint, accessID, accessKey)
  2002. c.Assert(err, IsNil)
  2003. bucketName := bucketNamePrefix + RandLowStr(5)
  2004. err = client.CreateBucket(bucketName)
  2005. c.Assert(err, IsNil)
  2006. // SetBucketEncryption:SM4 ,""
  2007. encryptionRule := ServerEncryptionRule{}
  2008. encryptionRule.SSEDefault.SSEAlgorithm = string(SM4Algorithm)
  2009. var responseHeader http.Header
  2010. err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
  2011. c.Assert(err, IsNil)
  2012. requestId := GetRequestId(responseHeader)
  2013. c.Assert(len(requestId) > 0, Equals, true)
  2014. // GetBucketEncryption
  2015. getResult, err := client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  2016. c.Assert(err, IsNil)
  2017. requestId = GetRequestId(responseHeader)
  2018. c.Assert(len(requestId) > 0, Equals, true)
  2019. // check encryption value
  2020. c.Assert(getResult.SSEDefault.SSEAlgorithm, Equals, string(SM4Algorithm))
  2021. c.Assert(getResult.SSEDefault.KMSMasterKeyID, Equals, "")
  2022. c.Assert(getResult.SSEDefault.KMSDataEncryption, Equals, "")
  2023. // Get default bucket info
  2024. bucketResult, err := client.GetBucketInfo(bucketName)
  2025. c.Assert(err, IsNil)
  2026. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, string(SM4Algorithm))
  2027. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
  2028. c.Assert(bucketResult.BucketInfo.SseRule.KMSDataEncryption, Equals, "")
  2029. err = client.DeleteBucket(bucketName)
  2030. c.Assert(err, IsNil)
  2031. }
  2032. func (s *OssClientSuite) TestBucketEncryptionWithKmsSm4(c *C) {
  2033. client, err := New(endpoint, accessID, accessKey)
  2034. c.Assert(err, IsNil)
  2035. bucketName := bucketNamePrefix + RandLowStr(5)
  2036. err = client.CreateBucket(bucketName)
  2037. c.Assert(err, IsNil)
  2038. // SetBucketEncryption:SM4 ,""
  2039. encryptionRule := ServerEncryptionRule{}
  2040. encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
  2041. encryptionRule.SSEDefault.KMSDataEncryption = string(SM4Algorithm)
  2042. var responseHeader http.Header
  2043. err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
  2044. c.Assert(err, IsNil)
  2045. requestId := GetRequestId(responseHeader)
  2046. c.Assert(len(requestId) > 0, Equals, true)
  2047. // GetBucketEncryption
  2048. getResult, err := client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  2049. c.Assert(err, IsNil)
  2050. requestId = GetRequestId(responseHeader)
  2051. c.Assert(len(requestId) > 0, Equals, true)
  2052. // check encryption value
  2053. c.Assert(getResult.SSEDefault.SSEAlgorithm, Equals, string(KMSAlgorithm))
  2054. c.Assert(getResult.SSEDefault.KMSMasterKeyID, Equals, "")
  2055. c.Assert(getResult.SSEDefault.KMSDataEncryption, Equals, string(SM4Algorithm))
  2056. // Get default bucket info
  2057. bucketResult, err := client.GetBucketInfo(bucketName)
  2058. c.Assert(err, IsNil)
  2059. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, string(KMSAlgorithm))
  2060. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
  2061. c.Assert(bucketResult.BucketInfo.SseRule.KMSDataEncryption, Equals, string(SM4Algorithm))
  2062. err = client.DeleteBucket(bucketName)
  2063. c.Assert(err, IsNil)
  2064. }
  2065. func (s *OssClientSuite) TestBucketEncyptionPutObjectSuccess(c *C) {
  2066. client, err := New(endpoint, accessID, accessKey)
  2067. c.Assert(err, IsNil)
  2068. bucketName := bucketNamePrefix + RandLowStr(5)
  2069. err = client.CreateBucket(bucketName)
  2070. c.Assert(err, IsNil)
  2071. // SetBucketEncryption:KMS ,""
  2072. encryptionRule := ServerEncryptionRule{}
  2073. encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
  2074. var responseHeader http.Header
  2075. err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
  2076. c.Assert(err, IsNil)
  2077. requestId := GetRequestId(responseHeader)
  2078. c.Assert(len(requestId) > 0, Equals, true)
  2079. // GetBucketEncryption
  2080. getResult, err := client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  2081. c.Assert(err, IsNil)
  2082. requestId = GetRequestId(responseHeader)
  2083. c.Assert(len(requestId) > 0, Equals, true)
  2084. // check encryption value
  2085. c.Assert(encryptionRule.SSEDefault.SSEAlgorithm, Equals, getResult.SSEDefault.SSEAlgorithm)
  2086. c.Assert(encryptionRule.SSEDefault.KMSMasterKeyID, Equals, getResult.SSEDefault.KMSMasterKeyID)
  2087. // Get default bucket info
  2088. bucketResult, err := client.GetBucketInfo(bucketName)
  2089. c.Assert(err, IsNil)
  2090. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "KMS")
  2091. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
  2092. c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
  2093. err = client.DeleteBucket(bucketName)
  2094. c.Assert(err, IsNil)
  2095. }
  2096. func (s *OssClientSuite) TestBucketEncyptionPutObjectError(c *C) {
  2097. client, err := New(endpoint, accessID, accessKey)
  2098. c.Assert(err, IsNil)
  2099. bucketName := bucketNamePrefix + RandLowStr(5)
  2100. err = client.CreateBucket(bucketName)
  2101. c.Assert(err, IsNil)
  2102. // SetBucketEncryption:KMS ,""
  2103. encryptionRule := ServerEncryptionRule{}
  2104. encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
  2105. kmsId := "123"
  2106. encryptionRule.SSEDefault.KMSMasterKeyID = kmsId
  2107. var responseHeader http.Header
  2108. err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
  2109. c.Assert(err, IsNil)
  2110. requestId := GetRequestId(responseHeader)
  2111. c.Assert(len(requestId) > 0, Equals, true)
  2112. // GetBucketEncryption
  2113. getResult, err := client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  2114. c.Assert(err, IsNil)
  2115. requestId = GetRequestId(responseHeader)
  2116. c.Assert(len(requestId) > 0, Equals, true)
  2117. // check encryption value
  2118. c.Assert(encryptionRule.SSEDefault.SSEAlgorithm, Equals, getResult.SSEDefault.SSEAlgorithm)
  2119. c.Assert(encryptionRule.SSEDefault.KMSMasterKeyID, Equals, getResult.SSEDefault.KMSMasterKeyID)
  2120. // Get default bucket info
  2121. bucketResult, err := client.GetBucketInfo(bucketName)
  2122. c.Assert(err, IsNil)
  2123. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "KMS")
  2124. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, kmsId)
  2125. c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
  2126. // put and get object failure
  2127. bucket, err := client.Bucket(bucketName)
  2128. c.Assert(err, IsNil)
  2129. // put object failure
  2130. objectName := objectNamePrefix + RandStr(8)
  2131. context := RandStr(100)
  2132. err = bucket.PutObject(objectName, strings.NewReader(context))
  2133. c.Assert(err, NotNil)
  2134. err = client.DeleteBucket(bucketName)
  2135. c.Assert(err, IsNil)
  2136. }
  2137. func (s *OssClientSuite) TestBucketTaggingOperation(c *C) {
  2138. client, err := New(endpoint, accessID, accessKey)
  2139. c.Assert(err, IsNil)
  2140. bucketName := bucketNamePrefix + RandLowStr(5)
  2141. err = client.CreateBucket(bucketName)
  2142. c.Assert(err, IsNil)
  2143. var respHeader http.Header
  2144. // Bucket Tagging
  2145. var tagging Tagging
  2146. tagging.Tags = []Tag{Tag{Key: "testkey2", Value: "testvalue2"}}
  2147. err = client.SetBucketTagging(bucketName, tagging, GetResponseHeader(&respHeader))
  2148. c.Assert(err, IsNil)
  2149. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  2150. getResult, err := client.GetBucketTagging(bucketName)
  2151. c.Assert(err, IsNil)
  2152. c.Assert(getResult.Tags[0].Key, Equals, tagging.Tags[0].Key)
  2153. c.Assert(getResult.Tags[0].Value, Equals, tagging.Tags[0].Value)
  2154. // delete BucketTagging
  2155. err = client.DeleteBucketTagging(bucketName, GetResponseHeader(&respHeader))
  2156. c.Assert(err, IsNil)
  2157. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  2158. getResult, err = client.GetBucketTagging(bucketName, GetResponseHeader(&respHeader))
  2159. c.Assert(err, IsNil)
  2160. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  2161. c.Assert(len(getResult.Tags), Equals, 0)
  2162. err = client.DeleteBucket(bucketName)
  2163. c.Assert(err, IsNil)
  2164. }
  2165. func (s *OssClientSuite) TestListBucketsTagging(c *C) {
  2166. client, err := New(endpoint, accessID, accessKey)
  2167. c.Assert(err, IsNil)
  2168. bucketName1 := bucketNamePrefix + RandLowStr(5)
  2169. err = client.CreateBucket(bucketName1)
  2170. c.Assert(err, IsNil)
  2171. bucketName2 := bucketNamePrefix + RandLowStr(5)
  2172. err = client.CreateBucket(bucketName2)
  2173. c.Assert(err, IsNil)
  2174. // Bucket Tagging
  2175. var tagging Tagging
  2176. tagging.Tags = []Tag{Tag{Key: "testkey", Value: "testvalue"}}
  2177. err = client.SetBucketTagging(bucketName1, tagging)
  2178. c.Assert(err, IsNil)
  2179. // list bucket
  2180. listResult, err := client.ListBuckets(TagKey("testkey"))
  2181. c.Assert(err, IsNil)
  2182. c.Assert(len(listResult.Buckets), Equals, 1)
  2183. c.Assert(listResult.Buckets[0].Name, Equals, bucketName1)
  2184. client.DeleteBucket(bucketName1)
  2185. client.DeleteBucket(bucketName2)
  2186. }
  2187. func (s *OssClientSuite) TestGetBucketStat(c *C) {
  2188. client, err := New(endpoint, accessID, accessKey)
  2189. c.Assert(err, IsNil)
  2190. bucketName := bucketNamePrefix + RandLowStr(5)
  2191. err = client.CreateBucket(bucketName)
  2192. c.Assert(err, IsNil)
  2193. bucket, err := client.Bucket(bucketName)
  2194. c.Assert(err, IsNil)
  2195. // put object
  2196. objectName := objectNamePrefix + RandLowStr(5)
  2197. err = bucket.PutObject(objectName, strings.NewReader(RandStr(10)))
  2198. c.Assert(err, IsNil)
  2199. bucket.DeleteObject(objectName)
  2200. err = bucket.PutObject(objectName, strings.NewReader(RandStr(10)))
  2201. c.Assert(err, IsNil)
  2202. bucket.DeleteObject(objectName)
  2203. _, err = client.GetBucketStat(bucketName)
  2204. c.Assert(err, IsNil)
  2205. client.DeleteBucket(bucketName)
  2206. }
  2207. func (s *OssBucketSuite) TestGetBucketVersioning(c *C) {
  2208. // create a bucket with default proprety
  2209. client, err := New(endpoint, accessID, accessKey)
  2210. c.Assert(err, IsNil)
  2211. bucketName := bucketNamePrefix + RandLowStr(6)
  2212. var respHeader http.Header
  2213. err = client.CreateBucket(bucketName, GetResponseHeader(&respHeader))
  2214. c.Assert(err, IsNil)
  2215. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  2216. // put bucket version:enabled
  2217. var versioningConfig VersioningConfig
  2218. versioningConfig.Status = string(VersionEnabled)
  2219. err = client.SetBucketVersioning(bucketName, versioningConfig)
  2220. c.Assert(err, IsNil)
  2221. // get bucket version success
  2222. versioningResult, err := client.GetBucketVersioning(bucketName, GetResponseHeader(&respHeader))
  2223. c.Assert(versioningResult.Status, Equals, "Enabled")
  2224. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  2225. ForceDeleteBucket(client, bucketName, c)
  2226. }
  2227. func (s *OssClientSuite) TestBucketPolicy(c *C) {
  2228. client, err := New(endpoint, accessID, accessKey)
  2229. c.Assert(err, IsNil)
  2230. bucketName := bucketNamePrefix + RandLowStr(5)
  2231. err = client.CreateBucket(bucketName)
  2232. c.Assert(err, IsNil)
  2233. var responseHeader http.Header
  2234. ret, err := client.GetBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2235. c.Assert(err, NotNil)
  2236. requestId := GetRequestId(responseHeader)
  2237. c.Assert(len(requestId) > 0, Equals, true)
  2238. policyInfo := `
  2239. {
  2240. "Version":"1",
  2241. "Statement":[
  2242. {
  2243. "Action":[
  2244. "oss:GetObject",
  2245. "oss:PutObject"
  2246. ],
  2247. "Effect":"Deny",
  2248. "Principal":"[123456790]",
  2249. "Resource":["acs:oss:*:1234567890:*/*"]
  2250. }
  2251. ]
  2252. }`
  2253. err = client.SetBucketPolicy(bucketName, policyInfo, GetResponseHeader(&responseHeader))
  2254. c.Assert(err, IsNil)
  2255. requestId = GetRequestId(responseHeader)
  2256. c.Assert(len(requestId) > 0, Equals, true)
  2257. ret, err = client.GetBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2258. c.Assert(err, IsNil)
  2259. testLogger.Println("policy:", ret)
  2260. c.Assert(ret, Equals, policyInfo)
  2261. requestId = GetRequestId(responseHeader)
  2262. c.Assert(len(requestId) > 0, Equals, true)
  2263. err = client.DeleteBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2264. c.Assert(err, IsNil)
  2265. requestId = GetRequestId(responseHeader)
  2266. c.Assert(len(requestId) > 0, Equals, true)
  2267. client.DeleteBucket(bucketName)
  2268. }
  2269. func (s *OssClientSuite) TestBucketPolicyNegative(c *C) {
  2270. client, err := New(endpoint, accessID, accessKey)
  2271. c.Assert(err, IsNil)
  2272. bucketName := bucketNamePrefix + RandLowStr(5)
  2273. err = client.CreateBucket(bucketName)
  2274. c.Assert(err, IsNil)
  2275. var responseHeader http.Header
  2276. _, err = client.GetBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2277. c.Assert(err, NotNil)
  2278. requestId := GetRequestId(responseHeader)
  2279. c.Assert(len(requestId) > 0, Equals, true)
  2280. // Setting the Version is 2, this is error policy
  2281. errPolicy := `
  2282. {
  2283. "Version":"2",
  2284. "Statement":[
  2285. {
  2286. "Action":[
  2287. "oss:GetObject",
  2288. "oss:PutObject"
  2289. ],
  2290. "Effect":"Deny",
  2291. "Principal":"[123456790]",
  2292. "Resource":["acs:oss:*:1234567890:*/*"]
  2293. }
  2294. ]
  2295. }`
  2296. err = client.SetBucketPolicy(bucketName, errPolicy, GetResponseHeader(&responseHeader))
  2297. c.Assert(err, NotNil)
  2298. testLogger.Println("err:", err)
  2299. requestId = GetRequestId(responseHeader)
  2300. c.Assert(len(requestId) > 0, Equals, true)
  2301. err = client.DeleteBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2302. c.Assert(err, IsNil)
  2303. bucketNameEmpty := bucketNamePrefix + RandLowStr(5)
  2304. client.DeleteBucket(bucketNameEmpty)
  2305. err = client.DeleteBucketPolicy(bucketNameEmpty, GetResponseHeader(&responseHeader))
  2306. c.Assert(err, NotNil)
  2307. requestId = GetRequestId(responseHeader)
  2308. c.Assert(len(requestId) > 0, Equals, true)
  2309. client.DeleteBucket(bucketName)
  2310. }
  2311. func (s *OssClientSuite) TestSetBucketRequestPayment(c *C) {
  2312. client, err := New(endpoint, accessID, accessKey)
  2313. c.Assert(err, IsNil)
  2314. bucketName := bucketNamePrefix + RandLowStr(5)
  2315. err = client.CreateBucket(bucketName)
  2316. c.Assert(err, IsNil)
  2317. reqPayConf := RequestPaymentConfiguration{
  2318. Payer: "Requester",
  2319. }
  2320. err = client.SetBucketRequestPayment(bucketName, reqPayConf)
  2321. c.Assert(err, IsNil)
  2322. ret, err := client.GetBucketRequestPayment(bucketName)
  2323. c.Assert(err, IsNil)
  2324. c.Assert(ret.Payer, Equals, "Requester")
  2325. client.DeleteBucket(bucketName)
  2326. c.Assert(err, IsNil)
  2327. }
  2328. func (s *OssClientSuite) TestSetBucketRequestPaymentNegative(c *C) {
  2329. client, err := New(endpoint, accessID, accessKey)
  2330. c.Assert(err, IsNil)
  2331. bucketName := bucketNamePrefix + RandLowStr(5)
  2332. err = client.CreateBucket(bucketName)
  2333. c.Assert(err, IsNil)
  2334. reqPayConf := RequestPaymentConfiguration{
  2335. Payer: "Requesterttttt", // this is a error configuration
  2336. }
  2337. err = client.SetBucketRequestPayment(bucketName, reqPayConf)
  2338. c.Assert(err, NotNil)
  2339. ret, err := client.GetBucketRequestPayment(bucketName)
  2340. c.Assert(err, IsNil)
  2341. c.Assert(ret.Payer, Equals, "BucketOwner")
  2342. client.DeleteBucket(bucketName)
  2343. c.Assert(err, IsNil)
  2344. }
  2345. func (s *OssClientSuite) TestBucketQos(c *C) {
  2346. client, err := New(endpoint, accessID, accessKey)
  2347. c.Assert(err, IsNil)
  2348. ret, err := client.GetUserQoSInfo()
  2349. c.Assert(err, IsNil)
  2350. testLogger.Println("QosInfo:", ret)
  2351. bucketName := bucketNamePrefix + RandLowStr(5)
  2352. _ = client.DeleteBucket(bucketName)
  2353. err = client.CreateBucket(bucketName)
  2354. c.Assert(err, IsNil)
  2355. _, err = client.GetBucketQosInfo(bucketName)
  2356. c.Assert(err, NotNil)
  2357. // case 1 set BucketQoSConfiguration every member
  2358. five := 5
  2359. four := 4
  2360. totalQps := 200
  2361. qosConf := BucketQoSConfiguration{
  2362. TotalUploadBandwidth: &five,
  2363. IntranetUploadBandwidth: &four,
  2364. ExtranetUploadBandwidth: &four,
  2365. TotalDownloadBandwidth: &four,
  2366. IntranetDownloadBandwidth: &four,
  2367. ExtranetDownloadBandwidth: &four,
  2368. TotalQPS: &totalQps,
  2369. IntranetQPS: &totalQps,
  2370. ExtranetQPS: &totalQps,
  2371. }
  2372. var responseHeader http.Header
  2373. err = client.SetBucketQoSInfo(bucketName, qosConf, GetResponseHeader(&responseHeader))
  2374. c.Assert(err, IsNil)
  2375. requestId := GetRequestId(responseHeader)
  2376. c.Assert(len(requestId) > 0, Equals, true)
  2377. // wait a moment for configuration effect
  2378. time.Sleep(time.Second)
  2379. retQos, err := client.GetBucketQosInfo(bucketName)
  2380. c.Assert(err, IsNil)
  2381. // set qosConf default value
  2382. qosConf.XMLName.Local = "QoSConfiguration"
  2383. c.Assert(struct2string(retQos, c), Equals, struct2string(qosConf, c))
  2384. // case 2 set BucketQoSConfiguration not every member
  2385. qosConfNo := BucketQoSConfiguration{
  2386. TotalUploadBandwidth: &five,
  2387. IntranetUploadBandwidth: &four,
  2388. ExtranetUploadBandwidth: &four,
  2389. TotalDownloadBandwidth: &four,
  2390. IntranetDownloadBandwidth: &four,
  2391. ExtranetDownloadBandwidth: &four,
  2392. TotalQPS: &totalQps,
  2393. }
  2394. err = client.SetBucketQoSInfo(bucketName, qosConfNo)
  2395. c.Assert(err, IsNil)
  2396. // wait a moment for configuration effect
  2397. time.Sleep(time.Second)
  2398. retQos, err = client.GetBucketQosInfo(bucketName)
  2399. c.Assert(err, IsNil)
  2400. // set qosConfNo default value
  2401. qosConfNo.XMLName.Local = "QoSConfiguration"
  2402. defNum := -1
  2403. qosConfNo.IntranetQPS = &defNum
  2404. qosConfNo.ExtranetQPS = &defNum
  2405. c.Assert(struct2string(retQos, c), Equals, struct2string(qosConfNo, c))
  2406. err = client.DeleteBucketQosInfo(bucketName)
  2407. c.Assert(err, IsNil)
  2408. // wait a moment for configuration effect
  2409. time.Sleep(time.Second)
  2410. _, err = client.GetBucketQosInfo(bucketName)
  2411. c.Assert(err, NotNil)
  2412. // this is a error qos configuration
  2413. to := *ret.TotalUploadBandwidth + 2
  2414. qosErrConf := BucketQoSConfiguration{
  2415. TotalUploadBandwidth: &to, // this exceed user TotalUploadBandwidth
  2416. IntranetUploadBandwidth: &four,
  2417. ExtranetUploadBandwidth: &four,
  2418. TotalDownloadBandwidth: &four,
  2419. IntranetDownloadBandwidth: &four,
  2420. ExtranetDownloadBandwidth: &four,
  2421. TotalQPS: &totalQps,
  2422. IntranetQPS: &totalQps,
  2423. ExtranetQPS: &totalQps,
  2424. }
  2425. err = client.SetBucketQoSInfo(bucketName, qosErrConf)
  2426. c.Assert(err, NotNil)
  2427. err = client.DeleteBucketQosInfo(bucketName)
  2428. c.Assert(err, IsNil)
  2429. err = client.DeleteBucket(bucketName)
  2430. c.Assert(err, IsNil)
  2431. }
  2432. // struct to string
  2433. func struct2string(obj interface{}, c *C) string {
  2434. t := reflect.TypeOf(obj)
  2435. v := reflect.ValueOf(obj)
  2436. var data = make(map[string]interface{})
  2437. for i := 0; i < t.NumField(); i++ {
  2438. data[t.Field(i).Name] = v.Field(i).Interface()
  2439. }
  2440. str, err := json.Marshal(data)
  2441. c.Assert(err, IsNil)
  2442. return string(str)
  2443. }
  2444. type TestCredentials struct {
  2445. }
  2446. func (testCreInf *TestCredentials) GetAccessKeyID() string {
  2447. return os.Getenv("OSS_TEST_ACCESS_KEY_ID")
  2448. }
  2449. func (testCreInf *TestCredentials) GetAccessKeySecret() string {
  2450. return os.Getenv("OSS_TEST_ACCESS_KEY_SECRET")
  2451. }
  2452. func (testCreInf *TestCredentials) GetSecurityToken() string {
  2453. return ""
  2454. }
  2455. type TestCredentialsProvider struct {
  2456. }
  2457. func (testInfBuild *TestCredentialsProvider) GetCredentials() Credentials {
  2458. return &TestCredentials{}
  2459. }
  2460. func (s *OssClientSuite) TestClientCredentialInfBuild(c *C) {
  2461. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  2462. var defaultBuild TestCredentialsProvider
  2463. client, err := New(endpoint, "", "", SetCredentialsProvider(&defaultBuild))
  2464. c.Assert(err, IsNil)
  2465. err = client.CreateBucket(bucketNameTest)
  2466. c.Assert(err, IsNil)
  2467. err = client.DeleteBucket(bucketNameTest)
  2468. c.Assert(err, IsNil)
  2469. }
  2470. func (s *OssClientSuite) TestClientSetLocalIpError(c *C) {
  2471. // create client and bucket
  2472. ipAddr, err := net.ResolveIPAddr("ip", "127.0.0.1")
  2473. c.Assert(err, IsNil)
  2474. localTCPAddr := &(net.TCPAddr{IP: ipAddr.IP})
  2475. client, err := New(endpoint, accessID, accessKey, SetLocalAddr(localTCPAddr))
  2476. c.Assert(err, IsNil)
  2477. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  2478. err = client.CreateBucket(bucketNameTest)
  2479. c.Assert(err, NotNil)
  2480. }
  2481. func (s *OssClientSuite) TestClientSetLocalIpSuccess(c *C) {
  2482. //get local ip
  2483. conn, err := net.Dial("udp", "8.8.8.8:80")
  2484. c.Assert(err, IsNil)
  2485. localAddr := conn.LocalAddr().(*net.UDPAddr)
  2486. localIp := localAddr.IP.String()
  2487. conn.Close()
  2488. ipAddr, err := net.ResolveIPAddr("ip", localIp)
  2489. c.Assert(err, IsNil)
  2490. localTCPAddr := &(net.TCPAddr{IP: ipAddr.IP})
  2491. client, err := New(endpoint, accessID, accessKey, SetLocalAddr(localTCPAddr))
  2492. c.Assert(err, IsNil)
  2493. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  2494. err = client.CreateBucket(bucketNameTest)
  2495. c.Assert(err, IsNil)
  2496. err = client.DeleteBucket(bucketNameTest)
  2497. c.Assert(err, IsNil)
  2498. }
  2499. // TestCreateBucketInvalidName
  2500. func (s *OssClientSuite) TestCreateBucketInvalidName(c *C) {
  2501. var bucketNameTest = "-" + bucketNamePrefix + RandLowStr(6)
  2502. client, err := New(endpoint, accessID, accessKey)
  2503. c.Assert(err, IsNil)
  2504. // Create
  2505. err = client.CreateBucket(bucketNameTest)
  2506. c.Assert(err, NotNil)
  2507. }
  2508. // TestClientProcessEndpointSuccess
  2509. func (s *OssClientSuite) TestClientProcessEndpointSuccess(c *C) {
  2510. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  2511. testEndpoint := endpoint + "/" + "sina.com" + "?" + "para=abc"
  2512. client, err := New(testEndpoint, accessID, accessKey)
  2513. c.Assert(err, IsNil)
  2514. // Create
  2515. err = client.CreateBucket(bucketNameTest)
  2516. c.Assert(err, IsNil)
  2517. // delete
  2518. err = client.DeleteBucket(bucketNameTest)
  2519. c.Assert(err, IsNil)
  2520. }
  2521. // TestClientProcessEndpointSuccess
  2522. func (s *OssClientSuite) TestClientProcessEndpointError(c *C) {
  2523. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  2524. testEndpoint := "https://127.0.0.1/" + endpoint
  2525. client, err := New(testEndpoint, accessID, accessKey)
  2526. c.Assert(err, IsNil)
  2527. // Create
  2528. err = client.CreateBucket(bucketNameTest)
  2529. c.Assert(err, NotNil)
  2530. }
  2531. // TestClientBucketError
  2532. func (s *OssClientSuite) TestClientBucketError(c *C) {
  2533. client, err := New(endpoint, accessID, accessKey)
  2534. c.Assert(err, IsNil)
  2535. bucketName := "-" + RandLowStr(5)
  2536. _, err = client.Bucket(bucketName)
  2537. c.Assert(err, NotNil)
  2538. }
  2539. func (s *OssClientSuite) TestSetBucketInventory(c *C) {
  2540. client, err := New(endpoint, accessID, accessKey)
  2541. c.Assert(err, IsNil)
  2542. bucketName := bucketNamePrefix + RandLowStr(5)
  2543. err = client.CreateBucket(bucketName)
  2544. c.Assert(err, IsNil)
  2545. // encryption config
  2546. var invSseOss InvSseOss
  2547. invSseKms := InvSseKms{
  2548. KmsId: "keyId",
  2549. }
  2550. var invEncryption InvEncryption
  2551. bl := true
  2552. // not any encryption
  2553. invConfig := InventoryConfiguration{
  2554. Id: "report1",
  2555. IsEnabled: &bl,
  2556. Prefix: "filterPrefix/",
  2557. OSSBucketDestination: OSSBucketDestination{
  2558. Format: "CSV",
  2559. AccountId: accountID,
  2560. RoleArn: stsARN,
  2561. Bucket: "acs:oss:::" + bucketName,
  2562. Prefix: "prefix1",
  2563. },
  2564. Frequency: "Daily",
  2565. IncludedObjectVersions: "All",
  2566. OptionalFields: OptionalFields{
  2567. Field: []string{
  2568. "Size", "LastModifiedDate", "ETag", "StorageClass", "IsMultipartUploaded", "EncryptionStatus",
  2569. },
  2570. },
  2571. }
  2572. // case 1: not any encryption
  2573. err = client.SetBucketInventory(bucketName, invConfig)
  2574. c.Assert(err, IsNil)
  2575. // case 2: use kms encryption
  2576. invConfig.Id = "report2"
  2577. invEncryption.SseKms = &invSseKms
  2578. invEncryption.SseOss = nil
  2579. invConfig.OSSBucketDestination.Encryption = &invEncryption
  2580. err = client.SetBucketInventory(bucketName, invConfig)
  2581. c.Assert(err, IsNil)
  2582. // case 3: use SseOss encryption
  2583. invConfig.Id = "report3"
  2584. invEncryption.SseKms = nil
  2585. invEncryption.SseOss = &invSseOss
  2586. invConfig.OSSBucketDestination.Encryption = &invEncryption
  2587. err = client.SetBucketInventory(bucketName, invConfig)
  2588. c.Assert(err, IsNil)
  2589. //case 4: use two type encryption
  2590. invConfig.Id = "report4"
  2591. invEncryption.SseKms = &invSseKms
  2592. invEncryption.SseOss = &invSseOss
  2593. invConfig.OSSBucketDestination.Encryption = &invEncryption
  2594. err = client.SetBucketInventory(bucketName, invConfig)
  2595. c.Assert(err, NotNil)
  2596. err = client.DeleteBucket(bucketName)
  2597. c.Assert(err, IsNil)
  2598. }
  2599. func (s *OssClientSuite) TestBucketInventory(c *C) {
  2600. client, err := New(endpoint, accessID, accessKey)
  2601. c.Assert(err, IsNil)
  2602. bucketName := bucketNamePrefix + RandLowStr(5)
  2603. err = client.CreateBucket(bucketName)
  2604. c.Assert(err, IsNil)
  2605. bl := true
  2606. invConfig := InventoryConfiguration{
  2607. Id: "report1",
  2608. IsEnabled: &bl,
  2609. Prefix: "filterPrefix/",
  2610. OSSBucketDestination: OSSBucketDestination{
  2611. Format: "CSV",
  2612. AccountId: accountID,
  2613. RoleArn: stsARN,
  2614. Bucket: "acs:oss:::" + bucketName,
  2615. Prefix: "prefix1",
  2616. },
  2617. Frequency: "Daily",
  2618. IncludedObjectVersions: "All",
  2619. OptionalFields: OptionalFields{
  2620. Field: []string{
  2621. "Size", "LastModifiedDate", "ETag", "StorageClass", "IsMultipartUploaded", "EncryptionStatus",
  2622. },
  2623. },
  2624. }
  2625. // case 1: test SetBucketInventory
  2626. err = client.SetBucketInventory(bucketName, invConfig)
  2627. c.Assert(err, IsNil)
  2628. // case 2: test GetBucketInventory
  2629. out, err := client.GetBucketInventory(bucketName, "report1")
  2630. c.Assert(err, IsNil)
  2631. invConfig.XMLName.Local = "InventoryConfiguration"
  2632. invConfig.OSSBucketDestination.XMLName.Local = "OSSBucketDestination"
  2633. invConfig.OptionalFields.XMLName.Local = "OptionalFields"
  2634. c.Assert(struct2string(invConfig, c), Equals, struct2string(out, c))
  2635. // case 3: test ListBucketInventory
  2636. invConfig2 := InventoryConfiguration{
  2637. Id: "report2",
  2638. IsEnabled: &bl,
  2639. Prefix: "filterPrefix/",
  2640. OSSBucketDestination: OSSBucketDestination{
  2641. Format: "CSV",
  2642. AccountId: accountID,
  2643. RoleArn: stsARN,
  2644. Bucket: "acs:oss:::" + bucketName,
  2645. Prefix: "prefix1",
  2646. },
  2647. Frequency: "Daily",
  2648. IncludedObjectVersions: "All",
  2649. OptionalFields: OptionalFields{
  2650. Field: []string{
  2651. "Size", "LastModifiedDate", "ETag", "StorageClass", "IsMultipartUploaded", "EncryptionStatus",
  2652. },
  2653. },
  2654. }
  2655. invConfig2.XMLName.Local = "InventoryConfiguration"
  2656. invConfig2.OSSBucketDestination.XMLName.Local = "OSSBucketDestination"
  2657. invConfig2.OptionalFields.XMLName.Local = "OptionalFields"
  2658. err = client.SetBucketInventory(bucketName, invConfig2)
  2659. c.Assert(err, IsNil)
  2660. listInvConf, err := client.ListBucketInventory(bucketName, "", Marker("report1"), MaxKeys(2))
  2661. c.Assert(err, IsNil)
  2662. var listInvLocal ListInventoryConfigurationsResult
  2663. listInvLocal.InventoryConfiguration = []InventoryConfiguration{
  2664. invConfig,
  2665. invConfig2,
  2666. }
  2667. bo := false
  2668. listInvLocal.IsTruncated = &bo
  2669. listInvLocal.XMLName.Local = "ListInventoryConfigurationsResult"
  2670. c.Assert(struct2string(listInvLocal, c), Equals, struct2string(listInvConf, c))
  2671. for i := 3; i < 109; i++ {
  2672. invConfig2 := InventoryConfiguration{
  2673. Id: "report" + strconv.Itoa(i),
  2674. IsEnabled: &bl,
  2675. Prefix: "filterPrefix/",
  2676. OSSBucketDestination: OSSBucketDestination{
  2677. Format: "CSV",
  2678. AccountId: accountID,
  2679. RoleArn: stsARN,
  2680. Bucket: "acs:oss:::" + bucketName,
  2681. Prefix: "prefix1",
  2682. },
  2683. Frequency: "Daily",
  2684. IncludedObjectVersions: "All",
  2685. OptionalFields: OptionalFields{
  2686. Field: []string{
  2687. "Size", "LastModifiedDate", "ETag", "StorageClass", "IsMultipartUploaded", "EncryptionStatus",
  2688. },
  2689. },
  2690. }
  2691. err = client.SetBucketInventory(bucketName, invConfig2)
  2692. c.Assert(err, IsNil)
  2693. }
  2694. token := ""
  2695. for {
  2696. listInvConf1, err := client.ListBucketInventory(bucketName, token)
  2697. c.Assert(err, IsNil)
  2698. token = listInvConf1.NextContinuationToken
  2699. testLogger.Println(listInvConf1.NextContinuationToken, *listInvConf1.IsTruncated, token)
  2700. if *listInvConf1.IsTruncated == false {
  2701. break
  2702. } else {
  2703. c.Assert(listInvConf1.NextContinuationToken, Equals, "report91")
  2704. }
  2705. }
  2706. // case 4: test DeleteBucketInventory
  2707. for i := 1; i < 109; i++ {
  2708. err = client.DeleteBucketInventory(bucketName, "report"+strconv.Itoa(i))
  2709. c.Assert(err, IsNil)
  2710. }
  2711. err = client.DeleteBucket(bucketName)
  2712. c.Assert(err, IsNil)
  2713. }
  2714. func (s *OssClientSuite) TestBucketInventoryNegative(c *C) {
  2715. client, err := New(endpoint, accessID, accessKey)
  2716. c.Assert(err, IsNil)
  2717. bucketName := bucketNamePrefix + RandLowStr(5)
  2718. err = client.CreateBucket(bucketName)
  2719. c.Assert(err, IsNil)
  2720. bl := true
  2721. invConfigErr := InventoryConfiguration{
  2722. Id: "report1",
  2723. IsEnabled: &bl,
  2724. Prefix: "filterPrefix/",
  2725. OSSBucketDestination: OSSBucketDestination{
  2726. Format: "CSV",
  2727. AccountId: accountID,
  2728. RoleArn: stsARN,
  2729. Bucket: "test",
  2730. Prefix: "prefix1",
  2731. },
  2732. Frequency: "Daily",
  2733. IncludedObjectVersions: "All",
  2734. OptionalFields: OptionalFields{
  2735. Field: []string{
  2736. "Size", "LastModifiedDate", "ETag", "StorageClass", "IsMultipartUploaded", "EncryptionStatus",
  2737. },
  2738. },
  2739. }
  2740. // case 1: test SetBucketInventory
  2741. err = client.SetBucketInventory(bucketName, invConfigErr)
  2742. c.Assert(err, NotNil)
  2743. // case 2: test GetBucketInventory
  2744. _, err = client.GetBucketInventory(bucketName, "report1")
  2745. c.Assert(err, NotNil)
  2746. // case 3: test ListBucketInventory
  2747. _, err = client.ListBucketInventory(bucketName, "", Marker("report1"), MaxKeys(2))
  2748. c.Assert(err, NotNil)
  2749. // case 4: test DeleteBucketInventory
  2750. err = client.DeleteBucketInventory(bucketName, "report1")
  2751. c.Assert(err, IsNil)
  2752. err = client.DeleteBucket(bucketName)
  2753. c.Assert(err, IsNil)
  2754. }
  2755. func (s *OssClientSuite) TestBucketAsyncTask(c *C) {
  2756. client, err := New(endpoint, accessID, accessKey)
  2757. c.Assert(err, IsNil)
  2758. bucketName := bucketNamePrefix + RandLowStr(5)
  2759. err = client.CreateBucket(bucketName)
  2760. c.Assert(err, IsNil)
  2761. objectName := objectNamePrefix + RandLowStr(6)
  2762. // set asyn task
  2763. asynConf := AsyncFetchTaskConfiguration{
  2764. Url: "http://www.baidu.com",
  2765. Object: objectName,
  2766. Host: "",
  2767. ContentMD5: "",
  2768. Callback: "",
  2769. StorageClass: "",
  2770. IgnoreSameKey: true,
  2771. }
  2772. asynResult, err := client.SetBucketAsyncTask(bucketName, asynConf)
  2773. c.Assert(err, IsNil)
  2774. c.Assert(len(asynResult.TaskId) > 0, Equals, true)
  2775. // get asyn task
  2776. asynTask, err := client.GetBucketAsyncTask(bucketName, asynResult.TaskId)
  2777. c.Assert(err, IsNil)
  2778. c.Assert(asynResult.TaskId, Equals, asynTask.TaskId)
  2779. c.Assert(len(asynTask.State) > 0, Equals, true)
  2780. c.Assert(asynConf.Url, Equals, asynTask.TaskInfo.Url)
  2781. c.Assert(asynConf.Object, Equals, asynTask.TaskInfo.Object)
  2782. c.Assert(asynConf.Callback, Equals, asynTask.TaskInfo.Callback)
  2783. c.Assert(asynConf.IgnoreSameKey, Equals, asynTask.TaskInfo.IgnoreSameKey)
  2784. err = client.DeleteBucket(bucketName)
  2785. c.Assert(err, IsNil)
  2786. }