client_test.go 41 KB

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