client_test.go 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019
  1. // client test
  2. // use gocheck, install gocheck to execute "go get gopkg.in/check.v1",
  3. // see https://labix.org/gocheck
  4. package oss
  5. import (
  6. "io/ioutil"
  7. "log"
  8. "math/rand"
  9. "net/http"
  10. "os"
  11. "runtime"
  12. "strings"
  13. "testing"
  14. "time"
  15. . "gopkg.in/check.v1"
  16. )
  17. // Test hooks up gocheck into the "go test" runner.
  18. func Test(t *testing.T) {
  19. TestingT(t)
  20. }
  21. type OssClientSuite struct{}
  22. var _ = Suite(&OssClientSuite{})
  23. var (
  24. // Endpoint/ID/Key
  25. endpoint = os.Getenv("OSS_TEST_ENDPOINT")
  26. accessID = os.Getenv("OSS_TEST_ACCESS_KEY_ID")
  27. accessKey = os.Getenv("OSS_TEST_ACCESS_KEY_SECRET")
  28. // Proxy
  29. proxyHost = os.Getenv("OSS_TEST_PROXY_HOST")
  30. proxyUser = os.Getenv("OSS_TEST_PROXY_USER")
  31. proxyPasswd = os.Getenv("OSS_TEST_PROXY_PASSWORD")
  32. // STS
  33. stsaccessID = os.Getenv("OSS_TEST_STS_ID")
  34. stsaccessKey = os.Getenv("OSS_TEST_STS_KEY")
  35. stsARN = os.Getenv("OSS_TEST_STS_ARN")
  36. )
  37. var (
  38. // prefix of bucket name for bucket ops test
  39. bucketNamePrefix = "go-sdk-test-bucket-"
  40. // bucket name for object ops test
  41. bucketName = bucketNamePrefix + randLowStr(6)
  42. archiveBucketName = bucketNamePrefix + "arch-" + randLowStr(6)
  43. // object name for object ops test
  44. objectNamePrefix = "go-sdk-test-object-"
  45. // sts region is one and only hangzhou
  46. stsRegion = "cn-hangzhou"
  47. )
  48. var (
  49. logPath = "go_sdk_test_" + time.Now().Format("20060102_150405") + ".log"
  50. testLogFile, _ = os.OpenFile(logPath, os.O_RDWR|os.O_CREATE, 0664)
  51. testLogger = log.New(testLogFile, "", log.Ldate|log.Ltime|log.Lshortfile)
  52. letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
  53. timeoutInOperation = 3 * time.Second
  54. )
  55. func randStr(n int) string {
  56. b := make([]rune, n)
  57. randMarker := rand.New(rand.NewSource(time.Now().UnixNano()))
  58. for i := range b {
  59. b[i] = letters[randMarker.Intn(len(letters))]
  60. }
  61. return string(b)
  62. }
  63. func createFile(fileName, content string, c *C) {
  64. fout, err := os.Create(fileName)
  65. defer fout.Close()
  66. c.Assert(err, IsNil)
  67. _, err = fout.WriteString(content)
  68. c.Assert(err, IsNil)
  69. }
  70. func randLowStr(n int) string {
  71. return strings.ToLower(randStr(n))
  72. }
  73. // SetUpSuite runs once when the suite starts running
  74. func (s *OssClientSuite) SetUpSuite(c *C) {
  75. client, err := New(endpoint, accessID, accessKey)
  76. c.Assert(err, IsNil)
  77. lbr, err := client.ListBuckets(Prefix(bucketNamePrefix), MaxKeys(1000))
  78. c.Assert(err, IsNil)
  79. for _, bucket := range lbr.Buckets {
  80. s.deleteBucket(client, bucket.Name, c)
  81. }
  82. testLogger.Println("test client started")
  83. }
  84. // TearDownSuite runs before each test or benchmark starts running
  85. func (s *OssClientSuite) TearDownSuite(c *C) {
  86. client, err := New(endpoint, accessID, accessKey)
  87. c.Assert(err, IsNil)
  88. lbr, err := client.ListBuckets(Prefix(bucketNamePrefix), MaxKeys(1000))
  89. c.Assert(err, IsNil)
  90. for _, bucket := range lbr.Buckets {
  91. s.deleteBucket(client, bucket.Name, c)
  92. }
  93. testLogger.Println("test client completed")
  94. }
  95. func (s *OssClientSuite) deleteBucket(client *Client, bucketName string, c *C) {
  96. bucket, err := client.Bucket(bucketName)
  97. c.Assert(err, IsNil)
  98. // Delete Object
  99. marker := Marker("")
  100. for {
  101. lor, err := bucket.ListObjects(marker)
  102. c.Assert(err, IsNil)
  103. for _, object := range lor.Objects {
  104. err = bucket.DeleteObject(object.Key)
  105. c.Assert(err, IsNil)
  106. }
  107. marker = Marker(lor.NextMarker)
  108. if !lor.IsTruncated {
  109. break
  110. }
  111. }
  112. // Delete Part
  113. keyMarker := KeyMarker("")
  114. uploadIDMarker := UploadIDMarker("")
  115. for {
  116. lmur, err := bucket.ListMultipartUploads(keyMarker, uploadIDMarker)
  117. c.Assert(err, IsNil)
  118. for _, upload := range lmur.Uploads {
  119. var imur = InitiateMultipartUploadResult{Bucket: bucketName,
  120. Key: upload.Key, UploadID: upload.UploadID}
  121. err = bucket.AbortMultipartUpload(imur)
  122. c.Assert(err, IsNil)
  123. }
  124. keyMarker = KeyMarker(lmur.NextKeyMarker)
  125. uploadIDMarker = UploadIDMarker(lmur.NextUploadIDMarker)
  126. if !lmur.IsTruncated {
  127. break
  128. }
  129. }
  130. // Delete Bucket
  131. err = client.DeleteBucket(bucketName)
  132. c.Assert(err, IsNil)
  133. }
  134. // SetUpTest runs after each test or benchmark runs
  135. func (s *OssClientSuite) SetUpTest(c *C) {
  136. }
  137. // TearDownTest runs once after all tests or benchmarks have finished running
  138. func (s *OssClientSuite) TearDownTest(c *C) {
  139. }
  140. // TestCreateBucket
  141. func (s *OssClientSuite) TestCreateBucket(c *C) {
  142. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  143. client, err := New(endpoint, accessID, accessKey)
  144. c.Assert(err, IsNil)
  145. // Create
  146. client.DeleteBucket(bucketNameTest)
  147. err = client.CreateBucket(bucketNameTest)
  148. c.Assert(err, IsNil)
  149. //sleep 3 seconds after create bucket
  150. time.Sleep(timeoutInOperation)
  151. // verify bucket is exist
  152. found, err := client.IsBucketExist(bucketNameTest)
  153. c.Assert(err, IsNil)
  154. c.Assert(found, Equals, true)
  155. res, err := client.GetBucketACL(bucketNameTest)
  156. c.Assert(err, IsNil)
  157. c.Assert(res.ACL, Equals, string(ACLPrivate))
  158. err = client.DeleteBucket(bucketNameTest)
  159. c.Assert(err, IsNil)
  160. time.Sleep(timeoutInOperation)
  161. // CreateBucket creates with ACLPublicRead
  162. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicRead))
  163. c.Assert(err, IsNil)
  164. time.Sleep(timeoutInOperation)
  165. res, err = client.GetBucketACL(bucketNameTest)
  166. c.Assert(err, IsNil)
  167. c.Assert(res.ACL, Equals, string(ACLPublicRead))
  168. err = client.DeleteBucket(bucketNameTest)
  169. c.Assert(err, IsNil)
  170. time.Sleep(timeoutInOperation)
  171. // ACLPublicReadWrite
  172. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicReadWrite))
  173. c.Assert(err, IsNil)
  174. time.Sleep(timeoutInOperation)
  175. res, err = client.GetBucketACL(bucketNameTest)
  176. c.Assert(err, IsNil)
  177. c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
  178. err = client.DeleteBucket(bucketNameTest)
  179. c.Assert(err, IsNil)
  180. time.Sleep(timeoutInOperation)
  181. // ACLPrivate
  182. err = client.CreateBucket(bucketNameTest, ACL(ACLPrivate))
  183. c.Assert(err, IsNil)
  184. time.Sleep(timeoutInOperation)
  185. res, err = client.GetBucketACL(bucketNameTest)
  186. c.Assert(err, IsNil)
  187. c.Assert(res.ACL, Equals, string(ACLPrivate))
  188. // Delete
  189. err = client.DeleteBucket(bucketNameTest)
  190. c.Assert(err, IsNil)
  191. // Create bucket with configuration and test GetBucketInfo
  192. for _, storage := range []StorageClassType{StorageStandard, StorageIA, StorageArchive} {
  193. bucketNameTest := bucketNamePrefix + randLowStr(6)
  194. err = client.CreateBucket(bucketNameTest, StorageClass(storage), ACL(ACLPublicRead))
  195. c.Assert(err, IsNil)
  196. time.Sleep(timeoutInOperation)
  197. res, err := client.GetBucketInfo(bucketNameTest)
  198. c.Assert(err, IsNil)
  199. c.Assert(res.BucketInfo.Name, Equals, bucketNameTest)
  200. c.Assert(res.BucketInfo.StorageClass, Equals, string(storage))
  201. c.Assert(res.BucketInfo.ACL, Equals, string(ACLPublicRead))
  202. // Delete
  203. err = client.DeleteBucket(bucketNameTest)
  204. c.Assert(err, IsNil)
  205. }
  206. // Error put bucket with configuration
  207. err = client.CreateBucket("ERRORBUCKETNAME", StorageClass(StorageArchive))
  208. c.Assert(err, NotNil)
  209. // Create bucket with configuration and test ListBuckets
  210. for _, storage := range []StorageClassType{StorageStandard, StorageIA, StorageArchive} {
  211. bucketNameTest := bucketNamePrefix + randLowStr(6)
  212. err = client.CreateBucket(bucketNameTest, StorageClass(storage))
  213. c.Assert(err, IsNil)
  214. time.Sleep(timeoutInOperation)
  215. res, err := client.GetBucketInfo(bucketNameTest)
  216. c.Assert(err, IsNil)
  217. c.Assert(res.BucketInfo.Name, Equals, bucketNameTest)
  218. c.Assert(res.BucketInfo.StorageClass, Equals, string(storage))
  219. // Delete
  220. err = client.DeleteBucket(bucketNameTest)
  221. c.Assert(err, IsNil)
  222. }
  223. }
  224. // TestCreateBucketNegative
  225. func (s *OssClientSuite) TestCreateBucketNegative(c *C) {
  226. client, err := New(endpoint, accessID, accessKey)
  227. c.Assert(err, IsNil)
  228. // Bucket name invalid
  229. err = client.CreateBucket("xx")
  230. c.Assert(err, NotNil)
  231. err = client.CreateBucket("XXXX")
  232. c.Assert(err, NotNil)
  233. testLogger.Println(err)
  234. err = client.CreateBucket("_bucket")
  235. c.Assert(err, NotNil)
  236. testLogger.Println(err)
  237. // ACL invalid
  238. err = client.CreateBucket(bucketNamePrefix+randLowStr(6), ACL("InvaldAcl"))
  239. c.Assert(err, NotNil)
  240. testLogger.Println(err)
  241. }
  242. // TestDeleteBucket
  243. func (s *OssClientSuite) TestDeleteBucket(c *C) {
  244. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  245. client, err := New(endpoint, accessID, accessKey)
  246. c.Assert(err, IsNil)
  247. // Create
  248. err = client.CreateBucket(bucketNameTest)
  249. c.Assert(err, IsNil)
  250. time.Sleep(timeoutInOperation)
  251. // Check
  252. found, err := client.IsBucketExist(bucketNameTest)
  253. c.Assert(err, IsNil)
  254. c.Assert(found, Equals, true)
  255. // Delete
  256. err = client.DeleteBucket(bucketNameTest)
  257. c.Assert(err, IsNil)
  258. time.Sleep(timeoutInOperation)
  259. // Check
  260. found, err = client.IsBucketExist(bucketNameTest)
  261. c.Assert(err, IsNil)
  262. c.Assert(found, Equals, false)
  263. err = client.DeleteBucket(bucketNameTest)
  264. c.Assert(err, NotNil)
  265. }
  266. // TestDeleteBucketNegative
  267. func (s *OssClientSuite) TestDeleteBucketNegative(c *C) {
  268. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  269. client, err := New(endpoint, accessID, accessKey)
  270. c.Assert(err, IsNil)
  271. // Bucket name invalid
  272. err = client.DeleteBucket("xx")
  273. c.Assert(err, NotNil)
  274. err = client.DeleteBucket("XXXX")
  275. c.Assert(err, NotNil)
  276. err = client.DeleteBucket("_bucket")
  277. c.Assert(err, NotNil)
  278. // Delete no exist bucket
  279. err = client.DeleteBucket("notexist")
  280. c.Assert(err, NotNil)
  281. // No permission to delete, this ak/sk for js sdk
  282. err = client.CreateBucket(bucketNameTest)
  283. c.Assert(err, IsNil)
  284. accessID := "<accessKeyId>"
  285. accessKey := "<accessKeySecret>"
  286. clientOtherUser, err := New(endpoint, accessID, accessKey)
  287. c.Assert(err, IsNil)
  288. err = clientOtherUser.DeleteBucket(bucketNameTest)
  289. c.Assert(err, NotNil)
  290. err = client.DeleteBucket(bucketNameTest)
  291. c.Assert(err, IsNil)
  292. }
  293. // TestListBucket
  294. func (s *OssClientSuite) TestListBucket(c *C) {
  295. var prefix = bucketNamePrefix + randLowStr(6)
  296. var bucketNameLbOne = prefix + "tlb1"
  297. var bucketNameLbTwo = prefix + "tlb2"
  298. var bucketNameLbThree = prefix + "tlb3"
  299. client, err := New(endpoint, accessID, accessKey)
  300. c.Assert(err, IsNil)
  301. // CreateBucket
  302. err = client.CreateBucket(bucketNameLbOne)
  303. c.Assert(err, IsNil)
  304. err = client.CreateBucket(bucketNameLbTwo)
  305. c.Assert(err, IsNil)
  306. err = client.CreateBucket(bucketNameLbThree)
  307. c.Assert(err, IsNil)
  308. // ListBuckets, specified prefix
  309. lbr, err := client.ListBuckets(Prefix(prefix), MaxKeys(2))
  310. c.Assert(err, IsNil)
  311. c.Assert(len(lbr.Buckets), Equals, 2)
  312. // ListBuckets, specified max keys
  313. lbr, err = client.ListBuckets(MaxKeys(2))
  314. c.Assert(err, IsNil)
  315. c.Assert(len(lbr.Buckets), Equals, 2)
  316. // ListBuckets, specified max keys
  317. lbr, err = client.ListBuckets(Marker(bucketNameLbOne), MaxKeys(1))
  318. c.Assert(err, IsNil)
  319. c.Assert(len(lbr.Buckets), Equals, 1)
  320. // ListBuckets, specified max keys
  321. lbr, err = client.ListBuckets(Marker(bucketNameLbOne))
  322. c.Assert(err, IsNil)
  323. c.Assert(len(lbr.Buckets) >= 2, Equals, true)
  324. // DeleteBucket
  325. err = client.DeleteBucket(bucketNameLbOne)
  326. c.Assert(err, IsNil)
  327. err = client.DeleteBucket(bucketNameLbTwo)
  328. c.Assert(err, IsNil)
  329. err = client.DeleteBucket(bucketNameLbThree)
  330. c.Assert(err, IsNil)
  331. }
  332. // TestListBucket
  333. func (s *OssClientSuite) TestIsBucketExist(c *C) {
  334. var prefix = bucketNamePrefix + randLowStr(6)
  335. var bucketNameLbOne = prefix + "tibe1"
  336. var bucketNameLbTwo = prefix + "tibe11"
  337. var bucketNameLbThree = prefix + "tibe111"
  338. client, err := New(endpoint, accessID, accessKey)
  339. c.Assert(err, IsNil)
  340. // CreateBucket
  341. err = client.CreateBucket(bucketNameLbOne)
  342. c.Assert(err, IsNil)
  343. err = client.CreateBucket(bucketNameLbTwo)
  344. c.Assert(err, IsNil)
  345. err = client.CreateBucket(bucketNameLbThree)
  346. c.Assert(err, IsNil)
  347. // Exist
  348. exist, err := client.IsBucketExist(bucketNameLbTwo)
  349. c.Assert(err, IsNil)
  350. c.Assert(exist, Equals, true)
  351. exist, err = client.IsBucketExist(bucketNameLbThree)
  352. c.Assert(err, IsNil)
  353. c.Assert(exist, Equals, true)
  354. exist, err = client.IsBucketExist(bucketNameLbOne)
  355. c.Assert(err, IsNil)
  356. c.Assert(exist, Equals, true)
  357. // Not exist
  358. exist, err = client.IsBucketExist(prefix + "tibe")
  359. c.Assert(err, IsNil)
  360. c.Assert(exist, Equals, false)
  361. exist, err = client.IsBucketExist(prefix + "tibe1111")
  362. c.Assert(err, IsNil)
  363. c.Assert(exist, Equals, false)
  364. // Negative
  365. exist, err = client.IsBucketExist("BucketNameInvalid")
  366. c.Assert(err, NotNil)
  367. // DeleteBucket
  368. err = client.DeleteBucket(bucketNameLbOne)
  369. c.Assert(err, IsNil)
  370. err = client.DeleteBucket(bucketNameLbTwo)
  371. c.Assert(err, IsNil)
  372. err = client.DeleteBucket(bucketNameLbThree)
  373. c.Assert(err, IsNil)
  374. }
  375. // TestSetBucketAcl
  376. func (s *OssClientSuite) TestSetBucketAcl(c *C) {
  377. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  378. client, err := New(endpoint, accessID, accessKey)
  379. c.Assert(err, IsNil)
  380. // Private
  381. err = client.CreateBucket(bucketNameTest)
  382. c.Assert(err, IsNil)
  383. res, err := client.GetBucketACL(bucketNameTest)
  384. c.Assert(err, IsNil)
  385. c.Assert(res.ACL, Equals, string(ACLPrivate))
  386. // Set ACL_PUBLIC_R
  387. err = client.SetBucketACL(bucketNameTest, ACLPublicRead)
  388. c.Assert(err, IsNil)
  389. time.Sleep(timeoutInOperation)
  390. res, err = client.GetBucketACL(bucketNameTest)
  391. c.Assert(err, IsNil)
  392. c.Assert(res.ACL, Equals, string(ACLPublicRead))
  393. // Set ACL_PUBLIC_RW
  394. err = client.SetBucketACL(bucketNameTest, ACLPublicReadWrite)
  395. c.Assert(err, IsNil)
  396. time.Sleep(timeoutInOperation)
  397. res, err = client.GetBucketACL(bucketNameTest)
  398. c.Assert(err, IsNil)
  399. c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
  400. // Set ACL_PUBLIC_RW
  401. err = client.SetBucketACL(bucketNameTest, ACLPrivate)
  402. c.Assert(err, IsNil)
  403. time.Sleep(timeoutInOperation)
  404. res, err = client.GetBucketACL(bucketNameTest)
  405. c.Assert(err, IsNil)
  406. c.Assert(res.ACL, Equals, string(ACLPrivate))
  407. err = client.DeleteBucket(bucketNameTest)
  408. c.Assert(err, IsNil)
  409. }
  410. // TestSetBucketAclNegative
  411. func (s *OssClientSuite) TestBucketAclNegative(c *C) {
  412. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  413. client, err := New(endpoint, accessID, accessKey)
  414. c.Assert(err, IsNil)
  415. err = client.CreateBucket(bucketNameTest)
  416. c.Assert(err, IsNil)
  417. err = client.SetBucketACL(bucketNameTest, "InvalidACL")
  418. c.Assert(err, NotNil)
  419. testLogger.Println(err)
  420. err = client.DeleteBucket(bucketNameTest)
  421. c.Assert(err, IsNil)
  422. }
  423. // TestGetBucketAcl
  424. func (s *OssClientSuite) TestGetBucketAcl(c *C) {
  425. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  426. client, err := New(endpoint, accessID, accessKey)
  427. c.Assert(err, IsNil)
  428. // Private
  429. err = client.CreateBucket(bucketNameTest)
  430. c.Assert(err, IsNil)
  431. time.Sleep(timeoutInOperation)
  432. res, err := client.GetBucketACL(bucketNameTest)
  433. c.Assert(err, IsNil)
  434. c.Assert(res.ACL, Equals, string(ACLPrivate))
  435. err = client.DeleteBucket(bucketNameTest)
  436. c.Assert(err, IsNil)
  437. time.Sleep(timeoutInOperation)
  438. // PublicRead
  439. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicRead))
  440. c.Assert(err, IsNil)
  441. time.Sleep(timeoutInOperation)
  442. res, err = client.GetBucketACL(bucketNameTest)
  443. c.Assert(err, IsNil)
  444. c.Assert(res.ACL, Equals, string(ACLPublicRead))
  445. err = client.DeleteBucket(bucketNameTest)
  446. c.Assert(err, IsNil)
  447. time.Sleep(timeoutInOperation)
  448. // PublicReadWrite
  449. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicReadWrite))
  450. c.Assert(err, IsNil)
  451. time.Sleep(timeoutInOperation)
  452. res, err = client.GetBucketACL(bucketNameTest)
  453. c.Assert(err, IsNil)
  454. c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
  455. err = client.DeleteBucket(bucketNameTest)
  456. c.Assert(err, IsNil)
  457. }
  458. // TestGetBucketAcl
  459. func (s *OssClientSuite) TestGetBucketLocation(c *C) {
  460. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  461. client, err := New(endpoint, accessID, accessKey)
  462. c.Assert(err, IsNil)
  463. // Private
  464. err = client.CreateBucket(bucketNameTest)
  465. c.Assert(err, IsNil)
  466. loc, err := client.GetBucketLocation(bucketNameTest)
  467. c.Assert(strings.HasPrefix(loc, "oss-"), Equals, true)
  468. err = client.DeleteBucket(bucketNameTest)
  469. c.Assert(err, IsNil)
  470. }
  471. // TestGetBucketLocationNegative
  472. func (s *OssClientSuite) TestGetBucketLocationNegative(c *C) {
  473. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  474. client, err := New(endpoint, accessID, accessKey)
  475. c.Assert(err, IsNil)
  476. // Not exist
  477. _, err = client.GetBucketLocation(bucketNameTest)
  478. c.Assert(err, NotNil)
  479. // Not exist
  480. _, err = client.GetBucketLocation("InvalidBucketName_")
  481. c.Assert(err, NotNil)
  482. }
  483. // TestSetBucketLifecycle
  484. func (s *OssClientSuite) TestSetBucketLifecycle(c *C) {
  485. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  486. var rule1 = BuildLifecycleRuleByDate("rule1", "one", true, 2015, 11, 11)
  487. var rule2 = BuildLifecycleRuleByDays("rule2", "two", true, 3)
  488. client, err := New(endpoint, accessID, accessKey)
  489. c.Assert(err, IsNil)
  490. err = client.CreateBucket(bucketNameTest)
  491. c.Assert(err, IsNil)
  492. // Set single rule
  493. var rules = []LifecycleRule{rule1}
  494. err = client.SetBucketLifecycle(bucketNameTest, rules)
  495. c.Assert(err, IsNil)
  496. // Double set rule
  497. err = client.SetBucketLifecycle(bucketNameTest, rules)
  498. c.Assert(err, IsNil)
  499. res, err := client.GetBucketLifecycle(bucketNameTest)
  500. c.Assert(err, IsNil)
  501. c.Assert(len(res.Rules), Equals, 1)
  502. c.Assert(res.Rules[0].ID, Equals, "rule1")
  503. err = client.DeleteBucketLifecycle(bucketNameTest)
  504. c.Assert(err, IsNil)
  505. // Set two rules
  506. rules = []LifecycleRule{rule1, rule2}
  507. err = client.SetBucketLifecycle(bucketNameTest, rules)
  508. c.Assert(err, IsNil)
  509. // Eliminate effect of cache
  510. time.Sleep(timeoutInOperation)
  511. res, err = client.GetBucketLifecycle(bucketNameTest)
  512. c.Assert(err, IsNil)
  513. c.Assert(len(res.Rules), Equals, 2)
  514. c.Assert(res.Rules[0].ID, Equals, "rule1")
  515. c.Assert(res.Rules[1].ID, Equals, "rule2")
  516. err = client.DeleteBucket(bucketNameTest)
  517. c.Assert(err, IsNil)
  518. }
  519. // TestSetBucketLifecycleNew
  520. func (s *OssClientSuite) TestSetBucketLifecycleNew(c *C) {
  521. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  522. client, err := New(endpoint, accessID, accessKey)
  523. c.Assert(err, IsNil)
  524. err = client.CreateBucket(bucketNameTest)
  525. c.Assert(err, IsNil)
  526. //invalid status of lifecyclerule
  527. expiration := LifecycleExpiration{
  528. Days: 30,
  529. }
  530. rule := LifecycleRule{
  531. ID: "rule1",
  532. Prefix: "one",
  533. Status: "Invalid",
  534. Expiration: &expiration,
  535. }
  536. rules := []LifecycleRule{rule}
  537. err = client.SetBucketLifecycle(bucketNameTest, rules)
  538. c.Assert(err, NotNil)
  539. //invalid value of CreatedBeforeDate
  540. expiration = LifecycleExpiration{
  541. CreatedBeforeDate: randStr(10),
  542. }
  543. rule = LifecycleRule{
  544. ID: "rule1",
  545. Prefix: "one",
  546. Status: "Enabled",
  547. Expiration: &expiration,
  548. }
  549. rules = []LifecycleRule{rule}
  550. err = client.SetBucketLifecycle(bucketNameTest, rules)
  551. c.Assert(err, NotNil)
  552. //invalid value of Days
  553. abortMPU := LifecycleAbortMultipartUpload{
  554. Days: -30,
  555. }
  556. rule = LifecycleRule{
  557. ID: "rule1",
  558. Prefix: "one",
  559. Status: "Enabled",
  560. AbortMultipartUpload: &abortMPU,
  561. }
  562. rules = []LifecycleRule{rule}
  563. err = client.SetBucketLifecycle(bucketNameTest, rules)
  564. c.Assert(err, NotNil)
  565. expiration = LifecycleExpiration{
  566. CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
  567. }
  568. rule1 := LifecycleRule{
  569. ID: "rule1",
  570. Prefix: "one",
  571. Status: "Enabled",
  572. Expiration: &expiration,
  573. }
  574. abortMPU = LifecycleAbortMultipartUpload{
  575. Days: 30,
  576. }
  577. rule2 := LifecycleRule{
  578. ID: "rule2",
  579. Prefix: "two",
  580. Status: "Enabled",
  581. Expiration: &expiration,
  582. AbortMultipartUpload: &abortMPU,
  583. }
  584. transition1 := LifecycleTransition{
  585. Days: 3,
  586. StorageClass: StorageIA,
  587. }
  588. transition2 := LifecycleTransition{
  589. Days: 30,
  590. StorageClass: StorageArchive,
  591. }
  592. transitions := []LifecycleTransition{transition1, transition2}
  593. rule3 := LifecycleRule{
  594. ID: "rule3",
  595. Prefix: "three",
  596. Status: "Enabled",
  597. AbortMultipartUpload: &abortMPU,
  598. Transitions: transitions,
  599. }
  600. // Set single rule
  601. rules = []LifecycleRule{rule1}
  602. err = client.SetBucketLifecycle(bucketNameTest, rules)
  603. c.Assert(err, IsNil)
  604. res, err := client.GetBucketLifecycle(bucketNameTest)
  605. c.Assert(err, IsNil)
  606. c.Assert(len(res.Rules), Equals, 1)
  607. c.Assert(res.Rules[0].ID, Equals, "rule1")
  608. c.Assert(res.Rules[0].Expiration, NotNil)
  609. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  610. err = client.DeleteBucketLifecycle(bucketNameTest)
  611. c.Assert(err, IsNil)
  612. // Set two rule: rule1 and rule2
  613. rules = []LifecycleRule{rule1, rule2}
  614. err = client.SetBucketLifecycle(bucketNameTest, rules)
  615. c.Assert(err, IsNil)
  616. res, err = client.GetBucketLifecycle(bucketNameTest)
  617. c.Assert(err, IsNil)
  618. c.Assert(len(res.Rules), Equals, 2)
  619. c.Assert(res.Rules[0].ID, Equals, "rule1")
  620. c.Assert(res.Rules[0].Expiration, NotNil)
  621. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  622. c.Assert(res.Rules[1].ID, Equals, "rule2")
  623. c.Assert(res.Rules[1].Expiration, NotNil)
  624. c.Assert(res.Rules[1].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  625. c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
  626. c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
  627. err = client.DeleteBucketLifecycle(bucketNameTest)
  628. c.Assert(err, IsNil)
  629. // Set two rule: rule2 and rule3
  630. rules = []LifecycleRule{rule2, rule3}
  631. err = client.SetBucketLifecycle(bucketNameTest, rules)
  632. c.Assert(err, IsNil)
  633. res, err = client.GetBucketLifecycle(bucketNameTest)
  634. c.Assert(err, IsNil)
  635. c.Assert(len(res.Rules), Equals, 2)
  636. c.Assert(res.Rules[0].ID, Equals, "rule2")
  637. c.Assert(res.Rules[0].Expiration, NotNil)
  638. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  639. c.Assert(res.Rules[0].AbortMultipartUpload, NotNil)
  640. c.Assert(res.Rules[0].AbortMultipartUpload.Days, Equals, 30)
  641. c.Assert(res.Rules[1].ID, Equals, "rule3")
  642. c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
  643. c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
  644. c.Assert(len(res.Rules[1].Transitions), Equals, 2)
  645. c.Assert(res.Rules[1].Transitions[0].StorageClass, Equals, StorageIA)
  646. c.Assert(res.Rules[1].Transitions[0].Days, Equals, 3)
  647. c.Assert(res.Rules[1].Transitions[1].StorageClass, Equals, StorageArchive)
  648. c.Assert(res.Rules[1].Transitions[1].Days, Equals, 30)
  649. err = client.DeleteBucketLifecycle(bucketNameTest)
  650. c.Assert(err, IsNil)
  651. // Set two rule: rule1 and rule3
  652. rules = []LifecycleRule{rule1, rule3}
  653. err = client.SetBucketLifecycle(bucketNameTest, rules)
  654. c.Assert(err, IsNil)
  655. res, err = client.GetBucketLifecycle(bucketNameTest)
  656. c.Assert(err, IsNil)
  657. c.Assert(len(res.Rules), Equals, 2)
  658. c.Assert(res.Rules[0].ID, Equals, "rule1")
  659. c.Assert(res.Rules[0].Expiration, NotNil)
  660. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  661. c.Assert(res.Rules[1].ID, Equals, "rule3")
  662. c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
  663. c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
  664. c.Assert(len(res.Rules[1].Transitions), Equals, 2)
  665. c.Assert(res.Rules[1].Transitions[0].StorageClass, Equals, StorageIA)
  666. c.Assert(res.Rules[1].Transitions[0].Days, Equals, 3)
  667. c.Assert(res.Rules[1].Transitions[1].StorageClass, Equals, StorageArchive)
  668. c.Assert(res.Rules[1].Transitions[1].Days, Equals, 30)
  669. err = client.DeleteBucketLifecycle(bucketNameTest)
  670. c.Assert(err, IsNil)
  671. // Set three rules
  672. rules = []LifecycleRule{rule1, rule2, rule3}
  673. err = client.SetBucketLifecycle(bucketNameTest, rules)
  674. c.Assert(err, IsNil)
  675. res, err = client.GetBucketLifecycle(bucketNameTest)
  676. c.Assert(err, IsNil)
  677. c.Assert(len(res.Rules), Equals, 3)
  678. c.Assert(res.Rules[0].ID, Equals, "rule1")
  679. c.Assert(res.Rules[0].Expiration, NotNil)
  680. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  681. c.Assert(res.Rules[1].ID, Equals, "rule2")
  682. c.Assert(res.Rules[1].Expiration, NotNil)
  683. c.Assert(res.Rules[1].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  684. c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
  685. c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
  686. c.Assert(res.Rules[2].ID, Equals, "rule3")
  687. c.Assert(res.Rules[2].AbortMultipartUpload, NotNil)
  688. c.Assert(res.Rules[2].AbortMultipartUpload.Days, Equals, 30)
  689. c.Assert(len(res.Rules[2].Transitions), Equals, 2)
  690. c.Assert(res.Rules[2].Transitions[0].StorageClass, Equals, StorageIA)
  691. c.Assert(res.Rules[2].Transitions[0].Days, Equals, 3)
  692. c.Assert(res.Rules[2].Transitions[1].StorageClass, Equals, StorageArchive)
  693. c.Assert(res.Rules[2].Transitions[1].Days, Equals, 30)
  694. err = client.DeleteBucket(bucketNameTest)
  695. c.Assert(err, IsNil)
  696. }
  697. // TestDeleteBucketLifecycle
  698. func (s *OssClientSuite) TestDeleteBucketLifecycle(c *C) {
  699. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  700. var rule1 = BuildLifecycleRuleByDate("rule1", "one", true, 2015, 11, 11)
  701. var rule2 = BuildLifecycleRuleByDays("rule2", "two", true, 3)
  702. var rules = []LifecycleRule{rule1, rule2}
  703. client, err := New(endpoint, accessID, accessKey)
  704. c.Assert(err, IsNil)
  705. err = client.CreateBucket(bucketNameTest)
  706. c.Assert(err, IsNil)
  707. //time.Sleep(timeoutInOperation)
  708. err = client.DeleteBucketLifecycle(bucketNameTest)
  709. c.Assert(err, IsNil)
  710. err = client.SetBucketLifecycle(bucketNameTest, rules)
  711. c.Assert(err, IsNil)
  712. //time.Sleep(timeoutInOperation)
  713. res, err := client.GetBucketLifecycle(bucketNameTest)
  714. c.Assert(err, IsNil)
  715. c.Assert(len(res.Rules), Equals, 2)
  716. // Delete
  717. err = client.DeleteBucketLifecycle(bucketNameTest)
  718. c.Assert(err, IsNil)
  719. //time.Sleep(timeoutInOperation)
  720. res, err = client.GetBucketLifecycle(bucketNameTest)
  721. c.Assert(err, NotNil)
  722. // Eliminate effect of cache
  723. //time.Sleep(timeoutInOperation)
  724. // Delete when not set
  725. err = client.DeleteBucketLifecycle(bucketNameTest)
  726. c.Assert(err, IsNil)
  727. err = client.DeleteBucket(bucketNameTest)
  728. c.Assert(err, IsNil)
  729. }
  730. // TestSetBucketLifecycleNegative
  731. func (s *OssClientSuite) TestBucketLifecycleNegative(c *C) {
  732. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  733. var rules = []LifecycleRule{}
  734. client, err := New(endpoint, accessID, accessKey)
  735. c.Assert(err, IsNil)
  736. err = client.CreateBucket(bucketNameTest)
  737. c.Assert(err, IsNil)
  738. // Set with no rule
  739. err = client.SetBucketLifecycle(bucketNameTest, rules)
  740. c.Assert(err, NotNil)
  741. err = client.DeleteBucket(bucketNameTest)
  742. c.Assert(err, IsNil)
  743. // Not exist
  744. err = client.SetBucketLifecycle(bucketNameTest, rules)
  745. c.Assert(err, NotNil)
  746. // Not exist
  747. _, err = client.GetBucketLifecycle(bucketNameTest)
  748. c.Assert(err, NotNil)
  749. // Not exist
  750. err = client.DeleteBucketLifecycle(bucketNameTest)
  751. c.Assert(err, NotNil)
  752. }
  753. // TestSetBucketReferer
  754. func (s *OssClientSuite) TestSetBucketReferer(c *C) {
  755. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  756. var referers = []string{"http://www.aliyun.com", "https://www.aliyun.com"}
  757. client, err := New(endpoint, accessID, accessKey)
  758. c.Assert(err, IsNil)
  759. err = client.CreateBucket(bucketNameTest)
  760. c.Assert(err, IsNil)
  761. time.Sleep(timeoutInOperation)
  762. res, err := client.GetBucketReferer(bucketNameTest)
  763. c.Assert(res.AllowEmptyReferer, Equals, true)
  764. c.Assert(len(res.RefererList), Equals, 0)
  765. // Set referers
  766. err = client.SetBucketReferer(bucketNameTest, referers, false)
  767. c.Assert(err, IsNil)
  768. time.Sleep(timeoutInOperation)
  769. res, err = client.GetBucketReferer(bucketNameTest)
  770. c.Assert(res.AllowEmptyReferer, Equals, false)
  771. c.Assert(len(res.RefererList), Equals, 2)
  772. c.Assert(res.RefererList[0], Equals, "http://www.aliyun.com")
  773. c.Assert(res.RefererList[1], Equals, "https://www.aliyun.com")
  774. // Reset referer, referers empty
  775. referers = []string{""}
  776. err = client.SetBucketReferer(bucketNameTest, referers, true)
  777. c.Assert(err, IsNil)
  778. referers = []string{}
  779. err = client.SetBucketReferer(bucketNameTest, referers, true)
  780. c.Assert(err, IsNil)
  781. res, err = client.GetBucketReferer(bucketNameTest)
  782. c.Assert(res.AllowEmptyReferer, Equals, true)
  783. c.Assert(len(res.RefererList), Equals, 0)
  784. err = client.DeleteBucket(bucketNameTest)
  785. c.Assert(err, IsNil)
  786. }
  787. // TestSetBucketRefererNegative
  788. func (s *OssClientSuite) TestBucketRefererNegative(c *C) {
  789. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  790. var referers = []string{""}
  791. client, err := New(endpoint, accessID, accessKey)
  792. c.Assert(err, IsNil)
  793. // Not exist
  794. _, err = client.GetBucketReferer(bucketNameTest)
  795. c.Assert(err, NotNil)
  796. testLogger.Println(err)
  797. // Not exist
  798. err = client.SetBucketReferer(bucketNameTest, referers, true)
  799. c.Assert(err, NotNil)
  800. testLogger.Println(err)
  801. }
  802. // TestSetBucketLogging
  803. func (s *OssClientSuite) TestSetBucketLogging(c *C) {
  804. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  805. var bucketNameTarget = bucketNamePrefix + randLowStr(6)
  806. client, err := New(endpoint, accessID, accessKey)
  807. c.Assert(err, IsNil)
  808. err = client.CreateBucket(bucketNameTest)
  809. c.Assert(err, IsNil)
  810. err = client.CreateBucket(bucketNameTarget)
  811. c.Assert(err, IsNil)
  812. time.Sleep(timeoutInOperation)
  813. // Set logging
  814. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
  815. c.Assert(err, IsNil)
  816. // Reset
  817. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", false)
  818. c.Assert(err, IsNil)
  819. time.Sleep(timeoutInOperation)
  820. res, err := client.GetBucketLogging(bucketNameTest)
  821. c.Assert(err, IsNil)
  822. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  823. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  824. err = client.DeleteBucketLogging(bucketNameTest)
  825. c.Assert(err, IsNil)
  826. // Set to self
  827. err = client.SetBucketLogging(bucketNameTest, bucketNameTest, "prefix", true)
  828. c.Assert(err, IsNil)
  829. err = client.DeleteBucket(bucketNameTest)
  830. c.Assert(err, IsNil)
  831. err = client.DeleteBucket(bucketNameTarget)
  832. c.Assert(err, IsNil)
  833. }
  834. // TestDeleteBucketLogging
  835. func (s *OssClientSuite) TestDeleteBucketLogging(c *C) {
  836. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  837. var bucketNameTarget = bucketNamePrefix + randLowStr(6)
  838. client, err := New(endpoint, accessID, accessKey)
  839. c.Assert(err, IsNil)
  840. err = client.CreateBucket(bucketNameTest)
  841. c.Assert(err, IsNil)
  842. err = client.CreateBucket(bucketNameTarget)
  843. c.Assert(err, IsNil)
  844. time.Sleep(timeoutInOperation)
  845. // Get when not set
  846. res, err := client.GetBucketLogging(bucketNameTest)
  847. c.Assert(err, IsNil)
  848. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  849. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  850. // Set
  851. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
  852. c.Assert(err, IsNil)
  853. // Get
  854. time.Sleep(timeoutInOperation)
  855. res, err = client.GetBucketLogging(bucketNameTest)
  856. c.Assert(err, IsNil)
  857. c.Assert(res.LoggingEnabled.TargetBucket, Equals, bucketNameTarget)
  858. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "prefix")
  859. // Set
  860. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", false)
  861. c.Assert(err, IsNil)
  862. // Get
  863. time.Sleep(timeoutInOperation)
  864. res, err = client.GetBucketLogging(bucketNameTest)
  865. c.Assert(err, IsNil)
  866. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  867. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  868. // Delete
  869. err = client.DeleteBucketLogging(bucketNameTest)
  870. c.Assert(err, IsNil)
  871. // Get after delete
  872. time.Sleep(timeoutInOperation)
  873. res, err = client.GetBucketLogging(bucketNameTest)
  874. c.Assert(err, IsNil)
  875. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  876. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  877. err = client.DeleteBucket(bucketNameTest)
  878. c.Assert(err, IsNil)
  879. err = client.DeleteBucket(bucketNameTarget)
  880. c.Assert(err, IsNil)
  881. }
  882. // TestSetBucketLoggingNegative
  883. func (s *OssClientSuite) TestSetBucketLoggingNegative(c *C) {
  884. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  885. var bucketNameTarget = bucketNamePrefix + randLowStr(6)
  886. client, err := New(endpoint, accessID, accessKey)
  887. c.Assert(err, IsNil)
  888. // Not exist
  889. _, err = client.GetBucketLogging(bucketNameTest)
  890. c.Assert(err, NotNil)
  891. // Not exist
  892. err = client.SetBucketLogging(bucketNameTest, "targetbucket", "prefix", true)
  893. c.Assert(err, NotNil)
  894. // Not exist
  895. err = client.DeleteBucketLogging(bucketNameTest)
  896. c.Assert(err, NotNil)
  897. err = client.CreateBucket(bucketNameTest)
  898. c.Assert(err, IsNil)
  899. time.Sleep(timeoutInOperation)
  900. // Target bucket not exist
  901. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
  902. c.Assert(err, NotNil)
  903. // Parameter invalid
  904. err = client.SetBucketLogging(bucketNameTest, "XXXX", "prefix", true)
  905. c.Assert(err, NotNil)
  906. err = client.SetBucketLogging(bucketNameTest, "xx", "prefix", true)
  907. c.Assert(err, NotNil)
  908. err = client.DeleteBucket(bucketNameTest)
  909. c.Assert(err, IsNil)
  910. }
  911. // TestSetBucketWebsite
  912. func (s *OssClientSuite) TestSetBucketWebsite(c *C) {
  913. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  914. var indexWebsite = "myindex.html"
  915. var errorWebsite = "myerror.html"
  916. client, err := New(endpoint, accessID, accessKey)
  917. c.Assert(err, IsNil)
  918. time.Sleep(timeoutInOperation)
  919. err = client.CreateBucket(bucketNameTest)
  920. c.Assert(err, IsNil)
  921. // Set
  922. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  923. c.Assert(err, IsNil)
  924. // Double set
  925. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  926. c.Assert(err, IsNil)
  927. res, err := client.GetBucketWebsite(bucketNameTest)
  928. c.Assert(err, IsNil)
  929. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  930. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  931. // Reset
  932. err = client.SetBucketWebsite(bucketNameTest, "your"+indexWebsite, "your"+errorWebsite)
  933. c.Assert(err, IsNil)
  934. time.Sleep(timeoutInOperation)
  935. res, err = client.GetBucketWebsite(bucketNameTest)
  936. c.Assert(err, IsNil)
  937. c.Assert(res.IndexDocument.Suffix, Equals, "your"+indexWebsite)
  938. c.Assert(res.ErrorDocument.Key, Equals, "your"+errorWebsite)
  939. err = client.DeleteBucketWebsite(bucketNameTest)
  940. c.Assert(err, IsNil)
  941. // Set after delete
  942. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  943. c.Assert(err, IsNil)
  944. // Eliminate effect of cache
  945. time.Sleep(timeoutInOperation)
  946. res, err = client.GetBucketWebsite(bucketNameTest)
  947. c.Assert(err, IsNil)
  948. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  949. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  950. err = client.DeleteBucket(bucketNameTest)
  951. c.Assert(err, IsNil)
  952. }
  953. // TestDeleteBucketWebsite
  954. func (s *OssClientSuite) TestDeleteBucketWebsite(c *C) {
  955. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  956. var indexWebsite = "myindex.html"
  957. var errorWebsite = "myerror.html"
  958. client, err := New(endpoint, accessID, accessKey)
  959. c.Assert(err, IsNil)
  960. err = client.CreateBucket(bucketNameTest)
  961. c.Assert(err, IsNil)
  962. time.Sleep(timeoutInOperation)
  963. // Get
  964. res, err := client.GetBucketWebsite(bucketNameTest)
  965. c.Assert(err, NotNil)
  966. // Detele without set
  967. err = client.DeleteBucketWebsite(bucketNameTest)
  968. c.Assert(err, IsNil)
  969. // Set
  970. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  971. c.Assert(err, IsNil)
  972. time.Sleep(timeoutInOperation)
  973. res, err = client.GetBucketWebsite(bucketNameTest)
  974. c.Assert(err, IsNil)
  975. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  976. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  977. // Detele
  978. time.Sleep(timeoutInOperation)
  979. err = client.DeleteBucketWebsite(bucketNameTest)
  980. c.Assert(err, IsNil)
  981. time.Sleep(timeoutInOperation)
  982. res, err = client.GetBucketWebsite(bucketNameTest)
  983. c.Assert(err, NotNil)
  984. // Detele after delete
  985. err = client.DeleteBucketWebsite(bucketNameTest)
  986. c.Assert(err, IsNil)
  987. err = client.DeleteBucket(bucketNameTest)
  988. c.Assert(err, IsNil)
  989. }
  990. // TestSetBucketWebsiteNegative
  991. func (s *OssClientSuite) TestSetBucketWebsiteNegative(c *C) {
  992. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  993. var indexWebsite = "myindex.html"
  994. var errorWebsite = "myerror.html"
  995. client, err := New(endpoint, accessID, accessKey)
  996. c.Assert(err, IsNil)
  997. err = client.DeleteBucket(bucketNameTest)
  998. // Not exist
  999. _, err = client.GetBucketWebsite(bucketNameTest)
  1000. c.Assert(err, NotNil)
  1001. err = client.DeleteBucketWebsite(bucketNameTest)
  1002. c.Assert(err, NotNil)
  1003. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  1004. c.Assert(err, NotNil)
  1005. err = client.CreateBucket(bucketNameTest)
  1006. c.Assert(err, IsNil)
  1007. // Set
  1008. time.Sleep(timeoutInOperation)
  1009. err = client.SetBucketWebsite(bucketNameTest, "myindex", "myerror")
  1010. c.Assert(err, IsNil)
  1011. res, err := client.GetBucketWebsite(bucketNameTest)
  1012. c.Assert(err, IsNil)
  1013. c.Assert(res.IndexDocument.Suffix, Equals, "myindex")
  1014. c.Assert(res.ErrorDocument.Key, Equals, "myerror")
  1015. // Detele
  1016. err = client.DeleteBucketWebsite(bucketNameTest)
  1017. c.Assert(err, IsNil)
  1018. time.Sleep(timeoutInOperation)
  1019. _, err = client.GetBucketWebsite(bucketNameTest)
  1020. c.Assert(err, NotNil)
  1021. // Detele after delete
  1022. err = client.DeleteBucketWebsite(bucketNameTest)
  1023. c.Assert(err, IsNil)
  1024. err = client.DeleteBucket(bucketNameTest)
  1025. c.Assert(err, IsNil)
  1026. }
  1027. // TestSetBucketWebsite
  1028. func (s *OssClientSuite) TestSetBucketCORS(c *C) {
  1029. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1030. var rule1 = CORSRule{
  1031. AllowedOrigin: []string{"*"},
  1032. AllowedMethod: []string{"PUT", "GET", "POST"},
  1033. AllowedHeader: []string{},
  1034. ExposeHeader: []string{},
  1035. MaxAgeSeconds: 100,
  1036. }
  1037. var rule2 = CORSRule{
  1038. AllowedOrigin: []string{"http://www.a.com", "http://www.b.com"},
  1039. AllowedMethod: []string{"GET"},
  1040. AllowedHeader: []string{"Authorization"},
  1041. ExposeHeader: []string{"x-oss-test", "x-oss-test1"},
  1042. MaxAgeSeconds: 200,
  1043. }
  1044. client, err := New(endpoint, accessID, accessKey)
  1045. c.Assert(err, IsNil)
  1046. err = client.CreateBucket(bucketNameTest)
  1047. c.Assert(err, IsNil)
  1048. time.Sleep(timeoutInOperation)
  1049. // Set
  1050. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1})
  1051. c.Assert(err, IsNil)
  1052. gbcr, err := client.GetBucketCORS(bucketNameTest)
  1053. c.Assert(err, IsNil)
  1054. c.Assert(len(gbcr.CORSRules), Equals, 1)
  1055. c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 1)
  1056. c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 3)
  1057. c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 0)
  1058. c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 0)
  1059. c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 100)
  1060. // Double set
  1061. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1})
  1062. c.Assert(err, IsNil)
  1063. gbcr, err = client.GetBucketCORS(bucketNameTest)
  1064. c.Assert(err, IsNil)
  1065. c.Assert(len(gbcr.CORSRules), Equals, 1)
  1066. c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 1)
  1067. c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 3)
  1068. c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 0)
  1069. c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 0)
  1070. c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 100)
  1071. // Set rule2
  1072. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule2})
  1073. c.Assert(err, IsNil)
  1074. time.Sleep(timeoutInOperation)
  1075. gbcr, err = client.GetBucketCORS(bucketNameTest)
  1076. c.Assert(err, IsNil)
  1077. c.Assert(len(gbcr.CORSRules), Equals, 1)
  1078. c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 2)
  1079. c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 1)
  1080. c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 1)
  1081. c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 2)
  1082. c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 200)
  1083. // Reset
  1084. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1, rule2})
  1085. c.Assert(err, IsNil)
  1086. time.Sleep(timeoutInOperation)
  1087. gbcr, err = client.GetBucketCORS(bucketNameTest)
  1088. c.Assert(err, IsNil)
  1089. c.Assert(len(gbcr.CORSRules), Equals, 2)
  1090. // Set after delete
  1091. err = client.DeleteBucketCORS(bucketNameTest)
  1092. c.Assert(err, IsNil)
  1093. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1, rule2})
  1094. c.Assert(err, IsNil)
  1095. time.Sleep(timeoutInOperation)
  1096. gbcr, err = client.GetBucketCORS(bucketNameTest)
  1097. c.Assert(err, IsNil)
  1098. c.Assert(len(gbcr.CORSRules), Equals, 2)
  1099. err = client.DeleteBucketCORS(bucketNameTest)
  1100. c.Assert(err, IsNil)
  1101. err = client.DeleteBucket(bucketNameTest)
  1102. c.Assert(err, IsNil)
  1103. }
  1104. // TestDeleteBucketCORS
  1105. func (s *OssClientSuite) TestDeleteBucketCORS(c *C) {
  1106. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1107. var rule = CORSRule{
  1108. AllowedOrigin: []string{"*"},
  1109. AllowedMethod: []string{"PUT", "GET", "POST"},
  1110. AllowedHeader: []string{},
  1111. ExposeHeader: []string{},
  1112. MaxAgeSeconds: 100,
  1113. }
  1114. client, err := New(endpoint, accessID, accessKey)
  1115. c.Assert(err, IsNil)
  1116. err = client.CreateBucket(bucketNameTest)
  1117. c.Assert(err, IsNil)
  1118. time.Sleep(timeoutInOperation)
  1119. // Delete not set
  1120. err = client.DeleteBucketCORS(bucketNameTest)
  1121. c.Assert(err, IsNil)
  1122. // Set
  1123. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
  1124. c.Assert(err, IsNil)
  1125. time.Sleep(timeoutInOperation)
  1126. _, err = client.GetBucketCORS(bucketNameTest)
  1127. c.Assert(err, IsNil)
  1128. // Detele
  1129. err = client.DeleteBucketCORS(bucketNameTest)
  1130. c.Assert(err, IsNil)
  1131. time.Sleep(timeoutInOperation)
  1132. _, err = client.GetBucketCORS(bucketNameTest)
  1133. c.Assert(err, NotNil)
  1134. // Detele after deleting
  1135. err = client.DeleteBucketCORS(bucketNameTest)
  1136. c.Assert(err, IsNil)
  1137. err = client.DeleteBucket(bucketNameTest)
  1138. c.Assert(err, IsNil)
  1139. }
  1140. // TestSetBucketCORSNegative
  1141. func (s *OssClientSuite) TestSetBucketCORSNegative(c *C) {
  1142. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1143. var rule = CORSRule{
  1144. AllowedOrigin: []string{"*"},
  1145. AllowedMethod: []string{"PUT", "GET", "POST"},
  1146. AllowedHeader: []string{},
  1147. ExposeHeader: []string{},
  1148. MaxAgeSeconds: 100,
  1149. }
  1150. client, err := New(endpoint, accessID, accessKey)
  1151. c.Assert(err, IsNil)
  1152. err = client.DeleteBucket(bucketNameTest)
  1153. // Not exist
  1154. _, err = client.GetBucketCORS(bucketNameTest)
  1155. c.Assert(err, NotNil)
  1156. err = client.DeleteBucketCORS(bucketNameTest)
  1157. c.Assert(err, NotNil)
  1158. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
  1159. c.Assert(err, NotNil)
  1160. err = client.CreateBucket(bucketNameTest)
  1161. c.Assert(err, IsNil)
  1162. time.Sleep(timeoutInOperation)
  1163. _, err = client.GetBucketCORS(bucketNameTest)
  1164. c.Assert(err, NotNil)
  1165. // Set
  1166. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
  1167. c.Assert(err, IsNil)
  1168. time.Sleep(timeoutInOperation)
  1169. _, err = client.GetBucketCORS(bucketNameTest)
  1170. c.Assert(err, IsNil)
  1171. // Delete
  1172. err = client.DeleteBucketCORS(bucketNameTest)
  1173. c.Assert(err, IsNil)
  1174. time.Sleep(timeoutInOperation)
  1175. _, err = client.GetBucketCORS(bucketNameTest)
  1176. c.Assert(err, NotNil)
  1177. // Delete after deleting
  1178. err = client.DeleteBucketCORS(bucketNameTest)
  1179. c.Assert(err, IsNil)
  1180. err = client.DeleteBucket(bucketNameTest)
  1181. c.Assert(err, IsNil)
  1182. }
  1183. // TestGetBucketInfo
  1184. func (s *OssClientSuite) TestGetBucketInfo(c *C) {
  1185. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1186. client, err := New(endpoint, accessID, accessKey)
  1187. c.Assert(err, IsNil)
  1188. err = client.CreateBucket(bucketNameTest)
  1189. c.Assert(err, IsNil)
  1190. res, err := client.GetBucketInfo(bucketNameTest)
  1191. c.Assert(err, IsNil)
  1192. c.Assert(res.BucketInfo.Name, Equals, bucketNameTest)
  1193. c.Assert(strings.HasPrefix(res.BucketInfo.Location, "oss-"), Equals, true)
  1194. c.Assert(res.BucketInfo.ACL, Equals, "private")
  1195. c.Assert(strings.HasSuffix(res.BucketInfo.ExtranetEndpoint, ".com"), Equals, true)
  1196. c.Assert(strings.HasSuffix(res.BucketInfo.IntranetEndpoint, ".com"), Equals, true)
  1197. c.Assert(res.BucketInfo.CreationDate, NotNil)
  1198. err = client.DeleteBucket(bucketNameTest)
  1199. c.Assert(err, IsNil)
  1200. }
  1201. // TestGetBucketInfoNegative
  1202. func (s *OssClientSuite) TestGetBucketInfoNegative(c *C) {
  1203. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1204. client, err := New(endpoint, accessID, accessKey)
  1205. c.Assert(err, IsNil)
  1206. // Not exist
  1207. _, err = client.GetBucketInfo(bucketNameTest)
  1208. c.Assert(err, NotNil)
  1209. // Bucket name invalid
  1210. _, err = client.GetBucketInfo("InvalidBucketName_")
  1211. c.Assert(err, NotNil)
  1212. }
  1213. // TestEndpointFormat
  1214. func (s *OssClientSuite) TestEndpointFormat(c *C) {
  1215. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1216. // http://host
  1217. client, err := New(endpoint, accessID, accessKey)
  1218. c.Assert(err, IsNil)
  1219. err = client.CreateBucket(bucketNameTest)
  1220. c.Assert(err, IsNil)
  1221. time.Sleep(timeoutInOperation)
  1222. res, err := client.GetBucketACL(bucketNameTest)
  1223. c.Assert(err, IsNil)
  1224. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1225. err = client.DeleteBucket(bucketNameTest)
  1226. c.Assert(err, IsNil)
  1227. time.Sleep(timeoutInOperation)
  1228. // http://host:port
  1229. client, err = New(endpoint+":80", accessID, accessKey)
  1230. c.Assert(err, IsNil)
  1231. err = client.CreateBucket(bucketNameTest)
  1232. c.Assert(err, IsNil)
  1233. time.Sleep(timeoutInOperation)
  1234. res, err = client.GetBucketACL(bucketNameTest)
  1235. c.Assert(err, IsNil)
  1236. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1237. err = client.DeleteBucket(bucketNameTest)
  1238. c.Assert(err, IsNil)
  1239. }
  1240. // TestCname
  1241. func (s *OssClientSuite) _TestCname(c *C) {
  1242. var bucketNameTest = "<my-bucket-cname>"
  1243. client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>", UseCname(true))
  1244. c.Assert(err, IsNil)
  1245. err = client.CreateBucket(bucketNameTest)
  1246. c.Assert(err, IsNil)
  1247. _, err = client.ListBuckets()
  1248. c.Assert(err, NotNil)
  1249. res, err := client.GetBucketACL(bucketNameTest)
  1250. c.Assert(err, IsNil)
  1251. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1252. }
  1253. // TestCnameNegative
  1254. func (s *OssClientSuite) _TestCnameNegative(c *C) {
  1255. var bucketNameTest = "<my-bucket-cname>"
  1256. client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>", UseCname(true))
  1257. c.Assert(err, IsNil)
  1258. err = client.CreateBucket(bucketNameTest)
  1259. c.Assert(err, NotNil)
  1260. _, err = client.ListBuckets()
  1261. c.Assert(err, NotNil)
  1262. _, err = client.GetBucketACL(bucketNameTest)
  1263. c.Assert(err, NotNil)
  1264. }
  1265. // _TestHTTPS
  1266. func (s *OssClientSuite) _TestHTTPS(c *C) {
  1267. var bucketNameTest = "<my-bucket-https>"
  1268. client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>")
  1269. c.Assert(err, IsNil)
  1270. err = client.CreateBucket(bucketNameTest)
  1271. c.Assert(err, IsNil)
  1272. res, err := client.GetBucketACL(bucketNameTest)
  1273. c.Assert(err, IsNil)
  1274. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1275. err = client.DeleteBucket(bucketNameTest)
  1276. c.Assert(err, IsNil)
  1277. }
  1278. // TestClientOption
  1279. func (s *OssClientSuite) TestClientOption(c *C) {
  1280. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1281. client, err := New(endpoint, accessID, accessKey, UseCname(true),
  1282. Timeout(11, 12), SecurityToken("token"), Proxy(proxyHost))
  1283. c.Assert(err, IsNil)
  1284. // CreateBucket timeout
  1285. err = client.CreateBucket(bucketNameTest)
  1286. c.Assert(err, NotNil)
  1287. c.Assert(client.Conn.config.HTTPTimeout.ConnectTimeout, Equals, time.Second*11)
  1288. c.Assert(client.Conn.config.HTTPTimeout.ReadWriteTimeout, Equals, time.Second*12)
  1289. c.Assert(client.Conn.config.HTTPTimeout.HeaderTimeout, Equals, time.Second*12)
  1290. c.Assert(client.Conn.config.HTTPTimeout.IdleConnTimeout, Equals, time.Second*12)
  1291. c.Assert(client.Conn.config.HTTPTimeout.LongTimeout, Equals, time.Second*12*10)
  1292. c.Assert(client.Conn.config.SecurityToken, Equals, "token")
  1293. c.Assert(client.Conn.config.IsCname, Equals, true)
  1294. c.Assert(client.Conn.config.IsUseProxy, Equals, true)
  1295. c.Assert(client.Config.ProxyHost, Equals, proxyHost)
  1296. client, err = New(endpoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
  1297. c.Assert(client.Conn.config.IsUseProxy, Equals, true)
  1298. c.Assert(client.Config.ProxyHost, Equals, proxyHost)
  1299. c.Assert(client.Conn.config.IsAuthProxy, Equals, true)
  1300. c.Assert(client.Conn.config.ProxyUser, Equals, proxyUser)
  1301. c.Assert(client.Conn.config.ProxyPassword, Equals, proxyPasswd)
  1302. client, err = New(endpoint, accessID, accessKey, UserAgent("go sdk user agent"))
  1303. c.Assert(client.Conn.config.UserAgent, Equals, "go sdk user agent")
  1304. // Check we can overide the http.Client
  1305. httpClient := new(http.Client)
  1306. client, err = New(endpoint, accessID, accessKey, HTTPClient(httpClient))
  1307. c.Assert(client.HTTPClient, Equals, httpClient)
  1308. c.Assert(client.Conn.client, Equals, httpClient)
  1309. client, err = New(endpoint, accessID, accessKey)
  1310. c.Assert(client.HTTPClient, IsNil)
  1311. }
  1312. // TestProxy
  1313. func (s *OssClientSuite) TestProxy(c *C) {
  1314. bucketNameTest := bucketNamePrefix + randLowStr(6)
  1315. objectName := "体育/奥运/首金"
  1316. objectValue := "大江东去,浪淘尽,千古风流人物。 故垒西边,人道是、三国周郎赤壁。"
  1317. client, err := New(endpoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
  1318. // Create bucket
  1319. err = client.CreateBucket(bucketNameTest)
  1320. c.Assert(err, IsNil)
  1321. // Get bucket info
  1322. _, err = client.GetBucketInfo(bucketNameTest)
  1323. c.Assert(err, IsNil)
  1324. bucket, err := client.Bucket(bucketNameTest)
  1325. // Sign URL
  1326. str, err := bucket.SignURL(objectName, HTTPPut, 60)
  1327. c.Assert(err, IsNil)
  1328. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  1329. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  1330. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  1331. // Put object with URL
  1332. err = bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  1333. c.Assert(err, IsNil)
  1334. // Sign URL for get object
  1335. str, err = bucket.SignURL(objectName, HTTPGet, 60)
  1336. c.Assert(err, IsNil)
  1337. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  1338. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  1339. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  1340. // Get object with URL
  1341. body, err := bucket.GetObjectWithURL(str)
  1342. c.Assert(err, IsNil)
  1343. str, err = readBody(body)
  1344. c.Assert(err, IsNil)
  1345. c.Assert(str, Equals, objectValue)
  1346. // Put object
  1347. err = bucket.PutObject(objectName, strings.NewReader(objectValue))
  1348. c.Assert(err, IsNil)
  1349. // Get object
  1350. _, err = bucket.GetObject(objectName)
  1351. c.Assert(err, IsNil)
  1352. // List objects
  1353. _, err = bucket.ListObjects()
  1354. c.Assert(err, IsNil)
  1355. // Delete object
  1356. err = bucket.DeleteObject(objectName)
  1357. c.Assert(err, IsNil)
  1358. // Delete bucket
  1359. err = client.DeleteBucket(bucketNameTest)
  1360. c.Assert(err, IsNil)
  1361. }
  1362. // Private
  1363. func (s *OssClientSuite) checkBucket(buckets []BucketProperties, bucket string) bool {
  1364. for _, v := range buckets {
  1365. if v.Name == bucket {
  1366. return true
  1367. }
  1368. }
  1369. return false
  1370. }
  1371. func (s *OssClientSuite) getBucket(buckets []BucketProperties, bucket string) (bool, BucketProperties) {
  1372. for _, v := range buckets {
  1373. if v.Name == bucket {
  1374. return true, v
  1375. }
  1376. }
  1377. return false, BucketProperties{}
  1378. }
  1379. func (s *OssClientSuite) TestHttpLogNotSignUrl(c *C) {
  1380. logName := "." + string(os.PathSeparator) + "test-go-sdk-httpdebug.log" + randStr(5)
  1381. f, err := os.OpenFile(logName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660)
  1382. c.Assert(err, IsNil)
  1383. client, err := New(endpoint, accessID, accessKey)
  1384. client.Config.LogLevel = Debug
  1385. client.Config.Logger = log.New(f, "", log.LstdFlags)
  1386. var testBucketName = bucketNamePrefix + randLowStr(6)
  1387. // CreateBucket
  1388. err = client.CreateBucket(testBucketName)
  1389. f.Close()
  1390. // read log file,get http info
  1391. contents, err := ioutil.ReadFile(logName)
  1392. c.Assert(err, IsNil)
  1393. httpContent := string(contents)
  1394. //fmt.Println(httpContent)
  1395. c.Assert(strings.Contains(httpContent, "signStr"), Equals, true)
  1396. c.Assert(strings.Contains(httpContent, "Method:"), Equals, true)
  1397. // delete test bucket and log
  1398. os.Remove(logName)
  1399. client.DeleteBucket(testBucketName)
  1400. }
  1401. func (s *OssClientSuite) TestHttpLogSignUrl(c *C) {
  1402. logName := "." + string(os.PathSeparator) + "test-go-sdk-httpdebug-signurl.log" + randStr(5)
  1403. f, err := os.OpenFile(logName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660)
  1404. c.Assert(err, IsNil)
  1405. client, err := New(endpoint, accessID, accessKey)
  1406. client.Config.LogLevel = Debug
  1407. client.Config.Logger = log.New(f, "", log.LstdFlags)
  1408. var testBucketName = bucketNamePrefix + randLowStr(6)
  1409. // CreateBucket
  1410. err = client.CreateBucket(testBucketName)
  1411. f.Close()
  1412. // clear log
  1413. f, err = os.OpenFile(logName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660)
  1414. client.Config.Logger = log.New(f, "", log.LstdFlags)
  1415. bucket, _ := client.Bucket(testBucketName)
  1416. objectName := objectNamePrefix + randStr(8)
  1417. objectValue := randStr(20)
  1418. // Sign URL for put
  1419. str, err := bucket.SignURL(objectName, HTTPPut, 60)
  1420. c.Assert(err, IsNil)
  1421. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  1422. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  1423. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  1424. // Error put object with URL
  1425. err = bucket.PutObjectWithURL(str, strings.NewReader(objectValue), ContentType("image/tiff"))
  1426. f.Close()
  1427. // read log file,get http info
  1428. contents, err := ioutil.ReadFile(logName)
  1429. c.Assert(err, IsNil)
  1430. httpContent := string(contents)
  1431. //fmt.Println(httpContent)
  1432. c.Assert(strings.Contains(httpContent, "signStr"), Equals, true)
  1433. c.Assert(strings.Contains(httpContent, "Method:"), Equals, true)
  1434. // delete test bucket and log
  1435. os.Remove(logName)
  1436. client.DeleteBucket(testBucketName)
  1437. }
  1438. func (s *OssClientSuite) TestSetLimitUploadSpeed(c *C) {
  1439. client, err := New(endpoint, accessID, accessKey)
  1440. c.Assert(err, IsNil)
  1441. err = client.LimitUploadSpeed(100)
  1442. goVersion := runtime.Version()
  1443. pSlice := strings.Split(strings.ToLower(goVersion), ".")
  1444. // compare with go1.7
  1445. if len(pSlice) >= 2 {
  1446. if pSlice[0] > "go1" {
  1447. c.Assert(err, IsNil)
  1448. } else if pSlice[0] == "go1" && pSlice[1] >= "7" {
  1449. c.Assert(err, IsNil)
  1450. } else {
  1451. c.Assert(err, NotNil)
  1452. }
  1453. } else {
  1454. c.Assert(err, NotNil)
  1455. }
  1456. }
  1457. func (s *OssClientSuite) TestBucketEncyptionError(c *C) {
  1458. client, err := New(endpoint, accessID, accessKey)
  1459. c.Assert(err, IsNil)
  1460. bucketName := bucketNamePrefix + randLowStr(5)
  1461. err = client.CreateBucket(bucketName)
  1462. c.Assert(err, IsNil)
  1463. // SetBucketEncryption:AES256 ,"123"
  1464. encryptionRule := ServerEncryptionRule{}
  1465. encryptionRule.SSEDefault.SSEAlgorithm = string(AESAlgorithm)
  1466. encryptionRule.SSEDefault.KMSMasterKeyID = "123"
  1467. err = client.SetBucketEncryption(bucketName, encryptionRule)
  1468. c.Assert(err, NotNil)
  1469. // GetBucketEncryption
  1470. _, err = client.GetBucketEncryption(bucketName)
  1471. c.Assert(err, NotNil)
  1472. // Get default bucket info
  1473. bucketResult, err := client.GetBucketInfo(bucketName)
  1474. c.Assert(err, IsNil)
  1475. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "")
  1476. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
  1477. err = client.DeleteBucket(bucketName)
  1478. c.Assert(err, IsNil)
  1479. }
  1480. func (s *OssClientSuite) TestBucketEncyptionPutAndGetAndDelete(c *C) {
  1481. client, err := New(endpoint, accessID, accessKey)
  1482. c.Assert(err, IsNil)
  1483. bucketName := bucketNamePrefix + randLowStr(5)
  1484. err = client.CreateBucket(bucketName)
  1485. c.Assert(err, IsNil)
  1486. // SetBucketEncryption:KMS ,""
  1487. encryptionRule := ServerEncryptionRule{}
  1488. encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
  1489. err = client.SetBucketEncryption(bucketName, encryptionRule)
  1490. c.Assert(err, IsNil)
  1491. // GetBucketEncryption
  1492. getResult, err := client.GetBucketEncryption(bucketName)
  1493. c.Assert(err, IsNil)
  1494. // check encryption value
  1495. c.Assert(encryptionRule.SSEDefault.SSEAlgorithm, Equals, getResult.SSEDefault.SSEAlgorithm)
  1496. c.Assert(encryptionRule.SSEDefault.KMSMasterKeyID, Equals, getResult.SSEDefault.KMSMasterKeyID)
  1497. // delete bucket encyption
  1498. err = client.DeleteBucketEncryption(bucketName)
  1499. c.Assert(err, IsNil)
  1500. // GetBucketEncryption failure
  1501. _, err = client.GetBucketEncryption(bucketName)
  1502. c.Assert(err, NotNil)
  1503. // Get default bucket info
  1504. bucketResult, err := client.GetBucketInfo(bucketName)
  1505. c.Assert(err, IsNil)
  1506. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "")
  1507. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
  1508. err = client.DeleteBucket(bucketName)
  1509. c.Assert(err, IsNil)
  1510. }
  1511. func (s *OssClientSuite) TestBucketEncyptionPutObjectSuccess(c *C) {
  1512. client, err := New(endpoint, accessID, accessKey)
  1513. c.Assert(err, IsNil)
  1514. bucketName := bucketNamePrefix + randLowStr(5)
  1515. err = client.CreateBucket(bucketName)
  1516. c.Assert(err, IsNil)
  1517. // SetBucketEncryption:KMS ,""
  1518. encryptionRule := ServerEncryptionRule{}
  1519. encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
  1520. err = client.SetBucketEncryption(bucketName, encryptionRule)
  1521. c.Assert(err, IsNil)
  1522. // GetBucketEncryption
  1523. getResult, err := client.GetBucketEncryption(bucketName)
  1524. c.Assert(err, IsNil)
  1525. // check encryption value
  1526. c.Assert(encryptionRule.SSEDefault.SSEAlgorithm, Equals, getResult.SSEDefault.SSEAlgorithm)
  1527. c.Assert(encryptionRule.SSEDefault.KMSMasterKeyID, Equals, getResult.SSEDefault.KMSMasterKeyID)
  1528. // Get default bucket info
  1529. bucketResult, err := client.GetBucketInfo(bucketName)
  1530. c.Assert(err, IsNil)
  1531. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "KMS")
  1532. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
  1533. err = client.DeleteBucket(bucketName)
  1534. c.Assert(err, IsNil)
  1535. }
  1536. func (s *OssClientSuite) TestBucketEncyptionPutObjectError(c *C) {
  1537. client, err := New(endpoint, accessID, accessKey)
  1538. c.Assert(err, IsNil)
  1539. bucketName := bucketNamePrefix + randLowStr(5)
  1540. err = client.CreateBucket(bucketName)
  1541. c.Assert(err, IsNil)
  1542. // SetBucketEncryption:KMS ,""
  1543. encryptionRule := ServerEncryptionRule{}
  1544. encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
  1545. encryptionRule.SSEDefault.KMSMasterKeyID = "123"
  1546. err = client.SetBucketEncryption(bucketName, encryptionRule)
  1547. c.Assert(err, IsNil)
  1548. // GetBucketEncryption
  1549. getResult, err := client.GetBucketEncryption(bucketName)
  1550. c.Assert(err, IsNil)
  1551. // check encryption value
  1552. c.Assert(encryptionRule.SSEDefault.SSEAlgorithm, Equals, getResult.SSEDefault.SSEAlgorithm)
  1553. c.Assert(encryptionRule.SSEDefault.KMSMasterKeyID, Equals, getResult.SSEDefault.KMSMasterKeyID)
  1554. // Get default bucket info
  1555. bucketResult, err := client.GetBucketInfo(bucketName)
  1556. c.Assert(err, IsNil)
  1557. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "KMS")
  1558. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "123")
  1559. // put and get object failure
  1560. bucket, err := client.Bucket(bucketName)
  1561. c.Assert(err, IsNil)
  1562. // put object failure
  1563. objectName := objectNamePrefix + randStr(8)
  1564. context := randStr(100)
  1565. err = bucket.PutObject(objectName, strings.NewReader(context))
  1566. c.Assert(err, NotNil)
  1567. err = client.DeleteBucket(bucketName)
  1568. c.Assert(err, IsNil)
  1569. }
  1570. func (s *OssClientSuite) TestGetBucketStat(c *C) {
  1571. client, err := New(endpoint, accessID, accessKey)
  1572. c.Assert(err, IsNil)
  1573. bucketName := bucketNamePrefix + randLowStr(5)
  1574. err = client.CreateBucket(bucketName)
  1575. c.Assert(err, IsNil)
  1576. bucket, err := client.Bucket(bucketName)
  1577. c.Assert(err, IsNil)
  1578. // put object
  1579. objectName := objectNamePrefix + randLowStr(5)
  1580. err = bucket.PutObject(objectName, strings.NewReader(randStr(10)))
  1581. c.Assert(err, IsNil)
  1582. bucket.DeleteObject(objectName)
  1583. err = bucket.PutObject(objectName, strings.NewReader(randStr(10)))
  1584. c.Assert(err, IsNil)
  1585. bucket.DeleteObject(objectName)
  1586. _, err = client.GetBucketStat(bucketName)
  1587. c.Assert(err, IsNil)
  1588. client.DeleteBucket(bucketName)
  1589. }