multicopy_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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, ""))
  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, ""))
  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. // Specify Routine and CP's path
  232. copyPartHooker = CopyErrorHooker
  233. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(2), Checkpoint(true, srcObjectName+".cp"))
  234. c.Assert(err, NotNil)
  235. c.Assert(err.Error(), Equals, "ErrorHooker")
  236. copyPartHooker = defaultCopyPartHook
  237. // Check CP
  238. ccp = copyCheckpoint{}
  239. err = ccp.load(srcObjectName + ".cp")
  240. c.Assert(err, IsNil)
  241. c.Assert(ccp.Magic, Equals, copyCpMagic)
  242. c.Assert(len(ccp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
  243. c.Assert(ccp.SrcBucketName, Equals, bucketName)
  244. c.Assert(ccp.SrcObjectKey, Equals, srcObjectName)
  245. c.Assert(ccp.DestBucketName, Equals, bucketName)
  246. c.Assert(ccp.DestObjectKey, Equals, destObjectName)
  247. c.Assert(len(ccp.CopyID), Equals, len("3F79722737D1469980DACEDCA325BB52"))
  248. c.Assert(ccp.ObjStat.Size, Equals, int64(482048))
  249. c.Assert(len(ccp.ObjStat.LastModified), Equals, len("2015-12-17 18:43:03 +0800 CST"))
  250. c.Assert(ccp.ObjStat.Etag, Equals, "\"2351E662233817A7AE974D8C5B0876DD-5\"")
  251. c.Assert(len(ccp.Parts), Equals, 5)
  252. c.Assert(len(ccp.todoParts()), Equals, 1)
  253. c.Assert(ccp.PartStat[4], Equals, false)
  254. // Second copy, finish the last part.
  255. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(2), Checkpoint(true, srcObjectName+".cp"))
  256. c.Assert(err, IsNil)
  257. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  258. c.Assert(err, IsNil)
  259. eq, err = compareFiles(fileName, newFile)
  260. c.Assert(err, IsNil)
  261. c.Assert(eq, Equals, true)
  262. err = s.bucket.DeleteObject(destObjectName)
  263. c.Assert(err, IsNil)
  264. os.Remove(newFile)
  265. err = ccp.load(srcObjectName + ".cp")
  266. c.Assert(err, NotNil)
  267. // First copy without error.
  268. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(3), Checkpoint(true, ""))
  269. c.Assert(err, IsNil)
  270. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  271. c.Assert(err, IsNil)
  272. eq, err = compareFiles(fileName, newFile)
  273. c.Assert(err, IsNil)
  274. c.Assert(eq, Equals, true)
  275. err = s.bucket.DeleteObject(destObjectName)
  276. c.Assert(err, IsNil)
  277. os.Remove(newFile)
  278. // Copy with multiple coroutines, no errors.
  279. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(10), Checkpoint(true, ""))
  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. // Option
  290. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(5), Checkpoint(true, ""), Meta("myprop", "mypropval"))
  291. c.Assert(err, IsNil)
  292. meta, err := s.bucket.GetObjectDetailedMeta(destObjectName)
  293. c.Assert(err, IsNil)
  294. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  295. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  296. c.Assert(err, IsNil)
  297. eq, err = compareFiles(fileName, newFile)
  298. c.Assert(err, IsNil)
  299. c.Assert(eq, Equals, true)
  300. err = s.bucket.DeleteObject(destObjectName)
  301. c.Assert(err, IsNil)
  302. os.Remove(newFile)
  303. // Delete the source file
  304. err = s.bucket.DeleteObject(srcObjectName)
  305. c.Assert(err, IsNil)
  306. }
  307. // TestCopyRoutineWithRecoveryNegative is a multiple routineed copy without checkpoint
  308. func (s *OssCopySuite) TestCopyRoutineWithRecoveryNegative(c *C) {
  309. srcObjectName := objectNamePrefix + "tcrwrn"
  310. destObjectName := srcObjectName + "-copy"
  311. // Source bucket does not exist
  312. err := s.bucket.CopyFile("NotExist", srcObjectName, destObjectName, 100*1024, Checkpoint(true, ""))
  313. c.Assert(err, NotNil)
  314. c.Assert(err, NotNil)
  315. // Source object does not exist
  316. err = s.bucket.CopyFile(bucketName, "NotExist", destObjectName, 100*1024, Routines(2), Checkpoint(true, ""))
  317. c.Assert(err, NotNil)
  318. // Specify part size is invalid.
  319. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024, Checkpoint(true, ""))
  320. c.Assert(err, NotNil)
  321. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*1024*1024*100, Routines(2), Checkpoint(true, ""))
  322. c.Assert(err, NotNil)
  323. }
  324. // TestCopyFileCrossBucket is a cross bucket's direct copy.
  325. func (s *OssCopySuite) TestCopyFileCrossBucket(c *C) {
  326. destBucketName := bucketName + "-cfcb-desc"
  327. srcObjectName := objectNamePrefix + "tcrtr"
  328. destObjectName := srcObjectName + "-copy"
  329. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  330. newFile := "copy-new-file.jpg"
  331. destBucket, err := s.client.Bucket(destBucketName)
  332. c.Assert(err, IsNil)
  333. // Create a target bucket
  334. err = s.client.CreateBucket(destBucketName)
  335. // Upload source file
  336. err = s.bucket.UploadFile(srcObjectName, fileName, 100*1024, Routines(3))
  337. c.Assert(err, IsNil)
  338. os.Remove(newFile)
  339. // Copy files
  340. err = destBucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(5), Checkpoint(true, ""))
  341. c.Assert(err, IsNil)
  342. err = destBucket.GetObjectToFile(destObjectName, newFile)
  343. c.Assert(err, IsNil)
  344. eq, err := compareFiles(fileName, newFile)
  345. c.Assert(err, IsNil)
  346. c.Assert(eq, Equals, true)
  347. err = destBucket.DeleteObject(destObjectName)
  348. c.Assert(err, IsNil)
  349. os.Remove(newFile)
  350. // Copy file with options
  351. err = destBucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(10), Checkpoint(true, "copy.cp"), Meta("myprop", "mypropval"))
  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. // Delete target bucket
  362. err = s.client.DeleteBucket(destBucketName)
  363. c.Assert(err, IsNil)
  364. }