multicopy_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. package oss
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "strings"
  7. "time"
  8. . "gopkg.in/check.v1"
  9. )
  10. type OssCopySuite struct {
  11. client *Client
  12. bucket *Bucket
  13. }
  14. var _ = Suite(&OssCopySuite{})
  15. // SetUpSuite runs once when the suite starts running
  16. func (s *OssCopySuite) SetUpSuite(c *C) {
  17. client, err := New(endpoint, accessID, accessKey)
  18. c.Assert(err, IsNil)
  19. s.client = client
  20. s.client.CreateBucket(bucketName)
  21. bucket, err := s.client.Bucket(bucketName)
  22. c.Assert(err, IsNil)
  23. s.bucket = bucket
  24. testLogger.Println("test copy started")
  25. }
  26. // TearDownSuite runs before each test or benchmark starts running
  27. func (s *OssCopySuite) TearDownSuite(c *C) {
  28. // Delete Part
  29. keyMarker := KeyMarker("")
  30. uploadIDMarker := UploadIDMarker("")
  31. for {
  32. lmur, err := s.bucket.ListMultipartUploads(keyMarker, uploadIDMarker)
  33. c.Assert(err, IsNil)
  34. for _, upload := range lmur.Uploads {
  35. var imur = InitiateMultipartUploadResult{Bucket: bucketName,
  36. Key: upload.Key, UploadID: upload.UploadID}
  37. err = s.bucket.AbortMultipartUpload(imur)
  38. c.Assert(err, IsNil)
  39. }
  40. keyMarker = KeyMarker(lmur.NextKeyMarker)
  41. uploadIDMarker = UploadIDMarker(lmur.NextUploadIDMarker)
  42. if !lmur.IsTruncated {
  43. break
  44. }
  45. }
  46. // Delete objects
  47. marker := Marker("")
  48. for {
  49. lor, err := s.bucket.ListObjects(marker)
  50. c.Assert(err, IsNil)
  51. for _, object := range lor.Objects {
  52. err = s.bucket.DeleteObject(object.Key)
  53. c.Assert(err, IsNil)
  54. }
  55. marker = Marker(lor.NextMarker)
  56. if !lor.IsTruncated {
  57. break
  58. }
  59. }
  60. // Delete bucket
  61. err := s.client.DeleteBucket(s.bucket.BucketName)
  62. c.Assert(err, IsNil)
  63. testLogger.Println("test copy completed")
  64. }
  65. // SetUpTest runs after each test or benchmark runs
  66. func (s *OssCopySuite) SetUpTest(c *C) {
  67. err := removeTempFiles("../oss", ".jpg")
  68. c.Assert(err, IsNil)
  69. }
  70. // TearDownTest runs once after all tests or benchmarks have finished running
  71. func (s *OssCopySuite) TearDownTest(c *C) {
  72. err := removeTempFiles("../oss", ".jpg")
  73. c.Assert(err, IsNil)
  74. }
  75. // TestCopyRoutineWithoutRecovery is multi-routine copy without resumable recovery
  76. func (s *OssCopySuite) TestCopyRoutineWithoutRecovery(c *C) {
  77. srcObjectName := objectNamePrefix + randStr(8)
  78. destObjectName := srcObjectName + "-dest"
  79. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  80. newFile := "copy-new-file.jpg"
  81. // Upload source file
  82. err := s.bucket.UploadFile(srcObjectName, fileName, 100*1024, Routines(3))
  83. c.Assert(err, IsNil)
  84. os.Remove(newFile)
  85. // Does not specify parameter 'routines', by default it's single routine
  86. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 100*1024)
  87. c.Assert(err, IsNil)
  88. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  89. c.Assert(err, IsNil)
  90. eq, err := compareFiles(fileName, newFile)
  91. c.Assert(err, IsNil)
  92. c.Assert(eq, Equals, true)
  93. err = s.bucket.DeleteObject(destObjectName)
  94. c.Assert(err, IsNil)
  95. os.Remove(newFile)
  96. // Specify one routine.
  97. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 100*1024, Routines(1))
  98. c.Assert(err, IsNil)
  99. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  100. c.Assert(err, IsNil)
  101. eq, err = compareFiles(fileName, newFile)
  102. c.Assert(err, IsNil)
  103. c.Assert(eq, Equals, true)
  104. err = s.bucket.DeleteObject(destObjectName)
  105. c.Assert(err, IsNil)
  106. os.Remove(newFile)
  107. // Specify three routines, which is less than parts count 5
  108. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 100*1024, Routines(3))
  109. c.Assert(err, IsNil)
  110. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  111. c.Assert(err, IsNil)
  112. eq, err = compareFiles(fileName, newFile)
  113. c.Assert(err, IsNil)
  114. c.Assert(eq, Equals, true)
  115. err = s.bucket.DeleteObject(destObjectName)
  116. c.Assert(err, IsNil)
  117. os.Remove(newFile)
  118. // Specify 5 routines which is the same as parts count
  119. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 100*1024, Routines(5))
  120. c.Assert(err, IsNil)
  121. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  122. c.Assert(err, IsNil)
  123. eq, err = compareFiles(fileName, newFile)
  124. c.Assert(err, IsNil)
  125. c.Assert(eq, Equals, true)
  126. err = s.bucket.DeleteObject(destObjectName)
  127. c.Assert(err, IsNil)
  128. os.Remove(newFile)
  129. // Specify routine count 10, which is more than parts count
  130. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 100*1024, Routines(10))
  131. c.Assert(err, IsNil)
  132. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  133. c.Assert(err, IsNil)
  134. eq, err = compareFiles(fileName, newFile)
  135. c.Assert(err, IsNil)
  136. c.Assert(eq, Equals, true)
  137. err = s.bucket.DeleteObject(destObjectName)
  138. c.Assert(err, IsNil)
  139. os.Remove(newFile)
  140. // Invalid routine count, will use single routine
  141. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 100*1024, Routines(-1))
  142. c.Assert(err, IsNil)
  143. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  144. c.Assert(err, IsNil)
  145. eq, err = compareFiles(fileName, newFile)
  146. c.Assert(err, IsNil)
  147. c.Assert(eq, Equals, true)
  148. err = s.bucket.DeleteObject(destObjectName)
  149. c.Assert(err, IsNil)
  150. os.Remove(newFile)
  151. // Option
  152. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 100*1024, Routines(3), Meta("myprop", "mypropval"))
  153. meta, err := s.bucket.GetObjectDetailedMeta(destObjectName)
  154. c.Assert(err, IsNil)
  155. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  156. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  157. c.Assert(err, IsNil)
  158. eq, err = compareFiles(fileName, newFile)
  159. c.Assert(err, IsNil)
  160. c.Assert(eq, Equals, true)
  161. err = s.bucket.DeleteObject(destObjectName)
  162. c.Assert(err, IsNil)
  163. os.Remove(newFile)
  164. err = s.bucket.DeleteObject(srcObjectName)
  165. c.Assert(err, IsNil)
  166. }
  167. // CopyErrorHooker is a copypart request hook
  168. func CopyErrorHooker(part copyPart) error {
  169. if part.Number == 5 {
  170. time.Sleep(time.Second)
  171. return fmt.Errorf("ErrorHooker")
  172. }
  173. return nil
  174. }
  175. // TestCopyRoutineWithoutRecoveryNegative is a multiple routines copy without checkpoint
  176. func (s *OssCopySuite) TestCopyRoutineWithoutRecoveryNegative(c *C) {
  177. srcObjectName := objectNamePrefix + randStr(8)
  178. destObjectName := srcObjectName + "-dest"
  179. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  180. // Upload source file
  181. err := s.bucket.UploadFile(srcObjectName, fileName, 100*1024, Routines(3))
  182. c.Assert(err, IsNil)
  183. copyPartHooker = CopyErrorHooker
  184. // Worker routine errors
  185. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 100*1024, Routines(2))
  186. c.Assert(err, NotNil)
  187. c.Assert(err.Error(), Equals, "ErrorHooker")
  188. copyPartHooker = defaultCopyPartHook
  189. // Source bucket does not exist
  190. err = s.bucket.CopyFile("notexist", srcObjectName, destObjectName, 100*1024, Routines(2))
  191. c.Assert(err, NotNil)
  192. // Target object does not exist
  193. err = s.bucket.CopyFile(bucketName, "notexist", destObjectName, 100*1024, Routines(2))
  194. // The part size is invalid
  195. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024, Routines(2))
  196. c.Assert(err, NotNil)
  197. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*1024*1024*100, Routines(2))
  198. c.Assert(err, NotNil)
  199. // Delete the source file
  200. err = s.bucket.DeleteObject(srcObjectName)
  201. c.Assert(err, IsNil)
  202. }
  203. // TestCopyRoutineWithRecovery is a multiple routines copy with resumable recovery
  204. func (s *OssCopySuite) TestCopyRoutineWithRecovery(c *C) {
  205. srcObjectName := objectNamePrefix + randStr(8)
  206. destObjectName := srcObjectName + "-dest"
  207. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  208. newFile := randStr(8) + ".jpg"
  209. // Upload source file
  210. err := s.bucket.UploadFile(srcObjectName, fileName, 100*1024, Routines(3))
  211. c.Assert(err, IsNil)
  212. os.Remove(newFile)
  213. // Routines default value, CP's default path is destObjectName+.cp
  214. // Copy object with checkpoint enabled, single runtine.
  215. // Copy 4 parts---the CopyErrorHooker makes sure the copy of part 5 will fail.
  216. copyPartHooker = CopyErrorHooker
  217. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Checkpoint(true, destObjectName+".cp"))
  218. c.Assert(err, NotNil)
  219. c.Assert(err.Error(), Equals, "ErrorHooker")
  220. copyPartHooker = defaultCopyPartHook
  221. // Check CP
  222. ccp := copyCheckpoint{}
  223. err = ccp.load(destObjectName + ".cp")
  224. c.Assert(err, IsNil)
  225. c.Assert(ccp.Magic, Equals, copyCpMagic)
  226. c.Assert(len(ccp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
  227. c.Assert(ccp.SrcBucketName, Equals, bucketName)
  228. c.Assert(ccp.SrcObjectKey, Equals, srcObjectName)
  229. c.Assert(ccp.DestBucketName, Equals, bucketName)
  230. c.Assert(ccp.DestObjectKey, Equals, destObjectName)
  231. c.Assert(len(ccp.CopyID), Equals, len("3F79722737D1469980DACEDCA325BB52"))
  232. c.Assert(ccp.ObjStat.Size, Equals, int64(482048))
  233. c.Assert(len(ccp.ObjStat.LastModified), Equals, len("2015-12-17 18:43:03 +0800 CST"))
  234. c.Assert(ccp.ObjStat.Etag, Equals, "\"2351E662233817A7AE974D8C5B0876DD-5\"")
  235. c.Assert(len(ccp.Parts), Equals, 5)
  236. c.Assert(len(ccp.todoParts()), Equals, 1)
  237. c.Assert(ccp.PartStat[4], Equals, false)
  238. // Second copy, finish the last part
  239. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Checkpoint(true, destObjectName+".cp"))
  240. c.Assert(err, IsNil)
  241. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  242. c.Assert(err, IsNil)
  243. eq, err := compareFiles(fileName, newFile)
  244. c.Assert(err, IsNil)
  245. c.Assert(eq, Equals, true)
  246. err = s.bucket.DeleteObject(destObjectName)
  247. c.Assert(err, IsNil)
  248. os.Remove(newFile)
  249. err = ccp.load(fileName + ".cp")
  250. c.Assert(err, NotNil)
  251. //multicopy with empty checkpoint path
  252. copyPartHooker = CopyErrorHooker
  253. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Checkpoint(true, ""))
  254. c.Assert(err, NotNil)
  255. c.Assert(err.Error(), Equals, "ErrorHooker")
  256. copyPartHooker = defaultCopyPartHook
  257. ccp = copyCheckpoint{}
  258. err = ccp.load(destObjectName + ".cp")
  259. c.Assert(err, NotNil)
  260. //multi copy with checkpoint dir
  261. copyPartHooker = CopyErrorHooker
  262. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(2), CheckpointDir(true, "./"))
  263. c.Assert(err, NotNil)
  264. c.Assert(err.Error(), Equals, "ErrorHooker")
  265. copyPartHooker = defaultCopyPartHook
  266. // Check CP
  267. ccp = copyCheckpoint{}
  268. cpConf := cpConfig{IsEnable: true, DirPath: "./"}
  269. cpFilePath := getCopyCpFilePath(&cpConf, bucketName, srcObjectName, s.bucket.BucketName, destObjectName, "")
  270. err = ccp.load(cpFilePath)
  271. c.Assert(err, IsNil)
  272. c.Assert(ccp.Magic, Equals, copyCpMagic)
  273. c.Assert(len(ccp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
  274. c.Assert(ccp.SrcBucketName, Equals, bucketName)
  275. c.Assert(ccp.SrcObjectKey, Equals, srcObjectName)
  276. c.Assert(ccp.DestBucketName, Equals, bucketName)
  277. c.Assert(ccp.DestObjectKey, Equals, destObjectName)
  278. c.Assert(len(ccp.CopyID), Equals, len("3F79722737D1469980DACEDCA325BB52"))
  279. c.Assert(ccp.ObjStat.Size, Equals, int64(482048))
  280. c.Assert(len(ccp.ObjStat.LastModified), Equals, len("2015-12-17 18:43:03 +0800 CST"))
  281. c.Assert(ccp.ObjStat.Etag, Equals, "\"2351E662233817A7AE974D8C5B0876DD-5\"")
  282. c.Assert(len(ccp.Parts), Equals, 5)
  283. c.Assert(len(ccp.todoParts()), Equals, 1)
  284. c.Assert(ccp.PartStat[4], Equals, false)
  285. // Second copy, finish the last part.
  286. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(2), CheckpointDir(true, "./"))
  287. c.Assert(err, IsNil)
  288. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  289. c.Assert(err, IsNil)
  290. eq, err = compareFiles(fileName, newFile)
  291. c.Assert(err, IsNil)
  292. c.Assert(eq, Equals, true)
  293. err = s.bucket.DeleteObject(destObjectName)
  294. c.Assert(err, IsNil)
  295. os.Remove(newFile)
  296. err = ccp.load(srcObjectName + ".cp")
  297. c.Assert(err, NotNil)
  298. // First copy without error.
  299. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(3), Checkpoint(true, destObjectName+".cp"))
  300. c.Assert(err, IsNil)
  301. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  302. c.Assert(err, IsNil)
  303. eq, err = compareFiles(fileName, newFile)
  304. c.Assert(err, IsNil)
  305. c.Assert(eq, Equals, true)
  306. err = s.bucket.DeleteObject(destObjectName)
  307. c.Assert(err, IsNil)
  308. os.Remove(newFile)
  309. // Copy with multiple coroutines, no errors.
  310. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(10), Checkpoint(true, destObjectName+".cp"))
  311. c.Assert(err, IsNil)
  312. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  313. c.Assert(err, IsNil)
  314. eq, err = compareFiles(fileName, newFile)
  315. c.Assert(err, IsNil)
  316. c.Assert(eq, Equals, true)
  317. err = s.bucket.DeleteObject(destObjectName)
  318. c.Assert(err, IsNil)
  319. os.Remove(newFile)
  320. // Option
  321. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(5), Checkpoint(true, destObjectName+".cp"), Meta("myprop", "mypropval"))
  322. c.Assert(err, IsNil)
  323. meta, err := s.bucket.GetObjectDetailedMeta(destObjectName)
  324. c.Assert(err, IsNil)
  325. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  326. err = s.bucket.GetObjectToFile(destObjectName, newFile)
  327. c.Assert(err, IsNil)
  328. eq, err = compareFiles(fileName, newFile)
  329. c.Assert(err, IsNil)
  330. c.Assert(eq, Equals, true)
  331. err = s.bucket.DeleteObject(destObjectName)
  332. c.Assert(err, IsNil)
  333. os.Remove(newFile)
  334. // Delete the source file
  335. err = s.bucket.DeleteObject(srcObjectName)
  336. c.Assert(err, IsNil)
  337. }
  338. // TestCopyRoutineWithRecoveryNegative is a multiple routineed copy without checkpoint
  339. func (s *OssCopySuite) TestCopyRoutineWithRecoveryNegative(c *C) {
  340. srcObjectName := objectNamePrefix + randStr(8)
  341. destObjectName := srcObjectName + "-dest"
  342. // Source bucket does not exist
  343. err := s.bucket.CopyFile("notexist", srcObjectName, destObjectName, 100*1024, Checkpoint(true, destObjectName+".cp"))
  344. c.Assert(err, NotNil)
  345. c.Assert(err, NotNil)
  346. // Source object does not exist
  347. err = s.bucket.CopyFile(bucketName, "notexist", destObjectName, 100*1024, Routines(2), Checkpoint(true, destObjectName+".cp"))
  348. c.Assert(err, NotNil)
  349. // Specify part size is invalid.
  350. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024, Checkpoint(true, destObjectName+".cp"))
  351. c.Assert(err, NotNil)
  352. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*1024*1024*100, Routines(2), Checkpoint(true, destObjectName+".cp"))
  353. c.Assert(err, NotNil)
  354. }
  355. // TestCopyFileCrossBucket is a cross bucket's direct copy.
  356. func (s *OssCopySuite) TestCopyFileCrossBucket(c *C) {
  357. destBucketName := bucketName + "-desc"
  358. srcObjectName := objectNamePrefix + randStr(8)
  359. destObjectName := srcObjectName + "-dest"
  360. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  361. newFile := randStr(8) + ".jpg"
  362. destBucket, err := s.client.Bucket(destBucketName)
  363. c.Assert(err, IsNil)
  364. // Create a target bucket
  365. err = s.client.CreateBucket(destBucketName)
  366. // Upload source file
  367. err = s.bucket.UploadFile(srcObjectName, fileName, 100*1024, Routines(3))
  368. c.Assert(err, IsNil)
  369. os.Remove(newFile)
  370. // Copy files
  371. err = destBucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(5), Checkpoint(true, destObjectName+".cp"))
  372. c.Assert(err, IsNil)
  373. err = destBucket.GetObjectToFile(destObjectName, newFile)
  374. c.Assert(err, IsNil)
  375. eq, err := compareFiles(fileName, newFile)
  376. c.Assert(err, IsNil)
  377. c.Assert(eq, Equals, true)
  378. err = destBucket.DeleteObject(destObjectName)
  379. c.Assert(err, IsNil)
  380. os.Remove(newFile)
  381. // Copy file with options
  382. err = destBucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(10), Checkpoint(true, "copy.cp"), Meta("myprop", "mypropval"))
  383. c.Assert(err, IsNil)
  384. err = destBucket.GetObjectToFile(destObjectName, newFile)
  385. c.Assert(err, IsNil)
  386. eq, err = compareFiles(fileName, newFile)
  387. c.Assert(err, IsNil)
  388. c.Assert(eq, Equals, true)
  389. err = destBucket.DeleteObject(destObjectName)
  390. c.Assert(err, IsNil)
  391. os.Remove(newFile)
  392. // Delete target bucket
  393. err = s.client.DeleteBucket(destBucketName)
  394. c.Assert(err, IsNil)
  395. }
  396. func (s *OssCopySuite) TestVersioningCopyFileCrossBucket(c *C) {
  397. // create a bucket with default proprety
  398. client, err := New(endpoint, accessID, accessKey)
  399. c.Assert(err, IsNil)
  400. bucketName := bucketNamePrefix + randLowStr(6)
  401. err = client.CreateBucket(bucketName)
  402. c.Assert(err, IsNil)
  403. bucket, err := client.Bucket(bucketName)
  404. // put bucket version:enabled
  405. var versioningConfig VersioningConfig
  406. versioningConfig.Status = string(VersionEnabled)
  407. err = client.SetBucketVersioning(bucketName, versioningConfig)
  408. c.Assert(err, IsNil)
  409. // begin test
  410. objectName := objectNamePrefix + randStr(8)
  411. fileName := "test-file-" + randStr(8)
  412. fileData := randStr(500 * 1024)
  413. createFile(fileName, fileData, c)
  414. newFile := "test-file-" + randStr(8)
  415. destBucketName := bucketName + "-desc"
  416. srcObjectName := objectNamePrefix + randStr(8)
  417. destObjectName := srcObjectName + "-dest"
  418. // Create dest bucket
  419. err = client.CreateBucket(destBucketName)
  420. c.Assert(err, IsNil)
  421. destBucket, err := client.Bucket(destBucketName)
  422. c.Assert(err, IsNil)
  423. err = client.SetBucketVersioning(destBucketName, versioningConfig)
  424. c.Assert(err, IsNil)
  425. // Upload source file
  426. var respHeader http.Header
  427. options := []Option{Routines(3), GetResponseHeader(&respHeader)}
  428. err = bucket.UploadFile(srcObjectName, fileName, 100*1024, options...)
  429. versionId := GetVersionId(respHeader)
  430. c.Assert(len(versionId) > 0, Equals, true)
  431. c.Assert(err, IsNil)
  432. os.Remove(newFile)
  433. // overwrite emtpy object
  434. err = bucket.PutObject(srcObjectName, strings.NewReader(""))
  435. c.Assert(err, IsNil)
  436. // Copy files
  437. var respCopyHeader http.Header
  438. options = []Option{Routines(5), Checkpoint(true, destObjectName+".cp"), GetResponseHeader(&respCopyHeader), VersionId(versionId)}
  439. err = destBucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, options...)
  440. c.Assert(err, IsNil)
  441. versionIdCopy := GetVersionId(respCopyHeader)
  442. c.Assert(len(versionIdCopy) > 0, Equals, true)
  443. err = destBucket.GetObjectToFile(destObjectName, newFile, VersionId(versionIdCopy))
  444. c.Assert(err, IsNil)
  445. eq, err := compareFiles(fileName, newFile)
  446. c.Assert(err, IsNil)
  447. c.Assert(eq, Equals, true)
  448. err = destBucket.DeleteObject(destObjectName)
  449. c.Assert(err, IsNil)
  450. os.Remove(newFile)
  451. // Copy file with options meta
  452. options = []Option{Routines(10), Checkpoint(true, "copy.cp"), Meta("myprop", "mypropval"), GetResponseHeader(&respCopyHeader), VersionId(versionId)}
  453. err = destBucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, options...)
  454. c.Assert(err, IsNil)
  455. versionIdCopy = GetVersionId(respCopyHeader)
  456. err = destBucket.GetObjectToFile(destObjectName, newFile, VersionId(versionIdCopy))
  457. c.Assert(err, IsNil)
  458. eq, err = compareFiles(fileName, newFile)
  459. c.Assert(err, IsNil)
  460. c.Assert(eq, Equals, true)
  461. os.Remove(fileName)
  462. os.Remove(newFile)
  463. destBucket.DeleteObject(destObjectName)
  464. bucket.DeleteObject(objectName)
  465. forceDeleteBucket(client, bucketName, c)
  466. forceDeleteBucket(client, destBucketName, c)
  467. }
  468. // TestCopyFileChoiceOptions
  469. func (s *OssCopySuite) TestCopyFileChoiceOptions(c *C) {
  470. destBucketName := bucketName + "-desc"
  471. srcObjectName := objectNamePrefix + randStr(8)
  472. destObjectName := srcObjectName + "-dest"
  473. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  474. newFile := randStr(8) + ".jpg"
  475. destBucket, err := s.client.Bucket(destBucketName)
  476. c.Assert(err, IsNil)
  477. // Create a target bucket
  478. err = s.client.CreateBucket(destBucketName)
  479. // Upload source file
  480. err = s.bucket.UploadFile(srcObjectName, fileName, 100*1024, Routines(3))
  481. c.Assert(err, IsNil)
  482. os.Remove(newFile)
  483. // copyfile with properties
  484. options := []Option{
  485. ObjectACL(ACLPublicRead),
  486. RequestPayer(Requester),
  487. TrafficLimitHeader(1024 * 1024 * 8),
  488. ObjectStorageClass(StorageArchive),
  489. Routines(5),
  490. }
  491. // Copy files
  492. err = destBucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, options...)
  493. c.Assert(err, IsNil)
  494. err = destBucket.DeleteObject(destObjectName)
  495. c.Assert(err, IsNil)
  496. os.Remove(newFile)
  497. // Copy file with options
  498. options = []Option{
  499. ObjectACL(ACLPublicRead),
  500. RequestPayer(Requester),
  501. TrafficLimitHeader(1024 * 1024 * 8),
  502. ObjectStorageClass(StorageArchive),
  503. Routines(10),
  504. Checkpoint(true, "copy.cp"),
  505. }
  506. err = destBucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, options...)
  507. c.Assert(err, IsNil)
  508. err = destBucket.DeleteObject(destObjectName)
  509. c.Assert(err, IsNil)
  510. os.Remove(newFile)
  511. // Delete target bucket
  512. err = s.client.DeleteBucket(destBucketName)
  513. c.Assert(err, IsNil)
  514. }