multicopy_test.go 15 KB

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