multicopy_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. package oss
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. . "gopkg.in/check.v1"
  7. )
  8. type OssCopySuite struct {
  9. client *Client
  10. bucket *Bucket
  11. }
  12. var _ = Suite(&OssCopySuite{})
  13. // SetUpSuite runs once when the suite starts running
  14. func (s *OssCopySuite) SetUpSuite(c *C) {
  15. client, err := New(endpoint, accessID, accessKey)
  16. c.Assert(err, IsNil)
  17. s.client = client
  18. s.client.CreateBucket(bucketName)
  19. bucket, err := s.client.Bucket(bucketName)
  20. c.Assert(err, IsNil)
  21. s.bucket = bucket
  22. testLogger.Println("test copy started")
  23. }
  24. // TearDownSuite runs before each test or benchmark starts running
  25. func (s *OssCopySuite) TearDownSuite(c *C) {
  26. // Delete Part
  27. keyMarker := KeyMarker("")
  28. uploadIDMarker := UploadIDMarker("")
  29. for {
  30. lmur, err := s.bucket.ListMultipartUploads(keyMarker, uploadIDMarker)
  31. c.Assert(err, IsNil)
  32. for _, upload := range lmur.Uploads {
  33. var imur = InitiateMultipartUploadResult{Bucket: bucketName,
  34. Key: upload.Key, UploadID: upload.UploadID}
  35. err = s.bucket.AbortMultipartUpload(imur)
  36. c.Assert(err, IsNil)
  37. }
  38. keyMarker = KeyMarker(lmur.NextKeyMarker)
  39. uploadIDMarker = UploadIDMarker(lmur.NextUploadIDMarker)
  40. if !lmur.IsTruncated {
  41. break
  42. }
  43. }
  44. // Delete objects
  45. marker := Marker("")
  46. for {
  47. lor, err := s.bucket.ListObjects(marker)
  48. c.Assert(err, IsNil)
  49. for _, object := range lor.Objects {
  50. err = s.bucket.DeleteObject(object.Key)
  51. c.Assert(err, IsNil)
  52. }
  53. marker = Marker(lor.NextMarker)
  54. if !lor.IsTruncated {
  55. break
  56. }
  57. }
  58. // Delete bucket
  59. err := s.client.DeleteBucket(s.bucket.BucketName)
  60. c.Assert(err, IsNil)
  61. testLogger.Println("test copy completed")
  62. }
  63. // SetUpTest runs after each test or benchmark runs
  64. func (s *OssCopySuite) SetUpTest(c *C) {
  65. err := removeTempFiles("../oss", ".jpg")
  66. c.Assert(err, IsNil)
  67. }
  68. // TearDownTest runs once after all tests or benchmarks have finished running
  69. func (s *OssCopySuite) TearDownTest(c *C) {
  70. err := removeTempFiles("../oss", ".jpg")
  71. c.Assert(err, IsNil)
  72. }
  73. // TestCopyRoutineWithoutRecovery is multi-routine copy without resumable recovery
  74. func (s *OssCopySuite) TestCopyRoutineWithoutRecovery(c *C) {
  75. srcObjectName := objectNamePrefix + randStr(8)
  76. destObjectName := srcObjectName + "-dest"
  77. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  78. newFile := "copy-new-file.jpg"
  79. // Upload source file
  80. err := s.bucket.UploadFile(srcObjectName, fileName, 100*1024, Routines(3))
  81. c.Assert(err, IsNil)
  82. os.Remove(newFile)
  83. // Does not specify parameter 'routines', by default it's single routine
  84. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 100*1024)
  85. c.Assert(err, IsNil)
  86. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  87. c.Assert(err, IsNil)
  88. eq, err := compareFiles(fileName, newFile)
  89. c.Assert(err, IsNil)
  90. c.Assert(eq, Equals, true)
  91. err = s.bucket.DeleteObject(destObjectName)
  92. c.Assert(err, IsNil)
  93. os.Remove(newFile)
  94. // Specify one routine.
  95. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 100*1024, Routines(1))
  96. c.Assert(err, IsNil)
  97. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  98. c.Assert(err, IsNil)
  99. eq, err = compareFiles(fileName, newFile)
  100. c.Assert(err, IsNil)
  101. c.Assert(eq, Equals, true)
  102. err = s.bucket.DeleteObject(destObjectName)
  103. c.Assert(err, IsNil)
  104. os.Remove(newFile)
  105. // Specify three routines, which is less than parts count 5
  106. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 100*1024, Routines(3))
  107. c.Assert(err, IsNil)
  108. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  109. c.Assert(err, IsNil)
  110. eq, err = compareFiles(fileName, newFile)
  111. c.Assert(err, IsNil)
  112. c.Assert(eq, Equals, true)
  113. err = s.bucket.DeleteObject(destObjectName)
  114. c.Assert(err, IsNil)
  115. os.Remove(newFile)
  116. // Specify 5 routines which is the same as parts count
  117. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 100*1024, Routines(5))
  118. c.Assert(err, IsNil)
  119. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  120. c.Assert(err, IsNil)
  121. eq, err = compareFiles(fileName, newFile)
  122. c.Assert(err, IsNil)
  123. c.Assert(eq, Equals, true)
  124. err = s.bucket.DeleteObject(destObjectName)
  125. c.Assert(err, IsNil)
  126. os.Remove(newFile)
  127. // Specify routine count 10, which is more than parts count
  128. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 100*1024, Routines(10))
  129. c.Assert(err, IsNil)
  130. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  131. c.Assert(err, IsNil)
  132. eq, err = compareFiles(fileName, newFile)
  133. c.Assert(err, IsNil)
  134. c.Assert(eq, Equals, true)
  135. err = s.bucket.DeleteObject(destObjectName)
  136. c.Assert(err, IsNil)
  137. os.Remove(newFile)
  138. // Invalid routine count, will use single routine
  139. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 100*1024, Routines(-1))
  140. c.Assert(err, IsNil)
  141. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  142. c.Assert(err, IsNil)
  143. eq, err = compareFiles(fileName, newFile)
  144. c.Assert(err, IsNil)
  145. c.Assert(eq, Equals, true)
  146. err = s.bucket.DeleteObject(destObjectName)
  147. c.Assert(err, IsNil)
  148. os.Remove(newFile)
  149. // Option
  150. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 100*1024, Routines(3), Meta("myprop", "mypropval"))
  151. meta, err := s.bucket.GetObjectDetailedMeta(destObjectName)
  152. c.Assert(err, IsNil)
  153. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  154. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  155. c.Assert(err, IsNil)
  156. eq, err = compareFiles(fileName, newFile)
  157. c.Assert(err, IsNil)
  158. c.Assert(eq, Equals, true)
  159. err = s.bucket.DeleteObject(destObjectName)
  160. c.Assert(err, IsNil)
  161. os.Remove(newFile)
  162. err = s.bucket.DeleteObject(srcObjectName)
  163. c.Assert(err, IsNil)
  164. }
  165. // CopyErrorHooker is a copypart request hook
  166. func CopyErrorHooker(part copyPart) error {
  167. if part.Number == 5 {
  168. time.Sleep(time.Second)
  169. return fmt.Errorf("ErrorHooker")
  170. }
  171. return nil
  172. }
  173. // TestCopyRoutineWithoutRecoveryNegative is a multiple routines copy without checkpoint
  174. func (s *OssCopySuite) TestCopyRoutineWithoutRecoveryNegative(c *C) {
  175. srcObjectName := objectNamePrefix + randStr(8)
  176. destObjectName := srcObjectName + "-dest"
  177. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  178. // Upload source file
  179. err := s.bucket.UploadFile(srcObjectName, fileName, 100*1024, Routines(3))
  180. c.Assert(err, IsNil)
  181. copyPartHooker = CopyErrorHooker
  182. // Worker routine errors
  183. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 100*1024, Routines(2))
  184. c.Assert(err, NotNil)
  185. c.Assert(err.Error(), Equals, "ErrorHooker")
  186. copyPartHooker = defaultCopyPartHook
  187. // Source bucket does not exist
  188. err = s.bucket.CopyFile("NotExist", srcObjectName, destObjectName, 100*1024, Routines(2))
  189. c.Assert(err, NotNil)
  190. // Target object does not exist
  191. err = s.bucket.CopyFile(bucketName, "NotExist", destObjectName, 100*1024, Routines(2))
  192. // The part size is invalid
  193. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024, Routines(2))
  194. c.Assert(err, NotNil)
  195. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*1024*1024*100, Routines(2))
  196. c.Assert(err, NotNil)
  197. // Delete the source file
  198. err = s.bucket.DeleteObject(srcObjectName)
  199. c.Assert(err, IsNil)
  200. }
  201. // TestCopyRoutineWithRecovery is a multiple routines copy with resumable recovery
  202. func (s *OssCopySuite) TestCopyRoutineWithRecovery(c *C) {
  203. srcObjectName := objectNamePrefix + randStr(8)
  204. destObjectName := srcObjectName + "-dest"
  205. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  206. newFile := randStr(8) + ".jpg"
  207. // Upload source file
  208. err := s.bucket.UploadFile(srcObjectName, fileName, 100*1024, Routines(3))
  209. c.Assert(err, IsNil)
  210. os.Remove(newFile)
  211. // Routines default value, CP's default path is destObjectName+.cp
  212. // Copy object with checkpoint enabled, single runtine.
  213. // Copy 4 parts---the CopyErrorHooker makes sure the copy of part 5 will fail.
  214. copyPartHooker = CopyErrorHooker
  215. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Checkpoint(true, destObjectName+".cp"))
  216. c.Assert(err, NotNil)
  217. c.Assert(err.Error(), Equals, "ErrorHooker")
  218. copyPartHooker = defaultCopyPartHook
  219. // Check CP
  220. ccp := copyCheckpoint{}
  221. err = ccp.load(destObjectName + ".cp")
  222. c.Assert(err, IsNil)
  223. c.Assert(ccp.Magic, Equals, copyCpMagic)
  224. c.Assert(len(ccp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
  225. c.Assert(ccp.SrcBucketName, Equals, bucketName)
  226. c.Assert(ccp.SrcObjectKey, Equals, srcObjectName)
  227. c.Assert(ccp.DestBucketName, Equals, bucketName)
  228. c.Assert(ccp.DestObjectKey, Equals, destObjectName)
  229. c.Assert(len(ccp.CopyID), Equals, len("3F79722737D1469980DACEDCA325BB52"))
  230. c.Assert(ccp.ObjStat.Size, Equals, int64(482048))
  231. c.Assert(len(ccp.ObjStat.LastModified), Equals, len("2015-12-17 18:43:03 +0800 CST"))
  232. c.Assert(ccp.ObjStat.Etag, Equals, "\"2351E662233817A7AE974D8C5B0876DD-5\"")
  233. c.Assert(len(ccp.Parts), Equals, 5)
  234. c.Assert(len(ccp.todoParts()), Equals, 1)
  235. c.Assert(ccp.PartStat[4], Equals, false)
  236. // Second copy, finish the last part
  237. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Checkpoint(true, destObjectName+".cp"))
  238. c.Assert(err, IsNil)
  239. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  240. c.Assert(err, IsNil)
  241. eq, err := compareFiles(fileName, newFile)
  242. c.Assert(err, IsNil)
  243. c.Assert(eq, Equals, true)
  244. err = s.bucket.DeleteObject(destObjectName)
  245. c.Assert(err, IsNil)
  246. os.Remove(newFile)
  247. err = ccp.load(fileName + ".cp")
  248. c.Assert(err, NotNil)
  249. //multicopy with empty checkpoint path
  250. copyPartHooker = CopyErrorHooker
  251. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Checkpoint(true, ""))
  252. c.Assert(err, NotNil)
  253. c.Assert(err.Error(), Equals, "ErrorHooker")
  254. copyPartHooker = defaultCopyPartHook
  255. ccp = copyCheckpoint{}
  256. err = ccp.load(destObjectName + ".cp")
  257. c.Assert(err, NotNil)
  258. //multi copy with checkpoint dir
  259. copyPartHooker = CopyErrorHooker
  260. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(2), CheckpointDir(true, "./"))
  261. c.Assert(err, NotNil)
  262. c.Assert(err.Error(), Equals, "ErrorHooker")
  263. copyPartHooker = defaultCopyPartHook
  264. // Check CP
  265. ccp = copyCheckpoint{}
  266. cpConf := cpConfig{IsEnable: true, DirPath: "./"}
  267. cpFilePath := getCopyCpFilePath(&cpConf, bucketName, srcObjectName, s.bucket.BucketName, destObjectName)
  268. err = ccp.load(cpFilePath)
  269. c.Assert(err, IsNil)
  270. c.Assert(ccp.Magic, Equals, copyCpMagic)
  271. c.Assert(len(ccp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
  272. c.Assert(ccp.SrcBucketName, Equals, bucketName)
  273. c.Assert(ccp.SrcObjectKey, Equals, srcObjectName)
  274. c.Assert(ccp.DestBucketName, Equals, bucketName)
  275. c.Assert(ccp.DestObjectKey, Equals, destObjectName)
  276. c.Assert(len(ccp.CopyID), Equals, len("3F79722737D1469980DACEDCA325BB52"))
  277. c.Assert(ccp.ObjStat.Size, Equals, int64(482048))
  278. c.Assert(len(ccp.ObjStat.LastModified), Equals, len("2015-12-17 18:43:03 +0800 CST"))
  279. c.Assert(ccp.ObjStat.Etag, Equals, "\"2351E662233817A7AE974D8C5B0876DD-5\"")
  280. c.Assert(len(ccp.Parts), Equals, 5)
  281. c.Assert(len(ccp.todoParts()), Equals, 1)
  282. c.Assert(ccp.PartStat[4], Equals, false)
  283. // Second copy, finish the last part.
  284. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(2), CheckpointDir(true, "./"))
  285. c.Assert(err, IsNil)
  286. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  287. c.Assert(err, IsNil)
  288. eq, err = compareFiles(fileName, newFile)
  289. c.Assert(err, IsNil)
  290. c.Assert(eq, Equals, true)
  291. err = s.bucket.DeleteObject(destObjectName)
  292. c.Assert(err, IsNil)
  293. os.Remove(newFile)
  294. err = ccp.load(srcObjectName + ".cp")
  295. c.Assert(err, NotNil)
  296. // First copy without error.
  297. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(3), Checkpoint(true, destObjectName+".cp"))
  298. c.Assert(err, IsNil)
  299. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  300. c.Assert(err, IsNil)
  301. eq, err = compareFiles(fileName, newFile)
  302. c.Assert(err, IsNil)
  303. c.Assert(eq, Equals, true)
  304. err = s.bucket.DeleteObject(destObjectName)
  305. c.Assert(err, IsNil)
  306. os.Remove(newFile)
  307. // Copy with multiple coroutines, no errors.
  308. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(10), Checkpoint(true, destObjectName+".cp"))
  309. c.Assert(err, IsNil)
  310. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  311. c.Assert(err, IsNil)
  312. eq, err = compareFiles(fileName, newFile)
  313. c.Assert(err, IsNil)
  314. c.Assert(eq, Equals, true)
  315. err = s.bucket.DeleteObject(destObjectName)
  316. c.Assert(err, IsNil)
  317. os.Remove(newFile)
  318. // Option
  319. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(5), Checkpoint(true, destObjectName+".cp"), Meta("myprop", "mypropval"))
  320. c.Assert(err, IsNil)
  321. meta, err := s.bucket.GetObjectDetailedMeta(destObjectName)
  322. c.Assert(err, IsNil)
  323. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  324. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  325. c.Assert(err, IsNil)
  326. eq, err = compareFiles(fileName, newFile)
  327. c.Assert(err, IsNil)
  328. c.Assert(eq, Equals, true)
  329. err = s.bucket.DeleteObject(destObjectName)
  330. c.Assert(err, IsNil)
  331. os.Remove(newFile)
  332. // Delete the source file
  333. err = s.bucket.DeleteObject(srcObjectName)
  334. c.Assert(err, IsNil)
  335. }
  336. // TestCopyRoutineWithRecoveryNegative is a multiple routineed copy without checkpoint
  337. func (s *OssCopySuite) TestCopyRoutineWithRecoveryNegative(c *C) {
  338. srcObjectName := objectNamePrefix + randStr(8)
  339. destObjectName := srcObjectName + "-dest"
  340. // Source bucket does not exist
  341. err := s.bucket.CopyFile("NotExist", srcObjectName, destObjectName, 100*1024, Checkpoint(true, destObjectName+".cp"))
  342. c.Assert(err, NotNil)
  343. c.Assert(err, NotNil)
  344. // Source object does not exist
  345. err = s.bucket.CopyFile(bucketName, "NotExist", destObjectName, 100*1024, Routines(2), Checkpoint(true, destObjectName+".cp"))
  346. c.Assert(err, NotNil)
  347. // Specify part size is invalid.
  348. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024, Checkpoint(true, destObjectName+".cp"))
  349. c.Assert(err, NotNil)
  350. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*1024*1024*100, Routines(2), Checkpoint(true, destObjectName+".cp"))
  351. c.Assert(err, NotNil)
  352. }
  353. // TestCopyFileCrossBucket is a cross bucket's direct copy.
  354. func (s *OssCopySuite) TestCopyFileCrossBucket(c *C) {
  355. destBucketName := bucketName + "-desc"
  356. srcObjectName := objectNamePrefix + randStr(8)
  357. destObjectName := srcObjectName + "-dest"
  358. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  359. newFile := randStr(8) + ".jpg"
  360. destBucket, err := s.client.Bucket(destBucketName)
  361. c.Assert(err, IsNil)
  362. // Create a target bucket
  363. err = s.client.CreateBucket(destBucketName)
  364. // Upload source file
  365. err = s.bucket.UploadFile(srcObjectName, fileName, 100*1024, Routines(3))
  366. c.Assert(err, IsNil)
  367. os.Remove(newFile)
  368. // Copy files
  369. err = destBucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(5), Checkpoint(true, destObjectName+".cp"))
  370. c.Assert(err, IsNil)
  371. err = destBucket.GetObjectToFile(destObjectName, newFile)
  372. c.Assert(err, IsNil)
  373. eq, err := compareFiles(fileName, newFile)
  374. c.Assert(err, IsNil)
  375. c.Assert(eq, Equals, true)
  376. err = destBucket.DeleteObject(destObjectName)
  377. c.Assert(err, IsNil)
  378. os.Remove(newFile)
  379. // Copy file with options
  380. err = destBucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(10), Checkpoint(true, "copy.cp"), Meta("myprop", "mypropval"))
  381. c.Assert(err, IsNil)
  382. err = destBucket.GetObjectToFile(destObjectName, newFile)
  383. c.Assert(err, IsNil)
  384. eq, err = compareFiles(fileName, newFile)
  385. c.Assert(err, IsNil)
  386. c.Assert(eq, Equals, true)
  387. err = destBucket.DeleteObject(destObjectName)
  388. c.Assert(err, IsNil)
  389. os.Remove(newFile)
  390. // Delete target bucket
  391. err = s.client.DeleteBucket(destBucketName)
  392. c.Assert(err, IsNil)
  393. }