client_test.go 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541
  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. "io/ioutil"
  7. "log"
  8. "math/rand"
  9. "net/http"
  10. "os"
  11. "runtime"
  12. "strconv"
  13. "strings"
  14. "testing"
  15. "time"
  16. . "gopkg.in/check.v1"
  17. )
  18. // Test hooks up gocheck into the "go test" runner.
  19. func Test(t *testing.T) {
  20. TestingT(t)
  21. }
  22. type OssClientSuite struct{}
  23. var _ = Suite(&OssClientSuite{})
  24. var (
  25. // Endpoint/ID/Key
  26. endpoint = os.Getenv("OSS_TEST_ENDPOINT")
  27. accessID = os.Getenv("OSS_TEST_ACCESS_KEY_ID")
  28. accessKey = os.Getenv("OSS_TEST_ACCESS_KEY_SECRET")
  29. // Proxy
  30. proxyHost = os.Getenv("OSS_TEST_PROXY_HOST")
  31. proxyUser = os.Getenv("OSS_TEST_PROXY_USER")
  32. proxyPasswd = os.Getenv("OSS_TEST_PROXY_PASSWORD")
  33. // STS
  34. stsaccessID = os.Getenv("OSS_TEST_STS_ID")
  35. stsaccessKey = os.Getenv("OSS_TEST_STS_KEY")
  36. stsARN = os.Getenv("OSS_TEST_STS_ARN")
  37. )
  38. var (
  39. // prefix of bucket name for bucket ops test
  40. bucketNamePrefix = "go-sdk-test-bucket-"
  41. // bucket name for object ops test
  42. bucketName = bucketNamePrefix + randLowStr(6)
  43. archiveBucketName = bucketNamePrefix + "arch-" + randLowStr(6)
  44. // object name for object ops test
  45. objectNamePrefix = "go-sdk-test-object-"
  46. // sts region is one and only hangzhou
  47. stsRegion = "cn-hangzhou"
  48. )
  49. var (
  50. logPath = "go_sdk_test_" + time.Now().Format("20060102_150405") + ".log"
  51. testLogFile, _ = os.OpenFile(logPath, os.O_RDWR|os.O_CREATE, 0664)
  52. testLogger = log.New(testLogFile, "", log.Ldate|log.Ltime|log.Lshortfile)
  53. letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
  54. timeoutInOperation = 3 * time.Second
  55. )
  56. func randStr(n int) string {
  57. b := make([]rune, n)
  58. randMarker := rand.New(rand.NewSource(time.Now().UnixNano()))
  59. for i := range b {
  60. b[i] = letters[randMarker.Intn(len(letters))]
  61. }
  62. return string(b)
  63. }
  64. func createFile(fileName, content string, c *C) {
  65. fout, err := os.Create(fileName)
  66. defer fout.Close()
  67. c.Assert(err, IsNil)
  68. _, err = fout.WriteString(content)
  69. c.Assert(err, IsNil)
  70. }
  71. func randLowStr(n int) string {
  72. return strings.ToLower(randStr(n))
  73. }
  74. func forceDeleteBucket(client *Client, bucketName string, c *C) {
  75. bucket, err := client.Bucket(bucketName)
  76. c.Assert(err, IsNil)
  77. // Delete Object
  78. marker := Marker("")
  79. for {
  80. lor, err := bucket.ListObjects(marker)
  81. c.Assert(err, IsNil)
  82. for _, object := range lor.Objects {
  83. err = bucket.DeleteObject(object.Key)
  84. c.Assert(err, IsNil)
  85. }
  86. marker = Marker(lor.NextMarker)
  87. if !lor.IsTruncated {
  88. break
  89. }
  90. }
  91. // Delete Object Versions and DeleteMarks
  92. keyMarker := KeyMarker("")
  93. versionIdMarker := VersionIdMarker("")
  94. options := []Option{keyMarker, versionIdMarker}
  95. for {
  96. lor, err := bucket.ListObjectVersions(options...)
  97. if err != nil {
  98. break
  99. }
  100. for _, object := range lor.ObjectDeleteMarkers {
  101. err = bucket.DeleteObject(object.Key, VersionId(object.VersionId))
  102. c.Assert(err, IsNil)
  103. }
  104. for _, object := range lor.ObjectVersions {
  105. err = bucket.DeleteObject(object.Key, VersionId(object.VersionId))
  106. c.Assert(err, IsNil)
  107. }
  108. keyMarker = KeyMarker(lor.NextKeyMarker)
  109. versionIdMarker := VersionIdMarker(lor.NextVersionIdMarker)
  110. options = []Option{keyMarker, versionIdMarker}
  111. if !lor.IsTruncated {
  112. break
  113. }
  114. }
  115. // Delete Part
  116. keyMarker = KeyMarker("")
  117. uploadIDMarker := UploadIDMarker("")
  118. for {
  119. lmur, err := bucket.ListMultipartUploads(keyMarker, uploadIDMarker)
  120. c.Assert(err, IsNil)
  121. for _, upload := range lmur.Uploads {
  122. var imur = InitiateMultipartUploadResult{Bucket: bucketName,
  123. Key: upload.Key, UploadID: upload.UploadID}
  124. err = bucket.AbortMultipartUpload(imur)
  125. c.Assert(err, IsNil)
  126. }
  127. keyMarker = KeyMarker(lmur.NextKeyMarker)
  128. uploadIDMarker = UploadIDMarker(lmur.NextUploadIDMarker)
  129. if !lmur.IsTruncated {
  130. break
  131. }
  132. }
  133. // delete live channel
  134. strMarker := ""
  135. for {
  136. result, err := bucket.ListLiveChannel(Marker(strMarker))
  137. c.Assert(err, IsNil)
  138. for _, channel := range result.LiveChannel {
  139. err := bucket.DeleteLiveChannel(channel.Name)
  140. c.Assert(err, IsNil)
  141. }
  142. if result.IsTruncated {
  143. strMarker = result.NextMarker
  144. } else {
  145. break
  146. }
  147. }
  148. // Delete Bucket
  149. err = client.DeleteBucket(bucketName)
  150. c.Assert(err, IsNil)
  151. }
  152. // SetUpSuite runs once when the suite starts running
  153. func (s *OssClientSuite) SetUpSuite(c *C) {
  154. client, err := New(endpoint, accessID, accessKey)
  155. c.Assert(err, IsNil)
  156. lbr, err := client.ListBuckets(Prefix(bucketNamePrefix), MaxKeys(1000))
  157. c.Assert(err, IsNil)
  158. for _, bucket := range lbr.Buckets {
  159. forceDeleteBucket(client, bucket.Name, c)
  160. }
  161. testLogger.Println("test client started")
  162. }
  163. // TearDownSuite runs before each test or benchmark starts running
  164. func (s *OssClientSuite) TearDownSuite(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. s.deleteBucket(client, bucket.Name, c)
  171. }
  172. testLogger.Println("test client completed")
  173. }
  174. func (s *OssClientSuite) deleteBucket(client *Client, bucketName string, c *C) {
  175. forceDeleteBucket(client, bucketName, c)
  176. }
  177. // SetUpTest runs after each test or benchmark runs
  178. func (s *OssClientSuite) SetUpTest(c *C) {
  179. }
  180. // TearDownTest runs once after all tests or benchmarks have finished running
  181. func (s *OssClientSuite) TearDownTest(c *C) {
  182. }
  183. // TestCreateBucket
  184. func (s *OssClientSuite) TestCreateBucket(c *C) {
  185. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  186. client, err := New(endpoint, accessID, accessKey)
  187. c.Assert(err, IsNil)
  188. // Create
  189. client.DeleteBucket(bucketNameTest)
  190. err = client.CreateBucket(bucketNameTest)
  191. c.Assert(err, IsNil)
  192. //sleep 3 seconds after create bucket
  193. time.Sleep(timeoutInOperation)
  194. // verify bucket is exist
  195. found, err := client.IsBucketExist(bucketNameTest)
  196. c.Assert(err, IsNil)
  197. c.Assert(found, Equals, true)
  198. res, err := client.GetBucketACL(bucketNameTest)
  199. c.Assert(err, IsNil)
  200. c.Assert(res.ACL, Equals, string(ACLPrivate))
  201. err = client.DeleteBucket(bucketNameTest)
  202. c.Assert(err, IsNil)
  203. time.Sleep(timeoutInOperation)
  204. // CreateBucket creates with ACLPublicRead
  205. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicRead))
  206. c.Assert(err, IsNil)
  207. time.Sleep(timeoutInOperation)
  208. res, err = client.GetBucketACL(bucketNameTest)
  209. c.Assert(err, IsNil)
  210. c.Assert(res.ACL, Equals, string(ACLPublicRead))
  211. err = client.DeleteBucket(bucketNameTest)
  212. c.Assert(err, IsNil)
  213. time.Sleep(timeoutInOperation)
  214. // ACLPublicReadWrite
  215. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicReadWrite))
  216. c.Assert(err, IsNil)
  217. time.Sleep(timeoutInOperation)
  218. res, err = client.GetBucketACL(bucketNameTest)
  219. c.Assert(err, IsNil)
  220. c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
  221. err = client.DeleteBucket(bucketNameTest)
  222. c.Assert(err, IsNil)
  223. time.Sleep(timeoutInOperation)
  224. // ACLPrivate
  225. err = client.CreateBucket(bucketNameTest, ACL(ACLPrivate))
  226. c.Assert(err, IsNil)
  227. time.Sleep(timeoutInOperation)
  228. res, err = client.GetBucketACL(bucketNameTest)
  229. c.Assert(err, IsNil)
  230. c.Assert(res.ACL, Equals, string(ACLPrivate))
  231. // Delete
  232. err = client.DeleteBucket(bucketNameTest)
  233. c.Assert(err, IsNil)
  234. // Create bucket with configuration and test GetBucketInfo
  235. for _, storage := range []StorageClassType{StorageStandard, StorageIA, StorageArchive} {
  236. bucketNameTest := bucketNamePrefix + randLowStr(6)
  237. err = client.CreateBucket(bucketNameTest, StorageClass(storage), ACL(ACLPublicRead))
  238. c.Assert(err, IsNil)
  239. time.Sleep(timeoutInOperation)
  240. res, err := client.GetBucketInfo(bucketNameTest)
  241. c.Assert(err, IsNil)
  242. c.Assert(res.BucketInfo.Name, Equals, bucketNameTest)
  243. c.Assert(res.BucketInfo.StorageClass, Equals, string(storage))
  244. c.Assert(res.BucketInfo.ACL, Equals, string(ACLPublicRead))
  245. // Delete
  246. err = client.DeleteBucket(bucketNameTest)
  247. c.Assert(err, IsNil)
  248. }
  249. // Error put bucket with configuration
  250. err = client.CreateBucket("ERRORBUCKETNAME", StorageClass(StorageArchive))
  251. c.Assert(err, NotNil)
  252. // Create bucket with configuration and test ListBuckets
  253. for _, storage := range []StorageClassType{StorageStandard, StorageIA, StorageArchive} {
  254. bucketNameTest := bucketNamePrefix + randLowStr(6)
  255. err = client.CreateBucket(bucketNameTest, StorageClass(storage))
  256. c.Assert(err, IsNil)
  257. time.Sleep(timeoutInOperation)
  258. res, err := client.GetBucketInfo(bucketNameTest)
  259. c.Assert(err, IsNil)
  260. c.Assert(res.BucketInfo.Name, Equals, bucketNameTest)
  261. c.Assert(res.BucketInfo.StorageClass, Equals, string(storage))
  262. // Delete
  263. err = client.DeleteBucket(bucketNameTest)
  264. c.Assert(err, IsNil)
  265. }
  266. }
  267. // TestCreateBucketNegative
  268. func (s *OssClientSuite) TestCreateBucketNegative(c *C) {
  269. client, err := New(endpoint, accessID, accessKey)
  270. c.Assert(err, IsNil)
  271. // Bucket name invalid
  272. err = client.CreateBucket("xx")
  273. c.Assert(err, NotNil)
  274. err = client.CreateBucket("XXXX")
  275. c.Assert(err, NotNil)
  276. testLogger.Println(err)
  277. err = client.CreateBucket("_bucket")
  278. c.Assert(err, NotNil)
  279. testLogger.Println(err)
  280. // ACL invalid
  281. err = client.CreateBucket(bucketNamePrefix+randLowStr(6), ACL("InvaldAcl"))
  282. c.Assert(err, NotNil)
  283. testLogger.Println(err)
  284. }
  285. // TestDeleteBucket
  286. func (s *OssClientSuite) TestDeleteBucket(c *C) {
  287. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  288. client, err := New(endpoint, accessID, accessKey)
  289. c.Assert(err, IsNil)
  290. // Create
  291. err = client.CreateBucket(bucketNameTest)
  292. c.Assert(err, IsNil)
  293. time.Sleep(timeoutInOperation)
  294. // Check
  295. found, err := client.IsBucketExist(bucketNameTest)
  296. c.Assert(err, IsNil)
  297. c.Assert(found, Equals, true)
  298. // Delete
  299. err = client.DeleteBucket(bucketNameTest)
  300. c.Assert(err, IsNil)
  301. time.Sleep(timeoutInOperation)
  302. // Check
  303. found, err = client.IsBucketExist(bucketNameTest)
  304. c.Assert(err, IsNil)
  305. c.Assert(found, Equals, false)
  306. err = client.DeleteBucket(bucketNameTest)
  307. c.Assert(err, NotNil)
  308. }
  309. // TestDeleteBucketNegative
  310. func (s *OssClientSuite) TestDeleteBucketNegative(c *C) {
  311. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  312. client, err := New(endpoint, accessID, accessKey)
  313. c.Assert(err, IsNil)
  314. // Bucket name invalid
  315. err = client.DeleteBucket("xx")
  316. c.Assert(err, NotNil)
  317. err = client.DeleteBucket("XXXX")
  318. c.Assert(err, NotNil)
  319. err = client.DeleteBucket("_bucket")
  320. c.Assert(err, NotNil)
  321. // Delete no exist bucket
  322. err = client.DeleteBucket("notexist")
  323. c.Assert(err, NotNil)
  324. // No permission to delete, this ak/sk for js sdk
  325. err = client.CreateBucket(bucketNameTest)
  326. c.Assert(err, IsNil)
  327. accessID := "<accessKeyId>"
  328. accessKey := "<accessKeySecret>"
  329. clientOtherUser, err := New(endpoint, accessID, accessKey)
  330. c.Assert(err, IsNil)
  331. err = clientOtherUser.DeleteBucket(bucketNameTest)
  332. c.Assert(err, NotNil)
  333. err = client.DeleteBucket(bucketNameTest)
  334. c.Assert(err, IsNil)
  335. }
  336. // TestListBucket
  337. func (s *OssClientSuite) TestListBucket(c *C) {
  338. var prefix = bucketNamePrefix + randLowStr(6)
  339. var bucketNameLbOne = prefix + "tlb1"
  340. var bucketNameLbTwo = prefix + "tlb2"
  341. var bucketNameLbThree = prefix + "tlb3"
  342. client, err := New(endpoint, accessID, accessKey)
  343. c.Assert(err, IsNil)
  344. // CreateBucket
  345. err = client.CreateBucket(bucketNameLbOne)
  346. c.Assert(err, IsNil)
  347. err = client.CreateBucket(bucketNameLbTwo)
  348. c.Assert(err, IsNil)
  349. err = client.CreateBucket(bucketNameLbThree)
  350. c.Assert(err, IsNil)
  351. // ListBuckets, specified prefix
  352. var respHeader http.Header
  353. lbr, err := client.ListBuckets(Prefix(prefix), MaxKeys(2), GetResponseHeader(&respHeader))
  354. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  355. c.Assert(err, IsNil)
  356. c.Assert(len(lbr.Buckets), Equals, 2)
  357. // ListBuckets, specified max keys
  358. lbr, err = client.ListBuckets(MaxKeys(2))
  359. c.Assert(err, IsNil)
  360. c.Assert(len(lbr.Buckets), Equals, 2)
  361. // ListBuckets, specified max keys
  362. lbr, err = client.ListBuckets(Marker(bucketNameLbOne), MaxKeys(1))
  363. c.Assert(err, IsNil)
  364. c.Assert(len(lbr.Buckets), Equals, 1)
  365. // ListBuckets, specified max keys
  366. lbr, err = client.ListBuckets(Marker(bucketNameLbOne))
  367. c.Assert(err, IsNil)
  368. c.Assert(len(lbr.Buckets) >= 2, Equals, true)
  369. // DeleteBucket
  370. err = client.DeleteBucket(bucketNameLbOne)
  371. c.Assert(err, IsNil)
  372. err = client.DeleteBucket(bucketNameLbTwo)
  373. c.Assert(err, IsNil)
  374. err = client.DeleteBucket(bucketNameLbThree)
  375. c.Assert(err, IsNil)
  376. }
  377. // TestListBucket
  378. func (s *OssClientSuite) TestIsBucketExist(c *C) {
  379. var prefix = bucketNamePrefix + randLowStr(6)
  380. var bucketNameLbOne = prefix + "tibe1"
  381. var bucketNameLbTwo = prefix + "tibe11"
  382. var bucketNameLbThree = prefix + "tibe111"
  383. client, err := New(endpoint, accessID, accessKey)
  384. c.Assert(err, IsNil)
  385. // CreateBucket
  386. err = client.CreateBucket(bucketNameLbOne)
  387. c.Assert(err, IsNil)
  388. err = client.CreateBucket(bucketNameLbTwo)
  389. c.Assert(err, IsNil)
  390. err = client.CreateBucket(bucketNameLbThree)
  391. c.Assert(err, IsNil)
  392. // Exist
  393. exist, err := client.IsBucketExist(bucketNameLbTwo)
  394. c.Assert(err, IsNil)
  395. c.Assert(exist, Equals, true)
  396. exist, err = client.IsBucketExist(bucketNameLbThree)
  397. c.Assert(err, IsNil)
  398. c.Assert(exist, Equals, true)
  399. exist, err = client.IsBucketExist(bucketNameLbOne)
  400. c.Assert(err, IsNil)
  401. c.Assert(exist, Equals, true)
  402. // Not exist
  403. exist, err = client.IsBucketExist(prefix + "tibe")
  404. c.Assert(err, IsNil)
  405. c.Assert(exist, Equals, false)
  406. exist, err = client.IsBucketExist(prefix + "tibe1111")
  407. c.Assert(err, IsNil)
  408. c.Assert(exist, Equals, false)
  409. // Negative
  410. exist, err = client.IsBucketExist("BucketNameInvalid")
  411. c.Assert(err, NotNil)
  412. // DeleteBucket
  413. err = client.DeleteBucket(bucketNameLbOne)
  414. c.Assert(err, IsNil)
  415. err = client.DeleteBucket(bucketNameLbTwo)
  416. c.Assert(err, IsNil)
  417. err = client.DeleteBucket(bucketNameLbThree)
  418. c.Assert(err, IsNil)
  419. }
  420. // TestSetBucketAcl
  421. func (s *OssClientSuite) TestSetBucketAcl(c *C) {
  422. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  423. client, err := New(endpoint, accessID, accessKey)
  424. c.Assert(err, IsNil)
  425. // Private
  426. err = client.CreateBucket(bucketNameTest)
  427. c.Assert(err, IsNil)
  428. res, err := client.GetBucketACL(bucketNameTest)
  429. c.Assert(err, IsNil)
  430. c.Assert(res.ACL, Equals, string(ACLPrivate))
  431. // Set ACL_PUBLIC_R
  432. err = client.SetBucketACL(bucketNameTest, ACLPublicRead)
  433. c.Assert(err, IsNil)
  434. time.Sleep(timeoutInOperation)
  435. res, err = client.GetBucketACL(bucketNameTest)
  436. c.Assert(err, IsNil)
  437. c.Assert(res.ACL, Equals, string(ACLPublicRead))
  438. // Set ACL_PUBLIC_RW
  439. err = client.SetBucketACL(bucketNameTest, ACLPublicReadWrite)
  440. c.Assert(err, IsNil)
  441. time.Sleep(timeoutInOperation)
  442. res, err = client.GetBucketACL(bucketNameTest)
  443. c.Assert(err, IsNil)
  444. c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
  445. // Set ACL_PUBLIC_RW
  446. err = client.SetBucketACL(bucketNameTest, ACLPrivate)
  447. c.Assert(err, IsNil)
  448. time.Sleep(timeoutInOperation)
  449. res, err = client.GetBucketACL(bucketNameTest)
  450. c.Assert(err, IsNil)
  451. c.Assert(res.ACL, Equals, string(ACLPrivate))
  452. err = client.DeleteBucket(bucketNameTest)
  453. c.Assert(err, IsNil)
  454. }
  455. // TestSetBucketAclNegative
  456. func (s *OssClientSuite) TestBucketAclNegative(c *C) {
  457. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  458. client, err := New(endpoint, accessID, accessKey)
  459. c.Assert(err, IsNil)
  460. err = client.CreateBucket(bucketNameTest)
  461. c.Assert(err, IsNil)
  462. err = client.SetBucketACL(bucketNameTest, "InvalidACL")
  463. c.Assert(err, NotNil)
  464. testLogger.Println(err)
  465. err = client.DeleteBucket(bucketNameTest)
  466. c.Assert(err, IsNil)
  467. }
  468. // TestGetBucketAcl
  469. func (s *OssClientSuite) TestGetBucketAcl(c *C) {
  470. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  471. client, err := New(endpoint, accessID, accessKey)
  472. c.Assert(err, IsNil)
  473. // Private
  474. err = client.CreateBucket(bucketNameTest)
  475. c.Assert(err, IsNil)
  476. time.Sleep(timeoutInOperation)
  477. res, err := client.GetBucketACL(bucketNameTest)
  478. c.Assert(err, IsNil)
  479. c.Assert(res.ACL, Equals, string(ACLPrivate))
  480. err = client.DeleteBucket(bucketNameTest)
  481. c.Assert(err, IsNil)
  482. time.Sleep(timeoutInOperation)
  483. // PublicRead
  484. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicRead))
  485. c.Assert(err, IsNil)
  486. time.Sleep(timeoutInOperation)
  487. res, err = client.GetBucketACL(bucketNameTest)
  488. c.Assert(err, IsNil)
  489. c.Assert(res.ACL, Equals, string(ACLPublicRead))
  490. err = client.DeleteBucket(bucketNameTest)
  491. c.Assert(err, IsNil)
  492. time.Sleep(timeoutInOperation)
  493. // PublicReadWrite
  494. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicReadWrite))
  495. c.Assert(err, IsNil)
  496. time.Sleep(timeoutInOperation)
  497. res, err = client.GetBucketACL(bucketNameTest)
  498. c.Assert(err, IsNil)
  499. c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
  500. err = client.DeleteBucket(bucketNameTest)
  501. c.Assert(err, IsNil)
  502. }
  503. // TestGetBucketAcl
  504. func (s *OssClientSuite) TestGetBucketLocation(c *C) {
  505. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  506. client, err := New(endpoint, accessID, accessKey)
  507. c.Assert(err, IsNil)
  508. // Private
  509. err = client.CreateBucket(bucketNameTest)
  510. c.Assert(err, IsNil)
  511. loc, err := client.GetBucketLocation(bucketNameTest)
  512. c.Assert(strings.HasPrefix(loc, "oss-"), Equals, true)
  513. err = client.DeleteBucket(bucketNameTest)
  514. c.Assert(err, IsNil)
  515. }
  516. // TestGetBucketLocationNegative
  517. func (s *OssClientSuite) TestGetBucketLocationNegative(c *C) {
  518. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  519. client, err := New(endpoint, accessID, accessKey)
  520. c.Assert(err, IsNil)
  521. // Not exist
  522. _, err = client.GetBucketLocation(bucketNameTest)
  523. c.Assert(err, NotNil)
  524. // Not exist
  525. _, err = client.GetBucketLocation("InvalidBucketName_")
  526. c.Assert(err, NotNil)
  527. }
  528. // TestSetBucketLifecycle
  529. func (s *OssClientSuite) TestSetBucketLifecycle(c *C) {
  530. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  531. var rule1 = BuildLifecycleRuleByDate("rule1", "one", true, 2015, 11, 11)
  532. var rule2 = BuildLifecycleRuleByDays("rule2", "two", true, 3)
  533. client, err := New(endpoint, accessID, accessKey)
  534. c.Assert(err, IsNil)
  535. err = client.CreateBucket(bucketNameTest)
  536. c.Assert(err, IsNil)
  537. // Set single rule
  538. var rules = []LifecycleRule{rule1}
  539. err = client.SetBucketLifecycle(bucketNameTest, rules)
  540. c.Assert(err, IsNil)
  541. // Double set rule
  542. err = client.SetBucketLifecycle(bucketNameTest, rules)
  543. c.Assert(err, IsNil)
  544. res, err := client.GetBucketLifecycle(bucketNameTest)
  545. c.Assert(err, IsNil)
  546. c.Assert(len(res.Rules), Equals, 1)
  547. c.Assert(res.Rules[0].ID, Equals, "rule1")
  548. err = client.DeleteBucketLifecycle(bucketNameTest)
  549. c.Assert(err, IsNil)
  550. // Set two rules
  551. rules = []LifecycleRule{rule1, rule2}
  552. err = client.SetBucketLifecycle(bucketNameTest, rules)
  553. c.Assert(err, IsNil)
  554. // Eliminate effect of cache
  555. time.Sleep(timeoutInOperation)
  556. res, err = client.GetBucketLifecycle(bucketNameTest)
  557. c.Assert(err, IsNil)
  558. c.Assert(len(res.Rules), Equals, 2)
  559. c.Assert(res.Rules[0].ID, Equals, "rule1")
  560. c.Assert(res.Rules[1].ID, Equals, "rule2")
  561. err = client.DeleteBucket(bucketNameTest)
  562. c.Assert(err, IsNil)
  563. }
  564. // TestSetBucketLifecycleNew
  565. func (s *OssClientSuite) TestSetBucketLifecycleNew(c *C) {
  566. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  567. client, err := New(endpoint, accessID, accessKey)
  568. c.Assert(err, IsNil)
  569. err = client.CreateBucket(bucketNameTest)
  570. c.Assert(err, IsNil)
  571. //invalid status of lifecyclerule
  572. expiration := LifecycleExpiration{
  573. Days: 30,
  574. }
  575. rule := LifecycleRule{
  576. ID: "rule1",
  577. Prefix: "one",
  578. Status: "Invalid",
  579. Expiration: &expiration,
  580. }
  581. rules := []LifecycleRule{rule}
  582. err = client.SetBucketLifecycle(bucketNameTest, rules)
  583. c.Assert(err, NotNil)
  584. //invalid value of CreatedBeforeDate
  585. expiration = LifecycleExpiration{
  586. CreatedBeforeDate: randStr(10),
  587. }
  588. rule = LifecycleRule{
  589. ID: "rule1",
  590. Prefix: "one",
  591. Status: "Enabled",
  592. Expiration: &expiration,
  593. }
  594. rules = []LifecycleRule{rule}
  595. err = client.SetBucketLifecycle(bucketNameTest, rules)
  596. c.Assert(err, NotNil)
  597. //invalid value of Days
  598. abortMPU := LifecycleAbortMultipartUpload{
  599. Days: -30,
  600. }
  601. rule = LifecycleRule{
  602. ID: "rule1",
  603. Prefix: "one",
  604. Status: "Enabled",
  605. AbortMultipartUpload: &abortMPU,
  606. }
  607. rules = []LifecycleRule{rule}
  608. err = client.SetBucketLifecycle(bucketNameTest, rules)
  609. c.Assert(err, NotNil)
  610. expiration = LifecycleExpiration{
  611. CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
  612. }
  613. rule1 := LifecycleRule{
  614. ID: "rule1",
  615. Prefix: "one",
  616. Status: "Enabled",
  617. Expiration: &expiration,
  618. }
  619. abortMPU = LifecycleAbortMultipartUpload{
  620. Days: 30,
  621. }
  622. rule2 := LifecycleRule{
  623. ID: "rule2",
  624. Prefix: "two",
  625. Status: "Enabled",
  626. Expiration: &expiration,
  627. AbortMultipartUpload: &abortMPU,
  628. }
  629. transition1 := LifecycleTransition{
  630. Days: 3,
  631. StorageClass: StorageIA,
  632. }
  633. transition2 := LifecycleTransition{
  634. Days: 30,
  635. StorageClass: StorageArchive,
  636. }
  637. transitions := []LifecycleTransition{transition1, transition2}
  638. rule3 := LifecycleRule{
  639. ID: "rule3",
  640. Prefix: "three",
  641. Status: "Enabled",
  642. AbortMultipartUpload: &abortMPU,
  643. Transitions: transitions,
  644. }
  645. // Set single rule
  646. rules = []LifecycleRule{rule1}
  647. err = client.SetBucketLifecycle(bucketNameTest, rules)
  648. c.Assert(err, IsNil)
  649. res, err := client.GetBucketLifecycle(bucketNameTest)
  650. c.Assert(err, IsNil)
  651. c.Assert(len(res.Rules), Equals, 1)
  652. c.Assert(res.Rules[0].ID, Equals, "rule1")
  653. c.Assert(res.Rules[0].Expiration, NotNil)
  654. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  655. err = client.DeleteBucketLifecycle(bucketNameTest)
  656. c.Assert(err, IsNil)
  657. // Set two rule: rule1 and rule2
  658. rules = []LifecycleRule{rule1, rule2}
  659. err = client.SetBucketLifecycle(bucketNameTest, rules)
  660. c.Assert(err, IsNil)
  661. res, err = client.GetBucketLifecycle(bucketNameTest)
  662. c.Assert(err, IsNil)
  663. c.Assert(len(res.Rules), Equals, 2)
  664. c.Assert(res.Rules[0].ID, Equals, "rule1")
  665. c.Assert(res.Rules[0].Expiration, NotNil)
  666. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  667. c.Assert(res.Rules[1].ID, Equals, "rule2")
  668. c.Assert(res.Rules[1].Expiration, NotNil)
  669. c.Assert(res.Rules[1].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  670. c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
  671. c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
  672. err = client.DeleteBucketLifecycle(bucketNameTest)
  673. c.Assert(err, IsNil)
  674. // Set two rule: rule2 and rule3
  675. rules = []LifecycleRule{rule2, rule3}
  676. err = client.SetBucketLifecycle(bucketNameTest, rules)
  677. c.Assert(err, IsNil)
  678. res, err = client.GetBucketLifecycle(bucketNameTest)
  679. c.Assert(err, IsNil)
  680. c.Assert(len(res.Rules), Equals, 2)
  681. c.Assert(res.Rules[0].ID, Equals, "rule2")
  682. c.Assert(res.Rules[0].Expiration, NotNil)
  683. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  684. c.Assert(res.Rules[0].AbortMultipartUpload, NotNil)
  685. c.Assert(res.Rules[0].AbortMultipartUpload.Days, Equals, 30)
  686. c.Assert(res.Rules[1].ID, Equals, "rule3")
  687. c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
  688. c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
  689. c.Assert(len(res.Rules[1].Transitions), Equals, 2)
  690. c.Assert(res.Rules[1].Transitions[0].StorageClass, Equals, StorageIA)
  691. c.Assert(res.Rules[1].Transitions[0].Days, Equals, 3)
  692. c.Assert(res.Rules[1].Transitions[1].StorageClass, Equals, StorageArchive)
  693. c.Assert(res.Rules[1].Transitions[1].Days, Equals, 30)
  694. err = client.DeleteBucketLifecycle(bucketNameTest)
  695. c.Assert(err, IsNil)
  696. // Set two rule: rule1 and rule3
  697. rules = []LifecycleRule{rule1, rule3}
  698. err = client.SetBucketLifecycle(bucketNameTest, rules)
  699. c.Assert(err, IsNil)
  700. res, err = client.GetBucketLifecycle(bucketNameTest)
  701. c.Assert(err, IsNil)
  702. c.Assert(len(res.Rules), Equals, 2)
  703. c.Assert(res.Rules[0].ID, Equals, "rule1")
  704. c.Assert(res.Rules[0].Expiration, NotNil)
  705. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  706. c.Assert(res.Rules[1].ID, Equals, "rule3")
  707. c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
  708. c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
  709. c.Assert(len(res.Rules[1].Transitions), Equals, 2)
  710. c.Assert(res.Rules[1].Transitions[0].StorageClass, Equals, StorageIA)
  711. c.Assert(res.Rules[1].Transitions[0].Days, Equals, 3)
  712. c.Assert(res.Rules[1].Transitions[1].StorageClass, Equals, StorageArchive)
  713. c.Assert(res.Rules[1].Transitions[1].Days, Equals, 30)
  714. err = client.DeleteBucketLifecycle(bucketNameTest)
  715. c.Assert(err, IsNil)
  716. // Set three rules
  717. rules = []LifecycleRule{rule1, rule2, rule3}
  718. err = client.SetBucketLifecycle(bucketNameTest, rules)
  719. c.Assert(err, IsNil)
  720. res, err = client.GetBucketLifecycle(bucketNameTest)
  721. c.Assert(err, IsNil)
  722. c.Assert(len(res.Rules), Equals, 3)
  723. c.Assert(res.Rules[0].ID, Equals, "rule1")
  724. c.Assert(res.Rules[0].Expiration, NotNil)
  725. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  726. c.Assert(res.Rules[1].ID, Equals, "rule2")
  727. c.Assert(res.Rules[1].Expiration, NotNil)
  728. c.Assert(res.Rules[1].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  729. c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
  730. c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
  731. c.Assert(res.Rules[2].ID, Equals, "rule3")
  732. c.Assert(res.Rules[2].AbortMultipartUpload, NotNil)
  733. c.Assert(res.Rules[2].AbortMultipartUpload.Days, Equals, 30)
  734. c.Assert(len(res.Rules[2].Transitions), Equals, 2)
  735. c.Assert(res.Rules[2].Transitions[0].StorageClass, Equals, StorageIA)
  736. c.Assert(res.Rules[2].Transitions[0].Days, Equals, 3)
  737. c.Assert(res.Rules[2].Transitions[1].StorageClass, Equals, StorageArchive)
  738. c.Assert(res.Rules[2].Transitions[1].Days, Equals, 30)
  739. err = client.DeleteBucket(bucketNameTest)
  740. c.Assert(err, IsNil)
  741. }
  742. // TestDeleteBucketLifecycle
  743. func (s *OssClientSuite) TestDeleteBucketLifecycle(c *C) {
  744. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  745. var rule1 = BuildLifecycleRuleByDate("rule1", "one", true, 2015, 11, 11)
  746. var rule2 = BuildLifecycleRuleByDays("rule2", "two", true, 3)
  747. var rules = []LifecycleRule{rule1, rule2}
  748. client, err := New(endpoint, accessID, accessKey)
  749. c.Assert(err, IsNil)
  750. err = client.CreateBucket(bucketNameTest)
  751. c.Assert(err, IsNil)
  752. //time.Sleep(timeoutInOperation)
  753. err = client.DeleteBucketLifecycle(bucketNameTest)
  754. c.Assert(err, IsNil)
  755. err = client.SetBucketLifecycle(bucketNameTest, rules)
  756. c.Assert(err, IsNil)
  757. //time.Sleep(timeoutInOperation)
  758. res, err := client.GetBucketLifecycle(bucketNameTest)
  759. c.Assert(err, IsNil)
  760. c.Assert(len(res.Rules), Equals, 2)
  761. // Delete
  762. err = client.DeleteBucketLifecycle(bucketNameTest)
  763. c.Assert(err, IsNil)
  764. //time.Sleep(timeoutInOperation)
  765. res, err = client.GetBucketLifecycle(bucketNameTest)
  766. c.Assert(err, NotNil)
  767. // Eliminate effect of cache
  768. //time.Sleep(timeoutInOperation)
  769. // Delete when not set
  770. err = client.DeleteBucketLifecycle(bucketNameTest)
  771. c.Assert(err, IsNil)
  772. err = client.DeleteBucket(bucketNameTest)
  773. c.Assert(err, IsNil)
  774. }
  775. // TestSetBucketLifecycleNegative
  776. func (s *OssClientSuite) TestBucketLifecycleNegative(c *C) {
  777. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  778. var rules = []LifecycleRule{}
  779. client, err := New(endpoint, accessID, accessKey)
  780. c.Assert(err, IsNil)
  781. err = client.CreateBucket(bucketNameTest)
  782. c.Assert(err, IsNil)
  783. // Set with no rule
  784. err = client.SetBucketLifecycle(bucketNameTest, rules)
  785. c.Assert(err, NotNil)
  786. err = client.DeleteBucket(bucketNameTest)
  787. c.Assert(err, IsNil)
  788. // Not exist
  789. err = client.SetBucketLifecycle(bucketNameTest, rules)
  790. c.Assert(err, NotNil)
  791. // Not exist
  792. _, err = client.GetBucketLifecycle(bucketNameTest)
  793. c.Assert(err, NotNil)
  794. // Not exist
  795. err = client.DeleteBucketLifecycle(bucketNameTest)
  796. c.Assert(err, NotNil)
  797. }
  798. // TestSetBucketReferer
  799. func (s *OssClientSuite) TestSetBucketReferer(c *C) {
  800. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  801. var referers = []string{"http://www.aliyun.com", "https://www.aliyun.com"}
  802. client, err := New(endpoint, accessID, accessKey)
  803. c.Assert(err, IsNil)
  804. err = client.CreateBucket(bucketNameTest)
  805. c.Assert(err, IsNil)
  806. time.Sleep(timeoutInOperation)
  807. res, err := client.GetBucketReferer(bucketNameTest)
  808. c.Assert(res.AllowEmptyReferer, Equals, true)
  809. c.Assert(len(res.RefererList), Equals, 0)
  810. // Set referers
  811. err = client.SetBucketReferer(bucketNameTest, referers, false)
  812. c.Assert(err, IsNil)
  813. time.Sleep(timeoutInOperation)
  814. res, err = client.GetBucketReferer(bucketNameTest)
  815. c.Assert(res.AllowEmptyReferer, Equals, false)
  816. c.Assert(len(res.RefererList), Equals, 2)
  817. c.Assert(res.RefererList[0], Equals, "http://www.aliyun.com")
  818. c.Assert(res.RefererList[1], Equals, "https://www.aliyun.com")
  819. // Reset referer, referers empty
  820. referers = []string{""}
  821. err = client.SetBucketReferer(bucketNameTest, referers, true)
  822. c.Assert(err, IsNil)
  823. referers = []string{}
  824. err = client.SetBucketReferer(bucketNameTest, referers, true)
  825. c.Assert(err, IsNil)
  826. res, err = client.GetBucketReferer(bucketNameTest)
  827. c.Assert(res.AllowEmptyReferer, Equals, true)
  828. c.Assert(len(res.RefererList), Equals, 0)
  829. err = client.DeleteBucket(bucketNameTest)
  830. c.Assert(err, IsNil)
  831. }
  832. // TestSetBucketRefererNegative
  833. func (s *OssClientSuite) TestBucketRefererNegative(c *C) {
  834. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  835. var referers = []string{""}
  836. client, err := New(endpoint, accessID, accessKey)
  837. c.Assert(err, IsNil)
  838. // Not exist
  839. _, err = client.GetBucketReferer(bucketNameTest)
  840. c.Assert(err, NotNil)
  841. testLogger.Println(err)
  842. // Not exist
  843. err = client.SetBucketReferer(bucketNameTest, referers, true)
  844. c.Assert(err, NotNil)
  845. testLogger.Println(err)
  846. }
  847. // TestSetBucketLogging
  848. func (s *OssClientSuite) TestSetBucketLogging(c *C) {
  849. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  850. var bucketNameTarget = bucketNameTest + "-target"
  851. client, err := New(endpoint, accessID, accessKey)
  852. c.Assert(err, IsNil)
  853. err = client.CreateBucket(bucketNameTest)
  854. c.Assert(err, IsNil)
  855. err = client.CreateBucket(bucketNameTarget)
  856. c.Assert(err, IsNil)
  857. time.Sleep(timeoutInOperation)
  858. // Set logging
  859. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
  860. c.Assert(err, IsNil)
  861. // Reset
  862. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", false)
  863. c.Assert(err, IsNil)
  864. time.Sleep(timeoutInOperation)
  865. res, err := client.GetBucketLogging(bucketNameTest)
  866. c.Assert(err, IsNil)
  867. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  868. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  869. err = client.DeleteBucketLogging(bucketNameTest)
  870. c.Assert(err, IsNil)
  871. // Set to self
  872. err = client.SetBucketLogging(bucketNameTest, bucketNameTest, "prefix", true)
  873. c.Assert(err, IsNil)
  874. err = client.DeleteBucket(bucketNameTest)
  875. c.Assert(err, IsNil)
  876. err = client.DeleteBucket(bucketNameTarget)
  877. c.Assert(err, IsNil)
  878. }
  879. // TestDeleteBucketLogging
  880. func (s *OssClientSuite) TestDeleteBucketLogging(c *C) {
  881. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  882. var bucketNameTarget = bucketNameTest + "-target"
  883. client, err := New(endpoint, accessID, accessKey)
  884. c.Assert(err, IsNil)
  885. err = client.CreateBucket(bucketNameTest)
  886. c.Assert(err, IsNil)
  887. err = client.CreateBucket(bucketNameTarget)
  888. c.Assert(err, IsNil)
  889. time.Sleep(timeoutInOperation)
  890. // Get when not set
  891. res, err := client.GetBucketLogging(bucketNameTest)
  892. c.Assert(err, IsNil)
  893. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  894. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  895. // Set
  896. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
  897. c.Assert(err, IsNil)
  898. // Get
  899. time.Sleep(timeoutInOperation)
  900. res, err = client.GetBucketLogging(bucketNameTest)
  901. c.Assert(err, IsNil)
  902. c.Assert(res.LoggingEnabled.TargetBucket, Equals, bucketNameTarget)
  903. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "prefix")
  904. // Set
  905. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", false)
  906. c.Assert(err, IsNil)
  907. // Get
  908. time.Sleep(timeoutInOperation)
  909. res, err = client.GetBucketLogging(bucketNameTest)
  910. c.Assert(err, IsNil)
  911. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  912. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  913. // Delete
  914. err = client.DeleteBucketLogging(bucketNameTest)
  915. c.Assert(err, IsNil)
  916. // Get after delete
  917. time.Sleep(timeoutInOperation)
  918. res, err = client.GetBucketLogging(bucketNameTest)
  919. c.Assert(err, IsNil)
  920. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  921. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  922. err = client.DeleteBucket(bucketNameTest)
  923. c.Assert(err, IsNil)
  924. err = client.DeleteBucket(bucketNameTarget)
  925. c.Assert(err, IsNil)
  926. }
  927. // TestSetBucketLoggingNegative
  928. func (s *OssClientSuite) TestSetBucketLoggingNegative(c *C) {
  929. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  930. var bucketNameTarget = bucketNameTest + "-target"
  931. client, err := New(endpoint, accessID, accessKey)
  932. c.Assert(err, IsNil)
  933. // Not exist
  934. _, err = client.GetBucketLogging(bucketNameTest)
  935. c.Assert(err, NotNil)
  936. // Not exist
  937. err = client.SetBucketLogging(bucketNameTest, "targetbucket", "prefix", true)
  938. c.Assert(err, NotNil)
  939. // Not exist
  940. err = client.DeleteBucketLogging(bucketNameTest)
  941. c.Assert(err, NotNil)
  942. err = client.CreateBucket(bucketNameTest)
  943. c.Assert(err, IsNil)
  944. time.Sleep(timeoutInOperation)
  945. // Target bucket not exist
  946. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
  947. c.Assert(err, NotNil)
  948. // Parameter invalid
  949. err = client.SetBucketLogging(bucketNameTest, "XXXX", "prefix", true)
  950. c.Assert(err, NotNil)
  951. err = client.SetBucketLogging(bucketNameTest, "xx", "prefix", true)
  952. c.Assert(err, NotNil)
  953. err = client.DeleteBucket(bucketNameTest)
  954. c.Assert(err, IsNil)
  955. }
  956. // TestSetBucketWebsite
  957. func (s *OssClientSuite) TestSetBucketWebsite(c *C) {
  958. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  959. var indexWebsite = "myindex.html"
  960. var errorWebsite = "myerror.html"
  961. client, err := New(endpoint, accessID, accessKey)
  962. c.Assert(err, IsNil)
  963. time.Sleep(timeoutInOperation)
  964. err = client.CreateBucket(bucketNameTest)
  965. c.Assert(err, IsNil)
  966. // Set
  967. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  968. c.Assert(err, IsNil)
  969. // Double set
  970. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  971. c.Assert(err, IsNil)
  972. res, err := client.GetBucketWebsite(bucketNameTest)
  973. c.Assert(err, IsNil)
  974. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  975. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  976. // Reset
  977. err = client.SetBucketWebsite(bucketNameTest, "your"+indexWebsite, "your"+errorWebsite)
  978. c.Assert(err, IsNil)
  979. time.Sleep(timeoutInOperation)
  980. res, err = client.GetBucketWebsite(bucketNameTest)
  981. c.Assert(err, IsNil)
  982. c.Assert(res.IndexDocument.Suffix, Equals, "your"+indexWebsite)
  983. c.Assert(res.ErrorDocument.Key, Equals, "your"+errorWebsite)
  984. err = client.DeleteBucketWebsite(bucketNameTest)
  985. c.Assert(err, IsNil)
  986. // Set after delete
  987. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  988. c.Assert(err, IsNil)
  989. // Eliminate effect of cache
  990. time.Sleep(timeoutInOperation)
  991. res, err = client.GetBucketWebsite(bucketNameTest)
  992. c.Assert(err, IsNil)
  993. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  994. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  995. err = client.DeleteBucket(bucketNameTest)
  996. c.Assert(err, IsNil)
  997. }
  998. // TestDeleteBucketWebsite
  999. func (s *OssClientSuite) TestDeleteBucketWebsite(c *C) {
  1000. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1001. var indexWebsite = "myindex.html"
  1002. var errorWebsite = "myerror.html"
  1003. client, err := New(endpoint, accessID, accessKey)
  1004. c.Assert(err, IsNil)
  1005. err = client.CreateBucket(bucketNameTest)
  1006. c.Assert(err, IsNil)
  1007. time.Sleep(timeoutInOperation)
  1008. // Get
  1009. res, err := client.GetBucketWebsite(bucketNameTest)
  1010. c.Assert(err, NotNil)
  1011. // Detele without set
  1012. err = client.DeleteBucketWebsite(bucketNameTest)
  1013. c.Assert(err, IsNil)
  1014. // Set
  1015. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  1016. c.Assert(err, IsNil)
  1017. time.Sleep(timeoutInOperation)
  1018. res, err = client.GetBucketWebsite(bucketNameTest)
  1019. c.Assert(err, IsNil)
  1020. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  1021. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  1022. // Detele
  1023. time.Sleep(timeoutInOperation)
  1024. err = client.DeleteBucketWebsite(bucketNameTest)
  1025. c.Assert(err, IsNil)
  1026. time.Sleep(timeoutInOperation)
  1027. res, err = client.GetBucketWebsite(bucketNameTest)
  1028. c.Assert(err, NotNil)
  1029. // Detele after delete
  1030. err = client.DeleteBucketWebsite(bucketNameTest)
  1031. c.Assert(err, IsNil)
  1032. err = client.DeleteBucket(bucketNameTest)
  1033. c.Assert(err, IsNil)
  1034. }
  1035. // TestSetBucketWebsiteNegative
  1036. func (s *OssClientSuite) TestSetBucketWebsiteNegative(c *C) {
  1037. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1038. var indexWebsite = "myindex.html"
  1039. var errorWebsite = "myerror.html"
  1040. client, err := New(endpoint, accessID, accessKey)
  1041. c.Assert(err, IsNil)
  1042. err = client.DeleteBucket(bucketNameTest)
  1043. // Not exist
  1044. _, err = client.GetBucketWebsite(bucketNameTest)
  1045. c.Assert(err, NotNil)
  1046. err = client.DeleteBucketWebsite(bucketNameTest)
  1047. c.Assert(err, NotNil)
  1048. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  1049. c.Assert(err, NotNil)
  1050. err = client.CreateBucket(bucketNameTest)
  1051. c.Assert(err, IsNil)
  1052. // Set
  1053. time.Sleep(timeoutInOperation)
  1054. err = client.SetBucketWebsite(bucketNameTest, "myindex", "myerror")
  1055. c.Assert(err, IsNil)
  1056. res, err := client.GetBucketWebsite(bucketNameTest)
  1057. c.Assert(err, IsNil)
  1058. c.Assert(res.IndexDocument.Suffix, Equals, "myindex")
  1059. c.Assert(res.ErrorDocument.Key, Equals, "myerror")
  1060. // Detele
  1061. err = client.DeleteBucketWebsite(bucketNameTest)
  1062. c.Assert(err, IsNil)
  1063. time.Sleep(timeoutInOperation)
  1064. _, err = client.GetBucketWebsite(bucketNameTest)
  1065. c.Assert(err, NotNil)
  1066. // Detele after delete
  1067. err = client.DeleteBucketWebsite(bucketNameTest)
  1068. c.Assert(err, IsNil)
  1069. err = client.DeleteBucket(bucketNameTest)
  1070. c.Assert(err, IsNil)
  1071. }
  1072. // TestSetBucketWebsiteDetail
  1073. func (s *OssClientSuite) TestSetBucketWebsiteDetail(c *C) {
  1074. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1075. var indexWebsite = "myindex.html"
  1076. var errorWebsite = "myerror.html"
  1077. client, err := New(endpoint, accessID, accessKey)
  1078. c.Assert(err, IsNil)
  1079. err = client.CreateBucket(bucketNameTest)
  1080. c.Assert(err, IsNil)
  1081. time.Sleep(timeoutInOperation)
  1082. btrue := true
  1083. bfalse := false
  1084. // Define one routing rule
  1085. ruleOk := RoutingRule{
  1086. RuleNumber: 1,
  1087. Condition: Condition{
  1088. KeyPrefixEquals: "",
  1089. HTTPErrorCodeReturnedEquals: 404,
  1090. },
  1091. Redirect: Redirect{
  1092. RedirectType: "Mirror",
  1093. // PassQueryString: &btrue, // set default value
  1094. MirrorURL: "http://www.test.com/",
  1095. // MirrorPassQueryString:&btrue, // set default value
  1096. // MirrorFollowRedirect:&bfalse, // set default value
  1097. // MirrorCheckMd5:&bfalse, // set default value
  1098. MirrorHeaders: MirrorHeaders{
  1099. // PassAll:&bfalse, // set default value
  1100. Pass: []string{"myheader-key1", "myheader-key2"},
  1101. Remove: []string{"myheader-key3", "myheader-key4"},
  1102. Set: []MirrorHeaderSet{
  1103. MirrorHeaderSet{
  1104. Key: "myheader-key5",
  1105. Value: "myheader-value5",
  1106. },
  1107. },
  1108. },
  1109. },
  1110. }
  1111. // Define array routing rule
  1112. ruleArrOk := []RoutingRule{
  1113. RoutingRule{
  1114. RuleNumber: 2,
  1115. Condition: Condition{
  1116. KeyPrefixEquals: "abc/",
  1117. HTTPErrorCodeReturnedEquals: 404,
  1118. IncludeHeader: []IncludeHeader{
  1119. IncludeHeader{
  1120. Key: "host",
  1121. Equals: "test.oss-cn-beijing-internal.aliyuncs.com",
  1122. },
  1123. },
  1124. },
  1125. Redirect: Redirect{
  1126. RedirectType: "AliCDN",
  1127. Protocol: "http",
  1128. HostName: "www.test.com",
  1129. PassQueryString: &bfalse,
  1130. ReplaceKeyWith: "prefix/${key}.suffix",
  1131. HttpRedirectCode: 301,
  1132. },
  1133. },
  1134. RoutingRule{
  1135. RuleNumber: 3,
  1136. Condition: Condition{
  1137. KeyPrefixEquals: "",
  1138. HTTPErrorCodeReturnedEquals: 404,
  1139. },
  1140. Redirect: Redirect{
  1141. RedirectType: "Mirror",
  1142. PassQueryString: &btrue,
  1143. MirrorURL: "http://www.test.com/",
  1144. MirrorPassQueryString: &btrue,
  1145. MirrorFollowRedirect: &bfalse,
  1146. MirrorCheckMd5: &bfalse,
  1147. MirrorHeaders: MirrorHeaders{
  1148. PassAll: &btrue,
  1149. Pass: []string{"myheader-key1", "myheader-key2"},
  1150. Remove: []string{"myheader-key3", "myheader-key4"},
  1151. Set: []MirrorHeaderSet{
  1152. MirrorHeaderSet{
  1153. Key: "myheader-key5",
  1154. Value: "myheader-value5",
  1155. },
  1156. },
  1157. },
  1158. },
  1159. },
  1160. }
  1161. // Set one routing rule
  1162. wxmlOne := WebsiteXML{}
  1163. wxmlOne.RoutingRules = append(wxmlOne.RoutingRules, ruleOk)
  1164. var responseHeader http.Header
  1165. err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlOne, GetResponseHeader(&responseHeader))
  1166. c.Assert(err, IsNil)
  1167. requestId := GetRequestId(responseHeader)
  1168. c.Assert(len(requestId) > 0, Equals, true)
  1169. res, err := client.GetBucketWebsite(bucketNameTest)
  1170. c.Assert(err, IsNil)
  1171. c.Assert(res.RoutingRules[0].Redirect.RedirectType, Equals, "Mirror")
  1172. c.Assert(*res.RoutingRules[0].Redirect.PassQueryString, Equals, false)
  1173. c.Assert(*res.RoutingRules[0].Redirect.MirrorPassQueryString, Equals, false)
  1174. c.Assert(*res.RoutingRules[0].Redirect.MirrorFollowRedirect, Equals, true)
  1175. c.Assert(*res.RoutingRules[0].Redirect.MirrorCheckMd5, Equals, false)
  1176. c.Assert(*res.RoutingRules[0].Redirect.MirrorHeaders.PassAll, Equals, false)
  1177. // Set one routing rule and IndexDocument, IndexDocument
  1178. wxml := WebsiteXML{
  1179. IndexDocument: IndexDocument{Suffix: indexWebsite},
  1180. ErrorDocument: ErrorDocument{Key: errorWebsite},
  1181. }
  1182. wxml.RoutingRules = append(wxml.RoutingRules, ruleOk)
  1183. err = client.SetBucketWebsiteDetail(bucketNameTest, wxml, GetResponseHeader(&responseHeader))
  1184. c.Assert(err, IsNil)
  1185. requestId = GetRequestId(responseHeader)
  1186. c.Assert(len(requestId) > 0, Equals, true)
  1187. res, err = client.GetBucketWebsite(bucketNameTest)
  1188. c.Assert(err, IsNil)
  1189. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  1190. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  1191. c.Assert(res.RoutingRules[0].Redirect.RedirectType, Equals, "Mirror")
  1192. // Set array routing rule
  1193. wxml.RoutingRules = append(wxml.RoutingRules, ruleArrOk...)
  1194. err = client.SetBucketWebsiteDetail(bucketNameTest, wxml, GetResponseHeader(&responseHeader))
  1195. c.Assert(err, IsNil)
  1196. requestId = GetRequestId(responseHeader)
  1197. c.Assert(len(requestId) > 0, Equals, true)
  1198. res, err = client.GetBucketWebsite(bucketNameTest)
  1199. c.Assert(err, IsNil)
  1200. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  1201. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  1202. c.Assert(len(res.RoutingRules), Equals, 3)
  1203. c.Assert(res.RoutingRules[1].Redirect.RedirectType, Equals, "AliCDN")
  1204. c.Assert(*res.RoutingRules[2].Redirect.MirrorPassQueryString, Equals, true)
  1205. c.Assert(*res.RoutingRules[2].Redirect.MirrorFollowRedirect, Equals, false)
  1206. // Define one error routing rule
  1207. ruleErr := RoutingRule{
  1208. RuleNumber: 1,
  1209. Redirect: Redirect{
  1210. RedirectType: "Mirror",
  1211. PassQueryString: &btrue,
  1212. },
  1213. }
  1214. // Define array error routing rule
  1215. rulesErrArr := []RoutingRule{
  1216. RoutingRule{
  1217. RuleNumber: 1,
  1218. Redirect: Redirect{
  1219. RedirectType: "Mirror",
  1220. PassQueryString: &btrue,
  1221. },
  1222. },
  1223. RoutingRule{
  1224. RuleNumber: 2,
  1225. Redirect: Redirect{
  1226. RedirectType: "Mirror",
  1227. PassQueryString: &btrue,
  1228. },
  1229. },
  1230. }
  1231. ruleIntErr := RoutingRule{
  1232. // RuleNumber:0, // set NULL value
  1233. Condition: Condition{
  1234. KeyPrefixEquals: "",
  1235. HTTPErrorCodeReturnedEquals: 404,
  1236. },
  1237. Redirect: Redirect{
  1238. RedirectType: "Mirror",
  1239. // PassQueryString: &btrue, // set default value
  1240. MirrorURL: "http://www.test.com/",
  1241. // MirrorPassQueryString:&btrue, // set default value
  1242. // MirrorFollowRedirect:&bfalse, // set default value
  1243. // MirrorCheckMd5:&bfalse, // set default value
  1244. MirrorHeaders: MirrorHeaders{
  1245. // PassAll:&bfalse, // set default value
  1246. Pass: []string{"myheader-key1", "myheader-key2"},
  1247. Remove: []string{"myheader-key3", "myheader-key4"},
  1248. Set: []MirrorHeaderSet{
  1249. MirrorHeaderSet{
  1250. Key: "myheader-key5",
  1251. Value: "myheader-value5",
  1252. },
  1253. },
  1254. },
  1255. },
  1256. }
  1257. // Set one int type error rule
  1258. wxmlIntErr := WebsiteXML{}
  1259. wxmlIntErr.RoutingRules = append(wxmlIntErr.RoutingRules, ruleIntErr)
  1260. err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlIntErr)
  1261. c.Assert(err, NotNil)
  1262. // Set one error rule
  1263. wxmlErr := WebsiteXML{}
  1264. wxmlErr.RoutingRules = append(wxmlErr.RoutingRules, ruleErr)
  1265. err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlErr)
  1266. c.Assert(err, NotNil)
  1267. // Set one error rule and one correct rule
  1268. wxmlErr.RoutingRules = append(wxmlErr.RoutingRules, ruleOk)
  1269. err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlErr)
  1270. c.Assert(err, NotNil)
  1271. wxmlErrRuleArr := WebsiteXML{}
  1272. wxmlErrRuleArr.RoutingRules = append(wxmlErrRuleArr.RoutingRules, rulesErrArr...)
  1273. // Set array error routing rule
  1274. err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlErrRuleArr)
  1275. c.Assert(err, NotNil)
  1276. err = client.DeleteBucketWebsite(bucketNameTest)
  1277. c.Assert(err, IsNil)
  1278. err = client.DeleteBucket(bucketNameTest)
  1279. c.Assert(err, IsNil)
  1280. }
  1281. // TestSetBucketCORS
  1282. func (s *OssClientSuite) TestSetBucketCORS(c *C) {
  1283. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1284. var rule1 = CORSRule{
  1285. AllowedOrigin: []string{"*"},
  1286. AllowedMethod: []string{"PUT", "GET", "POST"},
  1287. AllowedHeader: []string{},
  1288. ExposeHeader: []string{},
  1289. MaxAgeSeconds: 100,
  1290. }
  1291. var rule2 = CORSRule{
  1292. AllowedOrigin: []string{"http://www.a.com", "http://www.b.com"},
  1293. AllowedMethod: []string{"GET"},
  1294. AllowedHeader: []string{"Authorization"},
  1295. ExposeHeader: []string{"x-oss-test", "x-oss-test1"},
  1296. MaxAgeSeconds: 200,
  1297. }
  1298. client, err := New(endpoint, accessID, accessKey)
  1299. c.Assert(err, IsNil)
  1300. err = client.CreateBucket(bucketNameTest)
  1301. c.Assert(err, IsNil)
  1302. time.Sleep(timeoutInOperation)
  1303. // Set
  1304. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1})
  1305. c.Assert(err, IsNil)
  1306. gbcr, err := client.GetBucketCORS(bucketNameTest)
  1307. c.Assert(err, IsNil)
  1308. c.Assert(len(gbcr.CORSRules), Equals, 1)
  1309. c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 1)
  1310. c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 3)
  1311. c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 0)
  1312. c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 0)
  1313. c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 100)
  1314. // Double set
  1315. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1})
  1316. c.Assert(err, IsNil)
  1317. gbcr, err = client.GetBucketCORS(bucketNameTest)
  1318. c.Assert(err, IsNil)
  1319. c.Assert(len(gbcr.CORSRules), Equals, 1)
  1320. c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 1)
  1321. c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 3)
  1322. c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 0)
  1323. c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 0)
  1324. c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 100)
  1325. // Set rule2
  1326. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule2})
  1327. c.Assert(err, IsNil)
  1328. time.Sleep(timeoutInOperation)
  1329. gbcr, err = client.GetBucketCORS(bucketNameTest)
  1330. c.Assert(err, IsNil)
  1331. c.Assert(len(gbcr.CORSRules), Equals, 1)
  1332. c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 2)
  1333. c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 1)
  1334. c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 1)
  1335. c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 2)
  1336. c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 200)
  1337. // Reset
  1338. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1, rule2})
  1339. c.Assert(err, IsNil)
  1340. time.Sleep(timeoutInOperation)
  1341. gbcr, err = client.GetBucketCORS(bucketNameTest)
  1342. c.Assert(err, IsNil)
  1343. c.Assert(len(gbcr.CORSRules), Equals, 2)
  1344. // Set after delete
  1345. err = client.DeleteBucketCORS(bucketNameTest)
  1346. c.Assert(err, IsNil)
  1347. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1, rule2})
  1348. c.Assert(err, IsNil)
  1349. time.Sleep(timeoutInOperation)
  1350. gbcr, err = client.GetBucketCORS(bucketNameTest)
  1351. c.Assert(err, IsNil)
  1352. c.Assert(len(gbcr.CORSRules), Equals, 2)
  1353. err = client.DeleteBucketCORS(bucketNameTest)
  1354. c.Assert(err, IsNil)
  1355. err = client.DeleteBucket(bucketNameTest)
  1356. c.Assert(err, IsNil)
  1357. }
  1358. // TestDeleteBucketCORS
  1359. func (s *OssClientSuite) TestDeleteBucketCORS(c *C) {
  1360. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1361. var rule = CORSRule{
  1362. AllowedOrigin: []string{"*"},
  1363. AllowedMethod: []string{"PUT", "GET", "POST"},
  1364. AllowedHeader: []string{},
  1365. ExposeHeader: []string{},
  1366. MaxAgeSeconds: 100,
  1367. }
  1368. client, err := New(endpoint, accessID, accessKey)
  1369. c.Assert(err, IsNil)
  1370. err = client.CreateBucket(bucketNameTest)
  1371. c.Assert(err, IsNil)
  1372. time.Sleep(timeoutInOperation)
  1373. // Delete not set
  1374. err = client.DeleteBucketCORS(bucketNameTest)
  1375. c.Assert(err, IsNil)
  1376. // Set
  1377. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
  1378. c.Assert(err, IsNil)
  1379. time.Sleep(timeoutInOperation)
  1380. _, err = client.GetBucketCORS(bucketNameTest)
  1381. c.Assert(err, IsNil)
  1382. // Detele
  1383. err = client.DeleteBucketCORS(bucketNameTest)
  1384. c.Assert(err, IsNil)
  1385. time.Sleep(timeoutInOperation)
  1386. _, err = client.GetBucketCORS(bucketNameTest)
  1387. c.Assert(err, NotNil)
  1388. // Detele after deleting
  1389. err = client.DeleteBucketCORS(bucketNameTest)
  1390. c.Assert(err, IsNil)
  1391. err = client.DeleteBucket(bucketNameTest)
  1392. c.Assert(err, IsNil)
  1393. }
  1394. // TestSetBucketCORSNegative
  1395. func (s *OssClientSuite) TestSetBucketCORSNegative(c *C) {
  1396. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1397. var rule = CORSRule{
  1398. AllowedOrigin: []string{"*"},
  1399. AllowedMethod: []string{"PUT", "GET", "POST"},
  1400. AllowedHeader: []string{},
  1401. ExposeHeader: []string{},
  1402. MaxAgeSeconds: 100,
  1403. }
  1404. client, err := New(endpoint, accessID, accessKey)
  1405. c.Assert(err, IsNil)
  1406. err = client.DeleteBucket(bucketNameTest)
  1407. // Not exist
  1408. _, err = client.GetBucketCORS(bucketNameTest)
  1409. c.Assert(err, NotNil)
  1410. err = client.DeleteBucketCORS(bucketNameTest)
  1411. c.Assert(err, NotNil)
  1412. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
  1413. c.Assert(err, NotNil)
  1414. err = client.CreateBucket(bucketNameTest)
  1415. c.Assert(err, IsNil)
  1416. time.Sleep(timeoutInOperation)
  1417. _, err = client.GetBucketCORS(bucketNameTest)
  1418. c.Assert(err, NotNil)
  1419. // Set
  1420. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
  1421. c.Assert(err, IsNil)
  1422. time.Sleep(timeoutInOperation)
  1423. _, err = client.GetBucketCORS(bucketNameTest)
  1424. c.Assert(err, IsNil)
  1425. // Delete
  1426. err = client.DeleteBucketCORS(bucketNameTest)
  1427. c.Assert(err, IsNil)
  1428. time.Sleep(timeoutInOperation)
  1429. _, err = client.GetBucketCORS(bucketNameTest)
  1430. c.Assert(err, NotNil)
  1431. // Delete after deleting
  1432. err = client.DeleteBucketCORS(bucketNameTest)
  1433. c.Assert(err, IsNil)
  1434. err = client.DeleteBucket(bucketNameTest)
  1435. c.Assert(err, IsNil)
  1436. }
  1437. // TestGetBucketInfo
  1438. func (s *OssClientSuite) TestGetBucketInfo(c *C) {
  1439. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1440. client, err := New(endpoint, accessID, accessKey)
  1441. c.Assert(err, IsNil)
  1442. err = client.CreateBucket(bucketNameTest)
  1443. c.Assert(err, IsNil)
  1444. res, err := client.GetBucketInfo(bucketNameTest)
  1445. c.Assert(err, IsNil)
  1446. c.Assert(res.BucketInfo.Name, Equals, bucketNameTest)
  1447. c.Assert(strings.HasPrefix(res.BucketInfo.Location, "oss-"), Equals, true)
  1448. c.Assert(res.BucketInfo.ACL, Equals, "private")
  1449. c.Assert(strings.HasSuffix(res.BucketInfo.ExtranetEndpoint, ".com"), Equals, true)
  1450. c.Assert(strings.HasSuffix(res.BucketInfo.IntranetEndpoint, ".com"), Equals, true)
  1451. c.Assert(res.BucketInfo.CreationDate, NotNil)
  1452. err = client.DeleteBucket(bucketNameTest)
  1453. c.Assert(err, IsNil)
  1454. }
  1455. // TestGetBucketInfoNegative
  1456. func (s *OssClientSuite) TestGetBucketInfoNegative(c *C) {
  1457. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1458. client, err := New(endpoint, accessID, accessKey)
  1459. c.Assert(err, IsNil)
  1460. // Not exist
  1461. _, err = client.GetBucketInfo(bucketNameTest)
  1462. c.Assert(err, NotNil)
  1463. // Bucket name invalid
  1464. _, err = client.GetBucketInfo("InvalidBucketName_")
  1465. c.Assert(err, NotNil)
  1466. }
  1467. // TestEndpointFormat
  1468. func (s *OssClientSuite) TestEndpointFormat(c *C) {
  1469. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1470. // http://host
  1471. client, err := New(endpoint, accessID, accessKey)
  1472. c.Assert(err, IsNil)
  1473. err = client.CreateBucket(bucketNameTest)
  1474. c.Assert(err, IsNil)
  1475. time.Sleep(timeoutInOperation)
  1476. res, err := client.GetBucketACL(bucketNameTest)
  1477. c.Assert(err, IsNil)
  1478. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1479. err = client.DeleteBucket(bucketNameTest)
  1480. c.Assert(err, IsNil)
  1481. time.Sleep(timeoutInOperation)
  1482. // http://host:port
  1483. client, err = New(endpoint+":80", accessID, accessKey)
  1484. c.Assert(err, IsNil)
  1485. err = client.CreateBucket(bucketNameTest)
  1486. c.Assert(err, IsNil)
  1487. time.Sleep(timeoutInOperation)
  1488. res, err = client.GetBucketACL(bucketNameTest)
  1489. c.Assert(err, IsNil)
  1490. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1491. err = client.DeleteBucket(bucketNameTest)
  1492. c.Assert(err, IsNil)
  1493. }
  1494. // TestCname
  1495. func (s *OssClientSuite) _TestCname(c *C) {
  1496. var bucketNameTest = "<my-bucket-cname>"
  1497. client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>", UseCname(true))
  1498. c.Assert(err, IsNil)
  1499. err = client.CreateBucket(bucketNameTest)
  1500. c.Assert(err, IsNil)
  1501. _, err = client.ListBuckets()
  1502. c.Assert(err, NotNil)
  1503. res, err := client.GetBucketACL(bucketNameTest)
  1504. c.Assert(err, IsNil)
  1505. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1506. }
  1507. // TestCnameNegative
  1508. func (s *OssClientSuite) _TestCnameNegative(c *C) {
  1509. var bucketNameTest = "<my-bucket-cname>"
  1510. client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>", UseCname(true))
  1511. c.Assert(err, IsNil)
  1512. err = client.CreateBucket(bucketNameTest)
  1513. c.Assert(err, NotNil)
  1514. _, err = client.ListBuckets()
  1515. c.Assert(err, NotNil)
  1516. _, err = client.GetBucketACL(bucketNameTest)
  1517. c.Assert(err, NotNil)
  1518. }
  1519. // _TestHTTPS
  1520. func (s *OssClientSuite) _TestHTTPS(c *C) {
  1521. var bucketNameTest = "<my-bucket-https>"
  1522. client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>")
  1523. c.Assert(err, IsNil)
  1524. err = client.CreateBucket(bucketNameTest)
  1525. c.Assert(err, IsNil)
  1526. res, err := client.GetBucketACL(bucketNameTest)
  1527. c.Assert(err, IsNil)
  1528. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1529. err = client.DeleteBucket(bucketNameTest)
  1530. c.Assert(err, IsNil)
  1531. }
  1532. // TestClientOption
  1533. func (s *OssClientSuite) TestClientOption(c *C) {
  1534. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1535. client, err := New(endpoint, accessID, accessKey, UseCname(true),
  1536. Timeout(11, 12), SecurityToken("token"), Proxy(proxyHost))
  1537. c.Assert(err, IsNil)
  1538. // CreateBucket timeout
  1539. err = client.CreateBucket(bucketNameTest)
  1540. c.Assert(err, NotNil)
  1541. c.Assert(client.Conn.config.HTTPTimeout.ConnectTimeout, Equals, time.Second*11)
  1542. c.Assert(client.Conn.config.HTTPTimeout.ReadWriteTimeout, Equals, time.Second*12)
  1543. c.Assert(client.Conn.config.HTTPTimeout.HeaderTimeout, Equals, time.Second*12)
  1544. c.Assert(client.Conn.config.HTTPTimeout.IdleConnTimeout, Equals, time.Second*12)
  1545. c.Assert(client.Conn.config.HTTPTimeout.LongTimeout, Equals, time.Second*12*10)
  1546. c.Assert(client.Conn.config.SecurityToken, Equals, "token")
  1547. c.Assert(client.Conn.config.IsCname, Equals, true)
  1548. c.Assert(client.Conn.config.IsUseProxy, Equals, true)
  1549. c.Assert(client.Config.ProxyHost, Equals, proxyHost)
  1550. client, err = New(endpoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
  1551. c.Assert(client.Conn.config.IsUseProxy, Equals, true)
  1552. c.Assert(client.Config.ProxyHost, Equals, proxyHost)
  1553. c.Assert(client.Conn.config.IsAuthProxy, Equals, true)
  1554. c.Assert(client.Conn.config.ProxyUser, Equals, proxyUser)
  1555. c.Assert(client.Conn.config.ProxyPassword, Equals, proxyPasswd)
  1556. client, err = New(endpoint, accessID, accessKey, UserAgent("go sdk user agent"))
  1557. c.Assert(client.Conn.config.UserAgent, Equals, "go sdk user agent")
  1558. // Check we can overide the http.Client
  1559. httpClient := new(http.Client)
  1560. client, err = New(endpoint, accessID, accessKey, HTTPClient(httpClient))
  1561. c.Assert(client.HTTPClient, Equals, httpClient)
  1562. c.Assert(client.Conn.client, Equals, httpClient)
  1563. client, err = New(endpoint, accessID, accessKey)
  1564. c.Assert(client.HTTPClient, IsNil)
  1565. }
  1566. // TestProxy
  1567. func (s *OssClientSuite) TestProxy(c *C) {
  1568. bucketNameTest := bucketNamePrefix + randLowStr(6)
  1569. objectName := "体育/奥运/首金"
  1570. objectValue := "大江东去,浪淘尽,千古风流人物。 故垒西边,人道是、三国周郎赤壁。"
  1571. client, err := New(endpoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
  1572. // Create bucket
  1573. err = client.CreateBucket(bucketNameTest)
  1574. c.Assert(err, IsNil)
  1575. // Get bucket info
  1576. _, err = client.GetBucketInfo(bucketNameTest)
  1577. c.Assert(err, IsNil)
  1578. bucket, err := client.Bucket(bucketNameTest)
  1579. // Sign URL
  1580. str, err := bucket.SignURL(objectName, HTTPPut, 60)
  1581. c.Assert(err, IsNil)
  1582. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  1583. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  1584. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  1585. // Put object with URL
  1586. err = bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  1587. c.Assert(err, IsNil)
  1588. // Sign URL for get object
  1589. str, err = bucket.SignURL(objectName, HTTPGet, 60)
  1590. c.Assert(err, IsNil)
  1591. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  1592. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  1593. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  1594. // Get object with URL
  1595. body, err := bucket.GetObjectWithURL(str)
  1596. c.Assert(err, IsNil)
  1597. str, err = readBody(body)
  1598. c.Assert(err, IsNil)
  1599. c.Assert(str, Equals, objectValue)
  1600. // Put object
  1601. err = bucket.PutObject(objectName, strings.NewReader(objectValue))
  1602. c.Assert(err, IsNil)
  1603. // Get object
  1604. _, err = bucket.GetObject(objectName)
  1605. c.Assert(err, IsNil)
  1606. // List objects
  1607. _, err = bucket.ListObjects()
  1608. c.Assert(err, IsNil)
  1609. // Delete object
  1610. err = bucket.DeleteObject(objectName)
  1611. c.Assert(err, IsNil)
  1612. // Delete bucket
  1613. err = client.DeleteBucket(bucketNameTest)
  1614. c.Assert(err, IsNil)
  1615. }
  1616. // Private
  1617. func (s *OssClientSuite) checkBucket(buckets []BucketProperties, bucket string) bool {
  1618. for _, v := range buckets {
  1619. if v.Name == bucket {
  1620. return true
  1621. }
  1622. }
  1623. return false
  1624. }
  1625. func (s *OssClientSuite) getBucket(buckets []BucketProperties, bucket string) (bool, BucketProperties) {
  1626. for _, v := range buckets {
  1627. if v.Name == bucket {
  1628. return true, v
  1629. }
  1630. }
  1631. return false, BucketProperties{}
  1632. }
  1633. func (s *OssClientSuite) TestHttpLogNotSignUrl(c *C) {
  1634. logName := "." + string(os.PathSeparator) + "test-go-sdk-httpdebug.log" + randStr(5)
  1635. f, err := os.OpenFile(logName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660)
  1636. c.Assert(err, IsNil)
  1637. client, err := New(endpoint, accessID, accessKey)
  1638. client.Config.LogLevel = Debug
  1639. client.Config.Logger = log.New(f, "", log.LstdFlags)
  1640. var testBucketName = bucketNamePrefix + randLowStr(6)
  1641. // CreateBucket
  1642. err = client.CreateBucket(testBucketName)
  1643. f.Close()
  1644. // read log file,get http info
  1645. contents, err := ioutil.ReadFile(logName)
  1646. c.Assert(err, IsNil)
  1647. httpContent := string(contents)
  1648. //fmt.Println(httpContent)
  1649. c.Assert(strings.Contains(httpContent, "signStr"), Equals, true)
  1650. c.Assert(strings.Contains(httpContent, "Method:"), Equals, true)
  1651. // delete test bucket and log
  1652. os.Remove(logName)
  1653. client.DeleteBucket(testBucketName)
  1654. }
  1655. func (s *OssClientSuite) TestHttpLogSignUrl(c *C) {
  1656. logName := "." + string(os.PathSeparator) + "test-go-sdk-httpdebug-signurl.log" + randStr(5)
  1657. f, err := os.OpenFile(logName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660)
  1658. c.Assert(err, IsNil)
  1659. client, err := New(endpoint, accessID, accessKey)
  1660. client.Config.LogLevel = Debug
  1661. client.Config.Logger = log.New(f, "", log.LstdFlags)
  1662. var testBucketName = bucketNamePrefix + randLowStr(6)
  1663. // CreateBucket
  1664. err = client.CreateBucket(testBucketName)
  1665. f.Close()
  1666. // clear log
  1667. f, err = os.OpenFile(logName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660)
  1668. client.Config.Logger = log.New(f, "", log.LstdFlags)
  1669. bucket, _ := client.Bucket(testBucketName)
  1670. objectName := objectNamePrefix + randStr(8)
  1671. objectValue := randStr(20)
  1672. // Sign URL for put
  1673. str, err := bucket.SignURL(objectName, HTTPPut, 60)
  1674. c.Assert(err, IsNil)
  1675. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  1676. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  1677. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  1678. // Error put object with URL
  1679. err = bucket.PutObjectWithURL(str, strings.NewReader(objectValue), ContentType("image/tiff"))
  1680. f.Close()
  1681. // read log file,get http info
  1682. contents, err := ioutil.ReadFile(logName)
  1683. c.Assert(err, IsNil)
  1684. httpContent := string(contents)
  1685. //fmt.Println(httpContent)
  1686. c.Assert(strings.Contains(httpContent, "signStr"), Equals, true)
  1687. c.Assert(strings.Contains(httpContent, "Method:"), Equals, true)
  1688. // delete test bucket and log
  1689. os.Remove(logName)
  1690. client.DeleteBucket(testBucketName)
  1691. }
  1692. func (s *OssClientSuite) TestSetLimitUploadSpeed(c *C) {
  1693. client, err := New(endpoint, accessID, accessKey)
  1694. c.Assert(err, IsNil)
  1695. err = client.LimitUploadSpeed(100)
  1696. goVersion := runtime.Version()
  1697. pSlice := strings.Split(strings.ToLower(goVersion), ".")
  1698. // compare with go1.7
  1699. if len(pSlice) >= 2 {
  1700. if pSlice[0] > "go1" {
  1701. c.Assert(err, IsNil)
  1702. } else if pSlice[0] == "go1" {
  1703. subVersion, _ := strconv.Atoi(pSlice[1])
  1704. if subVersion >= 7 {
  1705. c.Assert(err, IsNil)
  1706. } else {
  1707. c.Assert(err, NotNil)
  1708. }
  1709. } else {
  1710. c.Assert(err, NotNil)
  1711. }
  1712. }
  1713. }
  1714. func (s *OssClientSuite) TestBucketEncyptionError(c *C) {
  1715. client, err := New(endpoint, accessID, accessKey)
  1716. c.Assert(err, IsNil)
  1717. bucketName := bucketNamePrefix + randLowStr(5)
  1718. err = client.CreateBucket(bucketName)
  1719. c.Assert(err, IsNil)
  1720. // SetBucketEncryption:AES256 ,"123"
  1721. encryptionRule := ServerEncryptionRule{}
  1722. encryptionRule.SSEDefault.SSEAlgorithm = string(AESAlgorithm)
  1723. encryptionRule.SSEDefault.KMSMasterKeyID = "123"
  1724. var responseHeader http.Header
  1725. err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
  1726. c.Assert(err, NotNil)
  1727. requestId := GetRequestId(responseHeader)
  1728. c.Assert(len(requestId) > 0, Equals, true)
  1729. // GetBucketEncryption
  1730. _, err = client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1731. c.Assert(err, NotNil)
  1732. requestId = GetRequestId(responseHeader)
  1733. c.Assert(len(requestId) > 0, Equals, true)
  1734. // Get default bucket info
  1735. bucketResult, err := client.GetBucketInfo(bucketName)
  1736. c.Assert(err, IsNil)
  1737. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "")
  1738. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
  1739. c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
  1740. err = client.DeleteBucket(bucketName)
  1741. c.Assert(err, IsNil)
  1742. }
  1743. func (s *OssClientSuite) TestBucketEncyptionPutAndGetAndDelete(c *C) {
  1744. client, err := New(endpoint, accessID, accessKey)
  1745. c.Assert(err, IsNil)
  1746. bucketName := bucketNamePrefix + randLowStr(5)
  1747. err = client.CreateBucket(bucketName)
  1748. c.Assert(err, IsNil)
  1749. // SetBucketEncryption:KMS ,""
  1750. encryptionRule := ServerEncryptionRule{}
  1751. encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
  1752. var responseHeader http.Header
  1753. err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
  1754. c.Assert(err, IsNil)
  1755. requestId := GetRequestId(responseHeader)
  1756. c.Assert(len(requestId) > 0, Equals, true)
  1757. // GetBucketEncryption
  1758. getResult, err := client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1759. c.Assert(err, IsNil)
  1760. requestId = GetRequestId(responseHeader)
  1761. c.Assert(len(requestId) > 0, Equals, true)
  1762. // check encryption value
  1763. c.Assert(encryptionRule.SSEDefault.SSEAlgorithm, Equals, getResult.SSEDefault.SSEAlgorithm)
  1764. c.Assert(encryptionRule.SSEDefault.KMSMasterKeyID, Equals, getResult.SSEDefault.KMSMasterKeyID)
  1765. // delete bucket encyption
  1766. err = client.DeleteBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1767. c.Assert(err, IsNil)
  1768. requestId = GetRequestId(responseHeader)
  1769. c.Assert(len(requestId) > 0, Equals, true)
  1770. // GetBucketEncryption failure
  1771. _, err = client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1772. c.Assert(err, NotNil)
  1773. requestId = GetRequestId(responseHeader)
  1774. c.Assert(len(requestId) > 0, Equals, true)
  1775. // Get default bucket info
  1776. bucketResult, err := client.GetBucketInfo(bucketName)
  1777. c.Assert(err, IsNil)
  1778. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "")
  1779. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
  1780. c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
  1781. err = client.DeleteBucket(bucketName)
  1782. c.Assert(err, IsNil)
  1783. }
  1784. func (s *OssClientSuite) TestBucketEncyptionPutObjectSuccess(c *C) {
  1785. client, err := New(endpoint, accessID, accessKey)
  1786. c.Assert(err, IsNil)
  1787. bucketName := bucketNamePrefix + randLowStr(5)
  1788. err = client.CreateBucket(bucketName)
  1789. c.Assert(err, IsNil)
  1790. // SetBucketEncryption:KMS ,""
  1791. encryptionRule := ServerEncryptionRule{}
  1792. encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
  1793. var responseHeader http.Header
  1794. err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
  1795. c.Assert(err, IsNil)
  1796. requestId := GetRequestId(responseHeader)
  1797. c.Assert(len(requestId) > 0, Equals, true)
  1798. // GetBucketEncryption
  1799. getResult, err := client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1800. c.Assert(err, IsNil)
  1801. requestId = GetRequestId(responseHeader)
  1802. c.Assert(len(requestId) > 0, Equals, true)
  1803. // check encryption value
  1804. c.Assert(encryptionRule.SSEDefault.SSEAlgorithm, Equals, getResult.SSEDefault.SSEAlgorithm)
  1805. c.Assert(encryptionRule.SSEDefault.KMSMasterKeyID, Equals, getResult.SSEDefault.KMSMasterKeyID)
  1806. // Get default bucket info
  1807. bucketResult, err := client.GetBucketInfo(bucketName)
  1808. c.Assert(err, IsNil)
  1809. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "KMS")
  1810. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
  1811. c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
  1812. // put and get object success
  1813. //bucket, err := client.Bucket(bucketName)
  1814. //c.Assert(err, IsNil)
  1815. // put object success
  1816. //objectName := objectNamePrefix + randStr(8)
  1817. //context := randStr(100)
  1818. //err = bucket.PutObject(objectName, strings.NewReader(context))
  1819. //c.Assert(err, IsNil)
  1820. // get object success
  1821. //body, err := bucket.GetObject(objectName)
  1822. //c.Assert(err, IsNil)
  1823. //str, err := readBody(body)
  1824. //c.Assert(err, IsNil)
  1825. //body.Close()
  1826. //c.Assert(str, Equals, context)
  1827. //bucket.DeleteObject(objectName)
  1828. err = client.DeleteBucket(bucketName)
  1829. c.Assert(err, IsNil)
  1830. }
  1831. func (s *OssClientSuite) TestBucketEncyptionPutObjectError(c *C) {
  1832. client, err := New(endpoint, accessID, accessKey)
  1833. c.Assert(err, IsNil)
  1834. bucketName := bucketNamePrefix + randLowStr(5)
  1835. err = client.CreateBucket(bucketName)
  1836. c.Assert(err, IsNil)
  1837. // SetBucketEncryption:KMS ,""
  1838. encryptionRule := ServerEncryptionRule{}
  1839. encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
  1840. encryptionRule.SSEDefault.KMSMasterKeyID = "123"
  1841. var responseHeader http.Header
  1842. err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
  1843. c.Assert(err, IsNil)
  1844. requestId := GetRequestId(responseHeader)
  1845. c.Assert(len(requestId) > 0, Equals, true)
  1846. // GetBucketEncryption
  1847. getResult, err := client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1848. c.Assert(err, IsNil)
  1849. requestId = GetRequestId(responseHeader)
  1850. c.Assert(len(requestId) > 0, Equals, true)
  1851. // check encryption value
  1852. c.Assert(encryptionRule.SSEDefault.SSEAlgorithm, Equals, getResult.SSEDefault.SSEAlgorithm)
  1853. c.Assert(encryptionRule.SSEDefault.KMSMasterKeyID, Equals, getResult.SSEDefault.KMSMasterKeyID)
  1854. // Get default bucket info
  1855. bucketResult, err := client.GetBucketInfo(bucketName)
  1856. c.Assert(err, IsNil)
  1857. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "KMS")
  1858. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "123")
  1859. c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
  1860. // put and get object failure
  1861. bucket, err := client.Bucket(bucketName)
  1862. c.Assert(err, IsNil)
  1863. // put object failure
  1864. objectName := objectNamePrefix + randStr(8)
  1865. context := randStr(100)
  1866. err = bucket.PutObject(objectName, strings.NewReader(context))
  1867. c.Assert(err, NotNil)
  1868. err = client.DeleteBucket(bucketName)
  1869. c.Assert(err, IsNil)
  1870. }
  1871. func (s *OssClientSuite) TestBucketTaggingOperation(c *C) {
  1872. client, err := New(endpoint, accessID, accessKey)
  1873. c.Assert(err, IsNil)
  1874. bucketName := bucketNamePrefix + randLowStr(5)
  1875. err = client.CreateBucket(bucketName)
  1876. c.Assert(err, IsNil)
  1877. var respHeader http.Header
  1878. // Bucket Tagging
  1879. var tagging Tagging
  1880. tagging.Tags = []Tag{Tag{Key: "testkey2", Value: "testvalue2"}}
  1881. err = client.SetBucketTagging(bucketName, tagging, GetResponseHeader(&respHeader))
  1882. c.Assert(err, IsNil)
  1883. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  1884. getResult, err := client.GetBucketTagging(bucketName)
  1885. c.Assert(err, IsNil)
  1886. c.Assert(getResult.Tags[0].Key, Equals, tagging.Tags[0].Key)
  1887. c.Assert(getResult.Tags[0].Value, Equals, tagging.Tags[0].Value)
  1888. // delete BucketTagging
  1889. err = client.DeleteBucketTagging(bucketName, GetResponseHeader(&respHeader))
  1890. c.Assert(err, IsNil)
  1891. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  1892. getResult, err = client.GetBucketTagging(bucketName, GetResponseHeader(&respHeader))
  1893. c.Assert(err, IsNil)
  1894. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  1895. c.Assert(len(getResult.Tags), Equals, 0)
  1896. err = client.DeleteBucket(bucketName)
  1897. c.Assert(err, IsNil)
  1898. }
  1899. func (s *OssClientSuite) TestListBucketsTagging(c *C) {
  1900. client, err := New(endpoint, accessID, accessKey)
  1901. c.Assert(err, IsNil)
  1902. bucketName1 := bucketNamePrefix + randLowStr(5)
  1903. err = client.CreateBucket(bucketName1)
  1904. c.Assert(err, IsNil)
  1905. bucketName2 := bucketNamePrefix + randLowStr(5)
  1906. err = client.CreateBucket(bucketName2)
  1907. c.Assert(err, IsNil)
  1908. // Bucket Tagging
  1909. var tagging Tagging
  1910. tagging.Tags = []Tag{Tag{Key: "testkey", Value: "testvalue"}}
  1911. err = client.SetBucketTagging(bucketName1, tagging)
  1912. c.Assert(err, IsNil)
  1913. // list bucket
  1914. listResult, err := client.ListBuckets(TagKey("testkey"))
  1915. c.Assert(err, IsNil)
  1916. c.Assert(len(listResult.Buckets), Equals, 1)
  1917. c.Assert(listResult.Buckets[0].Name, Equals, bucketName1)
  1918. client.DeleteBucket(bucketName1)
  1919. client.DeleteBucket(bucketName2)
  1920. }
  1921. func (s *OssClientSuite) TestGetBucketStat(c *C) {
  1922. client, err := New(endpoint, accessID, accessKey)
  1923. c.Assert(err, IsNil)
  1924. bucketName := bucketNamePrefix + randLowStr(5)
  1925. err = client.CreateBucket(bucketName)
  1926. c.Assert(err, IsNil)
  1927. bucket, err := client.Bucket(bucketName)
  1928. c.Assert(err, IsNil)
  1929. // put object
  1930. objectName := objectNamePrefix + randLowStr(5)
  1931. err = bucket.PutObject(objectName, strings.NewReader(randStr(10)))
  1932. c.Assert(err, IsNil)
  1933. bucket.DeleteObject(objectName)
  1934. err = bucket.PutObject(objectName, strings.NewReader(randStr(10)))
  1935. c.Assert(err, IsNil)
  1936. bucket.DeleteObject(objectName)
  1937. _, err = client.GetBucketStat(bucketName)
  1938. c.Assert(err, IsNil)
  1939. client.DeleteBucket(bucketName)
  1940. }
  1941. func (s *OssBucketSuite) TestGetBucketVersioning(c *C) {
  1942. // create a bucket with default proprety
  1943. client, err := New(endpoint, accessID, accessKey)
  1944. c.Assert(err, IsNil)
  1945. bucketName := bucketNamePrefix + randLowStr(6)
  1946. var respHeader http.Header
  1947. err = client.CreateBucket(bucketName, GetResponseHeader(&respHeader))
  1948. c.Assert(err, IsNil)
  1949. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  1950. // put bucket version:enabled
  1951. var versioningConfig VersioningConfig
  1952. versioningConfig.Status = string(VersionEnabled)
  1953. err = client.SetBucketVersioning(bucketName, versioningConfig)
  1954. c.Assert(err, IsNil)
  1955. // get bucket version success
  1956. versioningResult, err := client.GetBucketVersioning(bucketName, GetResponseHeader(&respHeader))
  1957. c.Assert(versioningResult.Status, Equals, "Enabled")
  1958. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  1959. forceDeleteBucket(client, bucketName, c)
  1960. }
  1961. func (s *OssClientSuite) TestBucketPolicy(c *C){
  1962. client, err := New(endpoint, accessID, accessKey)
  1963. c.Assert(err, IsNil)
  1964. bucketName := bucketNamePrefix + randLowStr(5)
  1965. err = client.CreateBucket(bucketName)
  1966. c.Assert(err, IsNil)
  1967. var responseHeader http.Header
  1968. ret, err := client.GetBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  1969. c.Assert(err, NotNil)
  1970. requestId := GetRequestId(responseHeader)
  1971. c.Assert(len(requestId) > 0, Equals, true)
  1972. policyInfo := `
  1973. {
  1974. "Version":"1",
  1975. "Statement":[
  1976. {
  1977. "Action":[
  1978. "oss:GetObject",
  1979. "oss:PutObject"
  1980. ],
  1981. "Effect":"Deny",
  1982. "Principal":"[123456790]",
  1983. "Resource":["acs:oss:*:1234567890:*/*"]
  1984. }
  1985. ]
  1986. }`
  1987. err = client.SetBucketPolicy(bucketName, policyInfo, GetResponseHeader(&responseHeader))
  1988. c.Assert(err, IsNil)
  1989. requestId = GetRequestId(responseHeader)
  1990. c.Assert(len(requestId) > 0, Equals, true)
  1991. ret, err = client.GetBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  1992. c.Assert(err, IsNil)
  1993. testLogger.Println("policy:", ret)
  1994. c.Assert(ret, Equals, policyInfo)
  1995. requestId = GetRequestId(responseHeader)
  1996. c.Assert(len(requestId) > 0, Equals, true)
  1997. err = client.DeleteBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  1998. c.Assert(err, IsNil)
  1999. requestId = GetRequestId(responseHeader)
  2000. c.Assert(len(requestId) > 0, Equals, true)
  2001. client.DeleteBucket(bucketName)
  2002. }
  2003. func (s *OssClientSuite) TestBucketPolicyNegative(c *C) {
  2004. client, err := New(endpoint, accessID, accessKey)
  2005. c.Assert(err, IsNil)
  2006. bucketName := bucketNamePrefix + randLowStr(5)
  2007. err = client.CreateBucket(bucketName)
  2008. c.Assert(err, IsNil)
  2009. var responseHeader http.Header
  2010. _, err = client.GetBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2011. c.Assert(err, NotNil)
  2012. requestId := GetRequestId(responseHeader)
  2013. c.Assert(len(requestId) > 0, Equals, true)
  2014. // Setting the Version is 2, this is error policy
  2015. errPolicy := `
  2016. {
  2017. "Version":"2",
  2018. "Statement":[
  2019. {
  2020. "Action":[
  2021. "oss:GetObject",
  2022. "oss:PutObject"
  2023. ],
  2024. "Effect":"Deny",
  2025. "Principal":"[123456790]",
  2026. "Resource":["acs:oss:*:1234567890:*/*"]
  2027. }
  2028. ]
  2029. }`
  2030. err = client.SetBucketPolicy(bucketName, errPolicy, GetResponseHeader(&responseHeader))
  2031. c.Assert(err, NotNil)
  2032. testLogger.Println("err:",err)
  2033. requestId = GetRequestId(responseHeader)
  2034. c.Assert(len(requestId) > 0, Equals, true)
  2035. err = client.DeleteBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2036. c.Assert(err, IsNil)
  2037. bucketNameEmpty := bucketNamePrefix + randLowStr(5)
  2038. client.DeleteBucket(bucketNameEmpty)
  2039. err = client.DeleteBucketPolicy(bucketNameEmpty, GetResponseHeader(&responseHeader))
  2040. c.Assert(err, NotNil)
  2041. requestId = GetRequestId(responseHeader)
  2042. c.Assert(len(requestId) > 0, Equals, true)
  2043. client.DeleteBucket(bucketName)
  2044. }