client_test.go 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841
  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. }