multicopy_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. // Run 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. // Run 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. // Run 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. // Run 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 多线程无断点恢复的复制
  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. // 上传源文件
  62. err := s.bucket.UploadFile(srcObjectName, fileName, 100*1024, Routines(3))
  63. c.Assert(err, IsNil)
  64. os.Remove(newFile)
  65. // 不指定Routines,默认单线程
  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. // 指定线程数1
  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. // 指定线程数3,小于分片数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. // 指定线程数5,等于分片数
  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. // 指定线程数10,大于分片数5
  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. // 线程值无效自动变成1
  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 CopyPart请求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 多线程无断点恢复的复制
  156. func (s *OssCopySuite) TestCopyRoutineWithoutRecoveryNegative(c *C) {
  157. srcObjectName := objectNamePrefix + "tcrwrn"
  158. destObjectName := srcObjectName + "-copy"
  159. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  160. // 上传源文件
  161. err := s.bucket.UploadFile(srcObjectName, fileName, 100*1024, Routines(3))
  162. c.Assert(err, IsNil)
  163. copyPartHooker = CopyErrorHooker
  164. // worker线程错误
  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. // 源Bucket不存在
  170. err = s.bucket.CopyFile("NotExist", srcObjectName, destObjectName, 100*1024, Routines(2))
  171. c.Assert(err, NotNil)
  172. // 源Object不存在
  173. err = s.bucket.CopyFile(bucketName, "NotExist", destObjectName, 100*1024, Routines(2))
  174. // 指定的分片大小无效
  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. // 删除源文件
  180. err = s.bucket.DeleteObject(srcObjectName)
  181. c.Assert(err, IsNil)
  182. }
  183. // TestCopyRoutineWithRecovery 多线程且有断点恢复的复制
  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. // 上传源文件
  190. err := s.bucket.UploadFile(srcObjectName, fileName, 100*1024, Routines(3))
  191. c.Assert(err, IsNil)
  192. os.Remove(newFile)
  193. // Routines默认值,CP开启默认路径是destObjectName+.cp
  194. // 第一次上传,上传4片
  195. copyPartHooker = CopyErrorHooker
  196. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Checkpoint(true, ""))
  197. c.Assert(err, NotNil)
  198. c.Assert(err.Error(), Equals, "ErrorHooker")
  199. copyPartHooker = defaultCopyPartHook
  200. // check cp
  201. ccp := copyCheckpoint{}
  202. err = ccp.load(destObjectName + ".cp")
  203. c.Assert(err, IsNil)
  204. c.Assert(ccp.Magic, Equals, copyCpMagic)
  205. c.Assert(len(ccp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
  206. c.Assert(ccp.SrcBucketName, Equals, bucketName)
  207. c.Assert(ccp.SrcObjectKey, Equals, srcObjectName)
  208. c.Assert(ccp.DestBucketName, Equals, bucketName)
  209. c.Assert(ccp.DestObjectKey, Equals, destObjectName)
  210. c.Assert(len(ccp.CopyID), Equals, len("3F79722737D1469980DACEDCA325BB52"))
  211. c.Assert(ccp.ObjStat.Size, Equals, int64(482048))
  212. c.Assert(len(ccp.ObjStat.LastModified), Equals, len("2015-12-17 18:43:03 +0800 CST"))
  213. c.Assert(ccp.ObjStat.Etag, Equals, "\"2351E662233817A7AE974D8C5B0876DD-5\"")
  214. c.Assert(len(ccp.Parts), Equals, 5)
  215. c.Assert(len(ccp.todoParts()), Equals, 1)
  216. c.Assert(ccp.PartStat[4], Equals, false)
  217. // 第二次上传,完成剩余的一片
  218. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Checkpoint(true, ""))
  219. c.Assert(err, IsNil)
  220. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  221. c.Assert(err, IsNil)
  222. eq, err := compareFiles(fileName, newFile)
  223. c.Assert(err, IsNil)
  224. c.Assert(eq, Equals, true)
  225. err = s.bucket.DeleteObject(destObjectName)
  226. c.Assert(err, IsNil)
  227. os.Remove(newFile)
  228. err = ccp.load(fileName + ".cp")
  229. c.Assert(err, NotNil)
  230. // Routines指定,CP指定
  231. copyPartHooker = CopyErrorHooker
  232. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(2), Checkpoint(true, srcObjectName+".cp"))
  233. c.Assert(err, NotNil)
  234. c.Assert(err.Error(), Equals, "ErrorHooker")
  235. copyPartHooker = defaultCopyPartHook
  236. // check cp
  237. ccp = copyCheckpoint{}
  238. err = ccp.load(srcObjectName + ".cp")
  239. c.Assert(err, IsNil)
  240. c.Assert(ccp.Magic, Equals, copyCpMagic)
  241. c.Assert(len(ccp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
  242. c.Assert(ccp.SrcBucketName, Equals, bucketName)
  243. c.Assert(ccp.SrcObjectKey, Equals, srcObjectName)
  244. c.Assert(ccp.DestBucketName, Equals, bucketName)
  245. c.Assert(ccp.DestObjectKey, Equals, destObjectName)
  246. c.Assert(len(ccp.CopyID), Equals, len("3F79722737D1469980DACEDCA325BB52"))
  247. c.Assert(ccp.ObjStat.Size, Equals, int64(482048))
  248. c.Assert(len(ccp.ObjStat.LastModified), Equals, len("2015-12-17 18:43:03 +0800 CST"))
  249. c.Assert(ccp.ObjStat.Etag, Equals, "\"2351E662233817A7AE974D8C5B0876DD-5\"")
  250. c.Assert(len(ccp.Parts), Equals, 5)
  251. c.Assert(len(ccp.todoParts()), Equals, 1)
  252. c.Assert(ccp.PartStat[4], Equals, false)
  253. // 第二次上传,完成剩余的一片
  254. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(2), Checkpoint(true, srcObjectName+".cp"))
  255. c.Assert(err, IsNil)
  256. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  257. c.Assert(err, IsNil)
  258. eq, err = compareFiles(fileName, newFile)
  259. c.Assert(err, IsNil)
  260. c.Assert(eq, Equals, true)
  261. err = s.bucket.DeleteObject(destObjectName)
  262. c.Assert(err, IsNil)
  263. os.Remove(newFile)
  264. err = ccp.load(srcObjectName + ".cp")
  265. c.Assert(err, NotNil)
  266. // 一次完成上传,中间没有错误
  267. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(3), Checkpoint(true, ""))
  268. c.Assert(err, IsNil)
  269. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  270. c.Assert(err, IsNil)
  271. eq, err = compareFiles(fileName, newFile)
  272. c.Assert(err, IsNil)
  273. c.Assert(eq, Equals, true)
  274. err = s.bucket.DeleteObject(destObjectName)
  275. c.Assert(err, IsNil)
  276. os.Remove(newFile)
  277. // 用多协程下载,中间没有错误
  278. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(10), Checkpoint(true, ""))
  279. c.Assert(err, IsNil)
  280. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  281. c.Assert(err, IsNil)
  282. eq, err = compareFiles(fileName, newFile)
  283. c.Assert(err, IsNil)
  284. c.Assert(eq, Equals, true)
  285. err = s.bucket.DeleteObject(destObjectName)
  286. c.Assert(err, IsNil)
  287. os.Remove(newFile)
  288. // option
  289. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(5), Checkpoint(true, ""), Meta("myprop", "mypropval"))
  290. c.Assert(err, IsNil)
  291. meta, err := s.bucket.GetObjectDetailedMeta(destObjectName)
  292. c.Assert(err, IsNil)
  293. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  294. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  295. c.Assert(err, IsNil)
  296. eq, err = compareFiles(fileName, newFile)
  297. c.Assert(err, IsNil)
  298. c.Assert(eq, Equals, true)
  299. err = s.bucket.DeleteObject(destObjectName)
  300. c.Assert(err, IsNil)
  301. os.Remove(newFile)
  302. // 删除源文件
  303. err = s.bucket.DeleteObject(srcObjectName)
  304. c.Assert(err, IsNil)
  305. }
  306. // TestCopyRoutineWithRecoveryNegative 多线程无断点恢复的复制
  307. func (s *OssCopySuite) TestCopyRoutineWithRecoveryNegative(c *C) {
  308. srcObjectName := objectNamePrefix + "tcrwrn"
  309. destObjectName := srcObjectName + "-copy"
  310. // 源Bucket不存在
  311. err := s.bucket.CopyFile("NotExist", srcObjectName, destObjectName, 100*1024, Checkpoint(true, ""))
  312. c.Assert(err, NotNil)
  313. c.Assert(err, NotNil)
  314. // 源Object不存在
  315. err = s.bucket.CopyFile(bucketName, "NotExist", destObjectName, 100*1024, Routines(2), Checkpoint(true, ""))
  316. c.Assert(err, NotNil)
  317. // 指定的分片大小无效
  318. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024, Checkpoint(true, ""))
  319. c.Assert(err, NotNil)
  320. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*1024*1024*100, Routines(2), Checkpoint(true, ""))
  321. c.Assert(err, NotNil)
  322. }
  323. // TestCopyFileCrossBucket 跨Bucket直接的复制
  324. func (s *OssCopySuite) TestCopyFileCrossBucket(c *C) {
  325. destBucketName := bucketName + "-cfcb-desc"
  326. srcObjectName := objectNamePrefix + "tcrtr"
  327. destObjectName := srcObjectName + "-copy"
  328. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  329. newFile := "copy-new-file.jpg"
  330. destBucket, err := s.client.Bucket(destBucketName)
  331. c.Assert(err, IsNil)
  332. // 创建目标Bucket
  333. err = s.client.CreateBucket(destBucketName)
  334. // 上传源文件
  335. err = s.bucket.UploadFile(srcObjectName, fileName, 100*1024, Routines(3))
  336. c.Assert(err, IsNil)
  337. os.Remove(newFile)
  338. // 复制文件
  339. err = destBucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(5), Checkpoint(true, ""))
  340. c.Assert(err, IsNil)
  341. err = destBucket.GetObjectToFile(destObjectName, newFile)
  342. c.Assert(err, IsNil)
  343. eq, err := compareFiles(fileName, newFile)
  344. c.Assert(err, IsNil)
  345. c.Assert(eq, Equals, true)
  346. err = destBucket.DeleteObject(destObjectName)
  347. c.Assert(err, IsNil)
  348. os.Remove(newFile)
  349. // 带option的复制
  350. err = destBucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(10), Checkpoint(true, "copy.cp"), Meta("myprop", "mypropval"))
  351. c.Assert(err, IsNil)
  352. err = destBucket.GetObjectToFile(destObjectName, newFile)
  353. c.Assert(err, IsNil)
  354. eq, err = compareFiles(fileName, newFile)
  355. c.Assert(err, IsNil)
  356. c.Assert(eq, Equals, true)
  357. err = destBucket.DeleteObject(destObjectName)
  358. c.Assert(err, IsNil)
  359. os.Remove(newFile)
  360. // 删除目标Bucket
  361. err = s.client.DeleteBucket(destBucketName)
  362. c.Assert(err, IsNil)
  363. }