client_test.go 43 KB

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