client_test.go 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971
  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. "encoding/json"
  7. "io/ioutil"
  8. "log"
  9. "math/rand"
  10. "net"
  11. "net/http"
  12. "os"
  13. "reflect"
  14. "runtime"
  15. "strconv"
  16. "strings"
  17. "testing"
  18. "time"
  19. . "gopkg.in/check.v1"
  20. )
  21. // Test hooks up gocheck into the "go test" runner.
  22. func Test(t *testing.T) {
  23. TestingT(t)
  24. }
  25. type OssClientSuite struct{}
  26. var _ = Suite(&OssClientSuite{})
  27. var (
  28. // Endpoint/ID/Key
  29. endpoint = os.Getenv("OSS_TEST_ENDPOINT")
  30. accessID = os.Getenv("OSS_TEST_ACCESS_KEY_ID")
  31. accessKey = os.Getenv("OSS_TEST_ACCESS_KEY_SECRET")
  32. // Proxy
  33. proxyHost = os.Getenv("OSS_TEST_PROXY_HOST")
  34. proxyUser = os.Getenv("OSS_TEST_PROXY_USER")
  35. proxyPasswd = os.Getenv("OSS_TEST_PROXY_PASSWORD")
  36. // STS
  37. stsaccessID = os.Getenv("OSS_TEST_STS_ID")
  38. stsaccessKey = os.Getenv("OSS_TEST_STS_KEY")
  39. stsARN = os.Getenv("OSS_TEST_STS_ARN")
  40. // Credential
  41. credentialAccessID = os.Getenv("OSS_CREDENTIAL_KEY_ID")
  42. credentialAccessKey = os.Getenv("OSS_CREDENTIAL_KEY_SECRET")
  43. credentialUID = os.Getenv("OSS_CREDENTIAL_UID")
  44. )
  45. var (
  46. // prefix of bucket name for bucket ops test
  47. bucketNamePrefix = "go-sdk-test-bucket-"
  48. // bucket name for object ops test
  49. bucketName = bucketNamePrefix + randLowStr(6)
  50. archiveBucketName = bucketNamePrefix + "arch-" + randLowStr(6)
  51. // object name for object ops test
  52. objectNamePrefix = "go-sdk-test-object-"
  53. // sts region is one and only hangzhou
  54. stsRegion = "cn-hangzhou"
  55. // Credentials
  56. credentialBucketName = bucketNamePrefix + randLowStr(6)
  57. )
  58. var (
  59. logPath = "go_sdk_test_" + time.Now().Format("20060102_150405") + ".log"
  60. testLogFile, _ = os.OpenFile(logPath, os.O_RDWR|os.O_CREATE, 0664)
  61. testLogger = log.New(testLogFile, "", log.Ldate|log.Ltime|log.Lshortfile)
  62. letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
  63. timeoutInOperation = 3 * time.Second
  64. )
  65. func randStr(n int) string {
  66. b := make([]rune, n)
  67. randMarker := rand.New(rand.NewSource(time.Now().UnixNano()))
  68. for i := range b {
  69. b[i] = letters[randMarker.Intn(len(letters))]
  70. }
  71. return string(b)
  72. }
  73. func createFile(fileName, content string, c *C) {
  74. fout, err := os.Create(fileName)
  75. defer fout.Close()
  76. c.Assert(err, IsNil)
  77. _, err = fout.WriteString(content)
  78. c.Assert(err, IsNil)
  79. }
  80. func randLowStr(n int) string {
  81. return strings.ToLower(randStr(n))
  82. }
  83. func forceDeleteBucket(client *Client, bucketName string, c *C) {
  84. bucket, err := client.Bucket(bucketName)
  85. c.Assert(err, IsNil)
  86. // Delete Object
  87. marker := Marker("")
  88. for {
  89. lor, err := bucket.ListObjects(marker)
  90. c.Assert(err, IsNil)
  91. for _, object := range lor.Objects {
  92. err = bucket.DeleteObject(object.Key)
  93. c.Assert(err, IsNil)
  94. }
  95. marker = Marker(lor.NextMarker)
  96. if !lor.IsTruncated {
  97. break
  98. }
  99. }
  100. // Delete Object Versions and DeleteMarks
  101. keyMarker := KeyMarker("")
  102. versionIdMarker := VersionIdMarker("")
  103. options := []Option{keyMarker, versionIdMarker}
  104. for {
  105. lor, err := bucket.ListObjectVersions(options...)
  106. if err != nil {
  107. break
  108. }
  109. for _, object := range lor.ObjectDeleteMarkers {
  110. err = bucket.DeleteObject(object.Key, VersionId(object.VersionId))
  111. c.Assert(err, IsNil)
  112. }
  113. for _, object := range lor.ObjectVersions {
  114. err = bucket.DeleteObject(object.Key, VersionId(object.VersionId))
  115. c.Assert(err, IsNil)
  116. }
  117. keyMarker = KeyMarker(lor.NextKeyMarker)
  118. versionIdMarker := VersionIdMarker(lor.NextVersionIdMarker)
  119. options = []Option{keyMarker, versionIdMarker}
  120. if !lor.IsTruncated {
  121. break
  122. }
  123. }
  124. // Delete Part
  125. keyMarker = KeyMarker("")
  126. uploadIDMarker := UploadIDMarker("")
  127. for {
  128. lmur, err := bucket.ListMultipartUploads(keyMarker, uploadIDMarker)
  129. c.Assert(err, IsNil)
  130. for _, upload := range lmur.Uploads {
  131. var imur = InitiateMultipartUploadResult{Bucket: bucketName,
  132. Key: upload.Key, UploadID: upload.UploadID}
  133. err = bucket.AbortMultipartUpload(imur)
  134. c.Assert(err, IsNil)
  135. }
  136. keyMarker = KeyMarker(lmur.NextKeyMarker)
  137. uploadIDMarker = UploadIDMarker(lmur.NextUploadIDMarker)
  138. if !lmur.IsTruncated {
  139. break
  140. }
  141. }
  142. // delete live channel
  143. strMarker := ""
  144. for {
  145. result, err := bucket.ListLiveChannel(Marker(strMarker))
  146. c.Assert(err, IsNil)
  147. for _, channel := range result.LiveChannel {
  148. err := bucket.DeleteLiveChannel(channel.Name)
  149. c.Assert(err, IsNil)
  150. }
  151. if result.IsTruncated {
  152. strMarker = result.NextMarker
  153. } else {
  154. break
  155. }
  156. }
  157. // Delete Bucket
  158. err = client.DeleteBucket(bucketName)
  159. c.Assert(err, IsNil)
  160. }
  161. // SetUpSuite runs once when the suite starts running
  162. func (s *OssClientSuite) SetUpSuite(c *C) {
  163. client, err := New(endpoint, accessID, accessKey)
  164. c.Assert(err, IsNil)
  165. lbr, err := client.ListBuckets(Prefix(bucketNamePrefix), MaxKeys(1000))
  166. c.Assert(err, IsNil)
  167. for _, bucket := range lbr.Buckets {
  168. forceDeleteBucket(client, bucket.Name, c)
  169. }
  170. testLogger.Println("test client started")
  171. }
  172. // TearDownSuite runs before each test or benchmark starts running
  173. func (s *OssClientSuite) TearDownSuite(c *C) {
  174. client, err := New(endpoint, accessID, accessKey)
  175. c.Assert(err, IsNil)
  176. lbr, err := client.ListBuckets(Prefix(bucketNamePrefix), MaxKeys(1000))
  177. c.Assert(err, IsNil)
  178. for _, bucket := range lbr.Buckets {
  179. s.deleteBucket(client, bucket.Name, c)
  180. }
  181. testLogger.Println("test client completed")
  182. }
  183. func (s *OssClientSuite) deleteBucket(client *Client, bucketName string, c *C) {
  184. forceDeleteBucket(client, bucketName, c)
  185. }
  186. // SetUpTest runs after each test or benchmark runs
  187. func (s *OssClientSuite) SetUpTest(c *C) {
  188. }
  189. // TearDownTest runs once after all tests or benchmarks have finished running
  190. func (s *OssClientSuite) TearDownTest(c *C) {
  191. }
  192. // TestCreateBucket
  193. func (s *OssClientSuite) TestCreateBucket(c *C) {
  194. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  195. client, err := New(endpoint, accessID, accessKey)
  196. c.Assert(err, IsNil)
  197. // Create
  198. client.DeleteBucket(bucketNameTest)
  199. err = client.CreateBucket(bucketNameTest)
  200. c.Assert(err, IsNil)
  201. //sleep 3 seconds after create bucket
  202. time.Sleep(timeoutInOperation)
  203. // verify bucket is exist
  204. found, err := client.IsBucketExist(bucketNameTest)
  205. c.Assert(err, IsNil)
  206. c.Assert(found, Equals, true)
  207. res, err := client.GetBucketACL(bucketNameTest)
  208. c.Assert(err, IsNil)
  209. c.Assert(res.ACL, Equals, string(ACLPrivate))
  210. err = client.DeleteBucket(bucketNameTest)
  211. c.Assert(err, IsNil)
  212. time.Sleep(timeoutInOperation)
  213. // CreateBucket creates with ACLPublicRead
  214. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicRead))
  215. c.Assert(err, IsNil)
  216. time.Sleep(timeoutInOperation)
  217. res, err = client.GetBucketACL(bucketNameTest)
  218. c.Assert(err, IsNil)
  219. c.Assert(res.ACL, Equals, string(ACLPublicRead))
  220. err = client.DeleteBucket(bucketNameTest)
  221. c.Assert(err, IsNil)
  222. time.Sleep(timeoutInOperation)
  223. // ACLPublicReadWrite
  224. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicReadWrite))
  225. c.Assert(err, IsNil)
  226. time.Sleep(timeoutInOperation)
  227. res, err = client.GetBucketACL(bucketNameTest)
  228. c.Assert(err, IsNil)
  229. c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
  230. err = client.DeleteBucket(bucketNameTest)
  231. c.Assert(err, IsNil)
  232. time.Sleep(timeoutInOperation)
  233. // ACLPrivate
  234. err = client.CreateBucket(bucketNameTest, ACL(ACLPrivate))
  235. c.Assert(err, IsNil)
  236. time.Sleep(timeoutInOperation)
  237. res, err = client.GetBucketACL(bucketNameTest)
  238. c.Assert(err, IsNil)
  239. c.Assert(res.ACL, Equals, string(ACLPrivate))
  240. // Delete
  241. err = client.DeleteBucket(bucketNameTest)
  242. c.Assert(err, IsNil)
  243. // Create bucket with configuration and test GetBucketInfo
  244. for _, storage := range []StorageClassType{StorageStandard, StorageIA, StorageArchive} {
  245. bucketNameTest := bucketNamePrefix + randLowStr(6)
  246. err = client.CreateBucket(bucketNameTest, StorageClass(storage), ACL(ACLPublicRead))
  247. c.Assert(err, IsNil)
  248. time.Sleep(timeoutInOperation)
  249. res, err := client.GetBucketInfo(bucketNameTest)
  250. c.Assert(err, IsNil)
  251. c.Assert(res.BucketInfo.Name, Equals, bucketNameTest)
  252. c.Assert(res.BucketInfo.StorageClass, Equals, string(storage))
  253. c.Assert(res.BucketInfo.ACL, Equals, string(ACLPublicRead))
  254. // Delete
  255. err = client.DeleteBucket(bucketNameTest)
  256. c.Assert(err, IsNil)
  257. }
  258. // Error put bucket with configuration
  259. err = client.CreateBucket("ERRORBUCKETNAME", StorageClass(StorageArchive))
  260. c.Assert(err, NotNil)
  261. // Create bucket with configuration and test ListBuckets
  262. for _, storage := range []StorageClassType{StorageStandard, StorageIA, StorageArchive} {
  263. bucketNameTest := bucketNamePrefix + randLowStr(6)
  264. err = client.CreateBucket(bucketNameTest, StorageClass(storage))
  265. c.Assert(err, IsNil)
  266. time.Sleep(timeoutInOperation)
  267. res, err := client.GetBucketInfo(bucketNameTest)
  268. c.Assert(err, IsNil)
  269. c.Assert(res.BucketInfo.Name, Equals, bucketNameTest)
  270. c.Assert(res.BucketInfo.StorageClass, Equals, string(storage))
  271. // Delete
  272. err = client.DeleteBucket(bucketNameTest)
  273. c.Assert(err, IsNil)
  274. }
  275. }
  276. func (s *OssClientSuite) TestCreateBucketRedundancyType(c *C) {
  277. bucketNameTest := bucketNamePrefix + randLowStr(6)
  278. client, err := New(endpoint, accessID, accessKey)
  279. c.Assert(err, IsNil)
  280. // CreateBucket creates without property
  281. err = client.CreateBucket(bucketNameTest)
  282. c.Assert(err, IsNil)
  283. client.DeleteBucket(bucketNameTest)
  284. time.Sleep(timeoutInOperation)
  285. // CreateBucket creates with RedundancyZRS
  286. err = client.CreateBucket(bucketNameTest, RedundancyType(RedundancyZRS))
  287. c.Assert(err, IsNil)
  288. res, err := client.GetBucketInfo(bucketNameTest)
  289. c.Assert(err, IsNil)
  290. c.Assert(res.BucketInfo.RedundancyType, Equals, string(RedundancyZRS))
  291. client.DeleteBucket(bucketNameTest)
  292. time.Sleep(timeoutInOperation)
  293. // CreateBucket creates with RedundancyLRS
  294. err = client.CreateBucket(bucketNameTest, RedundancyType(RedundancyLRS))
  295. c.Assert(err, IsNil)
  296. res, err = client.GetBucketInfo(bucketNameTest)
  297. c.Assert(err, IsNil)
  298. c.Assert(res.BucketInfo.RedundancyType, Equals, string(RedundancyLRS))
  299. c.Assert(res.BucketInfo.StorageClass, Equals, string(StorageStandard))
  300. client.DeleteBucket(bucketNameTest)
  301. time.Sleep(timeoutInOperation)
  302. // CreateBucket creates with ACLPublicRead RedundancyZRS
  303. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicRead), RedundancyType(RedundancyZRS))
  304. c.Assert(err, IsNil)
  305. res, err = client.GetBucketInfo(bucketNameTest)
  306. c.Assert(err, IsNil)
  307. c.Assert(res.BucketInfo.RedundancyType, Equals, string(RedundancyZRS))
  308. c.Assert(res.BucketInfo.ACL, Equals, string(ACLPublicRead))
  309. client.DeleteBucket(bucketNameTest)
  310. }
  311. // TestCreateBucketNegative
  312. func (s *OssClientSuite) TestCreateBucketNegative(c *C) {
  313. client, err := New(endpoint, accessID, accessKey)
  314. c.Assert(err, IsNil)
  315. // Bucket name invalid
  316. err = client.CreateBucket("xx")
  317. c.Assert(err, NotNil)
  318. err = client.CreateBucket("XXXX")
  319. c.Assert(err, NotNil)
  320. testLogger.Println(err)
  321. err = client.CreateBucket("_bucket")
  322. c.Assert(err, NotNil)
  323. testLogger.Println(err)
  324. // ACL invalid
  325. err = client.CreateBucket(bucketNamePrefix+randLowStr(6), ACL("InvaldAcl"))
  326. c.Assert(err, NotNil)
  327. testLogger.Println(err)
  328. }
  329. // TestDeleteBucket
  330. func (s *OssClientSuite) TestDeleteBucket(c *C) {
  331. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  332. client, err := New(endpoint, accessID, accessKey)
  333. c.Assert(err, IsNil)
  334. // Create
  335. err = client.CreateBucket(bucketNameTest)
  336. c.Assert(err, IsNil)
  337. time.Sleep(timeoutInOperation)
  338. // Check
  339. found, err := client.IsBucketExist(bucketNameTest)
  340. c.Assert(err, IsNil)
  341. c.Assert(found, Equals, true)
  342. // Delete
  343. err = client.DeleteBucket(bucketNameTest)
  344. c.Assert(err, IsNil)
  345. time.Sleep(timeoutInOperation)
  346. // Check
  347. found, err = client.IsBucketExist(bucketNameTest)
  348. c.Assert(err, IsNil)
  349. c.Assert(found, Equals, false)
  350. err = client.DeleteBucket(bucketNameTest)
  351. c.Assert(err, NotNil)
  352. }
  353. // TestDeleteBucketNegative
  354. func (s *OssClientSuite) TestDeleteBucketNegative(c *C) {
  355. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  356. client, err := New(endpoint, accessID, accessKey)
  357. c.Assert(err, IsNil)
  358. // Bucket name invalid
  359. err = client.DeleteBucket("xx")
  360. c.Assert(err, NotNil)
  361. err = client.DeleteBucket("XXXX")
  362. c.Assert(err, NotNil)
  363. err = client.DeleteBucket("_bucket")
  364. c.Assert(err, NotNil)
  365. // Delete no exist bucket
  366. err = client.DeleteBucket("notexist")
  367. c.Assert(err, NotNil)
  368. // No permission to delete, this ak/sk for js sdk
  369. err = client.CreateBucket(bucketNameTest)
  370. c.Assert(err, IsNil)
  371. accessID := "<accessKeyId>"
  372. accessKey := "<accessKeySecret>"
  373. clientOtherUser, err := New(endpoint, accessID, accessKey)
  374. c.Assert(err, IsNil)
  375. err = clientOtherUser.DeleteBucket(bucketNameTest)
  376. c.Assert(err, NotNil)
  377. err = client.DeleteBucket(bucketNameTest)
  378. c.Assert(err, IsNil)
  379. }
  380. // TestListBucket
  381. func (s *OssClientSuite) TestListBucket(c *C) {
  382. var prefix = bucketNamePrefix + randLowStr(6)
  383. var bucketNameLbOne = prefix + "tlb1"
  384. var bucketNameLbTwo = prefix + "tlb2"
  385. var bucketNameLbThree = prefix + "tlb3"
  386. client, err := New(endpoint, accessID, accessKey)
  387. c.Assert(err, IsNil)
  388. // CreateBucket
  389. err = client.CreateBucket(bucketNameLbOne)
  390. c.Assert(err, IsNil)
  391. err = client.CreateBucket(bucketNameLbTwo)
  392. c.Assert(err, IsNil)
  393. err = client.CreateBucket(bucketNameLbThree)
  394. c.Assert(err, IsNil)
  395. // ListBuckets, specified prefix
  396. var respHeader http.Header
  397. lbr, err := client.ListBuckets(Prefix(prefix), MaxKeys(2), GetResponseHeader(&respHeader))
  398. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  399. c.Assert(err, IsNil)
  400. c.Assert(len(lbr.Buckets), Equals, 2)
  401. // ListBuckets, specified max keys
  402. lbr, err = client.ListBuckets(MaxKeys(2))
  403. c.Assert(err, IsNil)
  404. c.Assert(len(lbr.Buckets), Equals, 2)
  405. // ListBuckets, specified max keys
  406. lbr, err = client.ListBuckets(Marker(bucketNameLbOne), MaxKeys(1))
  407. c.Assert(err, IsNil)
  408. c.Assert(len(lbr.Buckets), Equals, 1)
  409. // ListBuckets, specified max keys
  410. lbr, err = client.ListBuckets(Marker(bucketNameLbOne))
  411. c.Assert(err, IsNil)
  412. c.Assert(len(lbr.Buckets) >= 2, Equals, true)
  413. // DeleteBucket
  414. err = client.DeleteBucket(bucketNameLbOne)
  415. c.Assert(err, IsNil)
  416. err = client.DeleteBucket(bucketNameLbTwo)
  417. c.Assert(err, IsNil)
  418. err = client.DeleteBucket(bucketNameLbThree)
  419. c.Assert(err, IsNil)
  420. }
  421. // TestListBucket
  422. func (s *OssClientSuite) TestIsBucketExist(c *C) {
  423. var prefix = bucketNamePrefix + randLowStr(6)
  424. var bucketNameLbOne = prefix + "tibe1"
  425. var bucketNameLbTwo = prefix + "tibe11"
  426. var bucketNameLbThree = prefix + "tibe111"
  427. client, err := New(endpoint, accessID, accessKey)
  428. c.Assert(err, IsNil)
  429. // CreateBucket
  430. err = client.CreateBucket(bucketNameLbOne)
  431. c.Assert(err, IsNil)
  432. err = client.CreateBucket(bucketNameLbTwo)
  433. c.Assert(err, IsNil)
  434. err = client.CreateBucket(bucketNameLbThree)
  435. c.Assert(err, IsNil)
  436. // Exist
  437. exist, err := client.IsBucketExist(bucketNameLbTwo)
  438. c.Assert(err, IsNil)
  439. c.Assert(exist, Equals, true)
  440. exist, err = client.IsBucketExist(bucketNameLbThree)
  441. c.Assert(err, IsNil)
  442. c.Assert(exist, Equals, true)
  443. exist, err = client.IsBucketExist(bucketNameLbOne)
  444. c.Assert(err, IsNil)
  445. c.Assert(exist, Equals, true)
  446. // Not exist
  447. exist, err = client.IsBucketExist(prefix + "tibe")
  448. c.Assert(err, IsNil)
  449. c.Assert(exist, Equals, false)
  450. exist, err = client.IsBucketExist(prefix + "tibe1111")
  451. c.Assert(err, IsNil)
  452. c.Assert(exist, Equals, false)
  453. // Negative
  454. exist, err = client.IsBucketExist("BucketNameInvalid")
  455. c.Assert(err, NotNil)
  456. // DeleteBucket
  457. err = client.DeleteBucket(bucketNameLbOne)
  458. c.Assert(err, IsNil)
  459. err = client.DeleteBucket(bucketNameLbTwo)
  460. c.Assert(err, IsNil)
  461. err = client.DeleteBucket(bucketNameLbThree)
  462. c.Assert(err, IsNil)
  463. }
  464. // TestSetBucketAcl
  465. func (s *OssClientSuite) TestSetBucketAcl(c *C) {
  466. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  467. client, err := New(endpoint, accessID, accessKey)
  468. c.Assert(err, IsNil)
  469. // Private
  470. err = client.CreateBucket(bucketNameTest)
  471. c.Assert(err, IsNil)
  472. res, err := client.GetBucketACL(bucketNameTest)
  473. c.Assert(err, IsNil)
  474. c.Assert(res.ACL, Equals, string(ACLPrivate))
  475. // Set ACL_PUBLIC_R
  476. err = client.SetBucketACL(bucketNameTest, ACLPublicRead)
  477. c.Assert(err, IsNil)
  478. time.Sleep(timeoutInOperation)
  479. res, err = client.GetBucketACL(bucketNameTest)
  480. c.Assert(err, IsNil)
  481. c.Assert(res.ACL, Equals, string(ACLPublicRead))
  482. // Set ACL_PUBLIC_RW
  483. err = client.SetBucketACL(bucketNameTest, ACLPublicReadWrite)
  484. c.Assert(err, IsNil)
  485. time.Sleep(timeoutInOperation)
  486. res, err = client.GetBucketACL(bucketNameTest)
  487. c.Assert(err, IsNil)
  488. c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
  489. // Set ACL_PUBLIC_RW
  490. err = client.SetBucketACL(bucketNameTest, ACLPrivate)
  491. c.Assert(err, IsNil)
  492. time.Sleep(timeoutInOperation)
  493. res, err = client.GetBucketACL(bucketNameTest)
  494. c.Assert(err, IsNil)
  495. c.Assert(res.ACL, Equals, string(ACLPrivate))
  496. err = client.DeleteBucket(bucketNameTest)
  497. c.Assert(err, IsNil)
  498. }
  499. // TestSetBucketAclNegative
  500. func (s *OssClientSuite) TestBucketAclNegative(c *C) {
  501. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  502. client, err := New(endpoint, accessID, accessKey)
  503. c.Assert(err, IsNil)
  504. err = client.CreateBucket(bucketNameTest)
  505. c.Assert(err, IsNil)
  506. err = client.SetBucketACL(bucketNameTest, "InvalidACL")
  507. c.Assert(err, NotNil)
  508. testLogger.Println(err)
  509. err = client.DeleteBucket(bucketNameTest)
  510. c.Assert(err, IsNil)
  511. }
  512. // TestGetBucketAcl
  513. func (s *OssClientSuite) TestGetBucketAcl(c *C) {
  514. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  515. client, err := New(endpoint, accessID, accessKey)
  516. c.Assert(err, IsNil)
  517. // Private
  518. err = client.CreateBucket(bucketNameTest)
  519. c.Assert(err, IsNil)
  520. time.Sleep(timeoutInOperation)
  521. res, err := client.GetBucketACL(bucketNameTest)
  522. c.Assert(err, IsNil)
  523. c.Assert(res.ACL, Equals, string(ACLPrivate))
  524. err = client.DeleteBucket(bucketNameTest)
  525. c.Assert(err, IsNil)
  526. time.Sleep(timeoutInOperation)
  527. // PublicRead
  528. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicRead))
  529. c.Assert(err, IsNil)
  530. time.Sleep(timeoutInOperation)
  531. res, err = client.GetBucketACL(bucketNameTest)
  532. c.Assert(err, IsNil)
  533. c.Assert(res.ACL, Equals, string(ACLPublicRead))
  534. err = client.DeleteBucket(bucketNameTest)
  535. c.Assert(err, IsNil)
  536. time.Sleep(timeoutInOperation)
  537. // PublicReadWrite
  538. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicReadWrite))
  539. c.Assert(err, IsNil)
  540. time.Sleep(timeoutInOperation)
  541. res, err = client.GetBucketACL(bucketNameTest)
  542. c.Assert(err, IsNil)
  543. c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
  544. err = client.DeleteBucket(bucketNameTest)
  545. c.Assert(err, IsNil)
  546. }
  547. // TestGetBucketAcl
  548. func (s *OssClientSuite) TestGetBucketLocation(c *C) {
  549. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  550. client, err := New(endpoint, accessID, accessKey)
  551. c.Assert(err, IsNil)
  552. // Private
  553. err = client.CreateBucket(bucketNameTest)
  554. c.Assert(err, IsNil)
  555. loc, err := client.GetBucketLocation(bucketNameTest)
  556. c.Assert(strings.HasPrefix(loc, "oss-"), Equals, true)
  557. err = client.DeleteBucket(bucketNameTest)
  558. c.Assert(err, IsNil)
  559. }
  560. // TestGetBucketLocationNegative
  561. func (s *OssClientSuite) TestGetBucketLocationNegative(c *C) {
  562. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  563. client, err := New(endpoint, accessID, accessKey)
  564. c.Assert(err, IsNil)
  565. // Not exist
  566. _, err = client.GetBucketLocation(bucketNameTest)
  567. c.Assert(err, NotNil)
  568. // Not exist
  569. _, err = client.GetBucketLocation("InvalidBucketName_")
  570. c.Assert(err, NotNil)
  571. }
  572. // TestSetBucketLifecycle
  573. func (s *OssClientSuite) TestSetBucketLifecycle(c *C) {
  574. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  575. var rule1 = BuildLifecycleRuleByDate("rule1", "one", true, 2015, 11, 11)
  576. var rule2 = BuildLifecycleRuleByDays("rule2", "two", true, 3)
  577. client, err := New(endpoint, accessID, accessKey)
  578. c.Assert(err, IsNil)
  579. err = client.CreateBucket(bucketNameTest)
  580. c.Assert(err, IsNil)
  581. // Set single rule
  582. var rules = []LifecycleRule{rule1}
  583. err = client.SetBucketLifecycle(bucketNameTest, rules)
  584. c.Assert(err, IsNil)
  585. // Double set rule
  586. err = client.SetBucketLifecycle(bucketNameTest, rules)
  587. c.Assert(err, IsNil)
  588. res, err := client.GetBucketLifecycle(bucketNameTest)
  589. c.Assert(err, IsNil)
  590. c.Assert(len(res.Rules), Equals, 1)
  591. c.Assert(res.Rules[0].ID, Equals, "rule1")
  592. err = client.DeleteBucketLifecycle(bucketNameTest)
  593. c.Assert(err, IsNil)
  594. // Set two rules
  595. rules = []LifecycleRule{rule1, rule2}
  596. err = client.SetBucketLifecycle(bucketNameTest, rules)
  597. c.Assert(err, IsNil)
  598. // Eliminate effect of cache
  599. time.Sleep(timeoutInOperation)
  600. res, err = client.GetBucketLifecycle(bucketNameTest)
  601. c.Assert(err, IsNil)
  602. c.Assert(len(res.Rules), Equals, 2)
  603. c.Assert(res.Rules[0].ID, Equals, "rule1")
  604. c.Assert(res.Rules[1].ID, Equals, "rule2")
  605. err = client.DeleteBucket(bucketNameTest)
  606. c.Assert(err, IsNil)
  607. }
  608. // TestSetBucketLifecycleNew
  609. func (s *OssClientSuite) TestSetBucketLifecycleNew(c *C) {
  610. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  611. client, err := New(endpoint, accessID, accessKey)
  612. c.Assert(err, IsNil)
  613. err = client.CreateBucket(bucketNameTest)
  614. c.Assert(err, IsNil)
  615. //invalid status of lifecyclerule
  616. expiration := LifecycleExpiration{
  617. Days: 30,
  618. }
  619. rule := LifecycleRule{
  620. ID: "rule1",
  621. Prefix: "one",
  622. Status: "Invalid",
  623. Expiration: &expiration,
  624. }
  625. rules := []LifecycleRule{rule}
  626. err = client.SetBucketLifecycle(bucketNameTest, rules)
  627. c.Assert(err, NotNil)
  628. //invalid value of CreatedBeforeDate
  629. expiration = LifecycleExpiration{
  630. CreatedBeforeDate: randStr(10),
  631. }
  632. rule = LifecycleRule{
  633. ID: "rule1",
  634. Prefix: "one",
  635. Status: "Enabled",
  636. Expiration: &expiration,
  637. }
  638. rules = []LifecycleRule{rule}
  639. err = client.SetBucketLifecycle(bucketNameTest, rules)
  640. c.Assert(err, NotNil)
  641. //invalid value of Days
  642. abortMPU := LifecycleAbortMultipartUpload{
  643. Days: -30,
  644. }
  645. rule = LifecycleRule{
  646. ID: "rule1",
  647. Prefix: "one",
  648. Status: "Enabled",
  649. AbortMultipartUpload: &abortMPU,
  650. }
  651. rules = []LifecycleRule{rule}
  652. err = client.SetBucketLifecycle(bucketNameTest, rules)
  653. c.Assert(err, NotNil)
  654. expiration = LifecycleExpiration{
  655. CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
  656. }
  657. rule1 := LifecycleRule{
  658. ID: "rule1",
  659. Prefix: "one",
  660. Status: "Enabled",
  661. Expiration: &expiration,
  662. }
  663. abortMPU = LifecycleAbortMultipartUpload{
  664. Days: 30,
  665. }
  666. rule2 := LifecycleRule{
  667. ID: "rule2",
  668. Prefix: "two",
  669. Status: "Enabled",
  670. Expiration: &expiration,
  671. AbortMultipartUpload: &abortMPU,
  672. }
  673. transition1 := LifecycleTransition{
  674. Days: 3,
  675. StorageClass: StorageIA,
  676. }
  677. transition2 := LifecycleTransition{
  678. Days: 30,
  679. StorageClass: StorageArchive,
  680. }
  681. transitions := []LifecycleTransition{transition1, transition2}
  682. rule3 := LifecycleRule{
  683. ID: "rule3",
  684. Prefix: "three",
  685. Status: "Enabled",
  686. AbortMultipartUpload: &abortMPU,
  687. Transitions: transitions,
  688. }
  689. // Set single rule
  690. rules = []LifecycleRule{rule1}
  691. err = client.SetBucketLifecycle(bucketNameTest, rules)
  692. c.Assert(err, IsNil)
  693. res, err := client.GetBucketLifecycle(bucketNameTest)
  694. c.Assert(err, IsNil)
  695. c.Assert(len(res.Rules), Equals, 1)
  696. c.Assert(res.Rules[0].ID, Equals, "rule1")
  697. c.Assert(res.Rules[0].Expiration, NotNil)
  698. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  699. err = client.DeleteBucketLifecycle(bucketNameTest)
  700. c.Assert(err, IsNil)
  701. // Set two rule: rule1 and rule2
  702. rules = []LifecycleRule{rule1, rule2}
  703. err = client.SetBucketLifecycle(bucketNameTest, rules)
  704. c.Assert(err, IsNil)
  705. res, err = client.GetBucketLifecycle(bucketNameTest)
  706. c.Assert(err, IsNil)
  707. c.Assert(len(res.Rules), Equals, 2)
  708. c.Assert(res.Rules[0].ID, Equals, "rule1")
  709. c.Assert(res.Rules[0].Expiration, NotNil)
  710. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  711. c.Assert(res.Rules[1].ID, Equals, "rule2")
  712. c.Assert(res.Rules[1].Expiration, NotNil)
  713. c.Assert(res.Rules[1].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  714. c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
  715. c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
  716. err = client.DeleteBucketLifecycle(bucketNameTest)
  717. c.Assert(err, IsNil)
  718. // Set two rule: rule2 and rule3
  719. rules = []LifecycleRule{rule2, rule3}
  720. err = client.SetBucketLifecycle(bucketNameTest, rules)
  721. c.Assert(err, IsNil)
  722. res, err = client.GetBucketLifecycle(bucketNameTest)
  723. c.Assert(err, IsNil)
  724. c.Assert(len(res.Rules), Equals, 2)
  725. c.Assert(res.Rules[0].ID, Equals, "rule2")
  726. c.Assert(res.Rules[0].Expiration, NotNil)
  727. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  728. c.Assert(res.Rules[0].AbortMultipartUpload, NotNil)
  729. c.Assert(res.Rules[0].AbortMultipartUpload.Days, Equals, 30)
  730. c.Assert(res.Rules[1].ID, Equals, "rule3")
  731. c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
  732. c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
  733. c.Assert(len(res.Rules[1].Transitions), Equals, 2)
  734. c.Assert(res.Rules[1].Transitions[0].StorageClass, Equals, StorageIA)
  735. c.Assert(res.Rules[1].Transitions[0].Days, Equals, 3)
  736. c.Assert(res.Rules[1].Transitions[1].StorageClass, Equals, StorageArchive)
  737. c.Assert(res.Rules[1].Transitions[1].Days, Equals, 30)
  738. err = client.DeleteBucketLifecycle(bucketNameTest)
  739. c.Assert(err, IsNil)
  740. // Set two rule: rule1 and rule3
  741. rules = []LifecycleRule{rule1, rule3}
  742. err = client.SetBucketLifecycle(bucketNameTest, rules)
  743. c.Assert(err, IsNil)
  744. res, err = client.GetBucketLifecycle(bucketNameTest)
  745. c.Assert(err, IsNil)
  746. c.Assert(len(res.Rules), Equals, 2)
  747. c.Assert(res.Rules[0].ID, Equals, "rule1")
  748. c.Assert(res.Rules[0].Expiration, NotNil)
  749. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  750. c.Assert(res.Rules[1].ID, Equals, "rule3")
  751. c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
  752. c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
  753. c.Assert(len(res.Rules[1].Transitions), Equals, 2)
  754. c.Assert(res.Rules[1].Transitions[0].StorageClass, Equals, StorageIA)
  755. c.Assert(res.Rules[1].Transitions[0].Days, Equals, 3)
  756. c.Assert(res.Rules[1].Transitions[1].StorageClass, Equals, StorageArchive)
  757. c.Assert(res.Rules[1].Transitions[1].Days, Equals, 30)
  758. err = client.DeleteBucketLifecycle(bucketNameTest)
  759. c.Assert(err, IsNil)
  760. // Set three rules
  761. rules = []LifecycleRule{rule1, rule2, rule3}
  762. err = client.SetBucketLifecycle(bucketNameTest, rules)
  763. c.Assert(err, IsNil)
  764. res, err = client.GetBucketLifecycle(bucketNameTest)
  765. c.Assert(err, IsNil)
  766. c.Assert(len(res.Rules), Equals, 3)
  767. c.Assert(res.Rules[0].ID, Equals, "rule1")
  768. c.Assert(res.Rules[0].Expiration, NotNil)
  769. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  770. c.Assert(res.Rules[1].ID, Equals, "rule2")
  771. c.Assert(res.Rules[1].Expiration, NotNil)
  772. c.Assert(res.Rules[1].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  773. c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
  774. c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
  775. c.Assert(res.Rules[2].ID, Equals, "rule3")
  776. c.Assert(res.Rules[2].AbortMultipartUpload, NotNil)
  777. c.Assert(res.Rules[2].AbortMultipartUpload.Days, Equals, 30)
  778. c.Assert(len(res.Rules[2].Transitions), Equals, 2)
  779. c.Assert(res.Rules[2].Transitions[0].StorageClass, Equals, StorageIA)
  780. c.Assert(res.Rules[2].Transitions[0].Days, Equals, 3)
  781. c.Assert(res.Rules[2].Transitions[1].StorageClass, Equals, StorageArchive)
  782. c.Assert(res.Rules[2].Transitions[1].Days, Equals, 30)
  783. err = client.DeleteBucket(bucketNameTest)
  784. c.Assert(err, IsNil)
  785. }
  786. // TestSetBucketLifecycleAboutVersionObject
  787. func (s *OssClientSuite) TestSetBucketLifecycleAboutVersionObject(c *C) {
  788. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  789. client, err := New(endpoint, accessID, accessKey)
  790. c.Assert(err, IsNil)
  791. err = client.CreateBucket(bucketNameTest)
  792. c.Assert(err, IsNil)
  793. deleteMark := true
  794. expiration := LifecycleExpiration{
  795. ExpiredObjectDeleteMarker: &deleteMark,
  796. }
  797. versionExpiration := LifecycleVersionExpiration{
  798. NoncurrentDays: 20,
  799. }
  800. versionTransition := LifecycleVersionTransition{
  801. NoncurrentDays: 10,
  802. StorageClass: "IA",
  803. }
  804. rule := LifecycleRule{
  805. Status: "Enabled",
  806. Expiration: &expiration,
  807. NonVersionExpiration: &versionExpiration,
  808. NonVersionTransition: &versionTransition,
  809. }
  810. rules := []LifecycleRule{rule}
  811. err = client.SetBucketLifecycle(bucketNameTest, rules)
  812. c.Assert(err, IsNil)
  813. res, err := client.GetBucketLifecycle(bucketNameTest)
  814. c.Assert(err, IsNil)
  815. c.Assert(res.Rules[0].Expiration, NotNil)
  816. c.Assert(res.Rules[0].Expiration.Days, Equals, 0)
  817. c.Assert(res.Rules[0].Expiration.Date, Equals, "")
  818. c.Assert(*(res.Rules[0].Expiration.ExpiredObjectDeleteMarker), Equals, true)
  819. c.Assert(res.Rules[0].NonVersionExpiration.NoncurrentDays, Equals, 20)
  820. c.Assert(res.Rules[0].NonVersionTransition.NoncurrentDays, Equals, 10)
  821. c.Assert(res.Rules[0].NonVersionTransition.StorageClass, Equals, StorageClassType("IA"))
  822. err = client.DeleteBucket(bucketNameTest)
  823. c.Assert(err, IsNil)
  824. }
  825. // TestDeleteBucketLifecycle
  826. func (s *OssClientSuite) TestDeleteBucketLifecycle(c *C) {
  827. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  828. var rule1 = BuildLifecycleRuleByDate("rule1", "one", true, 2015, 11, 11)
  829. var rule2 = BuildLifecycleRuleByDays("rule2", "two", true, 3)
  830. var rules = []LifecycleRule{rule1, rule2}
  831. client, err := New(endpoint, accessID, accessKey)
  832. c.Assert(err, IsNil)
  833. err = client.CreateBucket(bucketNameTest)
  834. c.Assert(err, IsNil)
  835. //time.Sleep(timeoutInOperation)
  836. err = client.DeleteBucketLifecycle(bucketNameTest)
  837. c.Assert(err, IsNil)
  838. err = client.SetBucketLifecycle(bucketNameTest, rules)
  839. c.Assert(err, IsNil)
  840. //time.Sleep(timeoutInOperation)
  841. res, err := client.GetBucketLifecycle(bucketNameTest)
  842. c.Assert(err, IsNil)
  843. c.Assert(len(res.Rules), Equals, 2)
  844. // Delete
  845. err = client.DeleteBucketLifecycle(bucketNameTest)
  846. c.Assert(err, IsNil)
  847. //time.Sleep(timeoutInOperation)
  848. res, err = client.GetBucketLifecycle(bucketNameTest)
  849. c.Assert(err, NotNil)
  850. // Eliminate effect of cache
  851. //time.Sleep(timeoutInOperation)
  852. // Delete when not set
  853. err = client.DeleteBucketLifecycle(bucketNameTest)
  854. c.Assert(err, IsNil)
  855. err = client.DeleteBucket(bucketNameTest)
  856. c.Assert(err, IsNil)
  857. }
  858. // TestSetBucketLifecycleNegative
  859. func (s *OssClientSuite) TestBucketLifecycleNegative(c *C) {
  860. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  861. var rules = []LifecycleRule{}
  862. client, err := New(endpoint, accessID, accessKey)
  863. c.Assert(err, IsNil)
  864. err = client.CreateBucket(bucketNameTest)
  865. c.Assert(err, IsNil)
  866. // Set with no rule
  867. err = client.SetBucketLifecycle(bucketNameTest, rules)
  868. c.Assert(err, NotNil)
  869. err = client.DeleteBucket(bucketNameTest)
  870. c.Assert(err, IsNil)
  871. // Not exist
  872. err = client.SetBucketLifecycle(bucketNameTest, rules)
  873. c.Assert(err, NotNil)
  874. // Not exist
  875. _, err = client.GetBucketLifecycle(bucketNameTest)
  876. c.Assert(err, NotNil)
  877. // Not exist
  878. err = client.DeleteBucketLifecycle(bucketNameTest)
  879. c.Assert(err, NotNil)
  880. }
  881. // TestSetBucketReferer
  882. func (s *OssClientSuite) TestSetBucketReferer(c *C) {
  883. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  884. var referers = []string{"http://www.aliyun.com", "https://www.aliyun.com"}
  885. client, err := New(endpoint, accessID, accessKey)
  886. c.Assert(err, IsNil)
  887. err = client.CreateBucket(bucketNameTest)
  888. c.Assert(err, IsNil)
  889. time.Sleep(timeoutInOperation)
  890. res, err := client.GetBucketReferer(bucketNameTest)
  891. c.Assert(res.AllowEmptyReferer, Equals, true)
  892. c.Assert(len(res.RefererList), Equals, 0)
  893. // Set referers
  894. err = client.SetBucketReferer(bucketNameTest, referers, false)
  895. c.Assert(err, IsNil)
  896. time.Sleep(timeoutInOperation)
  897. res, err = client.GetBucketReferer(bucketNameTest)
  898. c.Assert(res.AllowEmptyReferer, Equals, false)
  899. c.Assert(len(res.RefererList), Equals, 2)
  900. c.Assert(res.RefererList[0], Equals, "http://www.aliyun.com")
  901. c.Assert(res.RefererList[1], Equals, "https://www.aliyun.com")
  902. // Reset referer, referers empty
  903. referers = []string{""}
  904. err = client.SetBucketReferer(bucketNameTest, referers, true)
  905. c.Assert(err, IsNil)
  906. referers = []string{}
  907. err = client.SetBucketReferer(bucketNameTest, referers, true)
  908. c.Assert(err, IsNil)
  909. res, err = client.GetBucketReferer(bucketNameTest)
  910. c.Assert(res.AllowEmptyReferer, Equals, true)
  911. c.Assert(len(res.RefererList), Equals, 0)
  912. err = client.DeleteBucket(bucketNameTest)
  913. c.Assert(err, IsNil)
  914. }
  915. // TestSetBucketRefererNegative
  916. func (s *OssClientSuite) TestBucketRefererNegative(c *C) {
  917. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  918. var referers = []string{""}
  919. client, err := New(endpoint, accessID, accessKey)
  920. c.Assert(err, IsNil)
  921. // Not exist
  922. _, err = client.GetBucketReferer(bucketNameTest)
  923. c.Assert(err, NotNil)
  924. testLogger.Println(err)
  925. // Not exist
  926. err = client.SetBucketReferer(bucketNameTest, referers, true)
  927. c.Assert(err, NotNil)
  928. testLogger.Println(err)
  929. }
  930. // TestSetBucketLogging
  931. func (s *OssClientSuite) TestSetBucketLogging(c *C) {
  932. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  933. var bucketNameTarget = bucketNameTest + "-target"
  934. client, err := New(endpoint, accessID, accessKey)
  935. c.Assert(err, IsNil)
  936. err = client.CreateBucket(bucketNameTest)
  937. c.Assert(err, IsNil)
  938. err = client.CreateBucket(bucketNameTarget)
  939. c.Assert(err, IsNil)
  940. time.Sleep(timeoutInOperation)
  941. // Set logging
  942. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
  943. c.Assert(err, IsNil)
  944. // Reset
  945. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", false)
  946. c.Assert(err, IsNil)
  947. time.Sleep(timeoutInOperation)
  948. res, err := client.GetBucketLogging(bucketNameTest)
  949. c.Assert(err, IsNil)
  950. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  951. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  952. err = client.DeleteBucketLogging(bucketNameTest)
  953. c.Assert(err, IsNil)
  954. // Set to self
  955. err = client.SetBucketLogging(bucketNameTest, bucketNameTest, "prefix", true)
  956. c.Assert(err, IsNil)
  957. err = client.DeleteBucket(bucketNameTest)
  958. c.Assert(err, IsNil)
  959. err = client.DeleteBucket(bucketNameTarget)
  960. c.Assert(err, IsNil)
  961. }
  962. // TestDeleteBucketLogging
  963. func (s *OssClientSuite) TestDeleteBucketLogging(c *C) {
  964. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  965. var bucketNameTarget = bucketNameTest + "-target"
  966. client, err := New(endpoint, accessID, accessKey)
  967. c.Assert(err, IsNil)
  968. err = client.CreateBucket(bucketNameTest)
  969. c.Assert(err, IsNil)
  970. err = client.CreateBucket(bucketNameTarget)
  971. c.Assert(err, IsNil)
  972. time.Sleep(timeoutInOperation)
  973. // Get when not set
  974. res, err := client.GetBucketLogging(bucketNameTest)
  975. c.Assert(err, IsNil)
  976. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  977. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  978. // Set
  979. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
  980. c.Assert(err, IsNil)
  981. // Get
  982. time.Sleep(timeoutInOperation)
  983. res, err = client.GetBucketLogging(bucketNameTest)
  984. c.Assert(err, IsNil)
  985. c.Assert(res.LoggingEnabled.TargetBucket, Equals, bucketNameTarget)
  986. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "prefix")
  987. // Set
  988. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", false)
  989. c.Assert(err, IsNil)
  990. // Get
  991. time.Sleep(timeoutInOperation)
  992. res, err = client.GetBucketLogging(bucketNameTest)
  993. c.Assert(err, IsNil)
  994. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  995. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  996. // Delete
  997. err = client.DeleteBucketLogging(bucketNameTest)
  998. c.Assert(err, IsNil)
  999. // Get after delete
  1000. time.Sleep(timeoutInOperation)
  1001. res, err = client.GetBucketLogging(bucketNameTest)
  1002. c.Assert(err, IsNil)
  1003. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  1004. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  1005. err = client.DeleteBucket(bucketNameTest)
  1006. c.Assert(err, IsNil)
  1007. err = client.DeleteBucket(bucketNameTarget)
  1008. c.Assert(err, IsNil)
  1009. }
  1010. // TestSetBucketLoggingNegative
  1011. func (s *OssClientSuite) TestSetBucketLoggingNegative(c *C) {
  1012. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1013. var bucketNameTarget = bucketNameTest + "-target"
  1014. client, err := New(endpoint, accessID, accessKey)
  1015. c.Assert(err, IsNil)
  1016. // Not exist
  1017. _, err = client.GetBucketLogging(bucketNameTest)
  1018. c.Assert(err, NotNil)
  1019. // Not exist
  1020. err = client.SetBucketLogging(bucketNameTest, "targetbucket", "prefix", true)
  1021. c.Assert(err, NotNil)
  1022. // Not exist
  1023. err = client.DeleteBucketLogging(bucketNameTest)
  1024. c.Assert(err, NotNil)
  1025. err = client.CreateBucket(bucketNameTest)
  1026. c.Assert(err, IsNil)
  1027. time.Sleep(timeoutInOperation)
  1028. // Target bucket not exist
  1029. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
  1030. c.Assert(err, NotNil)
  1031. // Parameter invalid
  1032. err = client.SetBucketLogging(bucketNameTest, "XXXX", "prefix", true)
  1033. c.Assert(err, NotNil)
  1034. err = client.SetBucketLogging(bucketNameTest, "xx", "prefix", true)
  1035. c.Assert(err, NotNil)
  1036. err = client.DeleteBucket(bucketNameTest)
  1037. c.Assert(err, IsNil)
  1038. }
  1039. // TestSetBucketWebsite
  1040. func (s *OssClientSuite) TestSetBucketWebsite(c *C) {
  1041. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1042. var indexWebsite = "myindex.html"
  1043. var errorWebsite = "myerror.html"
  1044. client, err := New(endpoint, accessID, accessKey)
  1045. c.Assert(err, IsNil)
  1046. time.Sleep(timeoutInOperation)
  1047. err = client.CreateBucket(bucketNameTest)
  1048. c.Assert(err, IsNil)
  1049. // Set
  1050. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  1051. c.Assert(err, IsNil)
  1052. // Double set
  1053. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  1054. c.Assert(err, IsNil)
  1055. res, err := client.GetBucketWebsite(bucketNameTest)
  1056. c.Assert(err, IsNil)
  1057. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  1058. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  1059. // Reset
  1060. err = client.SetBucketWebsite(bucketNameTest, "your"+indexWebsite, "your"+errorWebsite)
  1061. c.Assert(err, IsNil)
  1062. time.Sleep(timeoutInOperation)
  1063. res, err = client.GetBucketWebsite(bucketNameTest)
  1064. c.Assert(err, IsNil)
  1065. c.Assert(res.IndexDocument.Suffix, Equals, "your"+indexWebsite)
  1066. c.Assert(res.ErrorDocument.Key, Equals, "your"+errorWebsite)
  1067. err = client.DeleteBucketWebsite(bucketNameTest)
  1068. c.Assert(err, IsNil)
  1069. // Set after delete
  1070. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  1071. c.Assert(err, IsNil)
  1072. // Eliminate effect of cache
  1073. time.Sleep(timeoutInOperation)
  1074. res, err = client.GetBucketWebsite(bucketNameTest)
  1075. c.Assert(err, IsNil)
  1076. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  1077. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  1078. err = client.DeleteBucket(bucketNameTest)
  1079. c.Assert(err, IsNil)
  1080. }
  1081. // TestDeleteBucketWebsite
  1082. func (s *OssClientSuite) TestDeleteBucketWebsite(c *C) {
  1083. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1084. var indexWebsite = "myindex.html"
  1085. var errorWebsite = "myerror.html"
  1086. client, err := New(endpoint, accessID, accessKey)
  1087. c.Assert(err, IsNil)
  1088. err = client.CreateBucket(bucketNameTest)
  1089. c.Assert(err, IsNil)
  1090. time.Sleep(timeoutInOperation)
  1091. // Get
  1092. res, err := client.GetBucketWebsite(bucketNameTest)
  1093. c.Assert(err, NotNil)
  1094. // Detele without set
  1095. err = client.DeleteBucketWebsite(bucketNameTest)
  1096. c.Assert(err, IsNil)
  1097. // Set
  1098. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  1099. c.Assert(err, IsNil)
  1100. time.Sleep(timeoutInOperation)
  1101. res, err = client.GetBucketWebsite(bucketNameTest)
  1102. c.Assert(err, IsNil)
  1103. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  1104. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  1105. // Detele
  1106. time.Sleep(timeoutInOperation)
  1107. err = client.DeleteBucketWebsite(bucketNameTest)
  1108. c.Assert(err, IsNil)
  1109. time.Sleep(timeoutInOperation)
  1110. res, err = client.GetBucketWebsite(bucketNameTest)
  1111. c.Assert(err, NotNil)
  1112. // Detele after delete
  1113. err = client.DeleteBucketWebsite(bucketNameTest)
  1114. c.Assert(err, IsNil)
  1115. err = client.DeleteBucket(bucketNameTest)
  1116. c.Assert(err, IsNil)
  1117. }
  1118. // TestSetBucketWebsiteNegative
  1119. func (s *OssClientSuite) TestSetBucketWebsiteNegative(c *C) {
  1120. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1121. var indexWebsite = "myindex.html"
  1122. var errorWebsite = "myerror.html"
  1123. client, err := New(endpoint, accessID, accessKey)
  1124. c.Assert(err, IsNil)
  1125. err = client.DeleteBucket(bucketNameTest)
  1126. // Not exist
  1127. _, err = client.GetBucketWebsite(bucketNameTest)
  1128. c.Assert(err, NotNil)
  1129. err = client.DeleteBucketWebsite(bucketNameTest)
  1130. c.Assert(err, NotNil)
  1131. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  1132. c.Assert(err, NotNil)
  1133. err = client.CreateBucket(bucketNameTest)
  1134. c.Assert(err, IsNil)
  1135. // Set
  1136. time.Sleep(timeoutInOperation)
  1137. err = client.SetBucketWebsite(bucketNameTest, "myindex", "myerror")
  1138. c.Assert(err, IsNil)
  1139. res, err := client.GetBucketWebsite(bucketNameTest)
  1140. c.Assert(err, IsNil)
  1141. c.Assert(res.IndexDocument.Suffix, Equals, "myindex")
  1142. c.Assert(res.ErrorDocument.Key, Equals, "myerror")
  1143. // Detele
  1144. err = client.DeleteBucketWebsite(bucketNameTest)
  1145. c.Assert(err, IsNil)
  1146. time.Sleep(timeoutInOperation)
  1147. _, err = client.GetBucketWebsite(bucketNameTest)
  1148. c.Assert(err, NotNil)
  1149. // Detele after delete
  1150. err = client.DeleteBucketWebsite(bucketNameTest)
  1151. c.Assert(err, IsNil)
  1152. err = client.DeleteBucket(bucketNameTest)
  1153. c.Assert(err, IsNil)
  1154. }
  1155. // TestSetBucketWebsiteDetail
  1156. func (s *OssClientSuite) TestSetBucketWebsiteDetail(c *C) {
  1157. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1158. var indexWebsite = "myindex.html"
  1159. var errorWebsite = "myerror.html"
  1160. client, err := New(endpoint, accessID, accessKey)
  1161. c.Assert(err, IsNil)
  1162. err = client.CreateBucket(bucketNameTest)
  1163. c.Assert(err, IsNil)
  1164. time.Sleep(timeoutInOperation)
  1165. btrue := true
  1166. bfalse := false
  1167. // Define one routing rule
  1168. ruleOk := RoutingRule{
  1169. RuleNumber: 1,
  1170. Condition: Condition{
  1171. KeyPrefixEquals: "",
  1172. HTTPErrorCodeReturnedEquals: 404,
  1173. },
  1174. Redirect: Redirect{
  1175. RedirectType: "Mirror",
  1176. // PassQueryString: &btrue, // set default value
  1177. MirrorURL: "http://www.test.com/",
  1178. // MirrorPassQueryString:&btrue, // set default value
  1179. // MirrorFollowRedirect:&bfalse, // set default value
  1180. // MirrorCheckMd5:&bfalse, // set default value
  1181. MirrorHeaders: MirrorHeaders{
  1182. // PassAll:&bfalse, // set default value
  1183. Pass: []string{"myheader-key1", "myheader-key2"},
  1184. Remove: []string{"myheader-key3", "myheader-key4"},
  1185. Set: []MirrorHeaderSet{
  1186. MirrorHeaderSet{
  1187. Key: "myheader-key5",
  1188. Value: "myheader-value5",
  1189. },
  1190. },
  1191. },
  1192. },
  1193. }
  1194. // Define array routing rule
  1195. ruleArrOk := []RoutingRule{
  1196. RoutingRule{
  1197. RuleNumber: 2,
  1198. Condition: Condition{
  1199. KeyPrefixEquals: "abc/",
  1200. HTTPErrorCodeReturnedEquals: 404,
  1201. IncludeHeader: []IncludeHeader{
  1202. IncludeHeader{
  1203. Key: "host",
  1204. Equals: "test.oss-cn-beijing-internal.aliyuncs.com",
  1205. },
  1206. },
  1207. },
  1208. Redirect: Redirect{
  1209. RedirectType: "AliCDN",
  1210. Protocol: "http",
  1211. HostName: "www.test.com",
  1212. PassQueryString: &bfalse,
  1213. ReplaceKeyWith: "prefix/${key}.suffix",
  1214. HttpRedirectCode: 301,
  1215. },
  1216. },
  1217. RoutingRule{
  1218. RuleNumber: 3,
  1219. Condition: Condition{
  1220. KeyPrefixEquals: "",
  1221. HTTPErrorCodeReturnedEquals: 404,
  1222. },
  1223. Redirect: Redirect{
  1224. RedirectType: "Mirror",
  1225. PassQueryString: &btrue,
  1226. MirrorURL: "http://www.test.com/",
  1227. MirrorPassQueryString: &btrue,
  1228. MirrorFollowRedirect: &bfalse,
  1229. MirrorCheckMd5: &bfalse,
  1230. MirrorHeaders: MirrorHeaders{
  1231. PassAll: &btrue,
  1232. Pass: []string{"myheader-key1", "myheader-key2"},
  1233. Remove: []string{"myheader-key3", "myheader-key4"},
  1234. Set: []MirrorHeaderSet{
  1235. MirrorHeaderSet{
  1236. Key: "myheader-key5",
  1237. Value: "myheader-value5",
  1238. },
  1239. },
  1240. },
  1241. },
  1242. },
  1243. }
  1244. // Set one routing rule
  1245. wxmlOne := WebsiteXML{}
  1246. wxmlOne.RoutingRules = append(wxmlOne.RoutingRules, ruleOk)
  1247. var responseHeader http.Header
  1248. err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlOne, GetResponseHeader(&responseHeader))
  1249. c.Assert(err, IsNil)
  1250. requestId := GetRequestId(responseHeader)
  1251. c.Assert(len(requestId) > 0, Equals, true)
  1252. res, err := client.GetBucketWebsite(bucketNameTest)
  1253. c.Assert(err, IsNil)
  1254. c.Assert(res.RoutingRules[0].Redirect.RedirectType, Equals, "Mirror")
  1255. c.Assert(*res.RoutingRules[0].Redirect.PassQueryString, Equals, false)
  1256. c.Assert(*res.RoutingRules[0].Redirect.MirrorPassQueryString, Equals, false)
  1257. c.Assert(*res.RoutingRules[0].Redirect.MirrorFollowRedirect, Equals, true)
  1258. c.Assert(*res.RoutingRules[0].Redirect.MirrorCheckMd5, Equals, false)
  1259. c.Assert(*res.RoutingRules[0].Redirect.MirrorHeaders.PassAll, Equals, false)
  1260. // Set one routing rule and IndexDocument, IndexDocument
  1261. wxml := WebsiteXML{
  1262. IndexDocument: IndexDocument{Suffix: indexWebsite},
  1263. ErrorDocument: ErrorDocument{Key: errorWebsite},
  1264. }
  1265. wxml.RoutingRules = append(wxml.RoutingRules, ruleOk)
  1266. err = client.SetBucketWebsiteDetail(bucketNameTest, wxml, GetResponseHeader(&responseHeader))
  1267. c.Assert(err, IsNil)
  1268. requestId = GetRequestId(responseHeader)
  1269. c.Assert(len(requestId) > 0, Equals, true)
  1270. res, err = client.GetBucketWebsite(bucketNameTest)
  1271. c.Assert(err, IsNil)
  1272. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  1273. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  1274. c.Assert(res.RoutingRules[0].Redirect.RedirectType, Equals, "Mirror")
  1275. // Set array routing rule
  1276. wxml.RoutingRules = append(wxml.RoutingRules, ruleArrOk...)
  1277. err = client.SetBucketWebsiteDetail(bucketNameTest, wxml, GetResponseHeader(&responseHeader))
  1278. c.Assert(err, IsNil)
  1279. requestId = GetRequestId(responseHeader)
  1280. c.Assert(len(requestId) > 0, Equals, true)
  1281. res, err = client.GetBucketWebsite(bucketNameTest)
  1282. c.Assert(err, IsNil)
  1283. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  1284. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  1285. c.Assert(len(res.RoutingRules), Equals, 3)
  1286. c.Assert(res.RoutingRules[1].Redirect.RedirectType, Equals, "AliCDN")
  1287. c.Assert(*res.RoutingRules[2].Redirect.MirrorPassQueryString, Equals, true)
  1288. c.Assert(*res.RoutingRules[2].Redirect.MirrorFollowRedirect, Equals, false)
  1289. // Define one error routing rule
  1290. ruleErr := RoutingRule{
  1291. RuleNumber: 1,
  1292. Redirect: Redirect{
  1293. RedirectType: "Mirror",
  1294. PassQueryString: &btrue,
  1295. },
  1296. }
  1297. // Define array error routing rule
  1298. rulesErrArr := []RoutingRule{
  1299. RoutingRule{
  1300. RuleNumber: 1,
  1301. Redirect: Redirect{
  1302. RedirectType: "Mirror",
  1303. PassQueryString: &btrue,
  1304. },
  1305. },
  1306. RoutingRule{
  1307. RuleNumber: 2,
  1308. Redirect: Redirect{
  1309. RedirectType: "Mirror",
  1310. PassQueryString: &btrue,
  1311. },
  1312. },
  1313. }
  1314. ruleIntErr := RoutingRule{
  1315. // RuleNumber:0, // set NULL value
  1316. Condition: Condition{
  1317. KeyPrefixEquals: "",
  1318. HTTPErrorCodeReturnedEquals: 404,
  1319. },
  1320. Redirect: Redirect{
  1321. RedirectType: "Mirror",
  1322. // PassQueryString: &btrue, // set default value
  1323. MirrorURL: "http://www.test.com/",
  1324. // MirrorPassQueryString:&btrue, // set default value
  1325. // MirrorFollowRedirect:&bfalse, // set default value
  1326. // MirrorCheckMd5:&bfalse, // set default value
  1327. MirrorHeaders: MirrorHeaders{
  1328. // PassAll:&bfalse, // set default value
  1329. Pass: []string{"myheader-key1", "myheader-key2"},
  1330. Remove: []string{"myheader-key3", "myheader-key4"},
  1331. Set: []MirrorHeaderSet{
  1332. MirrorHeaderSet{
  1333. Key: "myheader-key5",
  1334. Value: "myheader-value5",
  1335. },
  1336. },
  1337. },
  1338. },
  1339. }
  1340. // Set one int type error rule
  1341. wxmlIntErr := WebsiteXML{}
  1342. wxmlIntErr.RoutingRules = append(wxmlIntErr.RoutingRules, ruleIntErr)
  1343. err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlIntErr)
  1344. c.Assert(err, NotNil)
  1345. // Set one error rule
  1346. wxmlErr := WebsiteXML{}
  1347. wxmlErr.RoutingRules = append(wxmlErr.RoutingRules, ruleErr)
  1348. err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlErr)
  1349. c.Assert(err, NotNil)
  1350. // Set one error rule and one correct rule
  1351. wxmlErr.RoutingRules = append(wxmlErr.RoutingRules, ruleOk)
  1352. err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlErr)
  1353. c.Assert(err, NotNil)
  1354. wxmlErrRuleArr := WebsiteXML{}
  1355. wxmlErrRuleArr.RoutingRules = append(wxmlErrRuleArr.RoutingRules, rulesErrArr...)
  1356. // Set array error routing rule
  1357. err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlErrRuleArr)
  1358. c.Assert(err, NotNil)
  1359. err = client.DeleteBucketWebsite(bucketNameTest)
  1360. c.Assert(err, IsNil)
  1361. err = client.DeleteBucket(bucketNameTest)
  1362. c.Assert(err, IsNil)
  1363. }
  1364. // TestSetBucketCORS
  1365. func (s *OssClientSuite) TestSetBucketCORS(c *C) {
  1366. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1367. var rule1 = CORSRule{
  1368. AllowedOrigin: []string{"*"},
  1369. AllowedMethod: []string{"PUT", "GET", "POST"},
  1370. AllowedHeader: []string{},
  1371. ExposeHeader: []string{},
  1372. MaxAgeSeconds: 100,
  1373. }
  1374. var rule2 = CORSRule{
  1375. AllowedOrigin: []string{"http://www.a.com", "http://www.b.com"},
  1376. AllowedMethod: []string{"GET"},
  1377. AllowedHeader: []string{"Authorization"},
  1378. ExposeHeader: []string{"x-oss-test", "x-oss-test1"},
  1379. MaxAgeSeconds: 200,
  1380. }
  1381. client, err := New(endpoint, accessID, accessKey)
  1382. c.Assert(err, IsNil)
  1383. err = client.CreateBucket(bucketNameTest)
  1384. c.Assert(err, IsNil)
  1385. time.Sleep(timeoutInOperation)
  1386. // Set
  1387. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1})
  1388. c.Assert(err, IsNil)
  1389. gbcr, err := client.GetBucketCORS(bucketNameTest)
  1390. c.Assert(err, IsNil)
  1391. c.Assert(len(gbcr.CORSRules), Equals, 1)
  1392. c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 1)
  1393. c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 3)
  1394. c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 0)
  1395. c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 0)
  1396. c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 100)
  1397. // Double set
  1398. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1})
  1399. c.Assert(err, IsNil)
  1400. gbcr, err = client.GetBucketCORS(bucketNameTest)
  1401. c.Assert(err, IsNil)
  1402. c.Assert(len(gbcr.CORSRules), Equals, 1)
  1403. c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 1)
  1404. c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 3)
  1405. c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 0)
  1406. c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 0)
  1407. c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 100)
  1408. // Set rule2
  1409. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule2})
  1410. c.Assert(err, IsNil)
  1411. time.Sleep(timeoutInOperation)
  1412. gbcr, err = client.GetBucketCORS(bucketNameTest)
  1413. c.Assert(err, IsNil)
  1414. c.Assert(len(gbcr.CORSRules), Equals, 1)
  1415. c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 2)
  1416. c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 1)
  1417. c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 1)
  1418. c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 2)
  1419. c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 200)
  1420. // Reset
  1421. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1, rule2})
  1422. c.Assert(err, IsNil)
  1423. time.Sleep(timeoutInOperation)
  1424. gbcr, err = client.GetBucketCORS(bucketNameTest)
  1425. c.Assert(err, IsNil)
  1426. c.Assert(len(gbcr.CORSRules), Equals, 2)
  1427. // Set after delete
  1428. err = client.DeleteBucketCORS(bucketNameTest)
  1429. c.Assert(err, IsNil)
  1430. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1, rule2})
  1431. c.Assert(err, IsNil)
  1432. time.Sleep(timeoutInOperation)
  1433. gbcr, err = client.GetBucketCORS(bucketNameTest)
  1434. c.Assert(err, IsNil)
  1435. c.Assert(len(gbcr.CORSRules), Equals, 2)
  1436. err = client.DeleteBucketCORS(bucketNameTest)
  1437. c.Assert(err, IsNil)
  1438. err = client.DeleteBucket(bucketNameTest)
  1439. c.Assert(err, IsNil)
  1440. }
  1441. // TestDeleteBucketCORS
  1442. func (s *OssClientSuite) TestDeleteBucketCORS(c *C) {
  1443. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1444. var rule = CORSRule{
  1445. AllowedOrigin: []string{"*"},
  1446. AllowedMethod: []string{"PUT", "GET", "POST"},
  1447. AllowedHeader: []string{},
  1448. ExposeHeader: []string{},
  1449. MaxAgeSeconds: 100,
  1450. }
  1451. client, err := New(endpoint, accessID, accessKey)
  1452. c.Assert(err, IsNil)
  1453. err = client.CreateBucket(bucketNameTest)
  1454. c.Assert(err, IsNil)
  1455. time.Sleep(timeoutInOperation)
  1456. // Delete not set
  1457. err = client.DeleteBucketCORS(bucketNameTest)
  1458. c.Assert(err, IsNil)
  1459. // Set
  1460. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
  1461. c.Assert(err, IsNil)
  1462. time.Sleep(timeoutInOperation)
  1463. _, err = client.GetBucketCORS(bucketNameTest)
  1464. c.Assert(err, IsNil)
  1465. // Detele
  1466. err = client.DeleteBucketCORS(bucketNameTest)
  1467. c.Assert(err, IsNil)
  1468. time.Sleep(timeoutInOperation)
  1469. _, err = client.GetBucketCORS(bucketNameTest)
  1470. c.Assert(err, NotNil)
  1471. // Detele after deleting
  1472. err = client.DeleteBucketCORS(bucketNameTest)
  1473. c.Assert(err, IsNil)
  1474. err = client.DeleteBucket(bucketNameTest)
  1475. c.Assert(err, IsNil)
  1476. }
  1477. // TestSetBucketCORSNegative
  1478. func (s *OssClientSuite) TestSetBucketCORSNegative(c *C) {
  1479. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1480. var rule = CORSRule{
  1481. AllowedOrigin: []string{"*"},
  1482. AllowedMethod: []string{"PUT", "GET", "POST"},
  1483. AllowedHeader: []string{},
  1484. ExposeHeader: []string{},
  1485. MaxAgeSeconds: 100,
  1486. }
  1487. client, err := New(endpoint, accessID, accessKey)
  1488. c.Assert(err, IsNil)
  1489. err = client.DeleteBucket(bucketNameTest)
  1490. // Not exist
  1491. _, err = client.GetBucketCORS(bucketNameTest)
  1492. c.Assert(err, NotNil)
  1493. err = client.DeleteBucketCORS(bucketNameTest)
  1494. c.Assert(err, NotNil)
  1495. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
  1496. c.Assert(err, NotNil)
  1497. err = client.CreateBucket(bucketNameTest)
  1498. c.Assert(err, IsNil)
  1499. time.Sleep(timeoutInOperation)
  1500. _, err = client.GetBucketCORS(bucketNameTest)
  1501. c.Assert(err, NotNil)
  1502. // Set
  1503. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
  1504. c.Assert(err, IsNil)
  1505. time.Sleep(timeoutInOperation)
  1506. _, err = client.GetBucketCORS(bucketNameTest)
  1507. c.Assert(err, IsNil)
  1508. // Delete
  1509. err = client.DeleteBucketCORS(bucketNameTest)
  1510. c.Assert(err, IsNil)
  1511. time.Sleep(timeoutInOperation)
  1512. _, err = client.GetBucketCORS(bucketNameTest)
  1513. c.Assert(err, NotNil)
  1514. // Delete after deleting
  1515. err = client.DeleteBucketCORS(bucketNameTest)
  1516. c.Assert(err, IsNil)
  1517. err = client.DeleteBucket(bucketNameTest)
  1518. c.Assert(err, IsNil)
  1519. }
  1520. // TestGetBucketInfo
  1521. func (s *OssClientSuite) TestGetBucketInfo(c *C) {
  1522. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1523. client, err := New(endpoint, accessID, accessKey)
  1524. c.Assert(err, IsNil)
  1525. err = client.CreateBucket(bucketNameTest)
  1526. c.Assert(err, IsNil)
  1527. res, err := client.GetBucketInfo(bucketNameTest)
  1528. c.Assert(err, IsNil)
  1529. c.Assert(res.BucketInfo.Name, Equals, bucketNameTest)
  1530. c.Assert(strings.HasPrefix(res.BucketInfo.Location, "oss-"), Equals, true)
  1531. c.Assert(res.BucketInfo.ACL, Equals, "private")
  1532. c.Assert(strings.HasSuffix(res.BucketInfo.ExtranetEndpoint, ".com"), Equals, true)
  1533. c.Assert(strings.HasSuffix(res.BucketInfo.IntranetEndpoint, ".com"), Equals, true)
  1534. c.Assert(res.BucketInfo.CreationDate, NotNil)
  1535. err = client.DeleteBucket(bucketNameTest)
  1536. c.Assert(err, IsNil)
  1537. }
  1538. // TestGetBucketInfoNegative
  1539. func (s *OssClientSuite) TestGetBucketInfoNegative(c *C) {
  1540. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1541. client, err := New(endpoint, accessID, accessKey)
  1542. c.Assert(err, IsNil)
  1543. // Not exist
  1544. _, err = client.GetBucketInfo(bucketNameTest)
  1545. c.Assert(err, NotNil)
  1546. // Bucket name invalid
  1547. _, err = client.GetBucketInfo("InvalidBucketName_")
  1548. c.Assert(err, NotNil)
  1549. }
  1550. // TestEndpointFormat
  1551. func (s *OssClientSuite) TestEndpointFormat(c *C) {
  1552. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1553. // http://host
  1554. client, err := New(endpoint, accessID, accessKey)
  1555. c.Assert(err, IsNil)
  1556. err = client.CreateBucket(bucketNameTest)
  1557. c.Assert(err, IsNil)
  1558. time.Sleep(timeoutInOperation)
  1559. res, err := client.GetBucketACL(bucketNameTest)
  1560. c.Assert(err, IsNil)
  1561. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1562. err = client.DeleteBucket(bucketNameTest)
  1563. c.Assert(err, IsNil)
  1564. time.Sleep(timeoutInOperation)
  1565. // http://host:port
  1566. client, err = New(endpoint+":80", accessID, accessKey)
  1567. c.Assert(err, IsNil)
  1568. err = client.CreateBucket(bucketNameTest)
  1569. c.Assert(err, IsNil)
  1570. time.Sleep(timeoutInOperation)
  1571. res, err = client.GetBucketACL(bucketNameTest)
  1572. c.Assert(err, IsNil)
  1573. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1574. err = client.DeleteBucket(bucketNameTest)
  1575. c.Assert(err, IsNil)
  1576. }
  1577. // TestCname
  1578. func (s *OssClientSuite) _TestCname(c *C) {
  1579. var bucketNameTest = "<my-bucket-cname>"
  1580. client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>", UseCname(true))
  1581. c.Assert(err, IsNil)
  1582. err = client.CreateBucket(bucketNameTest)
  1583. c.Assert(err, IsNil)
  1584. _, err = client.ListBuckets()
  1585. c.Assert(err, NotNil)
  1586. res, err := client.GetBucketACL(bucketNameTest)
  1587. c.Assert(err, IsNil)
  1588. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1589. }
  1590. // TestCnameNegative
  1591. func (s *OssClientSuite) _TestCnameNegative(c *C) {
  1592. var bucketNameTest = "<my-bucket-cname>"
  1593. client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>", UseCname(true))
  1594. c.Assert(err, IsNil)
  1595. err = client.CreateBucket(bucketNameTest)
  1596. c.Assert(err, NotNil)
  1597. _, err = client.ListBuckets()
  1598. c.Assert(err, NotNil)
  1599. _, err = client.GetBucketACL(bucketNameTest)
  1600. c.Assert(err, NotNil)
  1601. }
  1602. // _TestHTTPS
  1603. func (s *OssClientSuite) _TestHTTPS(c *C) {
  1604. var bucketNameTest = "<my-bucket-https>"
  1605. client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>")
  1606. c.Assert(err, IsNil)
  1607. err = client.CreateBucket(bucketNameTest)
  1608. c.Assert(err, IsNil)
  1609. res, err := client.GetBucketACL(bucketNameTest)
  1610. c.Assert(err, IsNil)
  1611. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1612. err = client.DeleteBucket(bucketNameTest)
  1613. c.Assert(err, IsNil)
  1614. }
  1615. // TestClientOption
  1616. func (s *OssClientSuite) TestClientOption(c *C) {
  1617. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  1618. client, err := New(endpoint, accessID, accessKey, UseCname(true),
  1619. Timeout(11, 12), SecurityToken("token"), Proxy(proxyHost))
  1620. c.Assert(err, IsNil)
  1621. // CreateBucket timeout
  1622. err = client.CreateBucket(bucketNameTest)
  1623. c.Assert(err, NotNil)
  1624. c.Assert(client.Conn.config.HTTPTimeout.ConnectTimeout, Equals, time.Second*11)
  1625. c.Assert(client.Conn.config.HTTPTimeout.ReadWriteTimeout, Equals, time.Second*12)
  1626. c.Assert(client.Conn.config.HTTPTimeout.HeaderTimeout, Equals, time.Second*12)
  1627. c.Assert(client.Conn.config.HTTPTimeout.IdleConnTimeout, Equals, time.Second*12)
  1628. c.Assert(client.Conn.config.HTTPTimeout.LongTimeout, Equals, time.Second*12*10)
  1629. c.Assert(client.Conn.config.SecurityToken, Equals, "token")
  1630. c.Assert(client.Conn.config.IsCname, Equals, true)
  1631. c.Assert(client.Conn.config.IsUseProxy, Equals, true)
  1632. c.Assert(client.Config.ProxyHost, Equals, proxyHost)
  1633. client, err = New(endpoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
  1634. c.Assert(client.Conn.config.IsUseProxy, Equals, true)
  1635. c.Assert(client.Config.ProxyHost, Equals, proxyHost)
  1636. c.Assert(client.Conn.config.IsAuthProxy, Equals, true)
  1637. c.Assert(client.Conn.config.ProxyUser, Equals, proxyUser)
  1638. c.Assert(client.Conn.config.ProxyPassword, Equals, proxyPasswd)
  1639. client, err = New(endpoint, accessID, accessKey, UserAgent("go sdk user agent"))
  1640. c.Assert(client.Conn.config.UserAgent, Equals, "go sdk user agent")
  1641. // Check we can overide the http.Client
  1642. httpClient := new(http.Client)
  1643. client, err = New(endpoint, accessID, accessKey, HTTPClient(httpClient))
  1644. c.Assert(client.HTTPClient, Equals, httpClient)
  1645. c.Assert(client.Conn.client, Equals, httpClient)
  1646. client, err = New(endpoint, accessID, accessKey)
  1647. c.Assert(client.HTTPClient, IsNil)
  1648. }
  1649. // TestProxy
  1650. func (s *OssClientSuite) TestProxy(c *C) {
  1651. bucketNameTest := bucketNamePrefix + randLowStr(6)
  1652. objectName := "体育/奥运/首金"
  1653. objectValue := "大江东去,浪淘尽,千古风流人物。 故垒西边,人道是、三国周郎赤壁。"
  1654. client, err := New(endpoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
  1655. // Create bucket
  1656. err = client.CreateBucket(bucketNameTest)
  1657. c.Assert(err, IsNil)
  1658. // Get bucket info
  1659. _, err = client.GetBucketInfo(bucketNameTest)
  1660. c.Assert(err, IsNil)
  1661. bucket, err := client.Bucket(bucketNameTest)
  1662. // Sign URL
  1663. str, err := bucket.SignURL(objectName, HTTPPut, 60)
  1664. c.Assert(err, IsNil)
  1665. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  1666. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  1667. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  1668. // Put object with URL
  1669. err = bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  1670. c.Assert(err, IsNil)
  1671. // Sign URL for get object
  1672. str, err = bucket.SignURL(objectName, HTTPGet, 60)
  1673. c.Assert(err, IsNil)
  1674. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  1675. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  1676. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  1677. // Get object with URL
  1678. body, err := bucket.GetObjectWithURL(str)
  1679. c.Assert(err, IsNil)
  1680. str, err = readBody(body)
  1681. c.Assert(err, IsNil)
  1682. c.Assert(str, Equals, objectValue)
  1683. // Put object
  1684. err = bucket.PutObject(objectName, strings.NewReader(objectValue))
  1685. c.Assert(err, IsNil)
  1686. // Get object
  1687. _, err = bucket.GetObject(objectName)
  1688. c.Assert(err, IsNil)
  1689. // List objects
  1690. _, err = bucket.ListObjects()
  1691. c.Assert(err, IsNil)
  1692. // Delete object
  1693. err = bucket.DeleteObject(objectName)
  1694. c.Assert(err, IsNil)
  1695. // Delete bucket
  1696. err = client.DeleteBucket(bucketNameTest)
  1697. c.Assert(err, IsNil)
  1698. }
  1699. // TestProxy for https endpoint
  1700. func (s *OssClientSuite) TestHttpsEndpointProxy(c *C) {
  1701. bucketNameTest := bucketNamePrefix + randLowStr(6)
  1702. objectName := objectNamePrefix + randLowStr(6)
  1703. objectValue := randLowStr(100)
  1704. httpsEndPoint := ""
  1705. if strings.HasPrefix(endpoint, "http://") {
  1706. httpsEndPoint = strings.Replace(endpoint, "http://", "https://", 1)
  1707. } else if !strings.HasPrefix(endpoint, "https://") {
  1708. httpsEndPoint = "https://" + endpoint
  1709. } else {
  1710. httpsEndPoint = endpoint
  1711. }
  1712. client, err := New(httpsEndPoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
  1713. // Create bucket
  1714. err = client.CreateBucket(bucketNameTest)
  1715. c.Assert(err, IsNil)
  1716. bucket, err := client.Bucket(bucketNameTest)
  1717. // Put object
  1718. err = bucket.PutObject(objectName, strings.NewReader(objectValue))
  1719. c.Assert(err, IsNil)
  1720. // Get object
  1721. _, err = bucket.GetObject(objectName)
  1722. c.Assert(err, IsNil)
  1723. // List objects
  1724. _, err = bucket.ListObjects()
  1725. c.Assert(err, IsNil)
  1726. // Delete object
  1727. err = bucket.DeleteObject(objectName)
  1728. c.Assert(err, IsNil)
  1729. // Delete bucket
  1730. err = client.DeleteBucket(bucketNameTest)
  1731. c.Assert(err, IsNil)
  1732. }
  1733. // Private
  1734. func (s *OssClientSuite) checkBucket(buckets []BucketProperties, bucket string) bool {
  1735. for _, v := range buckets {
  1736. if v.Name == bucket {
  1737. return true
  1738. }
  1739. }
  1740. return false
  1741. }
  1742. func (s *OssClientSuite) getBucket(buckets []BucketProperties, bucket string) (bool, BucketProperties) {
  1743. for _, v := range buckets {
  1744. if v.Name == bucket {
  1745. return true, v
  1746. }
  1747. }
  1748. return false, BucketProperties{}
  1749. }
  1750. func (s *OssClientSuite) TestHttpLogNotSignUrl(c *C) {
  1751. logName := "." + string(os.PathSeparator) + "test-go-sdk-httpdebug.log" + randStr(5)
  1752. f, err := os.OpenFile(logName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660)
  1753. c.Assert(err, IsNil)
  1754. client, err := New(endpoint, accessID, accessKey)
  1755. client.Config.LogLevel = Debug
  1756. client.Config.Logger = log.New(f, "", log.LstdFlags)
  1757. var testBucketName = bucketNamePrefix + randLowStr(6)
  1758. // CreateBucket
  1759. err = client.CreateBucket(testBucketName)
  1760. f.Close()
  1761. // read log file,get http info
  1762. contents, err := ioutil.ReadFile(logName)
  1763. c.Assert(err, IsNil)
  1764. httpContent := string(contents)
  1765. //fmt.Println(httpContent)
  1766. c.Assert(strings.Contains(httpContent, "signStr"), Equals, true)
  1767. c.Assert(strings.Contains(httpContent, "Method:"), Equals, true)
  1768. // delete test bucket and log
  1769. os.Remove(logName)
  1770. client.DeleteBucket(testBucketName)
  1771. }
  1772. func (s *OssClientSuite) TestHttpLogSignUrl(c *C) {
  1773. logName := "." + string(os.PathSeparator) + "test-go-sdk-httpdebug-signurl.log" + randStr(5)
  1774. f, err := os.OpenFile(logName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660)
  1775. c.Assert(err, IsNil)
  1776. client, err := New(endpoint, accessID, accessKey)
  1777. client.Config.LogLevel = Debug
  1778. client.Config.Logger = log.New(f, "", log.LstdFlags)
  1779. var testBucketName = bucketNamePrefix + randLowStr(6)
  1780. // CreateBucket
  1781. err = client.CreateBucket(testBucketName)
  1782. f.Close()
  1783. // clear log
  1784. f, err = os.OpenFile(logName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660)
  1785. client.Config.Logger = log.New(f, "", log.LstdFlags)
  1786. bucket, _ := client.Bucket(testBucketName)
  1787. objectName := objectNamePrefix + randStr(8)
  1788. objectValue := randStr(20)
  1789. // Sign URL for put
  1790. str, err := bucket.SignURL(objectName, HTTPPut, 60)
  1791. c.Assert(err, IsNil)
  1792. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  1793. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  1794. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  1795. // Error put object with URL
  1796. err = bucket.PutObjectWithURL(str, strings.NewReader(objectValue), ContentType("image/tiff"))
  1797. f.Close()
  1798. // read log file,get http info
  1799. contents, err := ioutil.ReadFile(logName)
  1800. c.Assert(err, IsNil)
  1801. httpContent := string(contents)
  1802. //fmt.Println(httpContent)
  1803. c.Assert(strings.Contains(httpContent, "signStr"), Equals, true)
  1804. c.Assert(strings.Contains(httpContent, "Method:"), Equals, true)
  1805. // delete test bucket and log
  1806. os.Remove(logName)
  1807. client.DeleteBucket(testBucketName)
  1808. }
  1809. func (s *OssClientSuite) TestSetLimitUploadSpeed(c *C) {
  1810. client, err := New(endpoint, accessID, accessKey)
  1811. c.Assert(err, IsNil)
  1812. err = client.LimitUploadSpeed(100)
  1813. goVersion := runtime.Version()
  1814. pSlice := strings.Split(strings.ToLower(goVersion), ".")
  1815. // compare with go1.7
  1816. if len(pSlice) >= 2 {
  1817. if pSlice[0] > "go1" {
  1818. c.Assert(err, IsNil)
  1819. } else if pSlice[0] == "go1" {
  1820. subVersion, _ := strconv.Atoi(pSlice[1])
  1821. if subVersion >= 7 {
  1822. c.Assert(err, IsNil)
  1823. } else {
  1824. c.Assert(err, NotNil)
  1825. }
  1826. } else {
  1827. c.Assert(err, NotNil)
  1828. }
  1829. }
  1830. }
  1831. func (s *OssClientSuite) TestBucketEncyptionError(c *C) {
  1832. client, err := New(endpoint, accessID, accessKey)
  1833. c.Assert(err, IsNil)
  1834. bucketName := bucketNamePrefix + randLowStr(5)
  1835. err = client.CreateBucket(bucketName)
  1836. c.Assert(err, IsNil)
  1837. // SetBucketEncryption:AES256 ,"123"
  1838. encryptionRule := ServerEncryptionRule{}
  1839. encryptionRule.SSEDefault.SSEAlgorithm = string(AESAlgorithm)
  1840. encryptionRule.SSEDefault.KMSMasterKeyID = "123"
  1841. var responseHeader http.Header
  1842. err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
  1843. c.Assert(err, NotNil)
  1844. requestId := GetRequestId(responseHeader)
  1845. c.Assert(len(requestId) > 0, Equals, true)
  1846. // GetBucketEncryption
  1847. _, err = client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1848. c.Assert(err, NotNil)
  1849. requestId = GetRequestId(responseHeader)
  1850. c.Assert(len(requestId) > 0, Equals, true)
  1851. // Get default bucket info
  1852. bucketResult, err := client.GetBucketInfo(bucketName)
  1853. c.Assert(err, IsNil)
  1854. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "")
  1855. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
  1856. c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
  1857. err = client.DeleteBucket(bucketName)
  1858. c.Assert(err, IsNil)
  1859. }
  1860. func (s *OssClientSuite) TestBucketEncyptionPutAndGetAndDelete(c *C) {
  1861. client, err := New(endpoint, accessID, accessKey)
  1862. c.Assert(err, IsNil)
  1863. bucketName := bucketNamePrefix + randLowStr(5)
  1864. err = client.CreateBucket(bucketName)
  1865. c.Assert(err, IsNil)
  1866. // SetBucketEncryption:KMS ,""
  1867. encryptionRule := ServerEncryptionRule{}
  1868. encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
  1869. var responseHeader http.Header
  1870. err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
  1871. c.Assert(err, IsNil)
  1872. requestId := GetRequestId(responseHeader)
  1873. c.Assert(len(requestId) > 0, Equals, true)
  1874. // GetBucketEncryption
  1875. getResult, err := client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1876. c.Assert(err, IsNil)
  1877. requestId = GetRequestId(responseHeader)
  1878. c.Assert(len(requestId) > 0, Equals, true)
  1879. // check encryption value
  1880. c.Assert(encryptionRule.SSEDefault.SSEAlgorithm, Equals, getResult.SSEDefault.SSEAlgorithm)
  1881. c.Assert(encryptionRule.SSEDefault.KMSMasterKeyID, Equals, getResult.SSEDefault.KMSMasterKeyID)
  1882. // delete bucket encyption
  1883. err = client.DeleteBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1884. c.Assert(err, IsNil)
  1885. requestId = GetRequestId(responseHeader)
  1886. c.Assert(len(requestId) > 0, Equals, true)
  1887. // GetBucketEncryption failure
  1888. _, err = client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1889. c.Assert(err, NotNil)
  1890. requestId = GetRequestId(responseHeader)
  1891. c.Assert(len(requestId) > 0, Equals, true)
  1892. // Get default bucket info
  1893. bucketResult, err := client.GetBucketInfo(bucketName)
  1894. c.Assert(err, IsNil)
  1895. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "")
  1896. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
  1897. c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
  1898. err = client.DeleteBucket(bucketName)
  1899. c.Assert(err, IsNil)
  1900. }
  1901. func (s *OssClientSuite) TestBucketEncyptionPutObjectSuccess(c *C) {
  1902. client, err := New(endpoint, accessID, accessKey)
  1903. c.Assert(err, IsNil)
  1904. bucketName := bucketNamePrefix + randLowStr(5)
  1905. err = client.CreateBucket(bucketName)
  1906. c.Assert(err, IsNil)
  1907. // SetBucketEncryption:KMS ,""
  1908. encryptionRule := ServerEncryptionRule{}
  1909. encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
  1910. var responseHeader http.Header
  1911. err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
  1912. c.Assert(err, IsNil)
  1913. requestId := GetRequestId(responseHeader)
  1914. c.Assert(len(requestId) > 0, Equals, true)
  1915. // GetBucketEncryption
  1916. getResult, err := client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1917. c.Assert(err, IsNil)
  1918. requestId = GetRequestId(responseHeader)
  1919. c.Assert(len(requestId) > 0, Equals, true)
  1920. // check encryption value
  1921. c.Assert(encryptionRule.SSEDefault.SSEAlgorithm, Equals, getResult.SSEDefault.SSEAlgorithm)
  1922. c.Assert(encryptionRule.SSEDefault.KMSMasterKeyID, Equals, getResult.SSEDefault.KMSMasterKeyID)
  1923. // Get default bucket info
  1924. bucketResult, err := client.GetBucketInfo(bucketName)
  1925. c.Assert(err, IsNil)
  1926. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "KMS")
  1927. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
  1928. c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
  1929. // put and get object success
  1930. //bucket, err := client.Bucket(bucketName)
  1931. //c.Assert(err, IsNil)
  1932. // put object success
  1933. //objectName := objectNamePrefix + randStr(8)
  1934. //context := randStr(100)
  1935. //err = bucket.PutObject(objectName, strings.NewReader(context))
  1936. //c.Assert(err, IsNil)
  1937. // get object success
  1938. //body, err := bucket.GetObject(objectName)
  1939. //c.Assert(err, IsNil)
  1940. //str, err := readBody(body)
  1941. //c.Assert(err, IsNil)
  1942. //body.Close()
  1943. //c.Assert(str, Equals, context)
  1944. //bucket.DeleteObject(objectName)
  1945. err = client.DeleteBucket(bucketName)
  1946. c.Assert(err, IsNil)
  1947. }
  1948. func (s *OssClientSuite) TestBucketEncyptionPutObjectError(c *C) {
  1949. client, err := New(endpoint, accessID, accessKey)
  1950. c.Assert(err, IsNil)
  1951. bucketName := bucketNamePrefix + randLowStr(5)
  1952. err = client.CreateBucket(bucketName)
  1953. c.Assert(err, IsNil)
  1954. // SetBucketEncryption:KMS ,""
  1955. encryptionRule := ServerEncryptionRule{}
  1956. encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
  1957. var responseHeader http.Header
  1958. err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
  1959. c.Assert(err, IsNil)
  1960. requestId := GetRequestId(responseHeader)
  1961. c.Assert(len(requestId) > 0, Equals, true)
  1962. // GetBucketEncryption
  1963. getResult, err := client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1964. c.Assert(err, IsNil)
  1965. requestId = GetRequestId(responseHeader)
  1966. c.Assert(len(requestId) > 0, Equals, true)
  1967. // check encryption value
  1968. c.Assert(encryptionRule.SSEDefault.SSEAlgorithm, Equals, getResult.SSEDefault.SSEAlgorithm)
  1969. c.Assert(encryptionRule.SSEDefault.KMSMasterKeyID, Equals, getResult.SSEDefault.KMSMasterKeyID)
  1970. // Get default bucket info
  1971. bucketResult, err := client.GetBucketInfo(bucketName)
  1972. c.Assert(err, IsNil)
  1973. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "KMS")
  1974. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "123")
  1975. c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
  1976. // put and get object failure
  1977. bucket, err := client.Bucket(bucketName)
  1978. c.Assert(err, IsNil)
  1979. // put object failure
  1980. objectName := objectNamePrefix + randStr(8)
  1981. context := randStr(100)
  1982. err = bucket.PutObject(objectName, strings.NewReader(context))
  1983. c.Assert(err, NotNil)
  1984. err = client.DeleteBucket(bucketName)
  1985. c.Assert(err, IsNil)
  1986. }
  1987. func (s *OssClientSuite) TestBucketTaggingOperation(c *C) {
  1988. client, err := New(endpoint, accessID, accessKey)
  1989. c.Assert(err, IsNil)
  1990. bucketName := bucketNamePrefix + randLowStr(5)
  1991. err = client.CreateBucket(bucketName)
  1992. c.Assert(err, IsNil)
  1993. var respHeader http.Header
  1994. // Bucket Tagging
  1995. var tagging Tagging
  1996. tagging.Tags = []Tag{Tag{Key: "testkey2", Value: "testvalue2"}}
  1997. err = client.SetBucketTagging(bucketName, tagging, GetResponseHeader(&respHeader))
  1998. c.Assert(err, IsNil)
  1999. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  2000. getResult, err := client.GetBucketTagging(bucketName)
  2001. c.Assert(err, IsNil)
  2002. c.Assert(getResult.Tags[0].Key, Equals, tagging.Tags[0].Key)
  2003. c.Assert(getResult.Tags[0].Value, Equals, tagging.Tags[0].Value)
  2004. // delete BucketTagging
  2005. err = client.DeleteBucketTagging(bucketName, GetResponseHeader(&respHeader))
  2006. c.Assert(err, IsNil)
  2007. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  2008. getResult, err = client.GetBucketTagging(bucketName, GetResponseHeader(&respHeader))
  2009. c.Assert(err, IsNil)
  2010. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  2011. c.Assert(len(getResult.Tags), Equals, 0)
  2012. err = client.DeleteBucket(bucketName)
  2013. c.Assert(err, IsNil)
  2014. }
  2015. func (s *OssClientSuite) TestListBucketsTagging(c *C) {
  2016. client, err := New(endpoint, accessID, accessKey)
  2017. c.Assert(err, IsNil)
  2018. bucketName1 := bucketNamePrefix + randLowStr(5)
  2019. err = client.CreateBucket(bucketName1)
  2020. c.Assert(err, IsNil)
  2021. bucketName2 := bucketNamePrefix + randLowStr(5)
  2022. err = client.CreateBucket(bucketName2)
  2023. c.Assert(err, IsNil)
  2024. // Bucket Tagging
  2025. var tagging Tagging
  2026. tagging.Tags = []Tag{Tag{Key: "testkey", Value: "testvalue"}}
  2027. err = client.SetBucketTagging(bucketName1, tagging)
  2028. c.Assert(err, IsNil)
  2029. // list bucket
  2030. listResult, err := client.ListBuckets(TagKey("testkey"))
  2031. c.Assert(err, IsNil)
  2032. c.Assert(len(listResult.Buckets), Equals, 1)
  2033. c.Assert(listResult.Buckets[0].Name, Equals, bucketName1)
  2034. client.DeleteBucket(bucketName1)
  2035. client.DeleteBucket(bucketName2)
  2036. }
  2037. func (s *OssClientSuite) TestGetBucketStat(c *C) {
  2038. client, err := New(endpoint, accessID, accessKey)
  2039. c.Assert(err, IsNil)
  2040. bucketName := bucketNamePrefix + randLowStr(5)
  2041. err = client.CreateBucket(bucketName)
  2042. c.Assert(err, IsNil)
  2043. bucket, err := client.Bucket(bucketName)
  2044. c.Assert(err, IsNil)
  2045. // put object
  2046. objectName := objectNamePrefix + randLowStr(5)
  2047. err = bucket.PutObject(objectName, strings.NewReader(randStr(10)))
  2048. c.Assert(err, IsNil)
  2049. bucket.DeleteObject(objectName)
  2050. err = bucket.PutObject(objectName, strings.NewReader(randStr(10)))
  2051. c.Assert(err, IsNil)
  2052. bucket.DeleteObject(objectName)
  2053. _, err = client.GetBucketStat(bucketName)
  2054. c.Assert(err, IsNil)
  2055. client.DeleteBucket(bucketName)
  2056. }
  2057. func (s *OssBucketSuite) TestGetBucketVersioning(c *C) {
  2058. // create a bucket with default proprety
  2059. client, err := New(endpoint, accessID, accessKey)
  2060. c.Assert(err, IsNil)
  2061. bucketName := bucketNamePrefix + randLowStr(6)
  2062. var respHeader http.Header
  2063. err = client.CreateBucket(bucketName, GetResponseHeader(&respHeader))
  2064. c.Assert(err, IsNil)
  2065. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  2066. // put bucket version:enabled
  2067. var versioningConfig VersioningConfig
  2068. versioningConfig.Status = string(VersionEnabled)
  2069. err = client.SetBucketVersioning(bucketName, versioningConfig)
  2070. c.Assert(err, IsNil)
  2071. // get bucket version success
  2072. versioningResult, err := client.GetBucketVersioning(bucketName, GetResponseHeader(&respHeader))
  2073. c.Assert(versioningResult.Status, Equals, "Enabled")
  2074. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  2075. forceDeleteBucket(client, bucketName, c)
  2076. }
  2077. func (s *OssClientSuite) TestBucketPolicy(c *C) {
  2078. client, err := New(endpoint, accessID, accessKey)
  2079. c.Assert(err, IsNil)
  2080. bucketName := bucketNamePrefix + randLowStr(5)
  2081. err = client.CreateBucket(bucketName)
  2082. c.Assert(err, IsNil)
  2083. var responseHeader http.Header
  2084. ret, err := client.GetBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2085. c.Assert(err, NotNil)
  2086. requestId := GetRequestId(responseHeader)
  2087. c.Assert(len(requestId) > 0, Equals, true)
  2088. policyInfo := `
  2089. {
  2090. "Version":"1",
  2091. "Statement":[
  2092. {
  2093. "Action":[
  2094. "oss:GetObject",
  2095. "oss:PutObject"
  2096. ],
  2097. "Effect":"Deny",
  2098. "Principal":"[123456790]",
  2099. "Resource":["acs:oss:*:1234567890:*/*"]
  2100. }
  2101. ]
  2102. }`
  2103. err = client.SetBucketPolicy(bucketName, policyInfo, GetResponseHeader(&responseHeader))
  2104. c.Assert(err, IsNil)
  2105. requestId = GetRequestId(responseHeader)
  2106. c.Assert(len(requestId) > 0, Equals, true)
  2107. ret, err = client.GetBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2108. c.Assert(err, IsNil)
  2109. testLogger.Println("policy:", ret)
  2110. c.Assert(ret, Equals, policyInfo)
  2111. requestId = GetRequestId(responseHeader)
  2112. c.Assert(len(requestId) > 0, Equals, true)
  2113. err = client.DeleteBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2114. c.Assert(err, IsNil)
  2115. requestId = GetRequestId(responseHeader)
  2116. c.Assert(len(requestId) > 0, Equals, true)
  2117. client.DeleteBucket(bucketName)
  2118. }
  2119. func (s *OssClientSuite) TestBucketPolicyNegative(c *C) {
  2120. client, err := New(endpoint, accessID, accessKey)
  2121. c.Assert(err, IsNil)
  2122. bucketName := bucketNamePrefix + randLowStr(5)
  2123. err = client.CreateBucket(bucketName)
  2124. c.Assert(err, IsNil)
  2125. var responseHeader http.Header
  2126. _, err = client.GetBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2127. c.Assert(err, NotNil)
  2128. requestId := GetRequestId(responseHeader)
  2129. c.Assert(len(requestId) > 0, Equals, true)
  2130. // Setting the Version is 2, this is error policy
  2131. errPolicy := `
  2132. {
  2133. "Version":"2",
  2134. "Statement":[
  2135. {
  2136. "Action":[
  2137. "oss:GetObject",
  2138. "oss:PutObject"
  2139. ],
  2140. "Effect":"Deny",
  2141. "Principal":"[123456790]",
  2142. "Resource":["acs:oss:*:1234567890:*/*"]
  2143. }
  2144. ]
  2145. }`
  2146. err = client.SetBucketPolicy(bucketName, errPolicy, GetResponseHeader(&responseHeader))
  2147. c.Assert(err, NotNil)
  2148. testLogger.Println("err:", err)
  2149. requestId = GetRequestId(responseHeader)
  2150. c.Assert(len(requestId) > 0, Equals, true)
  2151. err = client.DeleteBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2152. c.Assert(err, IsNil)
  2153. bucketNameEmpty := bucketNamePrefix + randLowStr(5)
  2154. client.DeleteBucket(bucketNameEmpty)
  2155. err = client.DeleteBucketPolicy(bucketNameEmpty, GetResponseHeader(&responseHeader))
  2156. c.Assert(err, NotNil)
  2157. requestId = GetRequestId(responseHeader)
  2158. c.Assert(len(requestId) > 0, Equals, true)
  2159. client.DeleteBucket(bucketName)
  2160. }
  2161. func (s *OssClientSuite) TestSetBucketRequestPayment(c *C) {
  2162. client, err := New(endpoint, accessID, accessKey)
  2163. c.Assert(err, IsNil)
  2164. bucketName := bucketNamePrefix + randLowStr(5)
  2165. err = client.CreateBucket(bucketName)
  2166. c.Assert(err, IsNil)
  2167. reqPayConf := RequestPaymentConfiguration{
  2168. Payer: "Requester",
  2169. }
  2170. err = client.SetBucketRequestPayment(bucketName, reqPayConf)
  2171. c.Assert(err, IsNil)
  2172. ret, err := client.GetBucketRequestPayment(bucketName)
  2173. c.Assert(err, IsNil)
  2174. c.Assert(ret.Payer, Equals, "Requester")
  2175. client.DeleteBucket(bucketName)
  2176. c.Assert(err, IsNil)
  2177. }
  2178. func (s *OssClientSuite) TestSetBucketRequestPaymentNegative(c *C) {
  2179. client, err := New(endpoint, accessID, accessKey)
  2180. c.Assert(err, IsNil)
  2181. bucketName := bucketNamePrefix + randLowStr(5)
  2182. err = client.CreateBucket(bucketName)
  2183. c.Assert(err, IsNil)
  2184. reqPayConf := RequestPaymentConfiguration{
  2185. Payer: "Requesterttttt", // this is a error configuration
  2186. }
  2187. err = client.SetBucketRequestPayment(bucketName, reqPayConf)
  2188. c.Assert(err, NotNil)
  2189. ret, err := client.GetBucketRequestPayment(bucketName)
  2190. c.Assert(err, IsNil)
  2191. c.Assert(ret.Payer, Equals, "BucketOwner")
  2192. client.DeleteBucket(bucketName)
  2193. c.Assert(err, IsNil)
  2194. }
  2195. func (s *OssClientSuite) TestBucketQos(c *C) {
  2196. client, err := New(endpoint, accessID, accessKey)
  2197. c.Assert(err, IsNil)
  2198. ret, err := client.GetUserQoSInfo()
  2199. c.Assert(err, IsNil)
  2200. testLogger.Println("QosInfo:", ret)
  2201. bucketName := bucketNamePrefix + randLowStr(5)
  2202. _ = client.DeleteBucket(bucketName)
  2203. err = client.CreateBucket(bucketName)
  2204. c.Assert(err, IsNil)
  2205. _, err = client.GetBucketQosInfo(bucketName)
  2206. c.Assert(err, NotNil)
  2207. // case 1 set BucketQoSConfiguration every member
  2208. five := 5
  2209. four := 4
  2210. totalQps := 200
  2211. qosConf := BucketQoSConfiguration{
  2212. TotalUploadBandwidth: &five,
  2213. IntranetUploadBandwidth: &four,
  2214. ExtranetUploadBandwidth: &four,
  2215. TotalDownloadBandwidth: &four,
  2216. IntranetDownloadBandwidth: &four,
  2217. ExtranetDownloadBandwidth: &four,
  2218. TotalQPS: &totalQps,
  2219. IntranetQPS: &totalQps,
  2220. ExtranetQPS: &totalQps,
  2221. }
  2222. var responseHeader http.Header
  2223. err = client.SetBucketQoSInfo(bucketName, qosConf, GetResponseHeader(&responseHeader))
  2224. c.Assert(err, IsNil)
  2225. requestId := GetRequestId(responseHeader)
  2226. c.Assert(len(requestId) > 0, Equals, true)
  2227. // wait a moment for configuration effect
  2228. time.Sleep(time.Second)
  2229. retQos, err := client.GetBucketQosInfo(bucketName)
  2230. c.Assert(err, IsNil)
  2231. // set qosConf default value
  2232. qosConf.XMLName.Local = "QoSConfiguration"
  2233. c.Assert(struct2string(retQos, c), Equals, struct2string(qosConf, c))
  2234. // case 2 set BucketQoSConfiguration not every member
  2235. qosConfNo := BucketQoSConfiguration{
  2236. TotalUploadBandwidth: &five,
  2237. IntranetUploadBandwidth: &four,
  2238. ExtranetUploadBandwidth: &four,
  2239. TotalDownloadBandwidth: &four,
  2240. IntranetDownloadBandwidth: &four,
  2241. ExtranetDownloadBandwidth: &four,
  2242. TotalQPS: &totalQps,
  2243. }
  2244. err = client.SetBucketQoSInfo(bucketName, qosConfNo)
  2245. c.Assert(err, IsNil)
  2246. // wait a moment for configuration effect
  2247. time.Sleep(time.Second)
  2248. retQos, err = client.GetBucketQosInfo(bucketName)
  2249. c.Assert(err, IsNil)
  2250. // set qosConfNo default value
  2251. qosConfNo.XMLName.Local = "QoSConfiguration"
  2252. defNum := -1
  2253. qosConfNo.IntranetQPS = &defNum
  2254. qosConfNo.ExtranetQPS = &defNum
  2255. c.Assert(struct2string(retQos, c), Equals, struct2string(qosConfNo, c))
  2256. err = client.DeleteBucketQosInfo(bucketName)
  2257. c.Assert(err, IsNil)
  2258. // wait a moment for configuration effect
  2259. time.Sleep(time.Second)
  2260. _, err = client.GetBucketQosInfo(bucketName)
  2261. c.Assert(err, NotNil)
  2262. // this is a error qos configuration
  2263. to := *ret.TotalUploadBandwidth + 2
  2264. qosErrConf := BucketQoSConfiguration{
  2265. TotalUploadBandwidth: &to, // this exceed user TotalUploadBandwidth
  2266. IntranetUploadBandwidth: &four,
  2267. ExtranetUploadBandwidth: &four,
  2268. TotalDownloadBandwidth: &four,
  2269. IntranetDownloadBandwidth: &four,
  2270. ExtranetDownloadBandwidth: &four,
  2271. TotalQPS: &totalQps,
  2272. IntranetQPS: &totalQps,
  2273. ExtranetQPS: &totalQps,
  2274. }
  2275. err = client.SetBucketQoSInfo(bucketName, qosErrConf)
  2276. c.Assert(err, NotNil)
  2277. err = client.DeleteBucketQosInfo(bucketName)
  2278. c.Assert(err, IsNil)
  2279. err = client.DeleteBucket(bucketName)
  2280. c.Assert(err, IsNil)
  2281. }
  2282. // struct to string
  2283. func struct2string(obj interface{}, c *C) string {
  2284. t := reflect.TypeOf(obj)
  2285. v := reflect.ValueOf(obj)
  2286. var data = make(map[string]interface{})
  2287. for i := 0; i < t.NumField(); i++ {
  2288. data[t.Field(i).Name] = v.Field(i).Interface()
  2289. }
  2290. str, err := json.Marshal(data)
  2291. c.Assert(err, IsNil)
  2292. return string(str)
  2293. }
  2294. type TestCredentials struct {
  2295. }
  2296. func (testCreInf *TestCredentials) GetAccessKeyID() string {
  2297. return os.Getenv("OSS_TEST_ACCESS_KEY_ID")
  2298. }
  2299. func (testCreInf *TestCredentials) GetAccessKeySecret() string {
  2300. return os.Getenv("OSS_TEST_ACCESS_KEY_SECRET")
  2301. }
  2302. func (testCreInf *TestCredentials) GetSecurityToken() string {
  2303. return ""
  2304. }
  2305. type TestCredentialsProvider struct {
  2306. }
  2307. func (testInfBuild *TestCredentialsProvider) GetCredentials() Credentials {
  2308. return &TestCredentials{}
  2309. }
  2310. func (s *OssClientSuite) TestClientCredentialInfBuild(c *C) {
  2311. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  2312. var defaultBuild TestCredentialsProvider
  2313. client, err := New(endpoint, "", "", SetCredentialsProvider(&defaultBuild))
  2314. c.Assert(err, IsNil)
  2315. err = client.CreateBucket(bucketNameTest)
  2316. c.Assert(err, IsNil)
  2317. err = client.DeleteBucket(bucketNameTest)
  2318. c.Assert(err, IsNil)
  2319. }
  2320. func (s *OssClientSuite) TestClientSetLocalIpError(c *C) {
  2321. // create client and bucket
  2322. ipAddr, err := net.ResolveIPAddr("ip", "127.0.0.1")
  2323. c.Assert(err, IsNil)
  2324. localTCPAddr := &(net.TCPAddr{IP: ipAddr.IP})
  2325. client, err := New(endpoint, accessID, accessKey, SetLocalAddr(localTCPAddr))
  2326. c.Assert(err, IsNil)
  2327. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  2328. err = client.CreateBucket(bucketNameTest)
  2329. c.Assert(err, NotNil)
  2330. }
  2331. func (s *OssClientSuite) TestClientSetLocalIpSuccess(c *C) {
  2332. //get local ip
  2333. conn, err := net.Dial("udp", "8.8.8.8:80")
  2334. c.Assert(err, IsNil)
  2335. localAddr := conn.LocalAddr().(*net.UDPAddr)
  2336. localIp := localAddr.IP.String()
  2337. conn.Close()
  2338. ipAddr, err := net.ResolveIPAddr("ip", localIp)
  2339. c.Assert(err, IsNil)
  2340. localTCPAddr := &(net.TCPAddr{IP: ipAddr.IP})
  2341. client, err := New(endpoint, accessID, accessKey, SetLocalAddr(localTCPAddr))
  2342. c.Assert(err, IsNil)
  2343. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  2344. err = client.CreateBucket(bucketNameTest)
  2345. c.Assert(err, IsNil)
  2346. err = client.DeleteBucket(bucketNameTest)
  2347. c.Assert(err, IsNil)
  2348. }
  2349. // TestCreateBucketInvalidName
  2350. func (s *OssClientSuite) TestCreateBucketInvalidName(c *C) {
  2351. var bucketNameTest = "-" + bucketNamePrefix + randLowStr(6)
  2352. client, err := New(endpoint, accessID, accessKey)
  2353. c.Assert(err, IsNil)
  2354. // Create
  2355. err = client.CreateBucket(bucketNameTest)
  2356. c.Assert(err, NotNil)
  2357. }
  2358. // TestClientProcessEndpointSuccess
  2359. func (s *OssClientSuite) TestClientProcessEndpointSuccess(c *C) {
  2360. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  2361. testEndpoint := endpoint + "/" + "sina.com" + "?" + "para=abc"
  2362. client, err := New(testEndpoint, accessID, accessKey)
  2363. c.Assert(err, IsNil)
  2364. // Create
  2365. err = client.CreateBucket(bucketNameTest)
  2366. c.Assert(err, IsNil)
  2367. // delete
  2368. err = client.DeleteBucket(bucketNameTest)
  2369. c.Assert(err, IsNil)
  2370. }
  2371. // TestClientProcessEndpointSuccess
  2372. func (s *OssClientSuite) TestClientProcessEndpointError(c *C) {
  2373. var bucketNameTest = bucketNamePrefix + randLowStr(6)
  2374. testEndpoint := "https://127.0.0.1/" + endpoint
  2375. client, err := New(testEndpoint, accessID, accessKey)
  2376. c.Assert(err, IsNil)
  2377. // Create
  2378. err = client.CreateBucket(bucketNameTest)
  2379. c.Assert(err, NotNil)
  2380. }
  2381. // TestClientBucketError
  2382. func (s *OssClientSuite) TestClientBucketError(c *C) {
  2383. client, err := New(endpoint, accessID, accessKey)
  2384. c.Assert(err, IsNil)
  2385. bucketName := "-" + randLowStr(5)
  2386. _, err = client.Bucket(bucketName)
  2387. c.Assert(err, NotNil)
  2388. }