client_test.go 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  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. "log"
  7. "math/rand"
  8. "os"
  9. "strings"
  10. "testing"
  11. "time"
  12. . "gopkg.in/check.v1"
  13. )
  14. // Hook up gocheck into the "go test" runner.
  15. func Test(t *testing.T) {
  16. TestingT(t)
  17. }
  18. type OssClientSuite struct{}
  19. var _ = Suite(&OssClientSuite{})
  20. var (
  21. // endpoint/id/key
  22. endpoint = os.Getenv("OSS_TEST_ENDPOINT")
  23. accessID = os.Getenv("OSS_TEST_ACCESS_KEY_ID")
  24. accessKey = os.Getenv("OSS_TEST_ACCESS_KEY_SECRET")
  25. // proxy
  26. proxyHost = os.Getenv("OSS_TEST_PROXY_HOST")
  27. proxyUser = os.Getenv("OSS_TEST_PROXY_USER")
  28. proxyPasswd = os.Getenv("OSS_TEST_PROXY_PASSWORD")
  29. // sts
  30. stsaccessID = os.Getenv("OSS_TEST_STS_ID")
  31. stsaccessKey = os.Getenv("OSS_TEST_STS_KEY")
  32. stsARN = os.Getenv("OSS_TEST_STS_ARN")
  33. )
  34. const (
  35. // prefix of bucket name for bucket ops test
  36. bucketNamePrefix = "go-sdk-test-bucket-xyzu-"
  37. // bucket name for object ops test
  38. bucketName = "go-sdk-test-bucket-xyzu-for-object"
  39. archiveBucketName = "go-sdk-test-bucket-xyzu-for-archive"
  40. // object name for object ops test
  41. objectNamePrefix = "go-sdk-test-object-xyzu-"
  42. // sts region is one and only hangzhou
  43. stsRegion = "cn-hangzhou"
  44. )
  45. var (
  46. logPath = "go_sdk_test_" + time.Now().Format("20060102_150405") + ".log"
  47. testLogFile, _ = os.OpenFile(logPath, os.O_RDWR|os.O_CREATE, 0664)
  48. testLogger = log.New(testLogFile, "", log.Ldate|log.Ltime|log.Lshortfile)
  49. letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
  50. )
  51. func randStr(n int) string {
  52. b := make([]rune, n)
  53. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  54. for i := range b {
  55. b[i] = letters[r.Intn(len(letters))]
  56. }
  57. return string(b)
  58. }
  59. func createFile(fileName, content string, c *C) {
  60. fout, err := os.Create(fileName)
  61. defer fout.Close()
  62. c.Assert(err, IsNil)
  63. _, err = fout.WriteString(content)
  64. c.Assert(err, IsNil)
  65. }
  66. func randLowStr(n int) string {
  67. return strings.ToLower(randStr(n))
  68. }
  69. // Run once when the suite starts running
  70. func (s *OssClientSuite) SetUpSuite(c *C) {
  71. client, err := New(endpoint, accessID, accessKey)
  72. c.Assert(err, IsNil)
  73. lbr, err := client.ListBuckets(Prefix(bucketNamePrefix), MaxKeys(1000))
  74. c.Assert(err, IsNil)
  75. for _, bucket := range lbr.Buckets {
  76. s.deleteBucket(client, bucket.Name, c)
  77. }
  78. testLogger.Println("test client started")
  79. }
  80. // Run before each test or benchmark starts running
  81. func (s *OssClientSuite) TearDownSuite(c *C) {
  82. client, err := New(endpoint, accessID, accessKey)
  83. c.Assert(err, IsNil)
  84. lbr, err := client.ListBuckets(Prefix(bucketNamePrefix), MaxKeys(1000))
  85. c.Assert(err, IsNil)
  86. for _, bucket := range lbr.Buckets {
  87. s.deleteBucket(client, bucket.Name, c)
  88. }
  89. testLogger.Println("test client completed")
  90. }
  91. func (s *OssClientSuite) deleteBucket(client *Client, bucketName string, c *C) {
  92. bucket, err := client.Bucket(bucketName)
  93. c.Assert(err, IsNil)
  94. // Delete Object
  95. lor, err := bucket.ListObjects()
  96. c.Assert(err, IsNil)
  97. for _, object := range lor.Objects {
  98. err = bucket.DeleteObject(object.Key)
  99. c.Assert(err, IsNil)
  100. }
  101. // Delete Part
  102. lmur, err := bucket.ListMultipartUploads()
  103. c.Assert(err, IsNil)
  104. for _, upload := range lmur.Uploads {
  105. var imur = InitiateMultipartUploadResult{Bucket: bucketName,
  106. Key: upload.Key, UploadID: upload.UploadID}
  107. err = bucket.AbortMultipartUpload(imur)
  108. c.Assert(err, IsNil)
  109. }
  110. // Delete Bucket
  111. err = client.DeleteBucket(bucketName)
  112. c.Assert(err, IsNil)
  113. }
  114. // Run after each test or benchmark runs
  115. func (s *OssClientSuite) SetUpTest(c *C) {
  116. }
  117. // Run once after all tests or benchmarks have finished running
  118. func (s *OssClientSuite) TearDownTest(c *C) {
  119. }
  120. // TestCreateBucket
  121. func (s *OssClientSuite) TestCreateBucket(c *C) {
  122. var bucketNameTest = bucketNamePrefix + "tcb"
  123. client, err := New(endpoint, accessID, accessKey)
  124. c.Assert(err, IsNil)
  125. // Create
  126. client.DeleteBucket(bucketNameTest)
  127. err = client.CreateBucket(bucketNameTest)
  128. c.Assert(err, IsNil)
  129. // Check
  130. lbr, err := client.ListBuckets()
  131. c.Assert(err, IsNil)
  132. found := s.checkBucket(lbr.Buckets, bucketNameTest)
  133. c.Assert(found, Equals, true)
  134. time.Sleep(5 * time.Second)
  135. res, err := client.GetBucketACL(bucketNameTest)
  136. c.Assert(err, IsNil)
  137. c.Assert(res.ACL, Equals, string(ACLPrivate))
  138. err = client.DeleteBucket(bucketNameTest)
  139. c.Assert(err, IsNil)
  140. // Create with ACLPublicRead
  141. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicRead))
  142. c.Assert(err, IsNil)
  143. time.Sleep(5 * time.Second)
  144. res, err = client.GetBucketACL(bucketNameTest)
  145. c.Assert(err, IsNil)
  146. c.Assert(res.ACL, Equals, string(ACLPublicRead))
  147. err = client.DeleteBucket(bucketNameTest)
  148. c.Assert(err, IsNil)
  149. time.Sleep(5 * time.Second)
  150. // ACLPublicReadWrite
  151. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicReadWrite))
  152. c.Assert(err, IsNil)
  153. res, err = client.GetBucketACL(bucketNameTest)
  154. c.Assert(err, IsNil)
  155. c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
  156. err = client.DeleteBucket(bucketNameTest)
  157. c.Assert(err, IsNil)
  158. time.Sleep(5 * time.Second)
  159. // ACLPrivate
  160. err = client.CreateBucket(bucketNameTest, ACL(ACLPrivate))
  161. c.Assert(err, IsNil)
  162. res, err = client.GetBucketACL(bucketNameTest)
  163. c.Assert(err, IsNil)
  164. c.Assert(res.ACL, Equals, string(ACLPrivate))
  165. // Delete
  166. err = client.DeleteBucket(bucketNameTest)
  167. c.Assert(err, IsNil)
  168. // create bucket with config and test get bucket info
  169. for _, storage := range []StorageClassType{StorageStandard, StorageIA, StorageArchive} {
  170. bucketNameTest := bucketNamePrefix + randLowStr(5)
  171. err = client.CreateBucket(bucketNameTest, StorageClass(storage), ACL(ACLPublicRead))
  172. c.Assert(err, IsNil)
  173. res, err := client.GetBucketInfo(bucketNameTest)
  174. c.Assert(err, IsNil)
  175. c.Assert(res.BucketInfo.Name, Equals, bucketNameTest)
  176. c.Assert(res.BucketInfo.StorageClass, Equals, string(storage))
  177. c.Assert(res.BucketInfo.ACL, Equals, string(ACLPublicRead))
  178. // Delete
  179. err = client.DeleteBucket(bucketNameTest)
  180. c.Assert(err, IsNil)
  181. }
  182. // error put bucket with config
  183. err = client.CreateBucket("ERRORBUCKETNAME", StorageClass(StorageArchive))
  184. c.Assert(err, NotNil)
  185. // create bucket with config and test list bucket
  186. for _, storage := range []StorageClassType{StorageStandard, StorageIA, StorageArchive} {
  187. bucketNameTest := bucketNamePrefix + randLowStr(5)
  188. err = client.CreateBucket(bucketNameTest, StorageClass(storage))
  189. c.Assert(err, IsNil)
  190. res, err := client.ListBuckets()
  191. c.Assert(err, IsNil)
  192. exist, b := s.getBucket(res.Buckets, bucketNameTest)
  193. c.Assert(exist, Equals, true)
  194. c.Assert(b.Name, Equals, bucketNameTest)
  195. c.Assert(b.StorageClass, Equals, string(storage))
  196. // Delete
  197. err = client.DeleteBucket(bucketNameTest)
  198. c.Assert(err, IsNil)
  199. }
  200. }
  201. // TestCreateBucketNegative
  202. func (s *OssClientSuite) TestCreateBucketNegative(c *C) {
  203. client, err := New(endpoint, accessID, accessKey)
  204. c.Assert(err, IsNil)
  205. // BucketName invalid
  206. err = client.CreateBucket("xx")
  207. c.Assert(err, NotNil)
  208. err = client.CreateBucket("XXXX")
  209. c.Assert(err, NotNil)
  210. testLogger.Println(err)
  211. err = client.CreateBucket("_bucket")
  212. c.Assert(err, NotNil)
  213. testLogger.Println(err)
  214. // Acl invalid
  215. err = client.CreateBucket(bucketNamePrefix+"tcbn", ACL("InvaldAcl"))
  216. c.Assert(err, NotNil)
  217. testLogger.Println(err)
  218. }
  219. // TestDeleteBucket
  220. func (s *OssClientSuite) TestDeleteBucket(c *C) {
  221. var bucketNameTest = bucketNamePrefix + "tdb"
  222. client, err := New(endpoint, accessID, accessKey)
  223. c.Assert(err, IsNil)
  224. // Create
  225. err = client.CreateBucket(bucketNameTest)
  226. c.Assert(err, IsNil)
  227. // Check
  228. lbr, err := client.ListBuckets()
  229. c.Assert(err, IsNil)
  230. found := s.checkBucket(lbr.Buckets, bucketNameTest)
  231. c.Assert(found, Equals, true)
  232. // Delete
  233. err = client.DeleteBucket(bucketNameTest)
  234. c.Assert(err, IsNil)
  235. time.Sleep(time.Second * 1)
  236. // Check
  237. lbr, err = client.ListBuckets()
  238. c.Assert(err, IsNil)
  239. // Sometimes failed because of cache
  240. found = s.checkBucket(lbr.Buckets, bucketNameTest)
  241. // c.Assert(found, Equals, false)
  242. err = client.DeleteBucket(bucketNameTest)
  243. // c.Assert(err, IsNil)
  244. }
  245. // TestDeleteBucketNegative
  246. func (s *OssClientSuite) TestDeleteBucketNegative(c *C) {
  247. var bucketNameTest = bucketNamePrefix + "tdbn"
  248. client, err := New(endpoint, accessID, accessKey)
  249. c.Assert(err, IsNil)
  250. // BucketName invalid
  251. err = client.DeleteBucket("xx")
  252. c.Assert(err, NotNil)
  253. err = client.DeleteBucket("XXXX")
  254. c.Assert(err, NotNil)
  255. err = client.DeleteBucket("_bucket")
  256. c.Assert(err, NotNil)
  257. // Delete no exist
  258. err = client.DeleteBucket("notexist")
  259. c.Assert(err, NotNil)
  260. // No permission to delete, this ak/sk for js sdk
  261. err = client.CreateBucket(bucketNameTest)
  262. c.Assert(err, IsNil)
  263. accessID := "<accessKeyId>"
  264. accessKey := "<accessKeySecret>"
  265. clientOtherUser, err := New(endpoint, accessID, accessKey)
  266. c.Assert(err, IsNil)
  267. err = clientOtherUser.DeleteBucket(bucketNameTest)
  268. c.Assert(err, NotNil)
  269. err = client.DeleteBucket(bucketNameTest)
  270. c.Assert(err, IsNil)
  271. }
  272. // TestListBucket
  273. func (s *OssClientSuite) TestListBucket(c *C) {
  274. var bucketNameLbOne = bucketNamePrefix + "tlb1"
  275. var bucketNameLbTwo = bucketNamePrefix + "tlb2"
  276. var bucketNameLbThree = bucketNamePrefix + "tlb3"
  277. client, err := New(endpoint, accessID, accessKey)
  278. c.Assert(err, IsNil)
  279. // CreateBucket
  280. err = client.CreateBucket(bucketNameLbOne)
  281. c.Assert(err, IsNil)
  282. err = client.CreateBucket(bucketNameLbTwo)
  283. c.Assert(err, IsNil)
  284. err = client.CreateBucket(bucketNameLbThree)
  285. c.Assert(err, IsNil)
  286. // ListBuckets, specified prefix
  287. lbr, err := client.ListBuckets(Prefix(bucketNamePrefix), MaxKeys(2))
  288. c.Assert(err, IsNil)
  289. c.Assert(len(lbr.Buckets), Equals, 2)
  290. // ListBuckets, specified max keys
  291. lbr, err = client.ListBuckets(MaxKeys(2))
  292. c.Assert(err, IsNil)
  293. c.Assert(len(lbr.Buckets), Equals, 2)
  294. // ListBuckets, specified max keys
  295. lbr, err = client.ListBuckets(Marker(bucketNameLbOne), MaxKeys(1))
  296. c.Assert(err, IsNil)
  297. c.Assert(len(lbr.Buckets), Equals, 1)
  298. // ListBuckets, specified max keys
  299. lbr, err = client.ListBuckets(Marker(bucketNameLbOne))
  300. c.Assert(err, IsNil)
  301. c.Assert(len(lbr.Buckets) >= 2, Equals, true)
  302. // DeleteBucket
  303. err = client.DeleteBucket(bucketNameLbOne)
  304. c.Assert(err, IsNil)
  305. err = client.DeleteBucket(bucketNameLbTwo)
  306. c.Assert(err, IsNil)
  307. err = client.DeleteBucket(bucketNameLbThree)
  308. c.Assert(err, IsNil)
  309. }
  310. // TestListBucket
  311. func (s *OssClientSuite) TestIsBucketExist(c *C) {
  312. var bucketNameLbOne = bucketNamePrefix + "tibe1"
  313. var bucketNameLbTwo = bucketNamePrefix + "tibe11"
  314. var bucketNameLbThree = bucketNamePrefix + "tibe111"
  315. client, err := New(endpoint, accessID, accessKey)
  316. c.Assert(err, IsNil)
  317. // CreateBucket
  318. err = client.CreateBucket(bucketNameLbOne)
  319. c.Assert(err, IsNil)
  320. err = client.CreateBucket(bucketNameLbTwo)
  321. c.Assert(err, IsNil)
  322. err = client.CreateBucket(bucketNameLbThree)
  323. c.Assert(err, IsNil)
  324. // exist
  325. exist, err := client.IsBucketExist(bucketNameLbTwo)
  326. c.Assert(err, IsNil)
  327. c.Assert(exist, Equals, true)
  328. exist, err = client.IsBucketExist(bucketNameLbThree)
  329. c.Assert(err, IsNil)
  330. c.Assert(exist, Equals, true)
  331. exist, err = client.IsBucketExist(bucketNameLbOne)
  332. c.Assert(err, IsNil)
  333. c.Assert(exist, Equals, true)
  334. // not exist
  335. exist, err = client.IsBucketExist(bucketNamePrefix + "tibe")
  336. c.Assert(err, IsNil)
  337. c.Assert(exist, Equals, false)
  338. exist, err = client.IsBucketExist(bucketNamePrefix + "tibe1111")
  339. c.Assert(err, IsNil)
  340. c.Assert(exist, Equals, false)
  341. // negative
  342. exist, err = client.IsBucketExist("BucketNameInvalid")
  343. c.Assert(err, NotNil)
  344. // DeleteBucket
  345. err = client.DeleteBucket(bucketNameLbOne)
  346. c.Assert(err, IsNil)
  347. err = client.DeleteBucket(bucketNameLbTwo)
  348. c.Assert(err, IsNil)
  349. err = client.DeleteBucket(bucketNameLbThree)
  350. c.Assert(err, IsNil)
  351. }
  352. // TestSetBucketAcl
  353. func (s *OssClientSuite) TestSetBucketAcl(c *C) {
  354. var bucketNameTest = bucketNamePrefix + "tsba"
  355. client, err := New(endpoint, accessID, accessKey)
  356. c.Assert(err, IsNil)
  357. // Private
  358. err = client.CreateBucket(bucketNameTest)
  359. c.Assert(err, IsNil)
  360. res, err := client.GetBucketACL(bucketNameTest)
  361. c.Assert(err, IsNil)
  362. c.Assert(res.ACL, Equals, string(ACLPrivate))
  363. // set ACL_PUBLIC_R
  364. err = client.SetBucketACL(bucketNameTest, ACLPublicRead)
  365. c.Assert(err, IsNil)
  366. time.Sleep(5 * time.Second)
  367. res, err = client.GetBucketACL(bucketNameTest)
  368. c.Assert(err, IsNil)
  369. c.Assert(res.ACL, Equals, string(ACLPublicRead))
  370. // set ACL_PUBLIC_RW
  371. err = client.SetBucketACL(bucketNameTest, ACLPublicReadWrite)
  372. c.Assert(err, IsNil)
  373. time.Sleep(5 * time.Second)
  374. res, err = client.GetBucketACL(bucketNameTest)
  375. c.Assert(err, IsNil)
  376. c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
  377. // set ACL_PUBLIC_RW
  378. err = client.SetBucketACL(bucketNameTest, ACLPrivate)
  379. c.Assert(err, IsNil)
  380. err = client.SetBucketACL(bucketNameTest, ACLPrivate)
  381. c.Assert(err, IsNil)
  382. time.Sleep(5 * time.Second)
  383. res, err = client.GetBucketACL(bucketNameTest)
  384. c.Assert(err, IsNil)
  385. c.Assert(res.ACL, Equals, string(ACLPrivate))
  386. err = client.DeleteBucket(bucketNameTest)
  387. c.Assert(err, IsNil)
  388. }
  389. // TestSetBucketAclNegative
  390. func (s *OssClientSuite) TestBucketAclNegative(c *C) {
  391. var bucketNameTest = bucketNamePrefix + "tsban"
  392. client, err := New(endpoint, accessID, accessKey)
  393. c.Assert(err, IsNil)
  394. err = client.CreateBucket(bucketNameTest)
  395. c.Assert(err, IsNil)
  396. err = client.SetBucketACL(bucketNameTest, "InvalidACL")
  397. c.Assert(err, NotNil)
  398. testLogger.Println(err)
  399. err = client.DeleteBucket(bucketNameTest)
  400. c.Assert(err, IsNil)
  401. }
  402. // TestGetBucketAcl
  403. func (s *OssClientSuite) TestGetBucketAcl(c *C) {
  404. var bucketNameTest = bucketNamePrefix + "tgba"
  405. client, err := New(endpoint, accessID, accessKey)
  406. c.Assert(err, IsNil)
  407. // Private
  408. err = client.CreateBucket(bucketNameTest)
  409. c.Assert(err, IsNil)
  410. res, err := client.GetBucketACL(bucketNameTest)
  411. c.Assert(err, IsNil)
  412. c.Assert(res.ACL, Equals, string(ACLPrivate))
  413. err = client.DeleteBucket(bucketNameTest)
  414. c.Assert(err, IsNil)
  415. time.Sleep(5 * time.Second)
  416. // PublicRead
  417. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicRead))
  418. c.Assert(err, IsNil)
  419. time.Sleep(5 * time.Second)
  420. res, err = client.GetBucketACL(bucketNameTest)
  421. c.Assert(err, IsNil)
  422. c.Assert(res.ACL, Equals, string(ACLPublicRead))
  423. err = client.DeleteBucket(bucketNameTest)
  424. c.Assert(err, IsNil)
  425. time.Sleep(5 * time.Second)
  426. // PublicReadWrite
  427. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicReadWrite))
  428. c.Assert(err, IsNil)
  429. time.Sleep(5 * time.Second)
  430. res, err = client.GetBucketACL(bucketNameTest)
  431. c.Assert(err, IsNil)
  432. c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
  433. err = client.DeleteBucket(bucketNameTest)
  434. c.Assert(err, IsNil)
  435. }
  436. // TestGetBucketAcl
  437. func (s *OssClientSuite) TestGetBucketLocation(c *C) {
  438. var bucketNameTest = bucketNamePrefix + "tgbl"
  439. client, err := New(endpoint, accessID, accessKey)
  440. c.Assert(err, IsNil)
  441. // Private
  442. err = client.CreateBucket(bucketNameTest)
  443. c.Assert(err, IsNil)
  444. loc, err := client.GetBucketLocation(bucketNameTest)
  445. c.Assert(strings.HasPrefix(loc, "oss-"), Equals, true)
  446. err = client.DeleteBucket(bucketNameTest)
  447. c.Assert(err, IsNil)
  448. }
  449. // TestGetBucketLocationNegative
  450. func (s *OssClientSuite) TestGetBucketLocationNegative(c *C) {
  451. var bucketNameTest = bucketNamePrefix + "tgblg"
  452. client, err := New(endpoint, accessID, accessKey)
  453. c.Assert(err, IsNil)
  454. // not exist
  455. _, err = client.GetBucketLocation(bucketNameTest)
  456. c.Assert(err, NotNil)
  457. // not exist
  458. _, err = client.GetBucketLocation("InvalidBucketName_")
  459. c.Assert(err, NotNil)
  460. }
  461. // TestSetBucketLifecycle
  462. func (s *OssClientSuite) TestSetBucketLifecycle(c *C) {
  463. var bucketNameTest = bucketNamePrefix + "tsbl"
  464. var rule1 = BuildLifecycleRuleByDate("idone", "one", true, 2015, 11, 11)
  465. var rule2 = BuildLifecycleRuleByDays("idtwo", "two", true, 3)
  466. client, err := New(endpoint, accessID, accessKey)
  467. c.Assert(err, IsNil)
  468. err = client.CreateBucket(bucketNameTest)
  469. c.Assert(err, IsNil)
  470. // set single rule
  471. var rules = []LifecycleRule{rule1}
  472. err = client.SetBucketLifecycle(bucketNameTest, rules)
  473. c.Assert(err, IsNil)
  474. // double set rule
  475. err = client.SetBucketLifecycle(bucketNameTest, rules)
  476. c.Assert(err, IsNil)
  477. res, err := client.GetBucketLifecycle(bucketNameTest)
  478. c.Assert(err, IsNil)
  479. c.Assert(len(res.Rules), Equals, 1)
  480. c.Assert(res.Rules[0].ID, Equals, "idone")
  481. err = client.DeleteBucketLifecycle(bucketNameTest)
  482. c.Assert(err, IsNil)
  483. // set two rules
  484. rules = []LifecycleRule{rule1, rule2}
  485. err = client.SetBucketLifecycle(bucketNameTest, rules)
  486. c.Assert(err, IsNil)
  487. // eliminate effect of cache
  488. time.Sleep(5 * time.Second)
  489. res, err = client.GetBucketLifecycle(bucketNameTest)
  490. c.Assert(err, IsNil)
  491. c.Assert(len(res.Rules), Equals, 2)
  492. c.Assert(res.Rules[0].ID, Equals, "idone")
  493. c.Assert(res.Rules[1].ID, Equals, "idtwo")
  494. err = client.DeleteBucket(bucketNameTest)
  495. c.Assert(err, IsNil)
  496. }
  497. // TestDeleteBucketLifecycle
  498. func (s *OssClientSuite) TestDeleteBucketLifecycle(c *C) {
  499. var bucketNameTest = bucketNamePrefix + "tdbl"
  500. var rule1 = BuildLifecycleRuleByDate("idone", "one", true, 2015, 11, 11)
  501. var rule2 = BuildLifecycleRuleByDays("idtwo", "two", true, 3)
  502. var rules = []LifecycleRule{rule1, rule2}
  503. client, err := New(endpoint, accessID, accessKey)
  504. c.Assert(err, IsNil)
  505. err = client.CreateBucket(bucketNameTest)
  506. c.Assert(err, IsNil)
  507. err = client.DeleteBucketLifecycle(bucketNameTest)
  508. c.Assert(err, IsNil)
  509. err = client.SetBucketLifecycle(bucketNameTest, rules)
  510. c.Assert(err, IsNil)
  511. time.Sleep(5 * time.Second)
  512. res, err := client.GetBucketLifecycle(bucketNameTest)
  513. c.Assert(err, IsNil)
  514. c.Assert(len(res.Rules), Equals, 2)
  515. // delete
  516. err = client.DeleteBucketLifecycle(bucketNameTest)
  517. c.Assert(err, IsNil)
  518. time.Sleep(5 * time.Second)
  519. res, err = client.GetBucketLifecycle(bucketNameTest)
  520. c.Assert(err, NotNil)
  521. // eliminate effect of cache
  522. time.Sleep(time.Second * 3)
  523. // delete when not set
  524. err = client.DeleteBucketLifecycle(bucketNameTest)
  525. c.Assert(err, IsNil)
  526. err = client.DeleteBucket(bucketNameTest)
  527. c.Assert(err, IsNil)
  528. }
  529. // TestSetBucketLifecycleNegative
  530. func (s *OssClientSuite) TestBucketLifecycleNegative(c *C) {
  531. var bucketNameTest = bucketNamePrefix + "tsbln"
  532. var rules = []LifecycleRule{}
  533. client, err := New(endpoint, accessID, accessKey)
  534. c.Assert(err, IsNil)
  535. err = client.CreateBucket(bucketNameTest)
  536. c.Assert(err, IsNil)
  537. // set with no rule
  538. err = client.SetBucketLifecycle(bucketNameTest, rules)
  539. c.Assert(err, NotNil)
  540. err = client.DeleteBucket(bucketNameTest)
  541. c.Assert(err, IsNil)
  542. // not exist
  543. err = client.SetBucketLifecycle(bucketNameTest, rules)
  544. c.Assert(err, NotNil)
  545. // not exist
  546. _, err = client.GetBucketLifecycle(bucketNameTest)
  547. c.Assert(err, NotNil)
  548. // not exist
  549. err = client.DeleteBucketLifecycle(bucketNameTest)
  550. c.Assert(err, NotNil)
  551. }
  552. // TestSetBucketReferer
  553. func (s *OssClientSuite) TestSetBucketReferer(c *C) {
  554. var bucketNameTest = bucketNamePrefix + "tsbr"
  555. var referers = []string{"http://www.aliyun.com", "https://www.aliyun.com"}
  556. client, err := New(endpoint, accessID, accessKey)
  557. c.Assert(err, IsNil)
  558. err = client.CreateBucket(bucketNameTest)
  559. c.Assert(err, IsNil)
  560. res, err := client.GetBucketReferer(bucketNameTest)
  561. c.Assert(res.AllowEmptyReferer, Equals, true)
  562. c.Assert(len(res.RefererList), Equals, 0)
  563. // set referers
  564. err = client.SetBucketReferer(bucketNameTest, referers, false)
  565. c.Assert(err, IsNil)
  566. time.Sleep(5 * time.Second)
  567. res, err = client.GetBucketReferer(bucketNameTest)
  568. c.Assert(res.AllowEmptyReferer, Equals, false)
  569. c.Assert(len(res.RefererList), Equals, 2)
  570. c.Assert(res.RefererList[0], Equals, "http://www.aliyun.com")
  571. c.Assert(res.RefererList[1], Equals, "https://www.aliyun.com")
  572. // reset referer, referers empty
  573. referers = []string{""}
  574. err = client.SetBucketReferer(bucketNameTest, referers, true)
  575. c.Assert(err, IsNil)
  576. referers = []string{}
  577. err = client.SetBucketReferer(bucketNameTest, referers, true)
  578. c.Assert(err, IsNil)
  579. res, err = client.GetBucketReferer(bucketNameTest)
  580. c.Assert(res.AllowEmptyReferer, Equals, true)
  581. c.Assert(len(res.RefererList), Equals, 0)
  582. err = client.DeleteBucket(bucketNameTest)
  583. c.Assert(err, IsNil)
  584. }
  585. // TestSetBucketRefererNegative
  586. func (s *OssClientSuite) TestBucketRefererNegative(c *C) {
  587. var bucketNameTest = bucketNamePrefix + "tsbrn"
  588. var referers = []string{""}
  589. client, err := New(endpoint, accessID, accessKey)
  590. c.Assert(err, IsNil)
  591. // not exist
  592. _, err = client.GetBucketReferer(bucketNameTest)
  593. c.Assert(err, NotNil)
  594. testLogger.Println(err)
  595. // not exist
  596. err = client.SetBucketReferer(bucketNameTest, referers, true)
  597. c.Assert(err, NotNil)
  598. testLogger.Println(err)
  599. }
  600. // TestSetBucketLogging
  601. func (s *OssClientSuite) TestSetBucketLogging(c *C) {
  602. var bucketNameTest = bucketNamePrefix + "tsbll"
  603. var bucketNameTarget = bucketNamePrefix + "tsbllt"
  604. client, err := New(endpoint, accessID, accessKey)
  605. c.Assert(err, IsNil)
  606. err = client.CreateBucket(bucketNameTest)
  607. c.Assert(err, IsNil)
  608. err = client.CreateBucket(bucketNameTarget)
  609. c.Assert(err, IsNil)
  610. time.Sleep(5 * time.Second)
  611. // set logging
  612. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
  613. c.Assert(err, IsNil)
  614. // reset
  615. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", false)
  616. c.Assert(err, IsNil)
  617. time.Sleep(5 * time.Second)
  618. res, err := client.GetBucketLogging(bucketNameTest)
  619. c.Assert(err, IsNil)
  620. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  621. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  622. err = client.DeleteBucketLogging(bucketNameTest)
  623. c.Assert(err, IsNil)
  624. // set to self
  625. err = client.SetBucketLogging(bucketNameTest, bucketNameTest, "prefix", true)
  626. c.Assert(err, IsNil)
  627. err = client.DeleteBucket(bucketNameTest)
  628. c.Assert(err, IsNil)
  629. err = client.DeleteBucket(bucketNameTarget)
  630. c.Assert(err, IsNil)
  631. }
  632. // TestDeleteBucketLogging
  633. func (s *OssClientSuite) TestDeleteBucketLogging(c *C) {
  634. var bucketNameTest = bucketNamePrefix + "tdbl"
  635. var bucketNameTarget = bucketNamePrefix + "tdblt"
  636. client, err := New(endpoint, accessID, accessKey)
  637. c.Assert(err, IsNil)
  638. err = client.CreateBucket(bucketNameTest)
  639. c.Assert(err, IsNil)
  640. err = client.CreateBucket(bucketNameTarget)
  641. c.Assert(err, IsNil)
  642. // get when not set
  643. res, err := client.GetBucketLogging(bucketNameTest)
  644. c.Assert(err, IsNil)
  645. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  646. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  647. // set
  648. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
  649. c.Assert(err, IsNil)
  650. // get
  651. time.Sleep(5 * time.Second)
  652. res, err = client.GetBucketLogging(bucketNameTest)
  653. c.Assert(err, IsNil)
  654. c.Assert(res.LoggingEnabled.TargetBucket, Equals, bucketNameTarget)
  655. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "prefix")
  656. // set
  657. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", false)
  658. c.Assert(err, IsNil)
  659. // get
  660. time.Sleep(5 * time.Second)
  661. res, err = client.GetBucketLogging(bucketNameTest)
  662. c.Assert(err, IsNil)
  663. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  664. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  665. // delete
  666. err = client.DeleteBucketLogging(bucketNameTest)
  667. c.Assert(err, IsNil)
  668. // get after delete
  669. time.Sleep(5 * time.Second)
  670. res, err = client.GetBucketLogging(bucketNameTest)
  671. c.Assert(err, IsNil)
  672. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  673. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  674. err = client.DeleteBucket(bucketNameTest)
  675. c.Assert(err, IsNil)
  676. err = client.DeleteBucket(bucketNameTarget)
  677. c.Assert(err, IsNil)
  678. }
  679. // TestSetBucketLoggingNegative
  680. func (s *OssClientSuite) TestSetBucketLoggingNegative(c *C) {
  681. var bucketNameTest = bucketNamePrefix + "tsblnn"
  682. var bucketNameTarget = bucketNamePrefix + "tsblnnt"
  683. client, err := New(endpoint, accessID, accessKey)
  684. c.Assert(err, IsNil)
  685. // not exist
  686. _, err = client.GetBucketLogging(bucketNameTest)
  687. c.Assert(err, NotNil)
  688. // not exist
  689. err = client.SetBucketLogging(bucketNameTest, "targetbucket", "prefix", true)
  690. c.Assert(err, NotNil)
  691. // not exist
  692. err = client.DeleteBucketLogging(bucketNameTest)
  693. c.Assert(err, NotNil)
  694. err = client.CreateBucket(bucketNameTest)
  695. c.Assert(err, IsNil)
  696. time.Sleep(5 * time.Second)
  697. // target bucket not exist
  698. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
  699. c.Assert(err, NotNil)
  700. // parameter invalid
  701. err = client.SetBucketLogging(bucketNameTest, "XXXX", "prefix", true)
  702. c.Assert(err, NotNil)
  703. err = client.SetBucketLogging(bucketNameTest, "xx", "prefix", true)
  704. c.Assert(err, NotNil)
  705. err = client.DeleteBucket(bucketNameTest)
  706. c.Assert(err, IsNil)
  707. }
  708. // TestSetBucketWebsite
  709. func (s *OssClientSuite) TestSetBucketWebsite(c *C) {
  710. var bucketNameTest = bucketNamePrefix + "tsbw"
  711. var indexWebsite = "myindex.html"
  712. var errorWebsite = "myerror.html"
  713. client, err := New(endpoint, accessID, accessKey)
  714. c.Assert(err, IsNil)
  715. err = client.CreateBucket(bucketNameTest)
  716. c.Assert(err, IsNil)
  717. // set
  718. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  719. c.Assert(err, IsNil)
  720. // double set
  721. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  722. c.Assert(err, IsNil)
  723. res, err := client.GetBucketWebsite(bucketNameTest)
  724. c.Assert(err, IsNil)
  725. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  726. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  727. // reset
  728. err = client.SetBucketWebsite(bucketNameTest, "your"+indexWebsite, "your"+errorWebsite)
  729. c.Assert(err, IsNil)
  730. time.Sleep(5 * time.Second)
  731. res, err = client.GetBucketWebsite(bucketNameTest)
  732. c.Assert(err, IsNil)
  733. c.Assert(res.IndexDocument.Suffix, Equals, "your"+indexWebsite)
  734. c.Assert(res.ErrorDocument.Key, Equals, "your"+errorWebsite)
  735. err = client.DeleteBucketWebsite(bucketNameTest)
  736. c.Assert(err, IsNil)
  737. // set after delete
  738. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  739. c.Assert(err, IsNil)
  740. // eliminate effect of cache
  741. time.Sleep(5 * time.Second)
  742. res, err = client.GetBucketWebsite(bucketNameTest)
  743. c.Assert(err, IsNil)
  744. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  745. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  746. err = client.DeleteBucket(bucketNameTest)
  747. c.Assert(err, IsNil)
  748. }
  749. // TestDeleteBucketWebsite
  750. func (s *OssClientSuite) TestDeleteBucketWebsite(c *C) {
  751. var bucketNameTest = bucketNamePrefix + "tdbw"
  752. var indexWebsite = "myindex.html"
  753. var errorWebsite = "myerror.html"
  754. client, err := New(endpoint, accessID, accessKey)
  755. c.Assert(err, IsNil)
  756. err = client.CreateBucket(bucketNameTest)
  757. c.Assert(err, IsNil)
  758. // get
  759. res, err := client.GetBucketWebsite(bucketNameTest)
  760. c.Assert(err, NotNil)
  761. // detele without set
  762. err = client.DeleteBucketWebsite(bucketNameTest)
  763. c.Assert(err, IsNil)
  764. // set
  765. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  766. c.Assert(err, IsNil)
  767. time.Sleep(5 * time.Second)
  768. res, err = client.GetBucketWebsite(bucketNameTest)
  769. c.Assert(err, IsNil)
  770. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  771. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  772. // detele
  773. time.Sleep(5 * time.Second)
  774. err = client.DeleteBucketWebsite(bucketNameTest)
  775. c.Assert(err, IsNil)
  776. time.Sleep(5 * time.Second)
  777. res, err = client.GetBucketWebsite(bucketNameTest)
  778. c.Assert(err, NotNil)
  779. // detele after delete
  780. err = client.DeleteBucketWebsite(bucketNameTest)
  781. c.Assert(err, IsNil)
  782. err = client.DeleteBucket(bucketNameTest)
  783. c.Assert(err, IsNil)
  784. }
  785. // TestSetBucketWebsiteNegative
  786. func (s *OssClientSuite) TestSetBucketWebsiteNegative(c *C) {
  787. var bucketNameTest = bucketNamePrefix + "tdbw"
  788. var indexWebsite = "myindex.html"
  789. var errorWebsite = "myerror.html"
  790. client, err := New(endpoint, accessID, accessKey)
  791. c.Assert(err, IsNil)
  792. err = client.DeleteBucket(bucketNameTest)
  793. // not exist
  794. _, err = client.GetBucketWebsite(bucketNameTest)
  795. c.Assert(err, NotNil)
  796. err = client.DeleteBucketWebsite(bucketNameTest)
  797. c.Assert(err, NotNil)
  798. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  799. c.Assert(err, NotNil)
  800. err = client.CreateBucket(bucketNameTest)
  801. c.Assert(err, IsNil)
  802. // set
  803. time.Sleep(5 * time.Second)
  804. err = client.SetBucketWebsite(bucketNameTest, "myindex", "myerror")
  805. c.Assert(err, IsNil)
  806. res, err := client.GetBucketWebsite(bucketNameTest)
  807. c.Assert(err, IsNil)
  808. c.Assert(res.IndexDocument.Suffix, Equals, "myindex")
  809. c.Assert(res.ErrorDocument.Key, Equals, "myerror")
  810. // detele
  811. err = client.DeleteBucketWebsite(bucketNameTest)
  812. c.Assert(err, IsNil)
  813. time.Sleep(5 * time.Second)
  814. _, err = client.GetBucketWebsite(bucketNameTest)
  815. c.Assert(err, NotNil)
  816. // detele after delete
  817. err = client.DeleteBucketWebsite(bucketNameTest)
  818. c.Assert(err, IsNil)
  819. err = client.DeleteBucket(bucketNameTest)
  820. c.Assert(err, IsNil)
  821. }
  822. // TestSetBucketWebsite
  823. func (s *OssClientSuite) TestSetBucketCORS(c *C) {
  824. var bucketNameTest = bucketNamePrefix + "tsbc"
  825. var rule1 = CORSRule{
  826. AllowedOrigin: []string{"*"},
  827. AllowedMethod: []string{"PUT", "GET", "POST"},
  828. AllowedHeader: []string{},
  829. ExposeHeader: []string{},
  830. MaxAgeSeconds: 100,
  831. }
  832. var rule2 = CORSRule{
  833. AllowedOrigin: []string{"http://www.a.com", "http://www.b.com"},
  834. AllowedMethod: []string{"GET"},
  835. AllowedHeader: []string{"Authorization"},
  836. ExposeHeader: []string{"x-oss-test", "x-oss-test1"},
  837. MaxAgeSeconds: 200,
  838. }
  839. client, err := New(endpoint, accessID, accessKey)
  840. c.Assert(err, IsNil)
  841. err = client.CreateBucket(bucketNameTest)
  842. c.Assert(err, IsNil)
  843. time.Sleep(5 * time.Second)
  844. // set
  845. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1})
  846. c.Assert(err, IsNil)
  847. gbcr, err := client.GetBucketCORS(bucketNameTest)
  848. c.Assert(err, IsNil)
  849. c.Assert(len(gbcr.CORSRules), Equals, 1)
  850. c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 1)
  851. c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 3)
  852. c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 0)
  853. c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 0)
  854. c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 100)
  855. // double set
  856. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1})
  857. c.Assert(err, IsNil)
  858. gbcr, err = client.GetBucketCORS(bucketNameTest)
  859. c.Assert(err, IsNil)
  860. c.Assert(len(gbcr.CORSRules), Equals, 1)
  861. c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 1)
  862. c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 3)
  863. c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 0)
  864. c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 0)
  865. c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 100)
  866. // set rule2
  867. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule2})
  868. c.Assert(err, IsNil)
  869. time.Sleep(5 * time.Second)
  870. gbcr, err = client.GetBucketCORS(bucketNameTest)
  871. c.Assert(err, IsNil)
  872. c.Assert(len(gbcr.CORSRules), Equals, 1)
  873. c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 2)
  874. c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 1)
  875. c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 1)
  876. c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 2)
  877. c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 200)
  878. // reset
  879. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1, rule2})
  880. c.Assert(err, IsNil)
  881. time.Sleep(5 * time.Second)
  882. gbcr, err = client.GetBucketCORS(bucketNameTest)
  883. c.Assert(err, IsNil)
  884. c.Assert(len(gbcr.CORSRules), Equals, 2)
  885. // set after delete
  886. err = client.DeleteBucketCORS(bucketNameTest)
  887. c.Assert(err, IsNil)
  888. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1, rule2})
  889. c.Assert(err, IsNil)
  890. time.Sleep(5 * time.Second)
  891. gbcr, err = client.GetBucketCORS(bucketNameTest)
  892. c.Assert(err, IsNil)
  893. c.Assert(len(gbcr.CORSRules), Equals, 2)
  894. err = client.DeleteBucketCORS(bucketNameTest)
  895. c.Assert(err, IsNil)
  896. err = client.DeleteBucket(bucketNameTest)
  897. c.Assert(err, IsNil)
  898. }
  899. // TestDeleteBucketCORS
  900. func (s *OssClientSuite) TestDeleteBucketCORS(c *C) {
  901. var bucketNameTest = bucketNamePrefix + "tdbc"
  902. var rule = CORSRule{
  903. AllowedOrigin: []string{"*"},
  904. AllowedMethod: []string{"PUT", "GET", "POST"},
  905. AllowedHeader: []string{},
  906. ExposeHeader: []string{},
  907. MaxAgeSeconds: 100,
  908. }
  909. client, err := New(endpoint, accessID, accessKey)
  910. c.Assert(err, IsNil)
  911. err = client.CreateBucket(bucketNameTest)
  912. c.Assert(err, IsNil)
  913. // delete not set
  914. err = client.DeleteBucketCORS(bucketNameTest)
  915. c.Assert(err, IsNil)
  916. // set
  917. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
  918. c.Assert(err, IsNil)
  919. time.Sleep(5 * time.Second)
  920. _, err = client.GetBucketCORS(bucketNameTest)
  921. c.Assert(err, IsNil)
  922. // detele
  923. err = client.DeleteBucketCORS(bucketNameTest)
  924. c.Assert(err, IsNil)
  925. time.Sleep(5 * time.Second)
  926. _, err = client.GetBucketCORS(bucketNameTest)
  927. c.Assert(err, NotNil)
  928. // detele after delete
  929. err = client.DeleteBucketCORS(bucketNameTest)
  930. c.Assert(err, IsNil)
  931. err = client.DeleteBucket(bucketNameTest)
  932. c.Assert(err, IsNil)
  933. }
  934. // TestSetBucketCORSNegative
  935. func (s *OssClientSuite) TestSetBucketCORSNegative(c *C) {
  936. var bucketNameTest = bucketNamePrefix + "tsbcn"
  937. var rule = CORSRule{
  938. AllowedOrigin: []string{"*"},
  939. AllowedMethod: []string{"PUT", "GET", "POST"},
  940. AllowedHeader: []string{},
  941. ExposeHeader: []string{},
  942. MaxAgeSeconds: 100,
  943. }
  944. client, err := New(endpoint, accessID, accessKey)
  945. c.Assert(err, IsNil)
  946. err = client.DeleteBucket(bucketNameTest)
  947. // not exist
  948. _, err = client.GetBucketCORS(bucketNameTest)
  949. c.Assert(err, NotNil)
  950. err = client.DeleteBucketCORS(bucketNameTest)
  951. c.Assert(err, NotNil)
  952. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
  953. c.Assert(err, NotNil)
  954. err = client.CreateBucket(bucketNameTest)
  955. c.Assert(err, IsNil)
  956. time.Sleep(5 * time.Second)
  957. _, err = client.GetBucketCORS(bucketNameTest)
  958. c.Assert(err, NotNil)
  959. // set
  960. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
  961. c.Assert(err, IsNil)
  962. time.Sleep(5 * time.Second)
  963. _, err = client.GetBucketCORS(bucketNameTest)
  964. c.Assert(err, IsNil)
  965. // detele
  966. err = client.DeleteBucketCORS(bucketNameTest)
  967. c.Assert(err, IsNil)
  968. time.Sleep(5 * time.Second)
  969. _, err = client.GetBucketCORS(bucketNameTest)
  970. c.Assert(err, NotNil)
  971. // detele after delete
  972. err = client.DeleteBucketCORS(bucketNameTest)
  973. c.Assert(err, IsNil)
  974. err = client.DeleteBucket(bucketNameTest)
  975. c.Assert(err, IsNil)
  976. }
  977. // TestGetBucketInfo
  978. func (s *OssClientSuite) TestGetBucketInfo(c *C) {
  979. var bucketNameTest = bucketNamePrefix + "tgbi"
  980. client, err := New(endpoint, accessID, accessKey)
  981. c.Assert(err, IsNil)
  982. err = client.CreateBucket(bucketNameTest)
  983. c.Assert(err, IsNil)
  984. res, err := client.GetBucketInfo(bucketNameTest)
  985. c.Assert(err, IsNil)
  986. c.Assert(res.BucketInfo.Name, Equals, bucketNameTest)
  987. c.Assert(strings.HasPrefix(res.BucketInfo.Location, "oss-"), Equals, true)
  988. c.Assert(res.BucketInfo.ACL, Equals, "private")
  989. c.Assert(strings.HasSuffix(res.BucketInfo.ExtranetEndpoint, ".com"), Equals, true)
  990. c.Assert(strings.HasSuffix(res.BucketInfo.IntranetEndpoint, ".com"), Equals, true)
  991. c.Assert(res.BucketInfo.CreationDate, NotNil)
  992. err = client.DeleteBucket(bucketNameTest)
  993. c.Assert(err, IsNil)
  994. }
  995. // TestGetBucketInfoNegative
  996. func (s *OssClientSuite) TestGetBucketInfoNegative(c *C) {
  997. var bucketNameTest = bucketNamePrefix + "tgbig"
  998. client, err := New(endpoint, accessID, accessKey)
  999. c.Assert(err, IsNil)
  1000. // not exist
  1001. _, err = client.GetBucketInfo(bucketNameTest)
  1002. c.Assert(err, NotNil)
  1003. // bucket name invalid
  1004. _, err = client.GetBucketInfo("InvalidBucketName_")
  1005. c.Assert(err, NotNil)
  1006. }
  1007. // TestEndpointFormat
  1008. func (s *OssClientSuite) TestEndpointFormat(c *C) {
  1009. var bucketNameTest = bucketNamePrefix + "tef"
  1010. // http://host
  1011. client, err := New(endpoint, accessID, accessKey)
  1012. c.Assert(err, IsNil)
  1013. err = client.CreateBucket(bucketNameTest)
  1014. c.Assert(err, IsNil)
  1015. res, err := client.GetBucketACL(bucketNameTest)
  1016. c.Assert(err, IsNil)
  1017. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1018. err = client.DeleteBucket(bucketNameTest)
  1019. c.Assert(err, IsNil)
  1020. time.Sleep(5 * time.Second)
  1021. // http://host:port
  1022. client, err = New(endpoint+":80", accessID, accessKey)
  1023. c.Assert(err, IsNil)
  1024. err = client.CreateBucket(bucketNameTest)
  1025. c.Assert(err, IsNil)
  1026. time.Sleep(5 * time.Second)
  1027. res, err = client.GetBucketACL(bucketNameTest)
  1028. c.Assert(err, IsNil)
  1029. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1030. err = client.DeleteBucket(bucketNameTest)
  1031. c.Assert(err, IsNil)
  1032. }
  1033. // TestCname
  1034. func (s *OssClientSuite) _TestCname(c *C) {
  1035. var bucketNameTest = "<my-bucket-cname>"
  1036. client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>", UseCname(true))
  1037. c.Assert(err, IsNil)
  1038. err = client.CreateBucket(bucketNameTest)
  1039. c.Assert(err, IsNil)
  1040. _, err = client.ListBuckets()
  1041. c.Assert(err, NotNil)
  1042. res, err := client.GetBucketACL(bucketNameTest)
  1043. c.Assert(err, IsNil)
  1044. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1045. }
  1046. // TestCnameNegative
  1047. func (s *OssClientSuite) _TestCnameNegative(c *C) {
  1048. var bucketNameTest = "<my-bucket-cname>"
  1049. client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>", UseCname(true))
  1050. c.Assert(err, IsNil)
  1051. err = client.CreateBucket(bucketNameTest)
  1052. c.Assert(err, NotNil)
  1053. _, err = client.ListBuckets()
  1054. c.Assert(err, NotNil)
  1055. _, err = client.GetBucketACL(bucketNameTest)
  1056. c.Assert(err, NotNil)
  1057. }
  1058. // _TestHTTPS
  1059. func (s *OssClientSuite) _TestHTTPS(c *C) {
  1060. var bucketNameTest = "<my-bucket-https>"
  1061. client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>")
  1062. c.Assert(err, IsNil)
  1063. err = client.CreateBucket(bucketNameTest)
  1064. c.Assert(err, IsNil)
  1065. res, err := client.GetBucketACL(bucketNameTest)
  1066. c.Assert(err, IsNil)
  1067. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1068. err = client.DeleteBucket(bucketNameTest)
  1069. c.Assert(err, IsNil)
  1070. }
  1071. // TestClientOption
  1072. func (s *OssClientSuite) TestClientOption(c *C) {
  1073. var bucketNameTest = bucketNamePrefix + "tco"
  1074. client, err := New(endpoint, accessID, accessKey, UseCname(true),
  1075. Timeout(11, 12), SecurityToken("token"), Proxy(proxyHost))
  1076. c.Assert(err, IsNil)
  1077. // Create Bucket timeout
  1078. err = client.CreateBucket(bucketNameTest)
  1079. c.Assert(err, NotNil)
  1080. c.Assert(client.Conn.config.HTTPTimeout.ConnectTimeout, Equals, time.Second*11)
  1081. c.Assert(client.Conn.config.HTTPTimeout.ReadWriteTimeout, Equals, time.Second*12)
  1082. c.Assert(client.Conn.config.HTTPTimeout.HeaderTimeout, Equals, time.Second*12)
  1083. c.Assert(client.Conn.config.HTTPTimeout.LongTimeout, Equals, time.Second*12*10)
  1084. c.Assert(client.Conn.config.SecurityToken, Equals, "token")
  1085. c.Assert(client.Conn.config.IsCname, Equals, true)
  1086. c.Assert(client.Conn.config.IsUseProxy, Equals, true)
  1087. c.Assert(client.Config.ProxyHost, Equals, proxyHost)
  1088. client, err = New(endpoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
  1089. c.Assert(client.Conn.config.IsUseProxy, Equals, true)
  1090. c.Assert(client.Config.ProxyHost, Equals, proxyHost)
  1091. c.Assert(client.Conn.config.IsAuthProxy, Equals, true)
  1092. c.Assert(client.Conn.config.ProxyUser, Equals, proxyUser)
  1093. c.Assert(client.Conn.config.ProxyPassword, Equals, proxyPasswd)
  1094. client, err = New(endpoint, accessID, accessKey, UserAgent("go sdk user agent"))
  1095. c.Assert(client.Conn.config.UserAgent, Equals, "go sdk user agent")
  1096. }
  1097. // TestProxy
  1098. func (s *OssClientSuite) TestProxy(c *C) {
  1099. bucketNameTest := bucketNamePrefix + "tp"
  1100. objectName := "体育/奥运/首金"
  1101. objectValue := "大江东去,浪淘尽,千古风流人物。 故垒西边,人道是、三国周郎赤壁。"
  1102. client, err := New(endpoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
  1103. // Create bucket
  1104. err = client.CreateBucket(bucketNameTest)
  1105. c.Assert(err, IsNil)
  1106. // Get bucket info
  1107. _, err = client.GetBucketInfo(bucketNameTest)
  1108. c.Assert(err, IsNil)
  1109. bucket, err := client.Bucket(bucketNameTest)
  1110. // Sign url
  1111. str, err := bucket.SignURL(objectName, HTTPPut, 60)
  1112. c.Assert(err, IsNil)
  1113. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  1114. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  1115. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  1116. // Put object with url
  1117. err = bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  1118. c.Assert(err, IsNil)
  1119. // sign url for get object
  1120. str, err = bucket.SignURL(objectName, HTTPGet, 60)
  1121. c.Assert(err, IsNil)
  1122. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  1123. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  1124. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  1125. // Get object with url
  1126. body, err := bucket.GetObjectWithURL(str)
  1127. c.Assert(err, IsNil)
  1128. str, err = readBody(body)
  1129. c.Assert(err, IsNil)
  1130. c.Assert(str, Equals, objectValue)
  1131. // Put object
  1132. err = bucket.PutObject(objectName, strings.NewReader(objectValue))
  1133. c.Assert(err, IsNil)
  1134. // Get Object
  1135. _, err = bucket.GetObject(objectName)
  1136. c.Assert(err, IsNil)
  1137. // List Objects
  1138. _, err = bucket.ListObjects()
  1139. c.Assert(err, IsNil)
  1140. // Delete Object
  1141. err = bucket.DeleteObject(objectName)
  1142. c.Assert(err, IsNil)
  1143. // Delete bucket
  1144. err = client.DeleteBucket(bucketNameTest)
  1145. c.Assert(err, IsNil)
  1146. }
  1147. // private
  1148. func (s *OssClientSuite) checkBucket(buckets []BucketProperties, bucket string) bool {
  1149. for _, v := range buckets {
  1150. if v.Name == bucket {
  1151. return true
  1152. }
  1153. }
  1154. return false
  1155. }
  1156. func (s *OssClientSuite) getBucket(buckets []BucketProperties, bucket string) (bool, BucketProperties) {
  1157. for _, v := range buckets {
  1158. if v.Name == bucket {
  1159. return true, v
  1160. }
  1161. }
  1162. return false, BucketProperties{}
  1163. }