multicopy_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. ForceDeleteBucket(s.client, destBucketName, c)
  394. }
  395. func (s *OssCopySuite) TestVersioningCopyFileCrossBucket(c *C) {
  396. // create a bucket with default proprety
  397. client, err := New(endpoint, accessID, accessKey)
  398. c.Assert(err, IsNil)
  399. bucketName := bucketNamePrefix + RandLowStr(6)
  400. err = client.CreateBucket(bucketName)
  401. c.Assert(err, IsNil)
  402. bucket, err := client.Bucket(bucketName)
  403. // put bucket version:enabled
  404. var versioningConfig VersioningConfig
  405. versioningConfig.Status = string(VersionEnabled)
  406. err = client.SetBucketVersioning(bucketName, versioningConfig)
  407. c.Assert(err, IsNil)
  408. // begin test
  409. objectName := objectNamePrefix + RandStr(8)
  410. fileName := "test-file-" + RandStr(8)
  411. fileData := RandStr(500 * 1024)
  412. CreateFile(fileName, fileData, c)
  413. newFile := "test-file-" + RandStr(8)
  414. destBucketName := bucketName + "-desc"
  415. srcObjectName := objectNamePrefix + RandStr(8)
  416. destObjectName := srcObjectName + "-dest"
  417. // Create dest bucket
  418. err = client.CreateBucket(destBucketName)
  419. c.Assert(err, IsNil)
  420. destBucket, err := client.Bucket(destBucketName)
  421. c.Assert(err, IsNil)
  422. err = client.SetBucketVersioning(destBucketName, versioningConfig)
  423. c.Assert(err, IsNil)
  424. // Upload source file
  425. var respHeader http.Header
  426. options := []Option{Routines(3), GetResponseHeader(&respHeader)}
  427. err = bucket.UploadFile(srcObjectName, fileName, 100*1024, options...)
  428. versionId := GetVersionId(respHeader)
  429. c.Assert(len(versionId) > 0, Equals, true)
  430. c.Assert(err, IsNil)
  431. os.Remove(newFile)
  432. // overwrite emtpy object
  433. err = bucket.PutObject(srcObjectName, strings.NewReader(""))
  434. c.Assert(err, IsNil)
  435. // Copy files
  436. var respCopyHeader http.Header
  437. options = []Option{Routines(5), Checkpoint(true, destObjectName+".cp"), GetResponseHeader(&respCopyHeader), VersionId(versionId)}
  438. err = destBucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, options...)
  439. c.Assert(err, IsNil)
  440. versionIdCopy := GetVersionId(respCopyHeader)
  441. c.Assert(len(versionIdCopy) > 0, Equals, true)
  442. err = destBucket.GetObjectToFile(destObjectName, newFile, VersionId(versionIdCopy))
  443. c.Assert(err, IsNil)
  444. eq, err := compareFiles(fileName, newFile)
  445. c.Assert(err, IsNil)
  446. c.Assert(eq, Equals, true)
  447. err = destBucket.DeleteObject(destObjectName)
  448. c.Assert(err, IsNil)
  449. os.Remove(newFile)
  450. // Copy file with options meta
  451. options = []Option{Routines(10), Checkpoint(true, "copy.cp"), Meta("myprop", "mypropval"), GetResponseHeader(&respCopyHeader), VersionId(versionId)}
  452. err = destBucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, options...)
  453. c.Assert(err, IsNil)
  454. versionIdCopy = GetVersionId(respCopyHeader)
  455. err = destBucket.GetObjectToFile(destObjectName, newFile, VersionId(versionIdCopy))
  456. c.Assert(err, IsNil)
  457. eq, err = compareFiles(fileName, newFile)
  458. c.Assert(err, IsNil)
  459. c.Assert(eq, Equals, true)
  460. os.Remove(fileName)
  461. os.Remove(newFile)
  462. destBucket.DeleteObject(destObjectName)
  463. bucket.DeleteObject(objectName)
  464. ForceDeleteBucket(client, bucketName, c)
  465. ForceDeleteBucket(client, destBucketName, c)
  466. }
  467. // TestCopyFileChoiceOptions
  468. func (s *OssCopySuite) TestCopyFileChoiceOptions(c *C) {
  469. destBucketName := bucketName + "-desc"
  470. srcObjectName := objectNamePrefix + RandStr(8)
  471. destObjectName := srcObjectName + "-dest"
  472. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  473. newFile := RandStr(8) + ".jpg"
  474. destBucket, err := s.client.Bucket(destBucketName)
  475. c.Assert(err, IsNil)
  476. // Create a target bucket
  477. err = s.client.CreateBucket(destBucketName)
  478. // Upload source file
  479. err = s.bucket.UploadFile(srcObjectName, fileName, 100*1024, Routines(3))
  480. c.Assert(err, IsNil)
  481. os.Remove(newFile)
  482. // copyfile with properties
  483. options := []Option{
  484. ObjectACL(ACLPublicRead),
  485. RequestPayer(Requester),
  486. TrafficLimitHeader(1024 * 1024 * 8),
  487. ObjectStorageClass(StorageArchive),
  488. ServerSideEncryption("AES256"),
  489. Routines(5), // without checkpoint
  490. }
  491. // Copy files
  492. err = destBucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, options...)
  493. c.Assert(err, IsNil)
  494. // check object
  495. meta, err := destBucket.GetObjectDetailedMeta(destObjectName)
  496. c.Assert(err, IsNil)
  497. c.Assert(meta.Get("X-Oss-Storage-Class"), Equals, "Archive")
  498. c.Assert(meta.Get("X-Oss-Server-Side-Encryption"), Equals, "AES256")
  499. aclResult, err := destBucket.GetObjectACL(destObjectName)
  500. c.Assert(aclResult.ACL, Equals, "public-read")
  501. c.Assert(err, IsNil)
  502. err = destBucket.DeleteObject(destObjectName)
  503. c.Assert(err, IsNil)
  504. os.Remove(newFile)
  505. // Copy file with options
  506. options = []Option{
  507. ObjectACL(ACLPublicRead),
  508. RequestPayer(Requester),
  509. TrafficLimitHeader(1024 * 1024 * 8),
  510. ObjectStorageClass(StorageArchive),
  511. ServerSideEncryption("AES256"),
  512. Routines(10),
  513. Checkpoint(true, "copy.cp"), // with checkpoint
  514. }
  515. err = destBucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, options...)
  516. c.Assert(err, IsNil)
  517. // check object
  518. meta, err = destBucket.GetObjectDetailedMeta(destObjectName)
  519. c.Assert(err, IsNil)
  520. c.Assert(meta.Get("X-Oss-Storage-Class"), Equals, "Archive")
  521. c.Assert(meta.Get("X-Oss-Server-Side-Encryption"), Equals, "AES256")
  522. aclResult, err = destBucket.GetObjectACL(destObjectName)
  523. c.Assert(aclResult.ACL, Equals, "public-read")
  524. c.Assert(err, IsNil)
  525. err = destBucket.DeleteObject(destObjectName)
  526. c.Assert(err, IsNil)
  527. os.Remove(newFile)
  528. // Delete target bucket
  529. err = s.client.DeleteBucket(destBucketName)
  530. c.Assert(err, IsNil)
  531. }