download_test.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. package oss
  2. import (
  3. "bytes"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "strings"
  8. "time"
  9. . "gopkg.in/check.v1"
  10. )
  11. type OssDownloadSuite struct {
  12. client *Client
  13. bucket *Bucket
  14. }
  15. var _ = Suite(&OssDownloadSuite{})
  16. // SetUpSuite runs once when the suite starts running
  17. func (s *OssDownloadSuite) SetUpSuite(c *C) {
  18. client, err := New(endpoint, accessID, accessKey)
  19. c.Assert(err, IsNil)
  20. s.client = client
  21. s.client.CreateBucket(bucketName)
  22. bucket, err := s.client.Bucket(bucketName)
  23. c.Assert(err, IsNil)
  24. s.bucket = bucket
  25. testLogger.Println("test download started")
  26. }
  27. // TearDownSuite runs before each test or benchmark starts running
  28. func (s *OssDownloadSuite) TearDownSuite(c *C) {
  29. // Delete part
  30. keyMarker := KeyMarker("")
  31. uploadIDMarker := UploadIDMarker("")
  32. for {
  33. lmur, err := s.bucket.ListMultipartUploads(keyMarker, uploadIDMarker)
  34. c.Assert(err, IsNil)
  35. for _, upload := range lmur.Uploads {
  36. var imur = InitiateMultipartUploadResult{Bucket: s.bucket.BucketName,
  37. Key: upload.Key, UploadID: upload.UploadID}
  38. err = s.bucket.AbortMultipartUpload(imur)
  39. c.Assert(err, IsNil)
  40. }
  41. keyMarker = KeyMarker(lmur.NextKeyMarker)
  42. uploadIDMarker = UploadIDMarker(lmur.NextUploadIDMarker)
  43. if !lmur.IsTruncated {
  44. break
  45. }
  46. }
  47. // Delete objects
  48. marker := Marker("")
  49. for {
  50. lor, err := s.bucket.ListObjects(marker)
  51. c.Assert(err, IsNil)
  52. for _, object := range lor.Objects {
  53. err = s.bucket.DeleteObject(object.Key)
  54. c.Assert(err, IsNil)
  55. }
  56. marker = Marker(lor.NextMarker)
  57. if !lor.IsTruncated {
  58. break
  59. }
  60. }
  61. // Delete bucket
  62. err := s.client.DeleteBucket(s.bucket.BucketName)
  63. c.Assert(err, IsNil)
  64. testLogger.Println("test download completed")
  65. }
  66. // SetUpTest runs after each test or benchmark runs
  67. func (s *OssDownloadSuite) SetUpTest(c *C) {
  68. err := removeTempFiles("../oss", ".jpg")
  69. c.Assert(err, IsNil)
  70. }
  71. // TearDownTest runs once after all tests or benchmarks have finished running
  72. func (s *OssDownloadSuite) TearDownTest(c *C) {
  73. err := removeTempFiles("../oss", ".jpg")
  74. c.Assert(err, IsNil)
  75. err = removeTempFiles("../oss", ".temp")
  76. c.Assert(err, IsNil)
  77. }
  78. // TestDownloadRoutineWithoutRecovery multipart downloads without checkpoint
  79. func (s *OssDownloadSuite) TestDownloadRoutineWithoutRecovery(c *C) {
  80. objectName := objectNamePrefix + randStr(8)
  81. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  82. newFile := randStr(8) + ".jpg"
  83. // Upload a file
  84. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  85. c.Assert(err, IsNil)
  86. // Download the file by default
  87. err = s.bucket.DownloadFile(objectName, newFile, 100*1024)
  88. c.Assert(err, IsNil)
  89. // Check
  90. eq, err := compareFiles(fileName, newFile)
  91. c.Assert(err, IsNil)
  92. c.Assert(eq, Equals, true)
  93. // Use 2 coroutines to download the file and total parts count is 5
  94. os.Remove(newFile)
  95. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(2))
  96. c.Assert(err, IsNil)
  97. // Check
  98. eq, err = compareFiles(fileName, newFile)
  99. c.Assert(err, IsNil)
  100. c.Assert(eq, Equals, true)
  101. // Use 5 coroutines to download the file and the total parts count is 5.
  102. os.Remove(newFile)
  103. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(5))
  104. c.Assert(err, IsNil)
  105. // Check
  106. eq, err = compareFiles(fileName, newFile)
  107. c.Assert(err, IsNil)
  108. c.Assert(eq, Equals, true)
  109. // Use 10 coroutines to download the file and the total parts count is 5.
  110. os.Remove(newFile)
  111. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(10))
  112. c.Assert(err, IsNil)
  113. // Check
  114. eq, err = compareFiles(fileName, newFile)
  115. c.Assert(err, IsNil)
  116. c.Assert(eq, Equals, true)
  117. err = s.bucket.DeleteObject(objectName)
  118. c.Assert(err, IsNil)
  119. }
  120. // DownErrorHooker requests hook by downloadPart
  121. func DownErrorHooker(part downloadPart) error {
  122. if part.Index == 4 {
  123. time.Sleep(time.Second)
  124. return fmt.Errorf("ErrorHooker")
  125. }
  126. return nil
  127. }
  128. // TestDownloadRoutineWithRecovery multi-routine resumable download
  129. func (s *OssDownloadSuite) TestDownloadRoutineWithRecovery(c *C) {
  130. objectName := objectNamePrefix + randStr(8)
  131. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  132. newFile := randStr(8) + ".jpg"
  133. // Upload a file
  134. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  135. c.Assert(err, IsNil)
  136. // Download a file with default checkpoint
  137. downloadPartHooker = DownErrorHooker
  138. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, newFile+".cp"))
  139. c.Assert(err, NotNil)
  140. c.Assert(err.Error(), Equals, "ErrorHooker")
  141. downloadPartHooker = defaultDownloadPartHook
  142. // Check
  143. dcp := downloadCheckpoint{}
  144. err = dcp.load(newFile + ".cp")
  145. c.Assert(err, IsNil)
  146. c.Assert(dcp.Magic, Equals, downloadCpMagic)
  147. c.Assert(len(dcp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
  148. c.Assert(dcp.FilePath, Equals, newFile)
  149. c.Assert(dcp.ObjStat.Size, Equals, int64(482048))
  150. c.Assert(len(dcp.ObjStat.LastModified) > 0, Equals, true)
  151. c.Assert(dcp.ObjStat.Etag, Equals, "\"2351E662233817A7AE974D8C5B0876DD-5\"")
  152. c.Assert(dcp.Object, Equals, objectName)
  153. c.Assert(len(dcp.Parts), Equals, 5)
  154. c.Assert(len(dcp.todoParts()), Equals, 1)
  155. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, newFile+".cp"))
  156. c.Assert(err, IsNil)
  157. //download success, checkpoint file has been deleted
  158. err = dcp.load(newFile + ".cp")
  159. c.Assert(err, NotNil)
  160. eq, err := compareFiles(fileName, newFile)
  161. c.Assert(err, IsNil)
  162. c.Assert(eq, Equals, true)
  163. // Resumable download with empty checkpoint file path
  164. downloadPartHooker = DownErrorHooker
  165. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, ""))
  166. c.Assert(err, NotNil)
  167. c.Assert(err.Error(), Equals, "ErrorHooker")
  168. downloadPartHooker = defaultDownloadPartHook
  169. dcp = downloadCheckpoint{}
  170. err = dcp.load(newFile + ".cp")
  171. c.Assert(err, NotNil)
  172. // Resumable download with checkpoint dir
  173. os.Remove(newFile)
  174. downloadPartHooker = DownErrorHooker
  175. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, CheckpointDir(true, "./"))
  176. c.Assert(err, NotNil)
  177. c.Assert(err.Error(), Equals, "ErrorHooker")
  178. downloadPartHooker = defaultDownloadPartHook
  179. // Check
  180. dcp = downloadCheckpoint{}
  181. cpConf := cpConfig{IsEnable: true, DirPath: "./"}
  182. cpFilePath := getDownloadCpFilePath(&cpConf, s.bucket.BucketName, objectName, "",newFile)
  183. err = dcp.load(cpFilePath)
  184. c.Assert(err, IsNil)
  185. c.Assert(dcp.Magic, Equals, downloadCpMagic)
  186. c.Assert(len(dcp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
  187. c.Assert(dcp.FilePath, Equals, newFile)
  188. c.Assert(dcp.ObjStat.Size, Equals, int64(482048))
  189. c.Assert(len(dcp.ObjStat.LastModified) > 0, Equals, true)
  190. c.Assert(dcp.ObjStat.Etag, Equals, "\"2351E662233817A7AE974D8C5B0876DD-5\"")
  191. c.Assert(dcp.Object, Equals, objectName)
  192. c.Assert(len(dcp.Parts), Equals, 5)
  193. c.Assert(len(dcp.todoParts()), Equals, 1)
  194. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, CheckpointDir(true, "./"))
  195. c.Assert(err, IsNil)
  196. //download success, checkpoint file has been deleted
  197. err = dcp.load(cpFilePath)
  198. c.Assert(err, NotNil)
  199. eq, err = compareFiles(fileName, newFile)
  200. c.Assert(err, IsNil)
  201. c.Assert(eq, Equals, true)
  202. // Resumable download with checkpoint at a time. No error is expected in the download procedure.
  203. os.Remove(newFile)
  204. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, newFile+".cp"))
  205. c.Assert(err, IsNil)
  206. err = dcp.load(newFile + ".cp")
  207. c.Assert(err, NotNil)
  208. eq, err = compareFiles(fileName, newFile)
  209. c.Assert(err, IsNil)
  210. c.Assert(eq, Equals, true)
  211. // Resumable download with checkpoint at a time. No error is expected in the download procedure.
  212. os.Remove(newFile)
  213. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(10), Checkpoint(true, newFile+".cp"))
  214. c.Assert(err, IsNil)
  215. err = dcp.load(newFile + ".cp")
  216. c.Assert(err, NotNil)
  217. eq, err = compareFiles(fileName, newFile)
  218. c.Assert(err, IsNil)
  219. c.Assert(eq, Equals, true)
  220. err = s.bucket.DeleteObject(objectName)
  221. c.Assert(err, IsNil)
  222. }
  223. // TestDownloadOption options
  224. func (s *OssDownloadSuite) TestDownloadOption(c *C) {
  225. objectName := objectNamePrefix + randStr(8)
  226. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  227. newFile := randStr(8) + ".jpg"
  228. // Upload the file
  229. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  230. c.Assert(err, IsNil)
  231. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  232. c.Assert(err, IsNil)
  233. // IfMatch
  234. os.Remove(newFile)
  235. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), IfMatch(meta.Get("Etag")))
  236. c.Assert(err, IsNil)
  237. eq, err := compareFiles(fileName, newFile)
  238. c.Assert(err, IsNil)
  239. c.Assert(eq, Equals, true)
  240. // IfNoneMatch
  241. os.Remove(newFile)
  242. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), IfNoneMatch(meta.Get("Etag")))
  243. c.Assert(err, NotNil)
  244. // IfMatch
  245. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), IfMatch(meta.Get("Etag")))
  246. c.Assert(err, IsNil)
  247. eq, err = compareFiles(fileName, newFile)
  248. c.Assert(err, IsNil)
  249. c.Assert(eq, Equals, true)
  250. // IfNoneMatch
  251. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), IfNoneMatch(meta.Get("Etag")))
  252. c.Assert(err, NotNil)
  253. }
  254. // TestDownloadObjectChange tests the file is updated during the upload
  255. func (s *OssDownloadSuite) TestDownloadObjectChange(c *C) {
  256. objectName := objectNamePrefix + randStr(8)
  257. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  258. newFile := randStr(8) + ".jpg"
  259. // Upload a file
  260. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  261. c.Assert(err, IsNil)
  262. // Download with default checkpoint
  263. downloadPartHooker = DownErrorHooker
  264. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, newFile+".cp"))
  265. c.Assert(err, NotNil)
  266. c.Assert(err.Error(), Equals, "ErrorHooker")
  267. downloadPartHooker = defaultDownloadPartHook
  268. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  269. c.Assert(err, IsNil)
  270. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, newFile+".cp"))
  271. c.Assert(err, IsNil)
  272. eq, err := compareFiles(fileName, newFile)
  273. c.Assert(err, IsNil)
  274. c.Assert(eq, Equals, true)
  275. }
  276. // TestDownloadNegative tests downloading negative
  277. func (s *OssDownloadSuite) TestDownloadNegative(c *C) {
  278. objectName := objectNamePrefix + randStr(8)
  279. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  280. newFile := randStr(8) + ".jpg"
  281. // Upload a file
  282. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  283. c.Assert(err, IsNil)
  284. // Worker routine error
  285. downloadPartHooker = DownErrorHooker
  286. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(2))
  287. c.Assert(err, NotNil)
  288. c.Assert(err.Error(), Equals, "ErrorHooker")
  289. downloadPartHooker = defaultDownloadPartHook
  290. // Local file does not exist
  291. err = s.bucket.DownloadFile(objectName, "/tmp/", 100*1024, Routines(2))
  292. c.Assert(err, NotNil)
  293. // Invalid part size
  294. err = s.bucket.DownloadFile(objectName, newFile, 0, Routines(2))
  295. c.Assert(err, NotNil)
  296. err = s.bucket.DownloadFile(objectName, newFile, 1024*1024*1024*100, Routines(2))
  297. c.Assert(err, IsNil)
  298. err = s.bucket.DeleteObject(objectName)
  299. c.Assert(err, IsNil)
  300. // Local file does not exist
  301. err = s.bucket.DownloadFile(objectName, "/tmp/", 100*1024)
  302. c.Assert(err, NotNil)
  303. err = s.bucket.DownloadFile(objectName, "/tmp/", 100*1024, Routines(2))
  304. c.Assert(err, NotNil)
  305. // Invalid part size
  306. err = s.bucket.DownloadFile(objectName, newFile, -1)
  307. c.Assert(err, NotNil)
  308. err = s.bucket.DownloadFile(objectName, newFile, 0, Routines(2))
  309. c.Assert(err, NotNil)
  310. err = s.bucket.DownloadFile(objectName, newFile, 1024*1024*1024*100)
  311. c.Assert(err, NotNil)
  312. err = s.bucket.DownloadFile(objectName, newFile, 1024*1024*1024*100, Routines(2))
  313. c.Assert(err, NotNil)
  314. }
  315. // TestDownloadWithRange tests concurrent downloading with range specified and checkpoint enabled
  316. func (s *OssDownloadSuite) TestDownloadWithRange(c *C) {
  317. objectName := objectNamePrefix + randStr(8)
  318. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  319. newFile := randStr(8) + ".jpg"
  320. newFileGet := randStr(8) + "-.jpg"
  321. // Upload a file
  322. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  323. c.Assert(err, IsNil)
  324. fileSize, err := getFileSize(fileName)
  325. c.Assert(err, IsNil)
  326. // Download with range, from 1024 to 4096
  327. os.Remove(newFile)
  328. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), Range(1024, 4095))
  329. c.Assert(err, IsNil)
  330. // Check
  331. eq, err := compareFilesWithRange(fileName, 1024, newFile, 0, 3072)
  332. c.Assert(err, IsNil)
  333. c.Assert(eq, Equals, true)
  334. os.Remove(newFileGet)
  335. err = s.bucket.GetObjectToFile(objectName, newFileGet, Range(1024, 4095))
  336. c.Assert(err, IsNil)
  337. // Compare get and download
  338. eq, err = compareFiles(newFile, newFileGet)
  339. c.Assert(err, IsNil)
  340. c.Assert(eq, Equals, true)
  341. // Download with range, from 1024 to 4096
  342. os.Remove(newFile)
  343. err = s.bucket.DownloadFile(objectName, newFile, 1024, Routines(3), NormalizedRange("1024-4095"))
  344. c.Assert(err, IsNil)
  345. // Check
  346. eq, err = compareFilesWithRange(fileName, 1024, newFile, 0, 3072)
  347. c.Assert(err, IsNil)
  348. c.Assert(eq, Equals, true)
  349. os.Remove(newFileGet)
  350. err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("1024-4095"))
  351. c.Assert(err, IsNil)
  352. // Compare get and download
  353. eq, err = compareFiles(newFile, newFileGet)
  354. c.Assert(err, IsNil)
  355. c.Assert(eq, Equals, true)
  356. // Download with range, from 2048 to the end
  357. os.Remove(newFile)
  358. err = s.bucket.DownloadFile(objectName, newFile, 1024*1024, Routines(3), NormalizedRange("2048-"))
  359. c.Assert(err, IsNil)
  360. // Check
  361. eq, err = compareFilesWithRange(fileName, 2048, newFile, 0, fileSize-2048)
  362. c.Assert(err, IsNil)
  363. c.Assert(eq, Equals, true)
  364. os.Remove(newFileGet)
  365. err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("2048-"))
  366. c.Assert(err, IsNil)
  367. // Compare get and download
  368. eq, err = compareFiles(newFile, newFileGet)
  369. c.Assert(err, IsNil)
  370. c.Assert(eq, Equals, true)
  371. // Download with range, the last 4096
  372. os.Remove(newFile)
  373. err = s.bucket.DownloadFile(objectName, newFile, 1024, Routines(3), NormalizedRange("-4096"))
  374. c.Assert(err, IsNil)
  375. // Check
  376. eq, err = compareFilesWithRange(fileName, fileSize-4096, newFile, 0, 4096)
  377. c.Assert(err, IsNil)
  378. c.Assert(eq, Equals, true)
  379. os.Remove(newFileGet)
  380. err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("-4096"))
  381. c.Assert(err, IsNil)
  382. // Compare get and download
  383. eq, err = compareFiles(newFile, newFileGet)
  384. c.Assert(err, IsNil)
  385. c.Assert(eq, Equals, true)
  386. err = s.bucket.DeleteObject(objectName)
  387. c.Assert(err, IsNil)
  388. }
  389. // TestDownloadWithCheckoutAndRange tests concurrent downloading with range specified and checkpoint enabled
  390. func (s *OssDownloadSuite) TestDownloadWithCheckoutAndRange(c *C) {
  391. objectName := objectNamePrefix + randStr(8)
  392. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  393. newFile := randStr(8) + ".jpg"
  394. newFileGet := randStr(8) + "-get.jpg"
  395. // Upload a file
  396. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), Checkpoint(true, fileName+".cp"))
  397. c.Assert(err, IsNil)
  398. fileSize, err := getFileSize(fileName)
  399. c.Assert(err, IsNil)
  400. // Download with range, from 1024 to 4096
  401. os.Remove(newFile)
  402. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), Checkpoint(true, newFile+".cp"), Range(1024, 4095))
  403. c.Assert(err, IsNil)
  404. // Check
  405. eq, err := compareFilesWithRange(fileName, 1024, newFile, 0, 3072)
  406. c.Assert(err, IsNil)
  407. c.Assert(eq, Equals, true)
  408. os.Remove(newFileGet)
  409. err = s.bucket.GetObjectToFile(objectName, newFileGet, Range(1024, 4095))
  410. c.Assert(err, IsNil)
  411. // Compare get and download
  412. eq, err = compareFiles(newFile, newFileGet)
  413. c.Assert(err, IsNil)
  414. c.Assert(eq, Equals, true)
  415. // Download with range, from 1024 to 4096
  416. os.Remove(newFile)
  417. err = s.bucket.DownloadFile(objectName, newFile, 1024, Routines(3), Checkpoint(true, newFile+".cp"), NormalizedRange("1024-4095"))
  418. c.Assert(err, IsNil)
  419. // Check
  420. eq, err = compareFilesWithRange(fileName, 1024, newFile, 0, 3072)
  421. c.Assert(err, IsNil)
  422. c.Assert(eq, Equals, true)
  423. os.Remove(newFileGet)
  424. err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("1024-4095"))
  425. c.Assert(err, IsNil)
  426. // Compare get and download
  427. eq, err = compareFiles(newFile, newFileGet)
  428. c.Assert(err, IsNil)
  429. c.Assert(eq, Equals, true)
  430. // Download with range, from 2048 to the end
  431. os.Remove(newFile)
  432. err = s.bucket.DownloadFile(objectName, newFile, 1024*1024, Routines(3), Checkpoint(true, newFile+".cp"), NormalizedRange("2048-"))
  433. c.Assert(err, IsNil)
  434. // Check
  435. eq, err = compareFilesWithRange(fileName, 2048, newFile, 0, fileSize-2048)
  436. c.Assert(err, IsNil)
  437. c.Assert(eq, Equals, true)
  438. os.Remove(newFileGet)
  439. err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("2048-"))
  440. c.Assert(err, IsNil)
  441. // Compare get and download
  442. eq, err = compareFiles(newFile, newFileGet)
  443. c.Assert(err, IsNil)
  444. c.Assert(eq, Equals, true)
  445. // Download with range, the last 4096 bytes
  446. os.Remove(newFile)
  447. err = s.bucket.DownloadFile(objectName, newFile, 1024, Routines(3), Checkpoint(true, newFile+".cp"), NormalizedRange("-4096"))
  448. c.Assert(err, IsNil)
  449. // Check
  450. eq, err = compareFilesWithRange(fileName, fileSize-4096, newFile, 0, 4096)
  451. c.Assert(err, IsNil)
  452. c.Assert(eq, Equals, true)
  453. os.Remove(newFileGet)
  454. err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("-4096"))
  455. c.Assert(err, IsNil)
  456. // Compare get and download
  457. eq, err = compareFiles(newFile, newFileGet)
  458. c.Assert(err, IsNil)
  459. c.Assert(eq, Equals, true)
  460. err = s.bucket.DeleteObject(objectName)
  461. c.Assert(err, IsNil)
  462. }
  463. // TestCombineCRCInDownloadParts tests combineCRCInParts
  464. func (s *OssDownloadSuite) TestCombineCRCInDownloadParts(c *C) {
  465. crc := combineCRCInParts(nil)
  466. c.Assert(crc == 0, Equals, true)
  467. crc = combineCRCInParts(make([]downloadPart, 0))
  468. c.Assert(crc == 0, Equals, true)
  469. parts := make([]downloadPart, 1)
  470. parts[0].CRC64 = 10278880121275185425
  471. crc = combineCRCInParts(parts)
  472. c.Assert(crc == 10278880121275185425, Equals, true)
  473. parts = make([]downloadPart, 2)
  474. parts[0].CRC64 = 6748440630437108969
  475. parts[0].Start = 0
  476. parts[0].End = 4
  477. parts[1].CRC64 = 10278880121275185425
  478. parts[1].Start = 5
  479. parts[1].End = 8
  480. crc = combineCRCInParts(parts)
  481. c.Assert(crc == 11051210869376104954, Equals, true)
  482. }
  483. func getFileSize(fileName string) (int64, error) {
  484. file, err := os.Open(fileName)
  485. if err != nil {
  486. return 0, err
  487. }
  488. defer file.Close()
  489. stat, err := file.Stat()
  490. if err != nil {
  491. return 0, err
  492. }
  493. return stat.Size(), nil
  494. }
  495. // compareFilesWithRange compares the content between fileL and fileR with specified range
  496. func compareFilesWithRange(fileL string, offsetL int64, fileR string, offsetR int64, size int64) (bool, error) {
  497. finL, err := os.Open(fileL)
  498. if err != nil {
  499. return false, err
  500. }
  501. defer finL.Close()
  502. finL.Seek(offsetL, os.SEEK_SET)
  503. finR, err := os.Open(fileR)
  504. if err != nil {
  505. return false, err
  506. }
  507. defer finR.Close()
  508. finR.Seek(offsetR, os.SEEK_SET)
  509. statL, err := finL.Stat()
  510. if err != nil {
  511. return false, err
  512. }
  513. statR, err := finR.Stat()
  514. if err != nil {
  515. return false, err
  516. }
  517. if (offsetL+size > statL.Size()) || (offsetR+size > statR.Size()) {
  518. return false, nil
  519. }
  520. part := statL.Size() - offsetL
  521. if part > 16*1024 {
  522. part = 16 * 1024
  523. }
  524. bufL := make([]byte, part)
  525. bufR := make([]byte, part)
  526. for readN := int64(0); readN < size; {
  527. n, _ := finL.Read(bufL)
  528. if 0 == n {
  529. break
  530. }
  531. n, _ = finR.Read(bufR)
  532. if 0 == n {
  533. break
  534. }
  535. tailer := part
  536. if tailer > size-readN {
  537. tailer = size - readN
  538. }
  539. readN += tailer
  540. if !bytes.Equal(bufL[0:tailer], bufR[0:tailer]) {
  541. return false, nil
  542. }
  543. }
  544. return true, nil
  545. }
  546. func (s *OssDownloadSuite) TestVersioningDownloadWithoutCheckPoint(c *C) {
  547. // create a bucket with default proprety
  548. client, err := New(endpoint, accessID, accessKey)
  549. c.Assert(err, IsNil)
  550. bucketName := bucketNamePrefix + randLowStr(6)
  551. err = client.CreateBucket(bucketName)
  552. c.Assert(err, IsNil)
  553. bucket, err := client.Bucket(bucketName)
  554. // put bucket version:enabled
  555. var versioningConfig VersioningConfig
  556. versioningConfig.Status = string(VersionEnabled)
  557. err = client.SetBucketVersioning(bucketName, versioningConfig)
  558. c.Assert(err, IsNil)
  559. // begin test
  560. objectName := objectNamePrefix + randStr(8)
  561. fileName := "test-file-" + randStr(8)
  562. fileData := randStr(500 * 1024)
  563. createFile(fileName, fileData, c)
  564. newFile := randStr(8) + ".jpg"
  565. newFileGet := randStr(8) + "-.jpg"
  566. // Upload a file
  567. var respHeader http.Header
  568. options := []Option{Routines(3), GetResponseHeader(&respHeader)}
  569. err = bucket.UploadFile(objectName, fileName, 100*1024, options...)
  570. c.Assert(err, IsNil)
  571. versionId := GetVersionId(respHeader)
  572. c.Assert(len(versionId) > 0, Equals, true)
  573. fileSize, err := getFileSize(fileName)
  574. c.Assert(err, IsNil)
  575. // overwrite emtpy object
  576. err = bucket.PutObject(objectName, strings.NewReader(""))
  577. c.Assert(err, IsNil)
  578. // Download with range, from 1024 to 4096
  579. os.Remove(newFile)
  580. options = []Option{Routines(3), Range(1024, 4095), VersionId(versionId)}
  581. err = bucket.DownloadFile(objectName, newFile, 100*1024, options...)
  582. c.Assert(err, IsNil)
  583. // Check
  584. eq, err := compareFilesWithRange(fileName, 1024, newFile, 0, 3072)
  585. c.Assert(err, IsNil)
  586. c.Assert(eq, Equals, true)
  587. os.Remove(newFileGet)
  588. options = []Option{Range(1024, 4095), VersionId(versionId)}
  589. err = bucket.GetObjectToFile(objectName, newFileGet, options...)
  590. c.Assert(err, IsNil)
  591. // Compare get and download
  592. eq, err = compareFiles(newFile, newFileGet)
  593. c.Assert(err, IsNil)
  594. c.Assert(eq, Equals, true)
  595. // Download with range, from 1024 to 4096
  596. os.Remove(newFile)
  597. options = []Option{Routines(3), NormalizedRange("1024-4095"), VersionId(versionId)}
  598. err = bucket.DownloadFile(objectName, newFile, 1024, options...)
  599. c.Assert(err, IsNil)
  600. // Check
  601. eq, err = compareFilesWithRange(fileName, 1024, newFile, 0, 3072)
  602. c.Assert(err, IsNil)
  603. c.Assert(eq, Equals, true)
  604. os.Remove(newFileGet)
  605. options = []Option{NormalizedRange("1024-4095"), VersionId(versionId)}
  606. err = bucket.GetObjectToFile(objectName, newFileGet, options...)
  607. c.Assert(err, IsNil)
  608. // Compare get and download
  609. eq, err = compareFiles(newFile, newFileGet)
  610. c.Assert(err, IsNil)
  611. c.Assert(eq, Equals, true)
  612. // Download with range, from 2048 to the end
  613. os.Remove(newFile)
  614. options = []Option{NormalizedRange("2048-"), VersionId(versionId)}
  615. err = bucket.DownloadFile(objectName, newFile, 1024*1024, options...)
  616. c.Assert(err, IsNil)
  617. // Check
  618. eq, err = compareFilesWithRange(fileName, 2048, newFile, 0, fileSize-2048)
  619. c.Assert(err, IsNil)
  620. c.Assert(eq, Equals, true)
  621. os.Remove(newFileGet)
  622. options = []Option{NormalizedRange("2048-"), VersionId(versionId)}
  623. err = bucket.GetObjectToFile(objectName, newFileGet, options...)
  624. c.Assert(err, IsNil)
  625. // Compare get and download
  626. eq, err = compareFiles(newFile, newFileGet)
  627. c.Assert(err, IsNil)
  628. c.Assert(eq, Equals, true)
  629. // Download with range, the last 4096
  630. os.Remove(newFile)
  631. options = []Option{Routines(3), NormalizedRange("-4096"), VersionId(versionId)}
  632. err = bucket.DownloadFile(objectName, newFile, 1024, options...)
  633. c.Assert(err, IsNil)
  634. // Check
  635. eq, err = compareFilesWithRange(fileName, fileSize-4096, newFile, 0, 4096)
  636. c.Assert(err, IsNil)
  637. c.Assert(eq, Equals, true)
  638. os.Remove(newFileGet)
  639. options = []Option{NormalizedRange("-4096"), VersionId(versionId)}
  640. err = bucket.GetObjectToFile(objectName, newFileGet, options...)
  641. c.Assert(err, IsNil)
  642. // Compare get and download
  643. eq, err = compareFiles(newFile, newFileGet)
  644. c.Assert(err, IsNil)
  645. c.Assert(eq, Equals, true)
  646. // download whole file
  647. os.Remove(newFileGet)
  648. options = []Option{Routines(3), VersionId(versionId)}
  649. err = bucket.GetObjectToFile(objectName, newFileGet, options...)
  650. c.Assert(err, IsNil)
  651. // Compare get and download
  652. eq, err = compareFiles(fileName, newFileGet)
  653. c.Assert(err, IsNil)
  654. c.Assert(eq, Equals, true)
  655. os.Remove(fileName)
  656. os.Remove(newFileGet)
  657. err = bucket.DeleteObject(objectName)
  658. c.Assert(err, IsNil)
  659. forceDeleteBucket(client, bucketName, c)
  660. }
  661. func (s *OssDownloadSuite) TestVersioningDownloadWithCheckPoint(c *C) {
  662. // create a bucket with default proprety
  663. client, err := New(endpoint, accessID, accessKey)
  664. c.Assert(err, IsNil)
  665. bucketName := bucketNamePrefix + randLowStr(6)
  666. err = client.CreateBucket(bucketName)
  667. c.Assert(err, IsNil)
  668. bucket, err := client.Bucket(bucketName)
  669. // put bucket version:enabled
  670. var versioningConfig VersioningConfig
  671. versioningConfig.Status = string(VersionEnabled)
  672. err = client.SetBucketVersioning(bucketName, versioningConfig)
  673. c.Assert(err, IsNil)
  674. // begin test
  675. objectName := objectNamePrefix + randStr(8)
  676. fileName := "test-file-" + randStr(8)
  677. fileData := randStr(500 * 1024)
  678. createFile(fileName, fileData, c)
  679. newFile := randStr(8) + ".jpg"
  680. // Upload a file
  681. var respHeader http.Header
  682. options := []Option{Routines(3), GetResponseHeader(&respHeader)}
  683. err = bucket.UploadFile(objectName, fileName, 100*1024, options...)
  684. c.Assert(err, IsNil)
  685. versionId := GetVersionId(respHeader)
  686. c.Assert(len(versionId) > 0, Equals, true)
  687. // Resumable download with checkpoint dir
  688. os.Remove(newFile)
  689. downloadPartHooker = DownErrorHooker
  690. options = []Option{CheckpointDir(true, "./"), VersionId(versionId)}
  691. strPayer := getPayer(options)
  692. c.Assert(strPayer, Equals, "")
  693. err = bucket.DownloadFile(objectName, newFile, 100*1024, options...)
  694. c.Assert(err, NotNil)
  695. c.Assert(err.Error(), Equals, "ErrorHooker")
  696. // download again
  697. downloadPartHooker = defaultDownloadPartHook
  698. options = []Option{CheckpointDir(true, "./"), VersionId(versionId), GetResponseHeader(&respHeader)}
  699. err = bucket.DownloadFile(objectName, newFile, 100*1024, options...)
  700. c.Assert(err, IsNil)
  701. c.Assert(GetVersionId(respHeader), Equals, versionId)
  702. eq, err := compareFiles(fileName, newFile)
  703. c.Assert(err, IsNil)
  704. c.Assert(eq, Equals, true)
  705. os.Remove(fileName)
  706. os.Remove(newFile)
  707. err = bucket.DeleteObject(objectName)
  708. c.Assert(err, IsNil)
  709. forceDeleteBucket(client, bucketName, c)
  710. }
  711. func (s *OssDownloadSuite) TestdownloadFileChoiceOptions(c *C) {
  712. // create a bucket with default proprety
  713. client, err := New(endpoint, accessID, accessKey)
  714. c.Assert(err, IsNil)
  715. bucketName := bucketNamePrefix + randLowStr(6)
  716. err = client.CreateBucket(bucketName)
  717. c.Assert(err, IsNil)
  718. bucket, err := client.Bucket(bucketName)
  719. // begin test
  720. objectName := objectNamePrefix + randStr(8)
  721. fileName := "test-file-" + randStr(8)
  722. fileData := randStr(500 * 1024)
  723. createFile(fileName, fileData, c)
  724. newFile := randStr(8) + ".jpg"
  725. // Upload a file
  726. var respHeader http.Header
  727. options := []Option{Routines(3), GetResponseHeader(&respHeader)}
  728. err = bucket.UploadFile(objectName, fileName, 100*1024, options...)
  729. c.Assert(err, IsNil)
  730. // Resumable download with checkpoint dir
  731. os.Remove(newFile)
  732. // downloadFile with properties
  733. options = []Option{
  734. ObjectACL(ACLPublicRead),
  735. RequestPayer(Requester),
  736. TrafficLimitHeader(1024 * 1024 * 8),
  737. }
  738. err = bucket.DownloadFile(objectName, newFile, 100*1024, options...)
  739. c.Assert(err, IsNil)
  740. eq, err := compareFiles(fileName, newFile)
  741. c.Assert(err, IsNil)
  742. c.Assert(eq, Equals, true)
  743. os.Remove(fileName)
  744. os.Remove(newFile)
  745. err = bucket.DeleteObject(objectName)
  746. c.Assert(err, IsNil)
  747. forceDeleteBucket(client, bucketName, c)
  748. }
  749. func (s *OssDownloadSuite) TestdownloadFileWithCpChoiceOptions(c *C) {
  750. // create a bucket with default proprety
  751. client, err := New(endpoint, accessID, accessKey)
  752. c.Assert(err, IsNil)
  753. bucketName := bucketNamePrefix + randLowStr(6)
  754. err = client.CreateBucket(bucketName)
  755. c.Assert(err, IsNil)
  756. bucket, err := client.Bucket(bucketName)
  757. // begin test
  758. objectName := objectNamePrefix + randStr(8)
  759. fileName := "test-file-" + randStr(8)
  760. fileData := randStr(500 * 1024)
  761. createFile(fileName, fileData, c)
  762. newFile := randStr(8) + ".jpg"
  763. // Upload a file
  764. var respHeader http.Header
  765. options := []Option{Routines(3), GetResponseHeader(&respHeader)}
  766. err = bucket.UploadFile(objectName, fileName, 100*1024, options...)
  767. c.Assert(err, IsNil)
  768. // Resumable download with checkpoint dir
  769. os.Remove(newFile)
  770. // DownloadFile with properties
  771. options = []Option{
  772. ObjectACL(ACLPublicRead),
  773. RequestPayer(Requester),
  774. TrafficLimitHeader(1024 * 1024 * 8),
  775. CheckpointDir(true, "./"),
  776. }
  777. err = bucket.DownloadFile(objectName, newFile, 100*1024, options...)
  778. c.Assert(err, IsNil)
  779. eq, err := compareFiles(fileName, newFile)
  780. c.Assert(err, IsNil)
  781. c.Assert(eq, Equals, true)
  782. os.Remove(fileName)
  783. os.Remove(newFile)
  784. err = bucket.DeleteObject(objectName)
  785. c.Assert(err, IsNil)
  786. forceDeleteBucket(client, bucketName, c)
  787. }