client_test.go 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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-xyzv-"
  37. // Bucket name for object ops test
  38. bucketName = "go-sdk-test-bucket-xyzv-for-object"
  39. archiveBucketName = "go-sdk-test-bucket-xyzv-for-archive"
  40. // Object name for object ops test
  41. objectNamePrefix = "go-sdk-test-object-xyzv-"
  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. // 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. // CreateBucket creates 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 configuration and test GetBucketInfo
  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 configuration
  183. err = client.CreateBucket("ERRORBUCKETNAME", StorageClass(StorageArchive))
  184. c.Assert(err, NotNil)
  185. // Create bucket with configuration and test ListBuckets
  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. // Bucket name 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. // Bucket name 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 bucket
  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 deleting
  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. // Delete
  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. // Delete after deleting
  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. // CreateBucket 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.IdleConnTimeout, Equals, time.Second*12)
  1084. c.Assert(client.Conn.config.HTTPTimeout.LongTimeout, Equals, time.Second*12*10)
  1085. c.Assert(client.Conn.config.SecurityToken, Equals, "token")
  1086. c.Assert(client.Conn.config.IsCname, Equals, true)
  1087. c.Assert(client.Conn.config.IsUseProxy, Equals, true)
  1088. c.Assert(client.Config.ProxyHost, Equals, proxyHost)
  1089. client, err = New(endpoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
  1090. c.Assert(client.Conn.config.IsUseProxy, Equals, true)
  1091. c.Assert(client.Config.ProxyHost, Equals, proxyHost)
  1092. c.Assert(client.Conn.config.IsAuthProxy, Equals, true)
  1093. c.Assert(client.Conn.config.ProxyUser, Equals, proxyUser)
  1094. c.Assert(client.Conn.config.ProxyPassword, Equals, proxyPasswd)
  1095. client, err = New(endpoint, accessID, accessKey, UserAgent("go sdk user agent"))
  1096. c.Assert(client.Conn.config.UserAgent, Equals, "go sdk user agent")
  1097. }
  1098. // TestProxy
  1099. func (s *OssClientSuite) TestProxy(c *C) {
  1100. bucketNameTest := bucketNamePrefix + "tp"
  1101. objectName := "体育/奥运/首金"
  1102. objectValue := "大江东去,浪淘尽,千古风流人物。 故垒西边,人道是、三国周郎赤壁。"
  1103. client, err := New(endpoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
  1104. // Create bucket
  1105. err = client.CreateBucket(bucketNameTest)
  1106. c.Assert(err, IsNil)
  1107. // Get bucket info
  1108. _, err = client.GetBucketInfo(bucketNameTest)
  1109. c.Assert(err, IsNil)
  1110. bucket, err := client.Bucket(bucketNameTest)
  1111. // Sign URL
  1112. str, err := bucket.SignURL(objectName, HTTPPut, 60)
  1113. c.Assert(err, IsNil)
  1114. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  1115. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  1116. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  1117. // Put object with URL
  1118. err = bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  1119. c.Assert(err, IsNil)
  1120. // Sign URL for get object
  1121. str, err = bucket.SignURL(objectName, HTTPGet, 60)
  1122. c.Assert(err, IsNil)
  1123. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  1124. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  1125. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  1126. // Get object with URL
  1127. body, err := bucket.GetObjectWithURL(str)
  1128. c.Assert(err, IsNil)
  1129. str, err = readBody(body)
  1130. c.Assert(err, IsNil)
  1131. c.Assert(str, Equals, objectValue)
  1132. // Put object
  1133. err = bucket.PutObject(objectName, strings.NewReader(objectValue))
  1134. c.Assert(err, IsNil)
  1135. // Get object
  1136. _, err = bucket.GetObject(objectName)
  1137. c.Assert(err, IsNil)
  1138. // List objects
  1139. _, err = bucket.ListObjects()
  1140. c.Assert(err, IsNil)
  1141. // Delete object
  1142. err = bucket.DeleteObject(objectName)
  1143. c.Assert(err, IsNil)
  1144. // Delete bucket
  1145. err = client.DeleteBucket(bucketNameTest)
  1146. c.Assert(err, IsNil)
  1147. }
  1148. // Private
  1149. func (s *OssClientSuite) checkBucket(buckets []BucketProperties, bucket string) bool {
  1150. for _, v := range buckets {
  1151. if v.Name == bucket {
  1152. return true
  1153. }
  1154. }
  1155. return false
  1156. }
  1157. func (s *OssClientSuite) getBucket(buckets []BucketProperties, bucket string) (bool, BucketProperties) {
  1158. for _, v := range buckets {
  1159. if v.Name == bucket {
  1160. return true, v
  1161. }
  1162. }
  1163. return false, BucketProperties{}
  1164. }