bucket_test.go 65 KB

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