client_test.go 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309
  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) TestBucketEncyptionPutAndGetAndDelete(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) TestBucketEncyptionPutObjectSuccess(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:KMS ,""
  2007. encryptionRule := ServerEncryptionRule{}
  2008. encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
  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(encryptionRule.SSEDefault.SSEAlgorithm, Equals, getResult.SSEDefault.SSEAlgorithm)
  2021. c.Assert(encryptionRule.SSEDefault.KMSMasterKeyID, Equals, getResult.SSEDefault.KMSMasterKeyID)
  2022. // Get default bucket info
  2023. bucketResult, err := client.GetBucketInfo(bucketName)
  2024. c.Assert(err, IsNil)
  2025. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "KMS")
  2026. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
  2027. c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
  2028. err = client.DeleteBucket(bucketName)
  2029. c.Assert(err, IsNil)
  2030. }
  2031. func (s *OssClientSuite) TestBucketEncyptionPutObjectError(c *C) {
  2032. client, err := New(endpoint, accessID, accessKey)
  2033. c.Assert(err, IsNil)
  2034. bucketName := bucketNamePrefix + RandLowStr(5)
  2035. err = client.CreateBucket(bucketName)
  2036. c.Assert(err, IsNil)
  2037. // SetBucketEncryption:KMS ,""
  2038. encryptionRule := ServerEncryptionRule{}
  2039. encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
  2040. kmsId := "123"
  2041. encryptionRule.SSEDefault.KMSMasterKeyID = kmsId
  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(encryptionRule.SSEDefault.SSEAlgorithm, Equals, getResult.SSEDefault.SSEAlgorithm)
  2054. c.Assert(encryptionRule.SSEDefault.KMSMasterKeyID, Equals, getResult.SSEDefault.KMSMasterKeyID)
  2055. // Get default bucket info
  2056. bucketResult, err := client.GetBucketInfo(bucketName)
  2057. c.Assert(err, IsNil)
  2058. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "KMS")
  2059. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, kmsId)
  2060. c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
  2061. // put and get object failure
  2062. bucket, err := client.Bucket(bucketName)
  2063. c.Assert(err, IsNil)
  2064. // put object failure
  2065. objectName := objectNamePrefix + RandStr(8)
  2066. context := RandStr(100)
  2067. err = bucket.PutObject(objectName, strings.NewReader(context))
  2068. c.Assert(err, NotNil)
  2069. err = client.DeleteBucket(bucketName)
  2070. c.Assert(err, IsNil)
  2071. }
  2072. func (s *OssClientSuite) TestBucketTaggingOperation(c *C) {
  2073. client, err := New(endpoint, accessID, accessKey)
  2074. c.Assert(err, IsNil)
  2075. bucketName := bucketNamePrefix + RandLowStr(5)
  2076. err = client.CreateBucket(bucketName)
  2077. c.Assert(err, IsNil)
  2078. var respHeader http.Header
  2079. // Bucket Tagging
  2080. var tagging Tagging
  2081. tagging.Tags = []Tag{Tag{Key: "testkey2", Value: "testvalue2"}}
  2082. err = client.SetBucketTagging(bucketName, tagging, GetResponseHeader(&respHeader))
  2083. c.Assert(err, IsNil)
  2084. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  2085. getResult, err := client.GetBucketTagging(bucketName)
  2086. c.Assert(err, IsNil)
  2087. c.Assert(getResult.Tags[0].Key, Equals, tagging.Tags[0].Key)
  2088. c.Assert(getResult.Tags[0].Value, Equals, tagging.Tags[0].Value)
  2089. // delete BucketTagging
  2090. err = client.DeleteBucketTagging(bucketName, GetResponseHeader(&respHeader))
  2091. c.Assert(err, IsNil)
  2092. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  2093. getResult, err = client.GetBucketTagging(bucketName, GetResponseHeader(&respHeader))
  2094. c.Assert(err, IsNil)
  2095. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  2096. c.Assert(len(getResult.Tags), Equals, 0)
  2097. err = client.DeleteBucket(bucketName)
  2098. c.Assert(err, IsNil)
  2099. }
  2100. func (s *OssClientSuite) TestListBucketsTagging(c *C) {
  2101. client, err := New(endpoint, accessID, accessKey)
  2102. c.Assert(err, IsNil)
  2103. bucketName1 := bucketNamePrefix + RandLowStr(5)
  2104. err = client.CreateBucket(bucketName1)
  2105. c.Assert(err, IsNil)
  2106. bucketName2 := bucketNamePrefix + RandLowStr(5)
  2107. err = client.CreateBucket(bucketName2)
  2108. c.Assert(err, IsNil)
  2109. // Bucket Tagging
  2110. var tagging Tagging
  2111. tagging.Tags = []Tag{Tag{Key: "testkey", Value: "testvalue"}}
  2112. err = client.SetBucketTagging(bucketName1, tagging)
  2113. c.Assert(err, IsNil)
  2114. // list bucket
  2115. listResult, err := client.ListBuckets(TagKey("testkey"))
  2116. c.Assert(err, IsNil)
  2117. c.Assert(len(listResult.Buckets), Equals, 1)
  2118. c.Assert(listResult.Buckets[0].Name, Equals, bucketName1)
  2119. client.DeleteBucket(bucketName1)
  2120. client.DeleteBucket(bucketName2)
  2121. }
  2122. func (s *OssClientSuite) TestGetBucketStat(c *C) {
  2123. client, err := New(endpoint, accessID, accessKey)
  2124. c.Assert(err, IsNil)
  2125. bucketName := bucketNamePrefix + RandLowStr(5)
  2126. err = client.CreateBucket(bucketName)
  2127. c.Assert(err, IsNil)
  2128. bucket, err := client.Bucket(bucketName)
  2129. c.Assert(err, IsNil)
  2130. // put object
  2131. objectName := objectNamePrefix + RandLowStr(5)
  2132. err = bucket.PutObject(objectName, strings.NewReader(RandStr(10)))
  2133. c.Assert(err, IsNil)
  2134. bucket.DeleteObject(objectName)
  2135. err = bucket.PutObject(objectName, strings.NewReader(RandStr(10)))
  2136. c.Assert(err, IsNil)
  2137. bucket.DeleteObject(objectName)
  2138. _, err = client.GetBucketStat(bucketName)
  2139. c.Assert(err, IsNil)
  2140. client.DeleteBucket(bucketName)
  2141. }
  2142. func (s *OssBucketSuite) TestGetBucketVersioning(c *C) {
  2143. // create a bucket with default proprety
  2144. client, err := New(endpoint, accessID, accessKey)
  2145. c.Assert(err, IsNil)
  2146. bucketName := bucketNamePrefix + RandLowStr(6)
  2147. var respHeader http.Header
  2148. err = client.CreateBucket(bucketName, GetResponseHeader(&respHeader))
  2149. c.Assert(err, IsNil)
  2150. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  2151. // put bucket version:enabled
  2152. var versioningConfig VersioningConfig
  2153. versioningConfig.Status = string(VersionEnabled)
  2154. err = client.SetBucketVersioning(bucketName, versioningConfig)
  2155. c.Assert(err, IsNil)
  2156. // get bucket version success
  2157. versioningResult, err := client.GetBucketVersioning(bucketName, GetResponseHeader(&respHeader))
  2158. c.Assert(versioningResult.Status, Equals, "Enabled")
  2159. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  2160. ForceDeleteBucket(client, bucketName, c)
  2161. }
  2162. func (s *OssClientSuite) TestBucketPolicy(c *C) {
  2163. client, err := New(endpoint, accessID, accessKey)
  2164. c.Assert(err, IsNil)
  2165. bucketName := bucketNamePrefix + RandLowStr(5)
  2166. err = client.CreateBucket(bucketName)
  2167. c.Assert(err, IsNil)
  2168. var responseHeader http.Header
  2169. ret, err := client.GetBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2170. c.Assert(err, NotNil)
  2171. requestId := GetRequestId(responseHeader)
  2172. c.Assert(len(requestId) > 0, Equals, true)
  2173. policyInfo := `
  2174. {
  2175. "Version":"1",
  2176. "Statement":[
  2177. {
  2178. "Action":[
  2179. "oss:GetObject",
  2180. "oss:PutObject"
  2181. ],
  2182. "Effect":"Deny",
  2183. "Principal":"[123456790]",
  2184. "Resource":["acs:oss:*:1234567890:*/*"]
  2185. }
  2186. ]
  2187. }`
  2188. err = client.SetBucketPolicy(bucketName, policyInfo, GetResponseHeader(&responseHeader))
  2189. c.Assert(err, IsNil)
  2190. requestId = GetRequestId(responseHeader)
  2191. c.Assert(len(requestId) > 0, Equals, true)
  2192. ret, err = client.GetBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2193. c.Assert(err, IsNil)
  2194. testLogger.Println("policy:", ret)
  2195. c.Assert(ret, Equals, policyInfo)
  2196. requestId = GetRequestId(responseHeader)
  2197. c.Assert(len(requestId) > 0, Equals, true)
  2198. err = client.DeleteBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2199. c.Assert(err, IsNil)
  2200. requestId = GetRequestId(responseHeader)
  2201. c.Assert(len(requestId) > 0, Equals, true)
  2202. client.DeleteBucket(bucketName)
  2203. }
  2204. func (s *OssClientSuite) TestBucketPolicyNegative(c *C) {
  2205. client, err := New(endpoint, accessID, accessKey)
  2206. c.Assert(err, IsNil)
  2207. bucketName := bucketNamePrefix + RandLowStr(5)
  2208. err = client.CreateBucket(bucketName)
  2209. c.Assert(err, IsNil)
  2210. var responseHeader http.Header
  2211. _, err = client.GetBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2212. c.Assert(err, NotNil)
  2213. requestId := GetRequestId(responseHeader)
  2214. c.Assert(len(requestId) > 0, Equals, true)
  2215. // Setting the Version is 2, this is error policy
  2216. errPolicy := `
  2217. {
  2218. "Version":"2",
  2219. "Statement":[
  2220. {
  2221. "Action":[
  2222. "oss:GetObject",
  2223. "oss:PutObject"
  2224. ],
  2225. "Effect":"Deny",
  2226. "Principal":"[123456790]",
  2227. "Resource":["acs:oss:*:1234567890:*/*"]
  2228. }
  2229. ]
  2230. }`
  2231. err = client.SetBucketPolicy(bucketName, errPolicy, GetResponseHeader(&responseHeader))
  2232. c.Assert(err, NotNil)
  2233. testLogger.Println("err:", err)
  2234. requestId = GetRequestId(responseHeader)
  2235. c.Assert(len(requestId) > 0, Equals, true)
  2236. err = client.DeleteBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2237. c.Assert(err, IsNil)
  2238. bucketNameEmpty := bucketNamePrefix + RandLowStr(5)
  2239. client.DeleteBucket(bucketNameEmpty)
  2240. err = client.DeleteBucketPolicy(bucketNameEmpty, GetResponseHeader(&responseHeader))
  2241. c.Assert(err, NotNil)
  2242. requestId = GetRequestId(responseHeader)
  2243. c.Assert(len(requestId) > 0, Equals, true)
  2244. client.DeleteBucket(bucketName)
  2245. }
  2246. func (s *OssClientSuite) TestSetBucketRequestPayment(c *C) {
  2247. client, err := New(endpoint, accessID, accessKey)
  2248. c.Assert(err, IsNil)
  2249. bucketName := bucketNamePrefix + RandLowStr(5)
  2250. err = client.CreateBucket(bucketName)
  2251. c.Assert(err, IsNil)
  2252. reqPayConf := RequestPaymentConfiguration{
  2253. Payer: "Requester",
  2254. }
  2255. err = client.SetBucketRequestPayment(bucketName, reqPayConf)
  2256. c.Assert(err, IsNil)
  2257. ret, err := client.GetBucketRequestPayment(bucketName)
  2258. c.Assert(err, IsNil)
  2259. c.Assert(ret.Payer, Equals, "Requester")
  2260. client.DeleteBucket(bucketName)
  2261. c.Assert(err, IsNil)
  2262. }
  2263. func (s *OssClientSuite) TestSetBucketRequestPaymentNegative(c *C) {
  2264. client, err := New(endpoint, accessID, accessKey)
  2265. c.Assert(err, IsNil)
  2266. bucketName := bucketNamePrefix + RandLowStr(5)
  2267. err = client.CreateBucket(bucketName)
  2268. c.Assert(err, IsNil)
  2269. reqPayConf := RequestPaymentConfiguration{
  2270. Payer: "Requesterttttt", // this is a error configuration
  2271. }
  2272. err = client.SetBucketRequestPayment(bucketName, reqPayConf)
  2273. c.Assert(err, NotNil)
  2274. ret, err := client.GetBucketRequestPayment(bucketName)
  2275. c.Assert(err, IsNil)
  2276. c.Assert(ret.Payer, Equals, "BucketOwner")
  2277. client.DeleteBucket(bucketName)
  2278. c.Assert(err, IsNil)
  2279. }
  2280. func (s *OssClientSuite) TestBucketQos(c *C) {
  2281. client, err := New(endpoint, accessID, accessKey)
  2282. c.Assert(err, IsNil)
  2283. ret, err := client.GetUserQoSInfo()
  2284. c.Assert(err, IsNil)
  2285. testLogger.Println("QosInfo:", ret)
  2286. bucketName := bucketNamePrefix + RandLowStr(5)
  2287. _ = client.DeleteBucket(bucketName)
  2288. err = client.CreateBucket(bucketName)
  2289. c.Assert(err, IsNil)
  2290. _, err = client.GetBucketQosInfo(bucketName)
  2291. c.Assert(err, NotNil)
  2292. // case 1 set BucketQoSConfiguration every member
  2293. five := 5
  2294. four := 4
  2295. totalQps := 200
  2296. qosConf := BucketQoSConfiguration{
  2297. TotalUploadBandwidth: &five,
  2298. IntranetUploadBandwidth: &four,
  2299. ExtranetUploadBandwidth: &four,
  2300. TotalDownloadBandwidth: &four,
  2301. IntranetDownloadBandwidth: &four,
  2302. ExtranetDownloadBandwidth: &four,
  2303. TotalQPS: &totalQps,
  2304. IntranetQPS: &totalQps,
  2305. ExtranetQPS: &totalQps,
  2306. }
  2307. var responseHeader http.Header
  2308. err = client.SetBucketQoSInfo(bucketName, qosConf, GetResponseHeader(&responseHeader))
  2309. c.Assert(err, IsNil)
  2310. requestId := GetRequestId(responseHeader)
  2311. c.Assert(len(requestId) > 0, Equals, true)
  2312. // wait a moment for configuration effect
  2313. time.Sleep(time.Second)
  2314. retQos, err := client.GetBucketQosInfo(bucketName)
  2315. c.Assert(err, IsNil)
  2316. // set qosConf default value
  2317. qosConf.XMLName.Local = "QoSConfiguration"
  2318. c.Assert(struct2string(retQos, c), Equals, struct2string(qosConf, c))
  2319. // case 2 set BucketQoSConfiguration not every member
  2320. qosConfNo := BucketQoSConfiguration{
  2321. TotalUploadBandwidth: &five,
  2322. IntranetUploadBandwidth: &four,
  2323. ExtranetUploadBandwidth: &four,
  2324. TotalDownloadBandwidth: &four,
  2325. IntranetDownloadBandwidth: &four,
  2326. ExtranetDownloadBandwidth: &four,
  2327. TotalQPS: &totalQps,
  2328. }
  2329. err = client.SetBucketQoSInfo(bucketName, qosConfNo)
  2330. c.Assert(err, IsNil)
  2331. // wait a moment for configuration effect
  2332. time.Sleep(time.Second)
  2333. retQos, err = client.GetBucketQosInfo(bucketName)
  2334. c.Assert(err, IsNil)
  2335. // set qosConfNo default value
  2336. qosConfNo.XMLName.Local = "QoSConfiguration"
  2337. defNum := -1
  2338. qosConfNo.IntranetQPS = &defNum
  2339. qosConfNo.ExtranetQPS = &defNum
  2340. c.Assert(struct2string(retQos, c), Equals, struct2string(qosConfNo, c))
  2341. err = client.DeleteBucketQosInfo(bucketName)
  2342. c.Assert(err, IsNil)
  2343. // wait a moment for configuration effect
  2344. time.Sleep(time.Second)
  2345. _, err = client.GetBucketQosInfo(bucketName)
  2346. c.Assert(err, NotNil)
  2347. // this is a error qos configuration
  2348. to := *ret.TotalUploadBandwidth + 2
  2349. qosErrConf := BucketQoSConfiguration{
  2350. TotalUploadBandwidth: &to, // this exceed user TotalUploadBandwidth
  2351. IntranetUploadBandwidth: &four,
  2352. ExtranetUploadBandwidth: &four,
  2353. TotalDownloadBandwidth: &four,
  2354. IntranetDownloadBandwidth: &four,
  2355. ExtranetDownloadBandwidth: &four,
  2356. TotalQPS: &totalQps,
  2357. IntranetQPS: &totalQps,
  2358. ExtranetQPS: &totalQps,
  2359. }
  2360. err = client.SetBucketQoSInfo(bucketName, qosErrConf)
  2361. c.Assert(err, NotNil)
  2362. err = client.DeleteBucketQosInfo(bucketName)
  2363. c.Assert(err, IsNil)
  2364. err = client.DeleteBucket(bucketName)
  2365. c.Assert(err, IsNil)
  2366. }
  2367. // struct to string
  2368. func struct2string(obj interface{}, c *C) string {
  2369. t := reflect.TypeOf(obj)
  2370. v := reflect.ValueOf(obj)
  2371. var data = make(map[string]interface{})
  2372. for i := 0; i < t.NumField(); i++ {
  2373. data[t.Field(i).Name] = v.Field(i).Interface()
  2374. }
  2375. str, err := json.Marshal(data)
  2376. c.Assert(err, IsNil)
  2377. return string(str)
  2378. }
  2379. type TestCredentials struct {
  2380. }
  2381. func (testCreInf *TestCredentials) GetAccessKeyID() string {
  2382. return os.Getenv("OSS_TEST_ACCESS_KEY_ID")
  2383. }
  2384. func (testCreInf *TestCredentials) GetAccessKeySecret() string {
  2385. return os.Getenv("OSS_TEST_ACCESS_KEY_SECRET")
  2386. }
  2387. func (testCreInf *TestCredentials) GetSecurityToken() string {
  2388. return ""
  2389. }
  2390. type TestCredentialsProvider struct {
  2391. }
  2392. func (testInfBuild *TestCredentialsProvider) GetCredentials() Credentials {
  2393. return &TestCredentials{}
  2394. }
  2395. func (s *OssClientSuite) TestClientCredentialInfBuild(c *C) {
  2396. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  2397. var defaultBuild TestCredentialsProvider
  2398. client, err := New(endpoint, "", "", SetCredentialsProvider(&defaultBuild))
  2399. c.Assert(err, IsNil)
  2400. err = client.CreateBucket(bucketNameTest)
  2401. c.Assert(err, IsNil)
  2402. err = client.DeleteBucket(bucketNameTest)
  2403. c.Assert(err, IsNil)
  2404. }
  2405. func (s *OssClientSuite) TestClientSetLocalIpError(c *C) {
  2406. // create client and bucket
  2407. ipAddr, err := net.ResolveIPAddr("ip", "127.0.0.1")
  2408. c.Assert(err, IsNil)
  2409. localTCPAddr := &(net.TCPAddr{IP: ipAddr.IP})
  2410. client, err := New(endpoint, accessID, accessKey, SetLocalAddr(localTCPAddr))
  2411. c.Assert(err, IsNil)
  2412. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  2413. err = client.CreateBucket(bucketNameTest)
  2414. c.Assert(err, NotNil)
  2415. }
  2416. func (s *OssClientSuite) TestClientSetLocalIpSuccess(c *C) {
  2417. //get local ip
  2418. conn, err := net.Dial("udp", "8.8.8.8:80")
  2419. c.Assert(err, IsNil)
  2420. localAddr := conn.LocalAddr().(*net.UDPAddr)
  2421. localIp := localAddr.IP.String()
  2422. conn.Close()
  2423. ipAddr, err := net.ResolveIPAddr("ip", localIp)
  2424. c.Assert(err, IsNil)
  2425. localTCPAddr := &(net.TCPAddr{IP: ipAddr.IP})
  2426. client, err := New(endpoint, accessID, accessKey, SetLocalAddr(localTCPAddr))
  2427. c.Assert(err, IsNil)
  2428. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  2429. err = client.CreateBucket(bucketNameTest)
  2430. c.Assert(err, IsNil)
  2431. err = client.DeleteBucket(bucketNameTest)
  2432. c.Assert(err, IsNil)
  2433. }
  2434. // TestCreateBucketInvalidName
  2435. func (s *OssClientSuite) TestCreateBucketInvalidName(c *C) {
  2436. var bucketNameTest = "-" + bucketNamePrefix + RandLowStr(6)
  2437. client, err := New(endpoint, accessID, accessKey)
  2438. c.Assert(err, IsNil)
  2439. // Create
  2440. err = client.CreateBucket(bucketNameTest)
  2441. c.Assert(err, NotNil)
  2442. }
  2443. // TestClientProcessEndpointSuccess
  2444. func (s *OssClientSuite) TestClientProcessEndpointSuccess(c *C) {
  2445. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  2446. testEndpoint := endpoint + "/" + "sina.com" + "?" + "para=abc"
  2447. client, err := New(testEndpoint, accessID, accessKey)
  2448. c.Assert(err, IsNil)
  2449. // Create
  2450. err = client.CreateBucket(bucketNameTest)
  2451. c.Assert(err, IsNil)
  2452. // delete
  2453. err = client.DeleteBucket(bucketNameTest)
  2454. c.Assert(err, IsNil)
  2455. }
  2456. // TestClientProcessEndpointSuccess
  2457. func (s *OssClientSuite) TestClientProcessEndpointError(c *C) {
  2458. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  2459. testEndpoint := "https://127.0.0.1/" + endpoint
  2460. client, err := New(testEndpoint, accessID, accessKey)
  2461. c.Assert(err, IsNil)
  2462. // Create
  2463. err = client.CreateBucket(bucketNameTest)
  2464. c.Assert(err, NotNil)
  2465. }
  2466. // TestClientBucketError
  2467. func (s *OssClientSuite) TestClientBucketError(c *C) {
  2468. client, err := New(endpoint, accessID, accessKey)
  2469. c.Assert(err, IsNil)
  2470. bucketName := "-" + RandLowStr(5)
  2471. _, err = client.Bucket(bucketName)
  2472. c.Assert(err, NotNil)
  2473. }
  2474. func (s *OssClientSuite) TestSetBucketInventory(c *C) {
  2475. client, err := New(endpoint, accessID, accessKey)
  2476. c.Assert(err, IsNil)
  2477. bucketName := bucketNamePrefix + RandLowStr(5)
  2478. err = client.CreateBucket(bucketName)
  2479. c.Assert(err, IsNil)
  2480. // encryption config
  2481. var invSseOss InvSseOss
  2482. invSseKms := InvSseKms{
  2483. KmsId: "keyId",
  2484. }
  2485. var invEncryption InvEncryption
  2486. bl := true
  2487. // not any encryption
  2488. invConfig := InventoryConfiguration{
  2489. Id: "report1",
  2490. IsEnabled: &bl,
  2491. Prefix: "filterPrefix/",
  2492. OSSBucketDestination: OSSBucketDestination{
  2493. Format: "CSV",
  2494. AccountId: accountID,
  2495. RoleArn: stsARN,
  2496. Bucket: "acs:oss:::" + bucketName,
  2497. Prefix: "prefix1",
  2498. },
  2499. Frequency: "Daily",
  2500. IncludedObjectVersions: "All",
  2501. OptionalFields: OptionalFields{
  2502. Field: []string{
  2503. "Size", "LastModifiedDate", "ETag", "StorageClass", "IsMultipartUploaded", "EncryptionStatus",
  2504. },
  2505. },
  2506. }
  2507. // case 1: not any encryption
  2508. err = client.SetBucketInventory(bucketName, invConfig)
  2509. c.Assert(err, IsNil)
  2510. // case 2: use kms encryption
  2511. invConfig.Id = "report2"
  2512. invEncryption.SseKms = &invSseKms
  2513. invEncryption.SseOss = nil
  2514. invConfig.OSSBucketDestination.Encryption = &invEncryption
  2515. err = client.SetBucketInventory(bucketName, invConfig)
  2516. c.Assert(err, IsNil)
  2517. // case 3: use SseOss encryption
  2518. invConfig.Id = "report3"
  2519. invEncryption.SseKms = nil
  2520. invEncryption.SseOss = &invSseOss
  2521. invConfig.OSSBucketDestination.Encryption = &invEncryption
  2522. err = client.SetBucketInventory(bucketName, invConfig)
  2523. c.Assert(err, IsNil)
  2524. //case 4: use two type encryption
  2525. invConfig.Id = "report4"
  2526. invEncryption.SseKms = &invSseKms
  2527. invEncryption.SseOss = &invSseOss
  2528. invConfig.OSSBucketDestination.Encryption = &invEncryption
  2529. err = client.SetBucketInventory(bucketName, invConfig)
  2530. c.Assert(err, NotNil)
  2531. err = client.DeleteBucket(bucketName)
  2532. c.Assert(err, IsNil)
  2533. }
  2534. func (s *OssClientSuite) TestBucketInventory(c *C) {
  2535. client, err := New(endpoint, accessID, accessKey)
  2536. c.Assert(err, IsNil)
  2537. bucketName := bucketNamePrefix + RandLowStr(5)
  2538. err = client.CreateBucket(bucketName)
  2539. c.Assert(err, IsNil)
  2540. bl := true
  2541. invConfig := InventoryConfiguration{
  2542. Id: "report1",
  2543. IsEnabled: &bl,
  2544. Prefix: "filterPrefix/",
  2545. OSSBucketDestination: OSSBucketDestination{
  2546. Format: "CSV",
  2547. AccountId: accountID,
  2548. RoleArn: stsARN,
  2549. Bucket: "acs:oss:::" + bucketName,
  2550. Prefix: "prefix1",
  2551. },
  2552. Frequency: "Daily",
  2553. IncludedObjectVersions: "All",
  2554. OptionalFields: OptionalFields{
  2555. Field: []string{
  2556. "Size", "LastModifiedDate", "ETag", "StorageClass", "IsMultipartUploaded", "EncryptionStatus",
  2557. },
  2558. },
  2559. }
  2560. // case 1: test SetBucketInventory
  2561. err = client.SetBucketInventory(bucketName, invConfig)
  2562. c.Assert(err, IsNil)
  2563. // case 2: test GetBucketInventory
  2564. out, err := client.GetBucketInventory(bucketName, "report1")
  2565. c.Assert(err, IsNil)
  2566. invConfig.XMLName.Local = "InventoryConfiguration"
  2567. invConfig.OSSBucketDestination.XMLName.Local = "OSSBucketDestination"
  2568. invConfig.OptionalFields.XMLName.Local = "OptionalFields"
  2569. c.Assert(struct2string(invConfig, c), Equals, struct2string(out, c))
  2570. // case 3: test ListBucketInventory
  2571. invConfig2 := InventoryConfiguration{
  2572. Id: "report2",
  2573. IsEnabled: &bl,
  2574. Prefix: "filterPrefix/",
  2575. OSSBucketDestination: OSSBucketDestination{
  2576. Format: "CSV",
  2577. AccountId: accountID,
  2578. RoleArn: stsARN,
  2579. Bucket: "acs:oss:::" + bucketName,
  2580. Prefix: "prefix1",
  2581. },
  2582. Frequency: "Daily",
  2583. IncludedObjectVersions: "All",
  2584. OptionalFields: OptionalFields{
  2585. Field: []string{
  2586. "Size", "LastModifiedDate", "ETag", "StorageClass", "IsMultipartUploaded", "EncryptionStatus",
  2587. },
  2588. },
  2589. }
  2590. invConfig2.XMLName.Local = "InventoryConfiguration"
  2591. invConfig2.OSSBucketDestination.XMLName.Local = "OSSBucketDestination"
  2592. invConfig2.OptionalFields.XMLName.Local = "OptionalFields"
  2593. err = client.SetBucketInventory(bucketName, invConfig2)
  2594. c.Assert(err, IsNil)
  2595. listInvConf, err := client.ListBucketInventory(bucketName, "", Marker("report1"), MaxKeys(2))
  2596. c.Assert(err, IsNil)
  2597. var listInvLocal ListInventoryConfigurationsResult
  2598. listInvLocal.InventoryConfiguration = []InventoryConfiguration{
  2599. invConfig,
  2600. invConfig2,
  2601. }
  2602. bo := false
  2603. listInvLocal.IsTruncated = &bo
  2604. listInvLocal.XMLName.Local = "ListInventoryConfigurationsResult"
  2605. c.Assert(struct2string(listInvLocal, c), Equals, struct2string(listInvConf, c))
  2606. for i := 3; i < 109; i++ {
  2607. invConfig2 := InventoryConfiguration{
  2608. Id: "report" + strconv.Itoa(i),
  2609. IsEnabled: &bl,
  2610. Prefix: "filterPrefix/",
  2611. OSSBucketDestination: OSSBucketDestination{
  2612. Format: "CSV",
  2613. AccountId: accountID,
  2614. RoleArn: stsARN,
  2615. Bucket: "acs:oss:::" + bucketName,
  2616. Prefix: "prefix1",
  2617. },
  2618. Frequency: "Daily",
  2619. IncludedObjectVersions: "All",
  2620. OptionalFields: OptionalFields{
  2621. Field: []string{
  2622. "Size", "LastModifiedDate", "ETag", "StorageClass", "IsMultipartUploaded", "EncryptionStatus",
  2623. },
  2624. },
  2625. }
  2626. err = client.SetBucketInventory(bucketName, invConfig2)
  2627. c.Assert(err, IsNil)
  2628. }
  2629. token := ""
  2630. for {
  2631. listInvConf1, err := client.ListBucketInventory(bucketName, token)
  2632. c.Assert(err, IsNil)
  2633. token = listInvConf1.NextContinuationToken
  2634. testLogger.Println(listInvConf1.NextContinuationToken, *listInvConf1.IsTruncated, token)
  2635. if *listInvConf1.IsTruncated == false {
  2636. break
  2637. } else {
  2638. c.Assert(listInvConf1.NextContinuationToken, Equals, "report91")
  2639. }
  2640. }
  2641. // case 4: test DeleteBucketInventory
  2642. for i := 1; i < 109; i++ {
  2643. err = client.DeleteBucketInventory(bucketName, "report"+strconv.Itoa(i))
  2644. c.Assert(err, IsNil)
  2645. }
  2646. err = client.DeleteBucket(bucketName)
  2647. c.Assert(err, IsNil)
  2648. }
  2649. func (s *OssClientSuite) TestBucketInventoryNegative(c *C) {
  2650. client, err := New(endpoint, accessID, accessKey)
  2651. c.Assert(err, IsNil)
  2652. bucketName := bucketNamePrefix + RandLowStr(5)
  2653. err = client.CreateBucket(bucketName)
  2654. c.Assert(err, IsNil)
  2655. bl := true
  2656. invConfigErr := InventoryConfiguration{
  2657. Id: "report1",
  2658. IsEnabled: &bl,
  2659. Prefix: "filterPrefix/",
  2660. OSSBucketDestination: OSSBucketDestination{
  2661. Format: "CSV",
  2662. AccountId: accountID,
  2663. RoleArn: stsARN,
  2664. Bucket: "test",
  2665. Prefix: "prefix1",
  2666. },
  2667. Frequency: "Daily",
  2668. IncludedObjectVersions: "All",
  2669. OptionalFields: OptionalFields{
  2670. Field: []string{
  2671. "Size", "LastModifiedDate", "ETag", "StorageClass", "IsMultipartUploaded", "EncryptionStatus",
  2672. },
  2673. },
  2674. }
  2675. // case 1: test SetBucketInventory
  2676. err = client.SetBucketInventory(bucketName, invConfigErr)
  2677. c.Assert(err, NotNil)
  2678. // case 2: test GetBucketInventory
  2679. _, err = client.GetBucketInventory(bucketName, "report1")
  2680. c.Assert(err, NotNil)
  2681. // case 3: test ListBucketInventory
  2682. _, err = client.ListBucketInventory(bucketName, "", Marker("report1"), MaxKeys(2))
  2683. c.Assert(err, NotNil)
  2684. // case 4: test DeleteBucketInventory
  2685. err = client.DeleteBucketInventory(bucketName, "report1")
  2686. c.Assert(err, IsNil)
  2687. err = client.DeleteBucket(bucketName)
  2688. c.Assert(err, IsNil)
  2689. }