bucket_test.go 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202
  1. // bucket test
  2. package oss
  3. import (
  4. "bytes"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "math/rand"
  10. "net/http"
  11. "os"
  12. "path/filepath"
  13. "strconv"
  14. "strings"
  15. "time"
  16. "github.com/baiyubin/aliyun-sts-go-sdk/sts"
  17. . "gopkg.in/check.v1"
  18. )
  19. type OssBucketSuite struct {
  20. client *Client
  21. bucket *Bucket
  22. archiveBucket *Bucket
  23. }
  24. var _ = Suite(&OssBucketSuite{})
  25. var (
  26. pastDate = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
  27. futureDate = time.Date(2049, time.January, 10, 23, 0, 0, 0, time.UTC)
  28. )
  29. // Run once when the suite starts running
  30. func (s *OssBucketSuite) SetUpSuite(c *C) {
  31. client, err := New(endpoint, accessID, accessKey)
  32. c.Assert(err, IsNil)
  33. s.client = client
  34. s.client.CreateBucket(bucketName)
  35. err = s.client.CreateBucket(archiveBucketName, StorageClass(StorageArchive))
  36. c.Assert(err, IsNil)
  37. time.Sleep(5 * time.Second)
  38. bucket, err := s.client.Bucket(bucketName)
  39. c.Assert(err, IsNil)
  40. s.bucket = bucket
  41. archiveBucket, err := s.client.Bucket(archiveBucketName)
  42. c.Assert(err, IsNil)
  43. s.archiveBucket = archiveBucket
  44. testLogger.Println("test bucket started")
  45. }
  46. // Run before each test or benchmark starts running
  47. func (s *OssBucketSuite) TearDownSuite(c *C) {
  48. for _, bucket := range []*Bucket{s.bucket, s.archiveBucket} {
  49. // Delete Multipart
  50. lmu, err := bucket.ListMultipartUploads()
  51. c.Assert(err, IsNil)
  52. for _, upload := range lmu.Uploads {
  53. imur := InitiateMultipartUploadResult{Bucket: bucketName, Key: upload.Key, UploadID: upload.UploadID}
  54. err = bucket.AbortMultipartUpload(imur)
  55. c.Assert(err, IsNil)
  56. }
  57. // Delete Objects
  58. lor, err := bucket.ListObjects()
  59. c.Assert(err, IsNil)
  60. for _, object := range lor.Objects {
  61. err = bucket.DeleteObject(object.Key)
  62. c.Assert(err, IsNil)
  63. }
  64. }
  65. testLogger.Println("test bucket completed")
  66. }
  67. // Run after each test or benchmark runs
  68. func (s *OssBucketSuite) SetUpTest(c *C) {
  69. err := removeTempFiles("../oss", ".jpg")
  70. c.Assert(err, IsNil)
  71. }
  72. // Run once after all tests or benchmarks have finished running
  73. func (s *OssBucketSuite) TearDownTest(c *C) {
  74. err := removeTempFiles("../oss", ".jpg")
  75. c.Assert(err, IsNil)
  76. err = removeTempFiles("../oss", ".txt")
  77. c.Assert(err, IsNil)
  78. err = removeTempFiles("../oss", ".temp")
  79. c.Assert(err, IsNil)
  80. err = removeTempFiles("../oss", ".txt1")
  81. c.Assert(err, IsNil)
  82. err = removeTempFiles("../oss", ".txt2")
  83. c.Assert(err, IsNil)
  84. }
  85. // TestPutObject
  86. func (s *OssBucketSuite) TestPutObject(c *C) {
  87. objectName := objectNamePrefix + "tpo"
  88. objectValue := "大江东去,浪淘尽,千古风流人物。 故垒西边,人道是、三国周郎赤壁。 乱石穿空,惊涛拍岸,卷起千堆雪。 江山如画,一时多少豪杰。" +
  89. "遥想公谨当年,小乔初嫁了,雄姿英发。 羽扇纶巾,谈笑间、樯橹灰飞烟灭。故国神游,多情应笑我,早生华发,人生如梦,一尊还酹江月。"
  90. // string put
  91. err := s.bucket.PutObject(objectName, strings.NewReader(objectValue))
  92. c.Assert(err, IsNil)
  93. // Check
  94. body, err := s.bucket.GetObject(objectName)
  95. c.Assert(err, IsNil)
  96. str, err := readBody(body)
  97. c.Assert(err, IsNil)
  98. c.Assert(str, Equals, objectValue)
  99. acl, err := s.bucket.GetObjectACL(objectName)
  100. c.Assert(err, IsNil)
  101. testLogger.Println("aclRes:", acl)
  102. c.Assert(acl.ACL, Equals, "default")
  103. err = s.bucket.DeleteObject(objectName)
  104. c.Assert(err, IsNil)
  105. // bytes put
  106. err = s.bucket.PutObject(objectName, bytes.NewReader([]byte(objectValue)))
  107. c.Assert(err, IsNil)
  108. // Check
  109. body, err = s.bucket.GetObject(objectName)
  110. c.Assert(err, IsNil)
  111. str, err = readBody(body)
  112. c.Assert(err, IsNil)
  113. c.Assert(str, Equals, objectValue)
  114. err = s.bucket.DeleteObject(objectName)
  115. c.Assert(err, IsNil)
  116. // file put
  117. err = createFileAndWrite(objectName+".txt", []byte(objectValue))
  118. c.Assert(err, IsNil)
  119. fd, err := os.Open(objectName + ".txt")
  120. c.Assert(err, IsNil)
  121. err = s.bucket.PutObject(objectName, fd)
  122. c.Assert(err, IsNil)
  123. os.Remove(objectName + ".txt")
  124. // Check
  125. body, err = s.bucket.GetObject(objectName)
  126. c.Assert(err, IsNil)
  127. str, err = readBody(body)
  128. c.Assert(err, IsNil)
  129. c.Assert(str, Equals, objectValue)
  130. err = s.bucket.DeleteObject(objectName)
  131. c.Assert(err, IsNil)
  132. // Put with properties
  133. objectName = objectNamePrefix + "tpox"
  134. options := []Option{
  135. Expires(futureDate),
  136. ObjectACL(ACLPublicRead),
  137. Meta("myprop", "mypropval"),
  138. }
  139. err = s.bucket.PutObject(objectName, strings.NewReader(objectValue), options...)
  140. c.Assert(err, IsNil)
  141. // Check
  142. body, err = s.bucket.GetObject(objectName)
  143. c.Assert(err, IsNil)
  144. str, err = readBody(body)
  145. c.Assert(err, IsNil)
  146. c.Assert(str, Equals, objectValue)
  147. acl, err = s.bucket.GetObjectACL(objectName)
  148. c.Assert(err, IsNil)
  149. testLogger.Println("GetObjectACL:", acl)
  150. c.Assert(acl.ACL, Equals, string(ACLPublicRead))
  151. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  152. c.Assert(err, IsNil)
  153. testLogger.Println("GetObjectDetailedMeta:", meta)
  154. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  155. err = s.bucket.DeleteObject(objectName)
  156. c.Assert(err, IsNil)
  157. }
  158. func (s *OssBucketSuite) TestSignURL(c *C) {
  159. objectName := objectNamePrefix + randStr(5)
  160. objectValue := randStr(20)
  161. filePath := randLowStr(10)
  162. content := "复写object"
  163. createFile(filePath, content, c)
  164. notExistfilePath := randLowStr(10)
  165. os.Remove(notExistfilePath)
  166. // sign url for put
  167. str, err := s.bucket.SignURL(objectName, HTTPPut, 60)
  168. c.Assert(err, IsNil)
  169. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  170. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  171. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  172. // error put object with url
  173. err = s.bucket.PutObjectWithURL(str, strings.NewReader(objectValue), ContentType("image/tiff"))
  174. c.Assert(err, NotNil)
  175. c.Assert(err.(ServiceError).Code, Equals, "SignatureDoesNotMatch")
  176. err = s.bucket.PutObjectFromFileWithURL(str, filePath, ContentType("image/tiff"))
  177. c.Assert(err, NotNil)
  178. c.Assert(err.(ServiceError).Code, Equals, "SignatureDoesNotMatch")
  179. // put object with url
  180. err = s.bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  181. c.Assert(err, IsNil)
  182. acl, err := s.bucket.GetObjectACL(objectName)
  183. c.Assert(err, IsNil)
  184. c.Assert(acl.ACL, Equals, "default")
  185. // get object meta
  186. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  187. c.Assert(err, IsNil)
  188. c.Assert(meta.Get(HTTPHeaderContentType), Equals, "application/octet-stream")
  189. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "")
  190. // sign url for get object
  191. str, err = s.bucket.SignURL(objectName, HTTPGet, 60)
  192. c.Assert(err, IsNil)
  193. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  194. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  195. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  196. // get object with url
  197. body, err := s.bucket.GetObjectWithURL(str)
  198. c.Assert(err, IsNil)
  199. str, err = readBody(body)
  200. c.Assert(err, IsNil)
  201. c.Assert(str, Equals, objectValue)
  202. // sign url for put with options
  203. options := []Option{
  204. ObjectACL(ACLPublicRead),
  205. Meta("myprop", "mypropval"),
  206. ContentType("image/tiff"),
  207. ResponseContentEncoding("deflate"),
  208. }
  209. str, err = s.bucket.SignURL(objectName, HTTPPut, 60, options...)
  210. c.Assert(err, IsNil)
  211. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  212. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  213. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  214. // put object with url from file
  215. // without option, error
  216. err = s.bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  217. c.Assert(err, NotNil)
  218. c.Assert(err.(ServiceError).Code, Equals, "SignatureDoesNotMatch")
  219. err = s.bucket.PutObjectFromFileWithURL(str, filePath)
  220. c.Assert(err, NotNil)
  221. c.Assert(err.(ServiceError).Code, Equals, "SignatureDoesNotMatch")
  222. // with option, error file
  223. err = s.bucket.PutObjectFromFileWithURL(str, notExistfilePath, options...)
  224. c.Assert(err, NotNil)
  225. // with option
  226. err = s.bucket.PutObjectFromFileWithURL(str, filePath, options...)
  227. c.Assert(err, IsNil)
  228. // get object meta
  229. meta, err = s.bucket.GetObjectDetailedMeta(objectName)
  230. c.Assert(err, IsNil)
  231. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  232. c.Assert(meta.Get(HTTPHeaderContentType), Equals, "image/tiff")
  233. acl, err = s.bucket.GetObjectACL(objectName)
  234. c.Assert(err, IsNil)
  235. c.Assert(acl.ACL, Equals, string(ACLPublicRead))
  236. // sign url for get object
  237. str, err = s.bucket.SignURL(objectName, HTTPGet, 60)
  238. c.Assert(err, IsNil)
  239. // get object to file with url
  240. newFile := randStr(10)
  241. err = s.bucket.GetObjectToFileWithURL(str, newFile)
  242. c.Assert(err, IsNil)
  243. eq, err := compareFiles(filePath, newFile)
  244. c.Assert(err, IsNil)
  245. c.Assert(eq, Equals, true)
  246. os.Remove(newFile)
  247. // get object to file error
  248. err = s.bucket.GetObjectToFileWithURL(str, newFile, options...)
  249. c.Assert(err, NotNil)
  250. c.Assert(err.(ServiceError).Code, Equals, "SignatureDoesNotMatch")
  251. _, err = os.Stat(newFile)
  252. c.Assert(err, NotNil)
  253. // get object error
  254. body, err = s.bucket.GetObjectWithURL(str, options...)
  255. c.Assert(err, NotNil)
  256. c.Assert(err.(ServiceError).Code, Equals, "SignatureDoesNotMatch")
  257. c.Assert(body, IsNil)
  258. // sign url for get object with options
  259. options = []Option{
  260. Expires(futureDate),
  261. ObjectACL(ACLPublicRead),
  262. Meta("myprop", "mypropval"),
  263. ContentType("image/tiff"),
  264. ResponseContentEncoding("deflate"),
  265. }
  266. str, err = s.bucket.SignURL(objectName, HTTPGet, 60, options...)
  267. c.Assert(err, IsNil)
  268. // get object to file with url and options
  269. err = s.bucket.GetObjectToFileWithURL(str, newFile, options...)
  270. c.Assert(err, IsNil)
  271. eq, err = compareFiles(filePath, newFile)
  272. c.Assert(err, IsNil)
  273. c.Assert(eq, Equals, true)
  274. os.Remove(newFile)
  275. // get object to file error
  276. err = s.bucket.GetObjectToFileWithURL(str, newFile)
  277. c.Assert(err, NotNil)
  278. c.Assert(err.(ServiceError).Code, Equals, "SignatureDoesNotMatch")
  279. _, err = os.Stat(newFile)
  280. c.Assert(err, NotNil)
  281. // get object error
  282. body, err = s.bucket.GetObjectWithURL(str)
  283. c.Assert(err, NotNil)
  284. c.Assert(err.(ServiceError).Code, Equals, "SignatureDoesNotMatch")
  285. c.Assert(body, IsNil)
  286. os.Remove(filePath)
  287. os.Remove(newFile)
  288. // sign url error
  289. str, err = s.bucket.SignURL(objectName, HTTPGet, -1)
  290. c.Assert(err, NotNil)
  291. err = s.bucket.DeleteObject(objectName)
  292. c.Assert(err, IsNil)
  293. // invalid url parse
  294. str = randStr(20)
  295. err = s.bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  296. c.Assert(err, NotNil)
  297. err = s.bucket.GetObjectToFileWithURL(str, newFile)
  298. c.Assert(err, NotNil)
  299. }
  300. func (s *OssBucketSuite) TestSignURLWithEscapedKey(c *C) {
  301. // key with '/'
  302. objectName := "zyimg/86/e8/653b5dc97bb0022051a84c632bc4"
  303. objectValue := "弃我去者,昨日之日不可留;乱我心者,今日之日多烦忧。长风万里送秋雁,对此可以酣高楼。蓬莱文章建安骨,中间小谢又清发。" +
  304. "俱怀逸兴壮思飞,欲上青天揽明月。抽刀断水水更流,举杯销愁愁更愁。人生在世不称意,明朝散发弄扁舟。"
  305. // sign url for put
  306. str, err := s.bucket.SignURL(objectName, HTTPPut, 60)
  307. c.Assert(err, IsNil)
  308. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  309. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  310. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  311. // put object with url
  312. err = s.bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  313. c.Assert(err, IsNil)
  314. // sign url for get object
  315. str, err = s.bucket.SignURL(objectName, HTTPGet, 60)
  316. c.Assert(err, IsNil)
  317. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  318. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  319. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  320. // get object with url
  321. body, err := s.bucket.GetObjectWithURL(str)
  322. c.Assert(err, IsNil)
  323. str, err = readBody(body)
  324. c.Assert(err, IsNil)
  325. c.Assert(str, Equals, objectValue)
  326. // key with escaped chars
  327. objectName = "<>[]()`?.,!@#$%^&'/*-_=+~:;"
  328. // sign url for put
  329. str, err = s.bucket.SignURL(objectName, HTTPPut, 60)
  330. c.Assert(err, IsNil)
  331. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  332. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  333. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  334. // put object with url
  335. err = s.bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  336. c.Assert(err, IsNil)
  337. // sign url for get object
  338. str, err = s.bucket.SignURL(objectName, HTTPGet, 60)
  339. c.Assert(err, IsNil)
  340. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  341. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  342. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  343. // get object with url
  344. body, err = s.bucket.GetObjectWithURL(str)
  345. c.Assert(err, IsNil)
  346. str, err = readBody(body)
  347. c.Assert(err, IsNil)
  348. c.Assert(str, Equals, objectValue)
  349. // key with Chinese chars
  350. objectName = "风吹柳花满店香,吴姬压酒劝客尝。金陵子弟来相送,欲行不行各尽觞。请君试问东流水,别意与之谁短长。"
  351. // sign url for put
  352. str, err = s.bucket.SignURL(objectName, HTTPPut, 60)
  353. c.Assert(err, IsNil)
  354. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  355. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  356. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  357. // put object with url
  358. err = s.bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  359. c.Assert(err, IsNil)
  360. // sign url for get object
  361. str, err = s.bucket.SignURL(objectName, HTTPGet, 60)
  362. c.Assert(err, IsNil)
  363. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  364. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  365. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  366. // get object with url
  367. body, err = s.bucket.GetObjectWithURL(str)
  368. c.Assert(err, IsNil)
  369. str, err = readBody(body)
  370. c.Assert(err, IsNil)
  371. c.Assert(str, Equals, objectValue)
  372. // key
  373. objectName = "test/此情无计可消除/才下眉头/却上 心头/。,;:‘’“”?()『』【】《》!@#¥%……&×/test+ =-_*&^%$#@!`~[]{}()<>|\\/?.,;.txt"
  374. // sign url for put
  375. str, err = s.bucket.SignURL(objectName, HTTPPut, 60)
  376. c.Assert(err, IsNil)
  377. // put object with url
  378. err = s.bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  379. c.Assert(err, IsNil)
  380. // sign url for get object
  381. str, err = s.bucket.SignURL(objectName, HTTPGet, 60)
  382. c.Assert(err, IsNil)
  383. // get object with url
  384. body, err = s.bucket.GetObjectWithURL(str)
  385. c.Assert(err, IsNil)
  386. str, err = readBody(body)
  387. c.Assert(err, IsNil)
  388. c.Assert(str, Equals, objectValue)
  389. // put object
  390. err = s.bucket.PutObject(objectName, bytes.NewReader([]byte(objectValue)))
  391. c.Assert(err, IsNil)
  392. // get object
  393. body, err = s.bucket.GetObject(objectName)
  394. c.Assert(err, IsNil)
  395. str, err = readBody(body)
  396. c.Assert(err, IsNil)
  397. c.Assert(str, Equals, objectValue)
  398. // delete object
  399. err = s.bucket.DeleteObject(objectName)
  400. c.Assert(err, IsNil)
  401. }
  402. func (s *OssBucketSuite) TestSignURLWithEscapedKeyAndPorxy(c *C) {
  403. // key with '/'
  404. objectName := "zyimg/86/e8/653b5dc97bb0022051a84c632bc4"
  405. objectValue := "弃我去者,昨日之日不可留;乱我心者,今日之日多烦忧。长风万里送秋雁,对此可以酣高楼。蓬莱文章建安骨,中间小谢又清发。" +
  406. "俱怀逸兴壮思飞,欲上青天揽明月。抽刀断水水更流,举杯销愁愁更愁。人生在世不称意,明朝散发弄扁舟。"
  407. client, err := New(endpoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
  408. bucket, err := client.Bucket(bucketName)
  409. // sign url for put
  410. str, err := bucket.SignURL(objectName, HTTPPut, 60)
  411. c.Assert(err, IsNil)
  412. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  413. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  414. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  415. // put object with url
  416. err = bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  417. c.Assert(err, IsNil)
  418. // sign url for get object
  419. str, err = bucket.SignURL(objectName, HTTPGet, 60)
  420. c.Assert(err, IsNil)
  421. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  422. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  423. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  424. // get object with url
  425. body, err := bucket.GetObjectWithURL(str)
  426. c.Assert(err, IsNil)
  427. str, err = readBody(body)
  428. c.Assert(err, IsNil)
  429. c.Assert(str, Equals, objectValue)
  430. // key with Chinese chars
  431. objectName = "test/此情无计可消除/才下眉头/却上 心头/。,;:‘’“”?()『』【】《》!@#¥%……&×/test+ =-_*&^%$#@!`~[]{}()<>|\\/?.,;.txt"
  432. // sign url for put
  433. str, err = bucket.SignURL(objectName, HTTPPut, 60)
  434. c.Assert(err, IsNil)
  435. // put object with url
  436. err = bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  437. c.Assert(err, IsNil)
  438. // sign url for get object
  439. str, err = bucket.SignURL(objectName, HTTPGet, 60)
  440. c.Assert(err, IsNil)
  441. // get object with url
  442. body, err = bucket.GetObjectWithURL(str)
  443. c.Assert(err, IsNil)
  444. str, err = readBody(body)
  445. c.Assert(err, IsNil)
  446. c.Assert(str, Equals, objectValue)
  447. // put object
  448. err = bucket.PutObject(objectName, bytes.NewReader([]byte(objectValue)))
  449. c.Assert(err, IsNil)
  450. // get object
  451. body, err = bucket.GetObject(objectName)
  452. c.Assert(err, IsNil)
  453. str, err = readBody(body)
  454. c.Assert(err, IsNil)
  455. c.Assert(str, Equals, objectValue)
  456. // delete object
  457. err = bucket.DeleteObject(objectName)
  458. c.Assert(err, IsNil)
  459. }
  460. // TestPutObjectType
  461. func (s *OssBucketSuite) TestPutObjectType(c *C) {
  462. objectName := objectNamePrefix + "tptt"
  463. objectValue := "乱石穿空,惊涛拍岸,卷起千堆雪。 江山如画,一时多少豪杰。"
  464. // Put
  465. err := s.bucket.PutObject(objectName, strings.NewReader(objectValue))
  466. c.Assert(err, IsNil)
  467. // Check
  468. time.Sleep(time.Second)
  469. body, err := s.bucket.GetObject(objectName)
  470. c.Assert(err, IsNil)
  471. str, err := readBody(body)
  472. c.Assert(err, IsNil)
  473. c.Assert(str, Equals, objectValue)
  474. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  475. c.Assert(err, IsNil)
  476. c.Assert(meta.Get("Content-Type"), Equals, "application/octet-stream")
  477. err = s.bucket.DeleteObject(objectName)
  478. c.Assert(err, IsNil)
  479. // Put
  480. err = s.bucket.PutObject(objectName+".txt", strings.NewReader(objectValue))
  481. c.Assert(err, IsNil)
  482. meta, err = s.bucket.GetObjectDetailedMeta(objectName + ".txt")
  483. c.Assert(err, IsNil)
  484. c.Assert(meta.Get("Content-Type"), Equals, "text/plain; charset=utf-8")
  485. err = s.bucket.DeleteObject(objectName + ".txt")
  486. c.Assert(err, IsNil)
  487. // Put
  488. err = s.bucket.PutObject(objectName+".apk", strings.NewReader(objectValue))
  489. c.Assert(err, IsNil)
  490. meta, err = s.bucket.GetObjectDetailedMeta(objectName + ".apk")
  491. c.Assert(err, IsNil)
  492. c.Assert(meta.Get("Content-Type"), Equals, "application/vnd.android.package-archive")
  493. err = s.bucket.DeleteObject(objectName + ".txt")
  494. c.Assert(err, IsNil)
  495. }
  496. // TestPutObject
  497. func (s *OssBucketSuite) TestPutObjectKeyChars(c *C) {
  498. objectName := objectNamePrefix + "tpokc"
  499. objectValue := "白日依山尽,黄河入海流。欲穷千里目,更上一层楼。"
  500. // Put
  501. objectKey := objectName + "十步杀一人,千里不留行。事了拂衣去,深藏身与名"
  502. err := s.bucket.PutObject(objectKey, strings.NewReader(objectValue))
  503. c.Assert(err, IsNil)
  504. // Check
  505. body, err := s.bucket.GetObject(objectKey)
  506. c.Assert(err, IsNil)
  507. str, err := readBody(body)
  508. c.Assert(err, IsNil)
  509. c.Assert(str, Equals, objectValue)
  510. err = s.bucket.DeleteObject(objectKey)
  511. c.Assert(err, IsNil)
  512. // Put
  513. objectKey = objectName + "ごきげん如何ですかおれの顔をよく拝んでおけ"
  514. err = s.bucket.PutObject(objectKey, strings.NewReader(objectValue))
  515. c.Assert(err, IsNil)
  516. // Check
  517. body, err = s.bucket.GetObject(objectKey)
  518. c.Assert(err, IsNil)
  519. str, err = readBody(body)
  520. c.Assert(err, IsNil)
  521. c.Assert(str, Equals, objectValue)
  522. err = s.bucket.DeleteObject(objectKey)
  523. c.Assert(err, IsNil)
  524. // Put
  525. objectKey = objectName + "~!@#$%^&*()_-+=|\\[]{}<>,./?"
  526. err = s.bucket.PutObject(objectKey, strings.NewReader(objectValue))
  527. c.Assert(err, IsNil)
  528. // Check
  529. body, err = s.bucket.GetObject(objectKey)
  530. c.Assert(err, IsNil)
  531. str, err = readBody(body)
  532. c.Assert(err, IsNil)
  533. c.Assert(str, Equals, objectValue)
  534. err = s.bucket.DeleteObject(objectKey)
  535. c.Assert(err, IsNil)
  536. // Put
  537. objectKey = "go/中国 日本 +-#&=*"
  538. err = s.bucket.PutObject(objectKey, strings.NewReader(objectValue))
  539. c.Assert(err, IsNil)
  540. // Check
  541. body, err = s.bucket.GetObject(objectKey)
  542. c.Assert(err, IsNil)
  543. str, err = readBody(body)
  544. c.Assert(err, IsNil)
  545. c.Assert(str, Equals, objectValue)
  546. err = s.bucket.DeleteObject(objectKey)
  547. c.Assert(err, IsNil)
  548. }
  549. // TestPutObjectNegative
  550. func (s *OssBucketSuite) TestPutObjectNegative(c *C) {
  551. objectName := objectNamePrefix + "tpon"
  552. objectValue := "大江东去,浪淘尽,千古风流人物。 "
  553. // Put
  554. objectName = objectNamePrefix + "tpox"
  555. err := s.bucket.PutObject(objectName, strings.NewReader(objectValue),
  556. Meta("meta-my", "myprop"))
  557. c.Assert(err, IsNil)
  558. // Check meta
  559. body, err := s.bucket.GetObject(objectName)
  560. c.Assert(err, IsNil)
  561. str, err := readBody(body)
  562. c.Assert(err, IsNil)
  563. c.Assert(str, Equals, objectValue)
  564. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  565. c.Assert(err, IsNil)
  566. c.Assert(meta.Get("X-Oss-Meta-My"), Not(Equals), "myprop")
  567. c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "")
  568. err = s.bucket.DeleteObject(objectName)
  569. c.Assert(err, IsNil)
  570. // invalid option
  571. err = s.bucket.PutObject(objectName, strings.NewReader(objectValue),
  572. IfModifiedSince(pastDate))
  573. c.Assert(err, NotNil)
  574. err = s.bucket.PutObjectFromFile(objectName, "bucket.go", IfModifiedSince(pastDate))
  575. c.Assert(err, NotNil)
  576. err = s.bucket.PutObjectFromFile(objectName, "/tmp/xxx")
  577. c.Assert(err, NotNil)
  578. }
  579. // TestPutObjectFromFile
  580. func (s *OssBucketSuite) TestPutObjectFromFile(c *C) {
  581. objectName := objectNamePrefix + "tpoff"
  582. localFile := "../sample/BingWallpaper-2015-11-07.jpg"
  583. newFile := "newpic11.jpg"
  584. // Put
  585. err := s.bucket.PutObjectFromFile(objectName, localFile)
  586. c.Assert(err, IsNil)
  587. // Check
  588. err = s.bucket.GetObjectToFile(objectName, newFile)
  589. c.Assert(err, IsNil)
  590. eq, err := compareFiles(localFile, newFile)
  591. c.Assert(err, IsNil)
  592. c.Assert(eq, Equals, true)
  593. acl, err := s.bucket.GetObjectACL(objectName)
  594. c.Assert(err, IsNil)
  595. testLogger.Println("aclRes:", acl)
  596. c.Assert(acl.ACL, Equals, "default")
  597. err = s.bucket.DeleteObject(objectName)
  598. c.Assert(err, IsNil)
  599. // Put with properties
  600. options := []Option{
  601. Expires(futureDate),
  602. ObjectACL(ACLPublicRead),
  603. Meta("myprop", "mypropval"),
  604. }
  605. os.Remove(newFile)
  606. err = s.bucket.PutObjectFromFile(objectName, localFile, options...)
  607. c.Assert(err, IsNil)
  608. // Check
  609. err = s.bucket.GetObjectToFile(objectName, newFile)
  610. c.Assert(err, IsNil)
  611. eq, err = compareFiles(localFile, newFile)
  612. c.Assert(err, IsNil)
  613. c.Assert(eq, Equals, true)
  614. acl, err = s.bucket.GetObjectACL(objectName)
  615. c.Assert(err, IsNil)
  616. testLogger.Println("GetObjectACL:", acl)
  617. c.Assert(acl.ACL, Equals, string(ACLPublicRead))
  618. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  619. c.Assert(err, IsNil)
  620. testLogger.Println("GetObjectDetailedMeta:", meta)
  621. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  622. err = s.bucket.DeleteObject(objectName)
  623. c.Assert(err, IsNil)
  624. os.Remove(newFile)
  625. }
  626. // TestPutObjectFromFile
  627. func (s *OssBucketSuite) TestPutObjectFromFileType(c *C) {
  628. objectName := objectNamePrefix + "tpoffwm"
  629. localFile := "../sample/BingWallpaper-2015-11-07.jpg"
  630. newFile := "newpic11.jpg"
  631. // Put
  632. err := s.bucket.PutObjectFromFile(objectName, localFile)
  633. c.Assert(err, IsNil)
  634. // Check
  635. err = s.bucket.GetObjectToFile(objectName, newFile)
  636. c.Assert(err, IsNil)
  637. eq, err := compareFiles(localFile, newFile)
  638. c.Assert(err, IsNil)
  639. c.Assert(eq, Equals, true)
  640. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  641. c.Assert(err, IsNil)
  642. c.Assert(meta.Get("Content-Type"), Equals, "image/jpeg")
  643. err = s.bucket.DeleteObject(objectName)
  644. c.Assert(err, IsNil)
  645. os.Remove(newFile)
  646. }
  647. // TestGetObject
  648. func (s *OssBucketSuite) TestGetObject(c *C) {
  649. objectName := objectNamePrefix + "tgo"
  650. objectValue := "长忆观潮,满郭人争江上望。来疑沧海尽成空,万面鼓声中。弄潮儿向涛头立,手把红旗旗不湿。别来几向梦中看,梦觉尚心寒。"
  651. // Put
  652. err := s.bucket.PutObject(objectName, strings.NewReader(objectValue))
  653. c.Assert(err, IsNil)
  654. // Check
  655. body, err := s.bucket.GetObject(objectName)
  656. c.Assert(err, IsNil)
  657. data, err := ioutil.ReadAll(body)
  658. body.Close()
  659. str := string(data)
  660. c.Assert(str, Equals, objectValue)
  661. testLogger.Println("GetObjec:", str)
  662. // Range
  663. var subObjectValue = string(([]byte(objectValue))[15:36])
  664. body, err = s.bucket.GetObject(objectName, Range(15, 35))
  665. c.Assert(err, IsNil)
  666. data, err = ioutil.ReadAll(body)
  667. body.Close()
  668. str = string(data)
  669. c.Assert(str, Equals, subObjectValue)
  670. testLogger.Println("GetObject:", str, ",", subObjectValue)
  671. // If-Modified-Since
  672. _, err = s.bucket.GetObject(objectName, IfModifiedSince(futureDate))
  673. c.Assert(err, NotNil)
  674. // If-Unmodified-Since
  675. body, err = s.bucket.GetObject(objectName, IfUnmodifiedSince(futureDate))
  676. c.Assert(err, IsNil)
  677. data, err = ioutil.ReadAll(body)
  678. body.Close()
  679. c.Assert(string(data), Equals, objectValue)
  680. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  681. c.Assert(err, IsNil)
  682. // If-Match
  683. body, err = s.bucket.GetObject(objectName, IfMatch(meta.Get("Etag")))
  684. c.Assert(err, IsNil)
  685. data, err = ioutil.ReadAll(body)
  686. body.Close()
  687. c.Assert(string(data), Equals, objectValue)
  688. // If-None-Match
  689. _, err = s.bucket.GetObject(objectName, IfNoneMatch(meta.Get("Etag")))
  690. c.Assert(err, NotNil)
  691. err = s.bucket.DeleteObject(objectName)
  692. c.Assert(err, IsNil)
  693. }
  694. // TestGetObjectNegative
  695. func (s *OssBucketSuite) TestGetObjectToWriterNegative(c *C) {
  696. objectName := objectNamePrefix + "tgotwn"
  697. objectValue := "长忆观潮,满郭人争江上望。"
  698. // object not exist
  699. _, err := s.bucket.GetObject("NotExist")
  700. c.Assert(err, NotNil)
  701. // constraint invalid
  702. err = s.bucket.PutObject(objectName, strings.NewReader(objectValue))
  703. c.Assert(err, IsNil)
  704. // out of range
  705. _, err = s.bucket.GetObject(objectName, Range(15, 1000))
  706. c.Assert(err, IsNil)
  707. // no exist
  708. err = s.bucket.GetObjectToFile(objectName, "/root/123abc9874")
  709. c.Assert(err, NotNil)
  710. // invalid option
  711. _, err = s.bucket.GetObject(objectName, ACL(ACLPublicRead))
  712. c.Assert(err, IsNil)
  713. err = s.bucket.GetObjectToFile(objectName, "newpic15.jpg", ACL(ACLPublicRead))
  714. c.Assert(err, IsNil)
  715. err = s.bucket.DeleteObject(objectName)
  716. c.Assert(err, IsNil)
  717. }
  718. // TestGetObjectToFile
  719. func (s *OssBucketSuite) TestGetObjectToFile(c *C) {
  720. objectName := objectNamePrefix + "tgotf"
  721. objectValue := "江南好,风景旧曾谙;日出江花红胜火,春来江水绿如蓝。能不忆江南?江南忆,最忆是杭州;山寺月中寻桂子,郡亭枕上看潮头。何日更重游!"
  722. newFile := "newpic15.jpg"
  723. // Put
  724. var val = []byte(objectValue)
  725. err := s.bucket.PutObject(objectName, strings.NewReader(objectValue))
  726. c.Assert(err, IsNil)
  727. // Check
  728. err = s.bucket.GetObjectToFile(objectName, newFile)
  729. c.Assert(err, IsNil)
  730. eq, err := compareFileData(newFile, val)
  731. c.Assert(err, IsNil)
  732. c.Assert(eq, Equals, true)
  733. os.Remove(newFile)
  734. // Range
  735. err = s.bucket.GetObjectToFile(objectName, newFile, Range(15, 35))
  736. c.Assert(err, IsNil)
  737. eq, err = compareFileData(newFile, val[15:36])
  738. c.Assert(err, IsNil)
  739. c.Assert(eq, Equals, true)
  740. os.Remove(newFile)
  741. err = s.bucket.GetObjectToFile(objectName, newFile, NormalizedRange("15-35"))
  742. c.Assert(err, IsNil)
  743. eq, err = compareFileData(newFile, val[15:36])
  744. c.Assert(err, IsNil)
  745. c.Assert(eq, Equals, true)
  746. os.Remove(newFile)
  747. err = s.bucket.GetObjectToFile(objectName, newFile, NormalizedRange("15-"))
  748. c.Assert(err, IsNil)
  749. eq, err = compareFileData(newFile, val[15:])
  750. c.Assert(err, IsNil)
  751. c.Assert(eq, Equals, true)
  752. os.Remove(newFile)
  753. err = s.bucket.GetObjectToFile(objectName, newFile, NormalizedRange("-10"))
  754. c.Assert(err, IsNil)
  755. eq, err = compareFileData(newFile, val[(len(val)-10):len(val)])
  756. c.Assert(err, IsNil)
  757. c.Assert(eq, Equals, true)
  758. os.Remove(newFile)
  759. // If-Modified-Since
  760. err = s.bucket.GetObjectToFile(objectName, newFile, IfModifiedSince(futureDate))
  761. c.Assert(err, NotNil)
  762. // If-Unmodified-Since
  763. err = s.bucket.GetObjectToFile(objectName, newFile, IfUnmodifiedSince(futureDate))
  764. c.Assert(err, IsNil)
  765. eq, err = compareFileData(newFile, val)
  766. c.Assert(err, IsNil)
  767. c.Assert(eq, Equals, true)
  768. os.Remove(newFile)
  769. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  770. c.Assert(err, IsNil)
  771. testLogger.Println("GetObjectDetailedMeta:", meta)
  772. // If-Match
  773. err = s.bucket.GetObjectToFile(objectName, newFile, IfMatch(meta.Get("Etag")))
  774. c.Assert(err, IsNil)
  775. eq, err = compareFileData(newFile, val)
  776. c.Assert(err, IsNil)
  777. c.Assert(eq, Equals, true)
  778. os.Remove(newFile)
  779. // If-None-Match
  780. err = s.bucket.GetObjectToFile(objectName, newFile, IfNoneMatch(meta.Get("Etag")))
  781. c.Assert(err, NotNil)
  782. err = s.bucket.DeleteObject(objectName)
  783. c.Assert(err, IsNil)
  784. }
  785. // TestListObjects
  786. func (s *OssBucketSuite) TestListObjects(c *C) {
  787. objectName := objectNamePrefix + "tlo"
  788. // list empty bucket
  789. lor, err := s.bucket.ListObjects()
  790. c.Assert(err, IsNil)
  791. left := len(lor.Objects)
  792. // Put three object
  793. err = s.bucket.PutObject(objectName+"1", strings.NewReader(""))
  794. c.Assert(err, IsNil)
  795. err = s.bucket.PutObject(objectName+"2", strings.NewReader(""))
  796. c.Assert(err, IsNil)
  797. err = s.bucket.PutObject(objectName+"3", strings.NewReader(""))
  798. c.Assert(err, IsNil)
  799. // list
  800. lor, err = s.bucket.ListObjects()
  801. c.Assert(err, IsNil)
  802. c.Assert(len(lor.Objects), Equals, left+3)
  803. // list with prefix
  804. lor, err = s.bucket.ListObjects(Prefix(objectName + "2"))
  805. c.Assert(err, IsNil)
  806. c.Assert(len(lor.Objects), Equals, 1)
  807. lor, err = s.bucket.ListObjects(Prefix(objectName + "22"))
  808. c.Assert(err, IsNil)
  809. c.Assert(len(lor.Objects), Equals, 0)
  810. // list with max keys
  811. lor, err = s.bucket.ListObjects(Prefix(objectName), MaxKeys(2))
  812. c.Assert(err, IsNil)
  813. c.Assert(len(lor.Objects), Equals, 2)
  814. // list with marker
  815. lor, err = s.bucket.ListObjects(Marker(objectName+"1"), MaxKeys(1))
  816. c.Assert(err, IsNil)
  817. c.Assert(len(lor.Objects), Equals, 1)
  818. err = s.bucket.DeleteObject(objectName + "1")
  819. c.Assert(err, IsNil)
  820. err = s.bucket.DeleteObject(objectName + "2")
  821. c.Assert(err, IsNil)
  822. err = s.bucket.DeleteObject(objectName + "3")
  823. c.Assert(err, IsNil)
  824. }
  825. // TestListObjects
  826. func (s *OssBucketSuite) TestListObjectsEncodingType(c *C) {
  827. objectName := objectNamePrefix + "床前明月光,疑是地上霜。举头望明月,低头思故乡。" + "tloet"
  828. for i := 0; i < 10; i++ {
  829. err := s.bucket.PutObject(objectName+strconv.Itoa(i), strings.NewReader(""))
  830. c.Assert(err, IsNil)
  831. }
  832. lor, err := s.bucket.ListObjects(Prefix(objectNamePrefix + "床前明月光,"))
  833. c.Assert(err, IsNil)
  834. c.Assert(len(lor.Objects), Equals, 10)
  835. lor, err = s.bucket.ListObjects(Prefix(objectNamePrefix + "床前明月光,"))
  836. c.Assert(err, IsNil)
  837. c.Assert(len(lor.Objects), Equals, 10)
  838. lor, err = s.bucket.ListObjects(Marker(objectNamePrefix + "床前明月光,疑是地上霜。举头望明月,低头思故乡。"))
  839. c.Assert(err, IsNil)
  840. c.Assert(len(lor.Objects), Equals, 10)
  841. lor, err = s.bucket.ListObjects(Prefix(objectNamePrefix + "床前明月光"))
  842. c.Assert(err, IsNil)
  843. for i, obj := range lor.Objects {
  844. c.Assert(obj.Key, Equals, objectNamePrefix+"床前明月光,疑是地上霜。举头望明月,低头思故乡。tloet"+strconv.Itoa(i))
  845. }
  846. for i := 0; i < 10; i++ {
  847. err = s.bucket.DeleteObject(objectName + strconv.Itoa(i))
  848. c.Assert(err, IsNil)
  849. }
  850. // 特殊字符
  851. objectName = "go go ` ~ ! @ # $ % ^ & * () - _ + =[] {} \\ | < > , . ? / 0"
  852. err = s.bucket.PutObject(objectName, strings.NewReader("明月几时有,把酒问青天"))
  853. c.Assert(err, IsNil)
  854. lor, err = s.bucket.ListObjects(Prefix(objectName))
  855. c.Assert(err, IsNil)
  856. c.Assert(len(lor.Objects), Equals, 1)
  857. err = s.bucket.DeleteObject(objectName)
  858. c.Assert(err, IsNil)
  859. objectName = "go/中国 日本 +-#&=*"
  860. err = s.bucket.PutObject(objectName, strings.NewReader("明月几时有,把酒问青天"))
  861. c.Assert(err, IsNil)
  862. lor, err = s.bucket.ListObjects(Prefix(objectName))
  863. c.Assert(err, IsNil)
  864. c.Assert(len(lor.Objects), Equals, 1)
  865. err = s.bucket.DeleteObject(objectName)
  866. c.Assert(err, IsNil)
  867. }
  868. // TestIsBucketExist
  869. func (s *OssBucketSuite) TestIsObjectExist(c *C) {
  870. objectName := objectNamePrefix + "tibe"
  871. // Put three object
  872. err := s.bucket.PutObject(objectName+"1", strings.NewReader(""))
  873. c.Assert(err, IsNil)
  874. err = s.bucket.PutObject(objectName+"11", strings.NewReader(""))
  875. c.Assert(err, IsNil)
  876. err = s.bucket.PutObject(objectName+"111", strings.NewReader(""))
  877. c.Assert(err, IsNil)
  878. // exist
  879. exist, err := s.bucket.IsObjectExist(objectName + "11")
  880. c.Assert(err, IsNil)
  881. c.Assert(exist, Equals, true)
  882. exist, err = s.bucket.IsObjectExist(objectName + "1")
  883. c.Assert(err, IsNil)
  884. c.Assert(exist, Equals, true)
  885. exist, err = s.bucket.IsObjectExist(objectName + "111")
  886. c.Assert(err, IsNil)
  887. c.Assert(exist, Equals, true)
  888. // not exist
  889. exist, err = s.bucket.IsObjectExist(objectName + "1111")
  890. c.Assert(err, IsNil)
  891. c.Assert(exist, Equals, false)
  892. exist, err = s.bucket.IsObjectExist(objectName)
  893. c.Assert(err, IsNil)
  894. c.Assert(exist, Equals, false)
  895. err = s.bucket.DeleteObject(objectName + "1")
  896. c.Assert(err, IsNil)
  897. err = s.bucket.DeleteObject(objectName + "11")
  898. c.Assert(err, IsNil)
  899. err = s.bucket.DeleteObject(objectName + "111")
  900. c.Assert(err, IsNil)
  901. }
  902. // TestDeleteObject
  903. func (s *OssBucketSuite) TestDeleteObject(c *C) {
  904. objectName := objectNamePrefix + "tdo"
  905. err := s.bucket.PutObject(objectName, strings.NewReader(""))
  906. c.Assert(err, IsNil)
  907. lor, err := s.bucket.ListObjects(Prefix(objectName))
  908. c.Assert(err, IsNil)
  909. c.Assert(len(lor.Objects), Equals, 1)
  910. // delete
  911. err = s.bucket.DeleteObject(objectName)
  912. c.Assert(err, IsNil)
  913. // duplicate delete
  914. err = s.bucket.DeleteObject(objectName)
  915. c.Assert(err, IsNil)
  916. lor, err = s.bucket.ListObjects(Prefix(objectName))
  917. c.Assert(err, IsNil)
  918. c.Assert(len(lor.Objects), Equals, 0)
  919. }
  920. // TestDeleteObjects
  921. func (s *OssBucketSuite) TestDeleteObjects(c *C) {
  922. objectName := objectNamePrefix + "tdos"
  923. // delete object
  924. err := s.bucket.PutObject(objectName, strings.NewReader(""))
  925. c.Assert(err, IsNil)
  926. res, err := s.bucket.DeleteObjects([]string{objectName})
  927. c.Assert(err, IsNil)
  928. c.Assert(len(res.DeletedObjects), Equals, 1)
  929. lor, err := s.bucket.ListObjects(Prefix(objectName))
  930. c.Assert(err, IsNil)
  931. c.Assert(len(lor.Objects), Equals, 0)
  932. // delete objects
  933. err = s.bucket.PutObject(objectName+"1", strings.NewReader(""))
  934. c.Assert(err, IsNil)
  935. err = s.bucket.PutObject(objectName+"2", strings.NewReader(""))
  936. c.Assert(err, IsNil)
  937. res, err = s.bucket.DeleteObjects([]string{objectName + "1", objectName + "2"})
  938. c.Assert(err, IsNil)
  939. c.Assert(len(res.DeletedObjects), Equals, 2)
  940. lor, err = s.bucket.ListObjects(Prefix(objectName))
  941. c.Assert(err, IsNil)
  942. c.Assert(len(lor.Objects), Equals, 0)
  943. // delete 0
  944. _, err = s.bucket.DeleteObjects([]string{})
  945. c.Assert(err, NotNil)
  946. // DeleteObjectsQuiet
  947. err = s.bucket.PutObject(objectName+"1", strings.NewReader(""))
  948. c.Assert(err, IsNil)
  949. err = s.bucket.PutObject(objectName+"2", strings.NewReader(""))
  950. c.Assert(err, IsNil)
  951. res, err = s.bucket.DeleteObjects([]string{objectName + "1", objectName + "2"},
  952. DeleteObjectsQuiet(false))
  953. c.Assert(err, IsNil)
  954. c.Assert(len(res.DeletedObjects), Equals, 2)
  955. lor, err = s.bucket.ListObjects(Prefix(objectName))
  956. c.Assert(err, IsNil)
  957. c.Assert(len(lor.Objects), Equals, 0)
  958. // DeleteObjectsQuiet
  959. err = s.bucket.PutObject(objectName+"1", strings.NewReader(""))
  960. c.Assert(err, IsNil)
  961. err = s.bucket.PutObject(objectName+"2", strings.NewReader(""))
  962. c.Assert(err, IsNil)
  963. res, err = s.bucket.DeleteObjects([]string{objectName + "1", objectName + "2"},
  964. DeleteObjectsQuiet(true))
  965. c.Assert(err, IsNil)
  966. c.Assert(len(res.DeletedObjects), Equals, 0)
  967. lor, err = s.bucket.ListObjects(Prefix(objectName))
  968. c.Assert(err, IsNil)
  969. c.Assert(len(lor.Objects), Equals, 0)
  970. // EncodingType
  971. err = s.bucket.PutObject("中国人", strings.NewReader(""))
  972. c.Assert(err, IsNil)
  973. res, err = s.bucket.DeleteObjects([]string{"中国人"})
  974. c.Assert(err, IsNil)
  975. c.Assert(len(res.DeletedObjects), Equals, 1)
  976. c.Assert(res.DeletedObjects[0], Equals, "中国人")
  977. // EncodingType
  978. err = s.bucket.PutObject("中国人", strings.NewReader(""))
  979. c.Assert(err, IsNil)
  980. res, err = s.bucket.DeleteObjects([]string{"中国人"}, DeleteObjectsQuiet(false))
  981. c.Assert(err, IsNil)
  982. c.Assert(len(res.DeletedObjects), Equals, 1)
  983. c.Assert(res.DeletedObjects[0], Equals, "中国人")
  984. // EncodingType
  985. err = s.bucket.PutObject("中国人", strings.NewReader(""))
  986. c.Assert(err, IsNil)
  987. res, err = s.bucket.DeleteObjects([]string{"中国人"}, DeleteObjectsQuiet(true))
  988. c.Assert(err, IsNil)
  989. c.Assert(len(res.DeletedObjects), Equals, 0)
  990. // 特殊字符
  991. key := "A ' < > \" & ~ ` ! @ # $ % ^ & * ( ) [] {} - _ + = / | \\ ? . , : ; A"
  992. err = s.bucket.PutObject(key, strings.NewReader("value"))
  993. c.Assert(err, IsNil)
  994. _, err = s.bucket.DeleteObjects([]string{key})
  995. c.Assert(err, IsNil)
  996. ress, err := s.bucket.ListObjects(Prefix(key))
  997. c.Assert(err, IsNil)
  998. c.Assert(len(ress.Objects), Equals, 0)
  999. // not exist
  1000. _, err = s.bucket.DeleteObjects([]string{"NotExistObject"})
  1001. c.Assert(err, IsNil)
  1002. }
  1003. // TestSetObjectMeta
  1004. func (s *OssBucketSuite) TestSetObjectMeta(c *C) {
  1005. objectName := objectNamePrefix + "tsom"
  1006. err := s.bucket.PutObject(objectName, strings.NewReader(""))
  1007. c.Assert(err, IsNil)
  1008. err = s.bucket.SetObjectMeta(objectName,
  1009. Expires(futureDate),
  1010. Meta("myprop", "mypropval"))
  1011. c.Assert(err, IsNil)
  1012. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  1013. c.Assert(err, IsNil)
  1014. testLogger.Println("Meta:", meta)
  1015. c.Assert(meta.Get("Expires"), Equals, futureDate.Format(http.TimeFormat))
  1016. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  1017. acl, err := s.bucket.GetObjectACL(objectName)
  1018. c.Assert(err, IsNil)
  1019. c.Assert(acl.ACL, Equals, "default")
  1020. // invalid option
  1021. err = s.bucket.SetObjectMeta(objectName, AcceptEncoding("url"))
  1022. c.Assert(err, IsNil)
  1023. // invalid option value
  1024. err = s.bucket.SetObjectMeta(objectName, ServerSideEncryption("invalid"))
  1025. c.Assert(err, NotNil)
  1026. err = s.bucket.DeleteObject(objectName)
  1027. c.Assert(err, IsNil)
  1028. // no exist
  1029. err = s.bucket.SetObjectMeta(objectName, Expires(futureDate))
  1030. c.Assert(err, NotNil)
  1031. }
  1032. // TestGetObjectMeta
  1033. func (s *OssBucketSuite) TestGetObjectMeta(c *C) {
  1034. objectName := objectNamePrefix + "tgom"
  1035. // Put
  1036. err := s.bucket.PutObject(objectName, strings.NewReader(""))
  1037. c.Assert(err, IsNil)
  1038. meta, err := s.bucket.GetObjectMeta(objectName)
  1039. c.Assert(err, IsNil)
  1040. c.Assert(len(meta) > 0, Equals, true)
  1041. err = s.bucket.DeleteObject(objectName)
  1042. c.Assert(err, IsNil)
  1043. _, err = s.bucket.GetObjectMeta("NotExistObject")
  1044. c.Assert(err, NotNil)
  1045. }
  1046. // TestGetObjectDetailedMeta
  1047. func (s *OssBucketSuite) TestGetObjectDetailedMeta(c *C) {
  1048. objectName := objectNamePrefix + "tgodm"
  1049. // Put
  1050. err := s.bucket.PutObject(objectName, strings.NewReader(""),
  1051. Expires(futureDate), Meta("myprop", "mypropval"))
  1052. c.Assert(err, IsNil)
  1053. // Check
  1054. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  1055. c.Assert(err, IsNil)
  1056. testLogger.Println("GetObjectDetailedMeta:", meta)
  1057. c.Assert(meta.Get("Expires"), Equals, futureDate.Format(http.TimeFormat))
  1058. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  1059. c.Assert(meta.Get("Content-Length"), Equals, "0")
  1060. c.Assert(len(meta.Get("Date")) > 0, Equals, true)
  1061. c.Assert(len(meta.Get("X-Oss-Request-Id")) > 0, Equals, true)
  1062. c.Assert(len(meta.Get("Last-Modified")) > 0, Equals, true)
  1063. // IfModifiedSince/IfModifiedSince
  1064. _, err = s.bucket.GetObjectDetailedMeta(objectName, IfModifiedSince(futureDate))
  1065. c.Assert(err, NotNil)
  1066. meta, err = s.bucket.GetObjectDetailedMeta(objectName, IfUnmodifiedSince(futureDate))
  1067. c.Assert(err, IsNil)
  1068. c.Assert(meta.Get("Expires"), Equals, futureDate.Format(http.TimeFormat))
  1069. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  1070. // IfMatch/IfNoneMatch
  1071. _, err = s.bucket.GetObjectDetailedMeta(objectName, IfNoneMatch(meta.Get("Etag")))
  1072. c.Assert(err, NotNil)
  1073. meta, err = s.bucket.GetObjectDetailedMeta(objectName, IfMatch(meta.Get("Etag")))
  1074. c.Assert(err, IsNil)
  1075. c.Assert(meta.Get("Expires"), Equals, futureDate.Format(http.TimeFormat))
  1076. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  1077. err = s.bucket.DeleteObject(objectName)
  1078. c.Assert(err, IsNil)
  1079. _, err = s.bucket.GetObjectDetailedMeta("NotExistObject")
  1080. c.Assert(err, NotNil)
  1081. }
  1082. // TestSetAndGetObjectAcl
  1083. func (s *OssBucketSuite) TestSetAndGetObjectAcl(c *C) {
  1084. objectName := objectNamePrefix + "tsgba"
  1085. err := s.bucket.PutObject(objectName, strings.NewReader(""))
  1086. c.Assert(err, IsNil)
  1087. // default
  1088. acl, err := s.bucket.GetObjectACL(objectName)
  1089. c.Assert(err, IsNil)
  1090. c.Assert(acl.ACL, Equals, "default")
  1091. // set ACL_PUBLIC_RW
  1092. err = s.bucket.SetObjectACL(objectName, ACLPublicReadWrite)
  1093. c.Assert(err, IsNil)
  1094. acl, err = s.bucket.GetObjectACL(objectName)
  1095. c.Assert(err, IsNil)
  1096. c.Assert(acl.ACL, Equals, string(ACLPublicReadWrite))
  1097. // set ACL_PRIVATE
  1098. err = s.bucket.SetObjectACL(objectName, ACLPrivate)
  1099. c.Assert(err, IsNil)
  1100. acl, err = s.bucket.GetObjectACL(objectName)
  1101. c.Assert(err, IsNil)
  1102. c.Assert(acl.ACL, Equals, string(ACLPrivate))
  1103. // set ACL_PUBLIC_R
  1104. err = s.bucket.SetObjectACL(objectName, ACLPublicRead)
  1105. c.Assert(err, IsNil)
  1106. acl, err = s.bucket.GetObjectACL(objectName)
  1107. c.Assert(err, IsNil)
  1108. c.Assert(acl.ACL, Equals, string(ACLPublicRead))
  1109. err = s.bucket.DeleteObject(objectName)
  1110. c.Assert(err, IsNil)
  1111. }
  1112. // TestSetAndGetObjectAclNegative
  1113. func (s *OssBucketSuite) TestSetAndGetObjectAclNegative(c *C) {
  1114. objectName := objectNamePrefix + "tsgban"
  1115. // object not exist
  1116. err := s.bucket.SetObjectACL(objectName, ACLPublicRead)
  1117. c.Assert(err, NotNil)
  1118. }
  1119. // TestCopyObject
  1120. func (s *OssBucketSuite) TestCopyObject(c *C) {
  1121. objectName := objectNamePrefix + "tco"
  1122. objectValue := "男儿何不带吴钩,收取关山五十州。请君暂上凌烟阁,若个书生万户侯?"
  1123. err := s.bucket.PutObject(objectName, strings.NewReader(objectValue),
  1124. ACL(ACLPublicRead), Meta("my", "myprop"))
  1125. c.Assert(err, IsNil)
  1126. // copy
  1127. var objectNameDest = objectName + "dest"
  1128. _, err = s.bucket.CopyObject(objectName, objectNameDest)
  1129. c.Assert(err, IsNil)
  1130. // check
  1131. lor, err := s.bucket.ListObjects(Prefix(objectName))
  1132. c.Assert(err, IsNil)
  1133. testLogger.Println("objects:", lor.Objects)
  1134. c.Assert(len(lor.Objects), Equals, 2)
  1135. body, err := s.bucket.GetObject(objectName)
  1136. c.Assert(err, IsNil)
  1137. str, err := readBody(body)
  1138. c.Assert(err, IsNil)
  1139. c.Assert(str, Equals, objectValue)
  1140. err = s.bucket.DeleteObject(objectNameDest)
  1141. c.Assert(err, IsNil)
  1142. // copy with constraints x-oss-copy-source-if-modified-since
  1143. _, err = s.bucket.CopyObject(objectName, objectNameDest, CopySourceIfModifiedSince(futureDate))
  1144. c.Assert(err, NotNil)
  1145. testLogger.Println("CopyObject:", err)
  1146. // copy with constraints x-oss-copy-source-if-unmodified-since
  1147. _, err = s.bucket.CopyObject(objectName, objectNameDest, CopySourceIfUnmodifiedSince(futureDate))
  1148. c.Assert(err, IsNil)
  1149. // check
  1150. lor, err = s.bucket.ListObjects(Prefix(objectName))
  1151. c.Assert(err, IsNil)
  1152. testLogger.Println("objects:", lor.Objects)
  1153. c.Assert(len(lor.Objects), Equals, 2)
  1154. body, err = s.bucket.GetObject(objectName)
  1155. c.Assert(err, IsNil)
  1156. str, err = readBody(body)
  1157. c.Assert(err, IsNil)
  1158. c.Assert(str, Equals, objectValue)
  1159. err = s.bucket.DeleteObject(objectNameDest)
  1160. c.Assert(err, IsNil)
  1161. // copy with constraints x-oss-copy-source-if-match
  1162. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  1163. c.Assert(err, IsNil)
  1164. testLogger.Println("GetObjectDetailedMeta:", meta)
  1165. _, err = s.bucket.CopyObject(objectName, objectNameDest, CopySourceIfMatch(meta.Get("Etag")))
  1166. c.Assert(err, IsNil)
  1167. // check
  1168. body, err = s.bucket.GetObject(objectName)
  1169. c.Assert(err, IsNil)
  1170. str, err = readBody(body)
  1171. c.Assert(err, IsNil)
  1172. c.Assert(str, Equals, objectValue)
  1173. err = s.bucket.DeleteObject(objectNameDest)
  1174. c.Assert(err, IsNil)
  1175. // copy with constraints x-oss-copy-source-if-none-match
  1176. _, err = s.bucket.CopyObject(objectName, objectNameDest, CopySourceIfNoneMatch(meta.Get("Etag")))
  1177. c.Assert(err, NotNil)
  1178. // copy with constraints x-oss-metadata-directive
  1179. _, err = s.bucket.CopyObject(objectName, objectNameDest, Meta("my", "mydestprop"),
  1180. MetadataDirective(MetaCopy))
  1181. c.Assert(err, IsNil)
  1182. // check
  1183. body, err = s.bucket.GetObject(objectName)
  1184. c.Assert(err, IsNil)
  1185. str, err = readBody(body)
  1186. c.Assert(err, IsNil)
  1187. c.Assert(str, Equals, objectValue)
  1188. destMeta, err := s.bucket.GetObjectDetailedMeta(objectNameDest)
  1189. c.Assert(err, IsNil)
  1190. c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
  1191. acl, err := s.bucket.GetObjectACL(objectNameDest)
  1192. c.Assert(err, IsNil)
  1193. c.Assert(acl.ACL, Equals, "default")
  1194. err = s.bucket.DeleteObject(objectNameDest)
  1195. c.Assert(err, IsNil)
  1196. // copy with constraints x-oss-metadata-directive and self defined dest object meta
  1197. options := []Option{
  1198. ObjectACL(ACLPublicReadWrite),
  1199. Meta("my", "mydestprop"),
  1200. MetadataDirective(MetaReplace),
  1201. }
  1202. _, err = s.bucket.CopyObject(objectName, objectNameDest, options...)
  1203. c.Assert(err, IsNil)
  1204. // check
  1205. body, err = s.bucket.GetObject(objectName)
  1206. c.Assert(err, IsNil)
  1207. str, err = readBody(body)
  1208. c.Assert(err, IsNil)
  1209. c.Assert(str, Equals, objectValue)
  1210. destMeta, err = s.bucket.GetObjectDetailedMeta(objectNameDest)
  1211. c.Assert(err, IsNil)
  1212. c.Assert(destMeta.Get("X-Oss-Meta-My"), Equals, "mydestprop")
  1213. acl, err = s.bucket.GetObjectACL(objectNameDest)
  1214. c.Assert(err, IsNil)
  1215. c.Assert(acl.ACL, Equals, string(ACLPublicReadWrite))
  1216. err = s.bucket.DeleteObject(objectNameDest)
  1217. c.Assert(err, IsNil)
  1218. err = s.bucket.DeleteObject(objectName)
  1219. c.Assert(err, IsNil)
  1220. }
  1221. // TestCopyObjectToOrFrom
  1222. func (s *OssBucketSuite) TestCopyObjectToOrFrom(c *C) {
  1223. objectName := objectNamePrefix + "tcotof"
  1224. objectValue := "男儿何不带吴钩,收取关山五十州。请君暂上凌烟阁,若个书生万户侯?"
  1225. destBucket := bucketName + "-dest"
  1226. objectNameDest := objectName + "dest"
  1227. s.client.CreateBucket(destBucket)
  1228. destBuck, err := s.client.Bucket(destBucket)
  1229. c.Assert(err, IsNil)
  1230. err = s.bucket.PutObject(objectName, strings.NewReader(objectValue))
  1231. c.Assert(err, IsNil)
  1232. // copy from
  1233. _, err = destBuck.CopyObjectFrom(bucketName, objectName, objectNameDest)
  1234. c.Assert(err, IsNil)
  1235. // check
  1236. body, err := destBuck.GetObject(objectNameDest)
  1237. c.Assert(err, IsNil)
  1238. str, err := readBody(body)
  1239. c.Assert(err, IsNil)
  1240. c.Assert(str, Equals, objectValue)
  1241. err = s.bucket.DeleteObject(objectName)
  1242. c.Assert(err, IsNil)
  1243. // copy to
  1244. _, err = destBuck.CopyObjectTo(bucketName, objectName, objectNameDest)
  1245. c.Assert(err, IsNil)
  1246. // check
  1247. body, err = s.bucket.GetObject(objectName)
  1248. c.Assert(err, IsNil)
  1249. str, err = readBody(body)
  1250. c.Assert(err, IsNil)
  1251. c.Assert(str, Equals, objectValue)
  1252. // clean
  1253. err = destBuck.DeleteObject(objectNameDest)
  1254. c.Assert(err, IsNil)
  1255. err = s.bucket.DeleteObject(objectName)
  1256. c.Assert(err, IsNil)
  1257. err = s.client.DeleteBucket(destBucket)
  1258. c.Assert(err, IsNil)
  1259. }
  1260. // TestCopyObjectToOrFromNegative
  1261. func (s *OssBucketSuite) TestCopyObjectToOrFromNegative(c *C) {
  1262. objectName := objectNamePrefix + "tcotofn"
  1263. destBucket := bucketName + "-destn"
  1264. objectNameDest := objectName + "destn"
  1265. // object no exist
  1266. _, err := s.bucket.CopyObjectTo(bucketName, objectName, objectNameDest)
  1267. c.Assert(err, NotNil)
  1268. // bucket no exist
  1269. _, err = s.bucket.CopyObjectFrom(destBucket, objectNameDest, objectName)
  1270. c.Assert(err, NotNil)
  1271. }
  1272. // TestAppendObject
  1273. func (s *OssBucketSuite) TestAppendObject(c *C) {
  1274. objectName := objectNamePrefix + "tao"
  1275. objectValue := "昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。"
  1276. var val = []byte(objectValue)
  1277. var localFile = "testx.txt"
  1278. var nextPos int64
  1279. var midPos = 1 + rand.Intn(len(val)-1)
  1280. var err = createFileAndWrite(localFile+"1", val[0:midPos])
  1281. c.Assert(err, IsNil)
  1282. err = createFileAndWrite(localFile+"2", val[midPos:])
  1283. c.Assert(err, IsNil)
  1284. // string append
  1285. nextPos, err = s.bucket.AppendObject(objectName, strings.NewReader("昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,"), nextPos)
  1286. c.Assert(err, IsNil)
  1287. nextPos, err = s.bucket.AppendObject(objectName, strings.NewReader("却道海棠依旧。知否?知否?应是绿肥红瘦。"), nextPos)
  1288. c.Assert(err, IsNil)
  1289. body, err := s.bucket.GetObject(objectName)
  1290. c.Assert(err, IsNil)
  1291. str, err := readBody(body)
  1292. c.Assert(err, IsNil)
  1293. c.Assert(str, Equals, objectValue)
  1294. err = s.bucket.DeleteObject(objectName)
  1295. c.Assert(err, IsNil)
  1296. // byte append
  1297. nextPos = 0
  1298. nextPos, err = s.bucket.AppendObject(objectName, bytes.NewReader(val[0:midPos]), nextPos)
  1299. c.Assert(err, IsNil)
  1300. nextPos, err = s.bucket.AppendObject(objectName, bytes.NewReader(val[midPos:]), nextPos)
  1301. c.Assert(err, IsNil)
  1302. body, err = s.bucket.GetObject(objectName)
  1303. c.Assert(err, IsNil)
  1304. str, err = readBody(body)
  1305. c.Assert(err, IsNil)
  1306. c.Assert(str, Equals, objectValue)
  1307. err = s.bucket.DeleteObject(objectName)
  1308. c.Assert(err, IsNil)
  1309. // file append
  1310. options := []Option{
  1311. ObjectACL(ACLPublicReadWrite),
  1312. Meta("my", "myprop"),
  1313. }
  1314. fd, err := os.Open(localFile + "1")
  1315. c.Assert(err, IsNil)
  1316. defer fd.Close()
  1317. nextPos = 0
  1318. nextPos, err = s.bucket.AppendObject(objectName, fd, nextPos, options...)
  1319. c.Assert(err, IsNil)
  1320. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  1321. c.Assert(err, IsNil)
  1322. testLogger.Println("GetObjectDetailedMeta:", meta, ",", nextPos)
  1323. c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Appendable")
  1324. c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
  1325. c.Assert(meta.Get("x-oss-Meta-Mine"), Equals, "")
  1326. c.Assert(meta.Get("X-Oss-Next-Append-Position"), Equals, strconv.FormatInt(nextPos, 10))
  1327. acl, err := s.bucket.GetObjectACL(objectName)
  1328. c.Assert(err, IsNil)
  1329. testLogger.Println("GetObjectACL:", acl)
  1330. c.Assert(acl.ACL, Equals, string(ACLPublicReadWrite))
  1331. // second append
  1332. options = []Option{
  1333. ObjectACL(ACLPublicRead),
  1334. Meta("my", "myproptwo"),
  1335. Meta("mine", "mypropmine"),
  1336. }
  1337. fd, err = os.Open(localFile + "2")
  1338. c.Assert(err, IsNil)
  1339. defer fd.Close()
  1340. nextPos, err = s.bucket.AppendObject(objectName, fd, nextPos, options...)
  1341. c.Assert(err, IsNil)
  1342. body, err = s.bucket.GetObject(objectName)
  1343. c.Assert(err, IsNil)
  1344. str, err = readBody(body)
  1345. c.Assert(err, IsNil)
  1346. c.Assert(str, Equals, objectValue)
  1347. meta, err = s.bucket.GetObjectDetailedMeta(objectName)
  1348. c.Assert(err, IsNil)
  1349. testLogger.Println("GetObjectDetailedMeta xxx:", meta)
  1350. c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Appendable")
  1351. c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
  1352. c.Assert(meta.Get("x-Oss-Meta-Mine"), Equals, "")
  1353. c.Assert(meta.Get("X-Oss-Next-Append-Position"), Equals, strconv.FormatInt(nextPos, 10))
  1354. acl, err = s.bucket.GetObjectACL(objectName)
  1355. c.Assert(err, IsNil)
  1356. c.Assert(acl.ACL, Equals, string(ACLPublicRead))
  1357. err = s.bucket.DeleteObject(objectName)
  1358. c.Assert(err, IsNil)
  1359. }
  1360. // TestAppendObjectNegative
  1361. func (s *OssBucketSuite) TestAppendObjectNegative(c *C) {
  1362. objectName := objectNamePrefix + "taon"
  1363. nextPos := int64(0)
  1364. nextPos, err := s.bucket.AppendObject(objectName, strings.NewReader("ObjectValue"), nextPos)
  1365. c.Assert(err, IsNil)
  1366. nextPos, err = s.bucket.AppendObject(objectName, strings.NewReader("ObjectValue"), 0)
  1367. c.Assert(err, NotNil)
  1368. err = s.bucket.DeleteObject(objectName)
  1369. c.Assert(err, IsNil)
  1370. }
  1371. // TestContentType
  1372. func (s *OssBucketSuite) TestAddContentType(c *C) {
  1373. opts := addContentType(nil, "abc.txt")
  1374. typ, err := findOption(opts, HTTPHeaderContentType, "")
  1375. c.Assert(err, IsNil)
  1376. c.Assert(typ, Equals, "text/plain; charset=utf-8")
  1377. opts = addContentType(nil)
  1378. typ, err = findOption(opts, HTTPHeaderContentType, "")
  1379. c.Assert(err, IsNil)
  1380. c.Assert(len(opts), Equals, 1)
  1381. c.Assert(typ, Equals, "application/octet-stream")
  1382. opts = addContentType(nil, "abc.txt", "abc.pdf")
  1383. typ, err = findOption(opts, HTTPHeaderContentType, "")
  1384. c.Assert(err, IsNil)
  1385. c.Assert(typ, Equals, "text/plain; charset=utf-8")
  1386. opts = addContentType(nil, "abc", "abc.txt", "abc.pdf")
  1387. typ, err = findOption(opts, HTTPHeaderContentType, "")
  1388. c.Assert(err, IsNil)
  1389. c.Assert(typ, Equals, "text/plain; charset=utf-8")
  1390. opts = addContentType(nil, "abc", "abc", "edf")
  1391. typ, err = findOption(opts, HTTPHeaderContentType, "")
  1392. c.Assert(err, IsNil)
  1393. c.Assert(typ, Equals, "application/octet-stream")
  1394. opts = addContentType([]Option{Meta("meta", "my")}, "abc", "abc.txt", "abc.pdf")
  1395. typ, err = findOption(opts, HTTPHeaderContentType, "")
  1396. c.Assert(err, IsNil)
  1397. c.Assert(len(opts), Equals, 2)
  1398. c.Assert(typ, Equals, "text/plain; charset=utf-8")
  1399. }
  1400. func (s *OssBucketSuite) TestGetConfig(c *C) {
  1401. client, err := New(endpoint, accessID, accessKey, UseCname(true),
  1402. Timeout(11, 12), SecurityToken("token"), EnableMD5(false))
  1403. c.Assert(err, IsNil)
  1404. bucket, err := client.Bucket(bucketName)
  1405. c.Assert(err, IsNil)
  1406. c.Assert(bucket.getConfig().HTTPTimeout.ConnectTimeout, Equals, time.Second*11)
  1407. c.Assert(bucket.getConfig().HTTPTimeout.ReadWriteTimeout, Equals, time.Second*12)
  1408. c.Assert(bucket.getConfig().HTTPTimeout.HeaderTimeout, Equals, time.Second*12)
  1409. c.Assert(bucket.getConfig().HTTPTimeout.IdleConnTimeout, Equals, time.Second*12)
  1410. c.Assert(bucket.getConfig().HTTPTimeout.LongTimeout, Equals, time.Second*12*10)
  1411. c.Assert(bucket.getConfig().SecurityToken, Equals, "token")
  1412. c.Assert(bucket.getConfig().IsCname, Equals, true)
  1413. c.Assert(bucket.getConfig().IsEnableMD5, Equals, false)
  1414. }
  1415. func (s *OssBucketSuite) TestSTSToken(c *C) {
  1416. objectName := objectNamePrefix + "tst"
  1417. objectValue := "红藕香残玉簟秋。轻解罗裳,独上兰舟。云中谁寄锦书来?雁字回时,月满西楼。"
  1418. stsClient := sts.NewClient(stsaccessID, stsaccessKey, stsARN, "oss_test_sess")
  1419. resp, err := stsClient.AssumeRole(1800)
  1420. c.Assert(err, IsNil)
  1421. client, err := New(endpoint, resp.Credentials.AccessKeyId, resp.Credentials.AccessKeySecret,
  1422. SecurityToken(resp.Credentials.SecurityToken))
  1423. c.Assert(err, IsNil)
  1424. bucket, err := client.Bucket(bucketName)
  1425. c.Assert(err, IsNil)
  1426. // Put
  1427. err = bucket.PutObject(objectName, strings.NewReader(objectValue))
  1428. c.Assert(err, IsNil)
  1429. // Get
  1430. body, err := s.bucket.GetObject(objectName)
  1431. c.Assert(err, IsNil)
  1432. str, err := readBody(body)
  1433. c.Assert(err, IsNil)
  1434. c.Assert(str, Equals, objectValue)
  1435. // List
  1436. lor, err := bucket.ListObjects()
  1437. c.Assert(err, IsNil)
  1438. testLogger.Println("Objects:", lor.Objects)
  1439. // Delete
  1440. err = bucket.DeleteObject(objectName)
  1441. c.Assert(err, IsNil)
  1442. }
  1443. func (s *OssBucketSuite) TestSTSTonekNegative(c *C) {
  1444. objectName := objectNamePrefix + "tstg"
  1445. localFile := objectName + ".jpg"
  1446. client, err := New(endpoint, accessID, accessKey, SecurityToken("Invalid"))
  1447. c.Assert(err, IsNil)
  1448. _, err = client.ListBuckets()
  1449. c.Assert(err, NotNil)
  1450. bucket, err := client.Bucket(bucketName)
  1451. c.Assert(err, IsNil)
  1452. err = bucket.PutObject(objectName, strings.NewReader(""))
  1453. c.Assert(err, NotNil)
  1454. err = bucket.PutObjectFromFile(objectName, "")
  1455. c.Assert(err, NotNil)
  1456. _, err = bucket.GetObject(objectName)
  1457. c.Assert(err, NotNil)
  1458. err = bucket.GetObjectToFile(objectName, "")
  1459. c.Assert(err, NotNil)
  1460. _, err = bucket.ListObjects()
  1461. c.Assert(err, NotNil)
  1462. err = bucket.SetObjectACL(objectName, ACLPublicRead)
  1463. c.Assert(err, NotNil)
  1464. _, err = bucket.GetObjectACL(objectName)
  1465. c.Assert(err, NotNil)
  1466. err = bucket.UploadFile(objectName, localFile, MinPartSize)
  1467. c.Assert(err, NotNil)
  1468. err = bucket.DownloadFile(objectName, localFile, MinPartSize)
  1469. c.Assert(err, NotNil)
  1470. _, err = bucket.IsObjectExist(objectName)
  1471. c.Assert(err, NotNil)
  1472. _, err = bucket.ListMultipartUploads()
  1473. c.Assert(err, NotNil)
  1474. err = bucket.DeleteObject(objectName)
  1475. c.Assert(err, NotNil)
  1476. _, err = bucket.DeleteObjects([]string{objectName})
  1477. c.Assert(err, NotNil)
  1478. err = client.DeleteBucket(bucketName)
  1479. c.Assert(err, NotNil)
  1480. }
  1481. func (s *OssBucketSuite) TestUploadBigFile(c *C) {
  1482. objectName := objectNamePrefix + "tubf"
  1483. bigFile := "D:\\tmp\\bigfile.zip"
  1484. newFile := "D:\\tmp\\newbigfile.zip"
  1485. exist, err := isFileExist(bigFile)
  1486. c.Assert(err, IsNil)
  1487. if !exist {
  1488. return
  1489. }
  1490. // Put
  1491. start := GetNowSec()
  1492. err = s.bucket.PutObjectFromFile(objectName, bigFile)
  1493. c.Assert(err, IsNil)
  1494. end := GetNowSec()
  1495. testLogger.Println("Put big file:", bigFile, "use sec:", end-start)
  1496. // Check
  1497. start = GetNowSec()
  1498. err = s.bucket.GetObjectToFile(objectName, newFile)
  1499. c.Assert(err, IsNil)
  1500. end = GetNowSec()
  1501. testLogger.Println("Get big file:", bigFile, "use sec:", end-start)
  1502. start = GetNowSec()
  1503. eq, err := compareFiles(bigFile, newFile)
  1504. c.Assert(err, IsNil)
  1505. c.Assert(eq, Equals, true)
  1506. end = GetNowSec()
  1507. testLogger.Println("Compare big file:", bigFile, "use sec:", end-start)
  1508. err = s.bucket.DeleteObject(objectName)
  1509. c.Assert(err, IsNil)
  1510. }
  1511. func (s *OssBucketSuite) TestSymlink(c *C) {
  1512. objectName := objectNamePrefix + "符号链接"
  1513. targetObjectName := objectNamePrefix + "符号链接目标文件"
  1514. err := s.bucket.DeleteObject(objectName)
  1515. c.Assert(err, IsNil)
  1516. err = s.bucket.DeleteObject(targetObjectName)
  1517. c.Assert(err, IsNil)
  1518. meta, err := s.bucket.GetSymlink(objectName)
  1519. c.Assert(err, NotNil)
  1520. // Put symlink
  1521. err = s.bucket.PutSymlink(objectName, targetObjectName)
  1522. c.Assert(err, IsNil)
  1523. err = s.bucket.PutObject(targetObjectName, strings.NewReader("target"))
  1524. c.Assert(err, IsNil)
  1525. err = s.bucket.PutSymlink(objectName, targetObjectName)
  1526. c.Assert(err, IsNil)
  1527. meta, err = s.bucket.GetSymlink(objectName)
  1528. c.Assert(err, IsNil)
  1529. c.Assert(meta.Get(HTTPHeaderOssSymlinkTarget), Equals, targetObjectName)
  1530. // List object
  1531. lor, err := s.bucket.ListObjects()
  1532. c.Assert(err, IsNil)
  1533. exist, v := s.getObject(lor.Objects, objectName)
  1534. c.Assert(exist, Equals, true)
  1535. c.Assert(v.Type, Equals, "Symlink")
  1536. body, err := s.bucket.GetObject(objectName)
  1537. c.Assert(err, IsNil)
  1538. str, err := readBody(body)
  1539. c.Assert(err, IsNil)
  1540. c.Assert(str, Equals, "target")
  1541. meta, err = s.bucket.GetSymlink(targetObjectName)
  1542. c.Assert(err, NotNil)
  1543. err = s.bucket.PutObject(objectName, strings.NewReader("src"))
  1544. c.Assert(err, IsNil)
  1545. body, err = s.bucket.GetObject(objectName)
  1546. c.Assert(err, IsNil)
  1547. str, err = readBody(body)
  1548. c.Assert(err, IsNil)
  1549. c.Assert(str, Equals, "src")
  1550. err = s.bucket.DeleteObject(objectName)
  1551. c.Assert(err, IsNil)
  1552. err = s.bucket.DeleteObject(targetObjectName)
  1553. c.Assert(err, IsNil)
  1554. // put symlink again
  1555. objectName = objectNamePrefix + "symlink"
  1556. targetObjectName = objectNamePrefix + "symlink-target"
  1557. err = s.bucket.PutSymlink(objectName, targetObjectName)
  1558. c.Assert(err, IsNil)
  1559. err = s.bucket.PutObject(targetObjectName, strings.NewReader("target1"))
  1560. c.Assert(err, IsNil)
  1561. meta, err = s.bucket.GetSymlink(objectName)
  1562. c.Assert(err, IsNil)
  1563. c.Assert(meta.Get(HTTPHeaderOssSymlinkTarget), Equals, targetObjectName)
  1564. body, err = s.bucket.GetObject(objectName)
  1565. c.Assert(err, IsNil)
  1566. str, err = readBody(body)
  1567. c.Assert(err, IsNil)
  1568. c.Assert(str, Equals, "target1")
  1569. err = s.bucket.DeleteObject(objectName)
  1570. c.Assert(err, IsNil)
  1571. err = s.bucket.DeleteObject(targetObjectName)
  1572. c.Assert(err, IsNil)
  1573. }
  1574. // TestRestoreObject
  1575. func (s *OssBucketSuite) TestRestoreObject(c *C) {
  1576. objectName := objectNamePrefix + "restore"
  1577. // List Object
  1578. lor, err := s.archiveBucket.ListObjects()
  1579. c.Assert(err, IsNil)
  1580. left := len(lor.Objects)
  1581. // Put three object
  1582. err = s.archiveBucket.PutObject(objectName, strings.NewReader(""))
  1583. c.Assert(err, IsNil)
  1584. // List
  1585. lor, err = s.archiveBucket.ListObjects()
  1586. c.Assert(err, IsNil)
  1587. c.Assert(len(lor.Objects), Equals, left+1)
  1588. for _, object := range lor.Objects {
  1589. c.Assert(object.StorageClass, Equals, string(StorageArchive))
  1590. c.Assert(object.Type, Equals, "Normal")
  1591. }
  1592. // Head Object
  1593. meta, err := s.archiveBucket.GetObjectDetailedMeta(objectName)
  1594. c.Assert(err, IsNil)
  1595. _, ok := meta["X-Oss-Restore"]
  1596. c.Assert(ok, Equals, false)
  1597. c.Assert(meta.Get("X-Oss-Storage-Class"), Equals, "Archive")
  1598. // Error Restore
  1599. err = s.archiveBucket.RestoreObject("notexistobject")
  1600. c.Assert(err, NotNil)
  1601. // Restore Object
  1602. err = s.archiveBucket.RestoreObject(objectName)
  1603. c.Assert(err, IsNil)
  1604. // Head Object
  1605. meta, err = s.archiveBucket.GetObjectDetailedMeta(objectName)
  1606. c.Assert(err, IsNil)
  1607. c.Assert(meta.Get("X-Oss-Restore"), Equals, "ongoing-request=\"true\"")
  1608. c.Assert(meta.Get("X-Oss-Storage-Class"), Equals, "Archive")
  1609. }
  1610. // private
  1611. func createFileAndWrite(fileName string, data []byte) error {
  1612. os.Remove(fileName)
  1613. fo, err := os.Create(fileName)
  1614. if err != nil {
  1615. return err
  1616. }
  1617. defer fo.Close()
  1618. bytes, err := fo.Write(data)
  1619. if err != nil {
  1620. return err
  1621. }
  1622. if bytes != len(data) {
  1623. return fmt.Errorf(fmt.Sprintf("write %d bytes not equal data length %d", bytes, len(data)))
  1624. }
  1625. return nil
  1626. }
  1627. // compare the content between fileL and fileR
  1628. func compareFiles(fileL string, fileR string) (bool, error) {
  1629. finL, err := os.Open(fileL)
  1630. if err != nil {
  1631. return false, err
  1632. }
  1633. defer finL.Close()
  1634. finR, err := os.Open(fileR)
  1635. if err != nil {
  1636. return false, err
  1637. }
  1638. defer finR.Close()
  1639. statL, err := finL.Stat()
  1640. if err != nil {
  1641. return false, err
  1642. }
  1643. statR, err := finR.Stat()
  1644. if err != nil {
  1645. return false, err
  1646. }
  1647. if statL.Size() != statR.Size() {
  1648. return false, nil
  1649. }
  1650. size := statL.Size()
  1651. if size > 102400 {
  1652. size = 102400
  1653. }
  1654. bufL := make([]byte, size)
  1655. bufR := make([]byte, size)
  1656. for {
  1657. n, _ := finL.Read(bufL)
  1658. if 0 == n {
  1659. break
  1660. }
  1661. n, _ = finR.Read(bufR)
  1662. if 0 == n {
  1663. break
  1664. }
  1665. if !bytes.Equal(bufL, bufR) {
  1666. return false, nil
  1667. }
  1668. }
  1669. return true, nil
  1670. }
  1671. // compare the content of file and data
  1672. func compareFileData(file string, data []byte) (bool, error) {
  1673. fin, err := os.Open(file)
  1674. if err != nil {
  1675. return false, err
  1676. }
  1677. defer fin.Close()
  1678. stat, err := fin.Stat()
  1679. if err != nil {
  1680. return false, err
  1681. }
  1682. if stat.Size() != (int64)(len(data)) {
  1683. return false, nil
  1684. }
  1685. buf := make([]byte, stat.Size())
  1686. n, err := fin.Read(buf)
  1687. if err != nil {
  1688. return false, err
  1689. }
  1690. if stat.Size() != (int64)(n) {
  1691. return false, errors.New("read error")
  1692. }
  1693. if !bytes.Equal(buf, data) {
  1694. return false, nil
  1695. }
  1696. return true, nil
  1697. }
  1698. func walkDir(dirPth, suffix string) ([]string, error) {
  1699. var files = []string{}
  1700. suffix = strings.ToUpper(suffix)
  1701. err := filepath.Walk(dirPth,
  1702. func(filename string, fi os.FileInfo, err error) error {
  1703. if err != nil {
  1704. return err
  1705. }
  1706. if fi.IsDir() {
  1707. return nil
  1708. }
  1709. if strings.HasSuffix(strings.ToUpper(fi.Name()), suffix) {
  1710. files = append(files, filename)
  1711. }
  1712. return nil
  1713. })
  1714. return files, err
  1715. }
  1716. func removeTempFiles(path string, prefix string) error {
  1717. files, err := walkDir(path, prefix)
  1718. if err != nil {
  1719. return nil
  1720. }
  1721. for _, file := range files {
  1722. os.Remove(file)
  1723. }
  1724. return nil
  1725. }
  1726. func isFileExist(filename string) (bool, error) {
  1727. _, err := os.Stat(filename)
  1728. if err != nil && os.IsNotExist(err) {
  1729. return false, nil
  1730. } else if err != nil {
  1731. return false, err
  1732. } else {
  1733. return true, nil
  1734. }
  1735. }
  1736. func readBody(body io.ReadCloser) (string, error) {
  1737. data, err := ioutil.ReadAll(body)
  1738. body.Close()
  1739. if err != nil {
  1740. return "", err
  1741. }
  1742. return string(data), nil
  1743. }
  1744. func (s *OssBucketSuite) getObject(objects []ObjectProperties, object string) (bool, ObjectProperties) {
  1745. for _, v := range objects {
  1746. if v.Key == object {
  1747. return true, v
  1748. }
  1749. }
  1750. return false, ObjectProperties{}
  1751. }