progress_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. // bucket test
  2. package oss
  3. import (
  4. "bytes"
  5. "io/ioutil"
  6. "math/rand"
  7. "os"
  8. "strings"
  9. "sync/atomic"
  10. . "gopkg.in/check.v1"
  11. )
  12. type OssProgressSuite struct {
  13. client *Client
  14. bucket *Bucket
  15. }
  16. var _ = Suite(&OssProgressSuite{})
  17. // SetUpSuite runs once when the suite starts running
  18. func (s *OssProgressSuite) SetUpSuite(c *C) {
  19. client, err := New(endpoint, accessID, accessKey)
  20. c.Assert(err, IsNil)
  21. s.client = client
  22. s.client.CreateBucket(bucketName)
  23. bucket, err := s.client.Bucket(bucketName)
  24. c.Assert(err, IsNil)
  25. s.bucket = bucket
  26. testLogger.Println("test progress started")
  27. }
  28. // TearDownSuite runs before each test or benchmark starts running
  29. func (s *OssProgressSuite) TearDownSuite(c *C) {
  30. // Abort multipart uploads
  31. keyMarker := KeyMarker("")
  32. uploadIDMarker := UploadIDMarker("")
  33. for {
  34. lmu, err := s.bucket.ListMultipartUploads(keyMarker, uploadIDMarker)
  35. c.Assert(err, IsNil)
  36. for _, upload := range lmu.Uploads {
  37. imur := InitiateMultipartUploadResult{Bucket: bucketName, Key: upload.Key, UploadID: upload.UploadID}
  38. err = s.bucket.AbortMultipartUpload(imur)
  39. c.Assert(err, IsNil)
  40. }
  41. keyMarker = KeyMarker(lmu.NextKeyMarker)
  42. uploadIDMarker = UploadIDMarker(lmu.NextUploadIDMarker)
  43. if !lmu.IsTruncated {
  44. break
  45. }
  46. }
  47. // Delete objects
  48. marker := Marker("")
  49. for {
  50. lor, err := s.bucket.ListObjects(marker)
  51. c.Assert(err, IsNil)
  52. for _, object := range lor.Objects {
  53. err = s.bucket.DeleteObject(object.Key)
  54. c.Assert(err, IsNil)
  55. }
  56. marker = Marker(lor.NextMarker)
  57. if !lor.IsTruncated {
  58. break
  59. }
  60. }
  61. // Delete bucket
  62. err := s.client.DeleteBucket(s.bucket.BucketName)
  63. c.Assert(err, IsNil)
  64. testLogger.Println("test progress completed")
  65. }
  66. // SetUpTest runs after each test or benchmark runs
  67. func (s *OssProgressSuite) SetUpTest(c *C) {
  68. err := removeTempFiles("../oss", ".jpg")
  69. c.Assert(err, IsNil)
  70. err = removeTempFiles("../oss", ".txt")
  71. c.Assert(err, IsNil)
  72. err = removeTempFiles("../oss", ".html")
  73. c.Assert(err, IsNil)
  74. }
  75. // TearDownTest runs once after all tests or benchmarks have finished running
  76. func (s *OssProgressSuite) TearDownTest(c *C) {
  77. err := removeTempFiles("../oss", ".jpg")
  78. c.Assert(err, IsNil)
  79. err = removeTempFiles("../oss", ".txt")
  80. c.Assert(err, IsNil)
  81. err = removeTempFiles("../oss", ".html")
  82. c.Assert(err, IsNil)
  83. }
  84. // OssProgressListener is the progress listener
  85. type OssProgressListener struct {
  86. TotalRwBytes int64
  87. }
  88. // ProgressChanged handles progress event
  89. func (listener *OssProgressListener) ProgressChanged(event *ProgressEvent) {
  90. switch event.EventType {
  91. case TransferStartedEvent:
  92. testLogger.Printf("Transfer Started, ConsumedBytes: %d, TotalBytes %d.\n",
  93. event.ConsumedBytes, event.TotalBytes)
  94. case TransferDataEvent:
  95. atomic.AddInt64(&listener.TotalRwBytes, event.RwBytes)
  96. testLogger.Printf("Transfer Data, ConsumedBytes: %d, TotalBytes %d, %d%%.\n",
  97. event.ConsumedBytes, event.TotalBytes, event.ConsumedBytes*100/event.TotalBytes)
  98. case TransferCompletedEvent:
  99. testLogger.Printf("Transfer Completed, ConsumedBytes: %d, TotalBytes %d.\n",
  100. event.ConsumedBytes, event.TotalBytes)
  101. case TransferFailedEvent:
  102. testLogger.Printf("Transfer Failed, ConsumedBytes: %d, TotalBytes %d.\n",
  103. event.ConsumedBytes, event.TotalBytes)
  104. default:
  105. }
  106. }
  107. // TestPutObject
  108. func (s *OssProgressSuite) TestPutObject(c *C) {
  109. objectName := RandStr(8) + ".jpg"
  110. localFile := "../sample/The Go Programming Language.html"
  111. fileInfo, err := os.Stat(localFile)
  112. c.Assert(err, IsNil)
  113. // PutObject
  114. fd, err := os.Open(localFile)
  115. c.Assert(err, IsNil)
  116. defer fd.Close()
  117. progressListener := OssProgressListener{}
  118. err = s.bucket.PutObject(objectName, fd, Progress(&progressListener))
  119. c.Assert(err, IsNil)
  120. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  121. // PutObjectFromFile
  122. progressListener.TotalRwBytes = 0
  123. err = s.bucket.PutObjectFromFile(objectName, localFile, Progress(&progressListener))
  124. c.Assert(err, IsNil)
  125. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  126. // DoPutObject
  127. fd, err = os.Open(localFile)
  128. c.Assert(err, IsNil)
  129. defer fd.Close()
  130. request := &PutObjectRequest{
  131. ObjectKey: objectName,
  132. Reader: fd,
  133. }
  134. progressListener.TotalRwBytes = 0
  135. options := []Option{Progress(&progressListener)}
  136. _, err = s.bucket.DoPutObject(request, options)
  137. c.Assert(err, IsNil)
  138. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  139. // PutObject size is 0
  140. progressListener.TotalRwBytes = 0
  141. err = s.bucket.PutObject(objectName, strings.NewReader(""), Progress(&progressListener))
  142. c.Assert(err, IsNil)
  143. c.Assert(progressListener.TotalRwBytes, Equals, int64(0))
  144. testLogger.Println("OssProgressSuite.TestPutObject")
  145. }
  146. // TestSignURL
  147. func (s *OssProgressSuite) SignURLTestFunc(c *C, authVersion AuthVersionType, extraHeaders []string) {
  148. objectName := objectNamePrefix + RandStr(8)
  149. filePath := RandLowStr(10)
  150. content := RandStr(20)
  151. CreateFile(filePath, content, c)
  152. oldType := s.bucket.Client.Config.AuthVersion
  153. oldHeaders := s.bucket.Client.Config.AdditionalHeaders
  154. s.bucket.Client.Config.AuthVersion = authVersion
  155. s.bucket.Client.Config.AdditionalHeaders = extraHeaders
  156. // Sign URL for put
  157. progressListener := OssProgressListener{}
  158. str, err := s.bucket.SignURL(objectName, HTTPPut, 60, Progress(&progressListener))
  159. c.Assert(err, IsNil)
  160. if s.bucket.Client.Config.AuthVersion == AuthV1 {
  161. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  162. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  163. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  164. } else {
  165. c.Assert(strings.Contains(str, HTTPParamSignatureVersion+"=OSS2"), Equals, true)
  166. c.Assert(strings.Contains(str, HTTPParamExpiresV2+"="), Equals, true)
  167. c.Assert(strings.Contains(str, HTTPParamAccessKeyIDV2+"="), Equals, true)
  168. c.Assert(strings.Contains(str, HTTPParamSignatureV2+"="), Equals, true)
  169. }
  170. // Put object with URL
  171. fd, err := os.Open(filePath)
  172. c.Assert(err, IsNil)
  173. defer fd.Close()
  174. err = s.bucket.PutObjectWithURL(str, fd, Progress(&progressListener))
  175. c.Assert(err, IsNil)
  176. c.Assert(progressListener.TotalRwBytes, Equals, int64(len(content)))
  177. // Put object from file with URL
  178. progressListener.TotalRwBytes = 0
  179. err = s.bucket.PutObjectFromFileWithURL(str, filePath, Progress(&progressListener))
  180. c.Assert(err, IsNil)
  181. c.Assert(progressListener.TotalRwBytes, Equals, int64(len(content)))
  182. // DoPutObject
  183. fd, err = os.Open(filePath)
  184. c.Assert(err, IsNil)
  185. defer fd.Close()
  186. progressListener.TotalRwBytes = 0
  187. options := []Option{Progress(&progressListener)}
  188. _, err = s.bucket.DoPutObjectWithURL(str, fd, options)
  189. c.Assert(err, IsNil)
  190. c.Assert(progressListener.TotalRwBytes, Equals, int64(len(content)))
  191. // Sign URL for get
  192. str, err = s.bucket.SignURL(objectName, HTTPGet, 60, Progress(&progressListener))
  193. c.Assert(err, IsNil)
  194. if s.bucket.Client.Config.AuthVersion == AuthV1 {
  195. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  196. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  197. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  198. } else {
  199. c.Assert(strings.Contains(str, HTTPParamSignatureVersion+"=OSS2"), Equals, true)
  200. c.Assert(strings.Contains(str, HTTPParamExpiresV2+"="), Equals, true)
  201. c.Assert(strings.Contains(str, HTTPParamAccessKeyIDV2+"="), Equals, true)
  202. c.Assert(strings.Contains(str, HTTPParamSignatureV2+"="), Equals, true)
  203. }
  204. // Get object with URL
  205. progressListener.TotalRwBytes = 0
  206. body, err := s.bucket.GetObjectWithURL(str, Progress(&progressListener))
  207. c.Assert(err, IsNil)
  208. str, err = readBody(body)
  209. c.Assert(err, IsNil)
  210. c.Assert(str, Equals, content)
  211. c.Assert(progressListener.TotalRwBytes, Equals, int64(len(content)))
  212. // Get object to file with URL
  213. progressListener.TotalRwBytes = 0
  214. str, err = s.bucket.SignURL(objectName, HTTPGet, 10, Progress(&progressListener))
  215. c.Assert(err, IsNil)
  216. newFile := RandStr(10)
  217. progressListener.TotalRwBytes = 0
  218. err = s.bucket.GetObjectToFileWithURL(str, newFile, Progress(&progressListener))
  219. c.Assert(progressListener.TotalRwBytes, Equals, int64(len(content)))
  220. c.Assert(err, IsNil)
  221. eq, err := compareFiles(filePath, newFile)
  222. c.Assert(err, IsNil)
  223. c.Assert(eq, Equals, true)
  224. os.Remove(filePath)
  225. os.Remove(newFile)
  226. err = s.bucket.DeleteObject(objectName)
  227. c.Assert(err, IsNil)
  228. testLogger.Println("OssProgressSuite.TestSignURL")
  229. s.bucket.Client.Config.AuthVersion = oldType
  230. s.bucket.Client.Config.AdditionalHeaders = oldHeaders
  231. }
  232. func (s *OssProgressSuite) TestSignURL(c *C) {
  233. s.SignURLTestFunc(c, AuthV1, []string{})
  234. s.SignURLTestFunc(c, AuthV2, []string{})
  235. s.SignURLTestFunc(c, AuthV2, []string{"host", "range", "user-agent"})
  236. }
  237. func (s *OssProgressSuite) TestPutObjectNegative(c *C) {
  238. objectName := objectNamePrefix + RandStr(8)
  239. localFile := "../sample/The Go Programming Language.html"
  240. // Invalid endpoint
  241. client, err := New("http://oss-cn-taikang.aliyuncs.com", accessID, accessKey)
  242. c.Assert(err, IsNil)
  243. bucket, err := client.Bucket(bucketName)
  244. c.Assert(err, IsNil)
  245. err = bucket.PutObjectFromFile(objectName, localFile, Progress(&OssProgressListener{}))
  246. testLogger.Println(err)
  247. c.Assert(err, NotNil)
  248. testLogger.Println("OssProgressSuite.TestPutObjectNegative")
  249. }
  250. // TestAppendObject
  251. func (s *OssProgressSuite) TestAppendObject(c *C) {
  252. objectName := objectNamePrefix + RandStr(8)
  253. objectValue := RandStr(100)
  254. var val = []byte(objectValue)
  255. var nextPos int64
  256. var midPos = 1 + rand.Intn(len(val)-1)
  257. // AppendObject
  258. progressListener := OssProgressListener{}
  259. nextPos, err := s.bucket.AppendObject(objectName, bytes.NewReader(val[0:midPos]), nextPos, Progress(&progressListener))
  260. c.Assert(err, IsNil)
  261. c.Assert(progressListener.TotalRwBytes, Equals, nextPos)
  262. // DoAppendObject
  263. request := &AppendObjectRequest{
  264. ObjectKey: objectName,
  265. Reader: bytes.NewReader(val[midPos:]),
  266. Position: nextPos,
  267. }
  268. options := []Option{Progress(&OssProgressListener{})}
  269. _, err = s.bucket.DoAppendObject(request, options)
  270. c.Assert(err, IsNil)
  271. testLogger.Println("OssProgressSuite.TestAppendObject")
  272. }
  273. // TestMultipartUpload
  274. func (s *OssProgressSuite) TestMultipartUpload(c *C) {
  275. objectName := objectNamePrefix + RandStr(8)
  276. var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
  277. fileInfo, err := os.Stat(fileName)
  278. c.Assert(err, IsNil)
  279. chunks, err := SplitFileByPartNum(fileName, 3)
  280. c.Assert(err, IsNil)
  281. testLogger.Println("chunks:", chunks)
  282. fd, err := os.Open(fileName)
  283. c.Assert(err, IsNil)
  284. defer fd.Close()
  285. // Initiate
  286. progressListener := OssProgressListener{}
  287. imur, err := s.bucket.InitiateMultipartUpload(objectName)
  288. c.Assert(err, IsNil)
  289. // UploadPart
  290. var parts []UploadPart
  291. for _, chunk := range chunks {
  292. fd.Seek(chunk.Offset, os.SEEK_SET)
  293. part, err := s.bucket.UploadPart(imur, fd, chunk.Size, chunk.Number, Progress(&progressListener))
  294. c.Assert(err, IsNil)
  295. parts = append(parts, part)
  296. }
  297. // Complete
  298. _, err = s.bucket.CompleteMultipartUpload(imur, parts)
  299. c.Assert(err, IsNil)
  300. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  301. err = s.bucket.DeleteObject(objectName)
  302. c.Assert(err, IsNil)
  303. testLogger.Println("OssProgressSuite.TestMultipartUpload")
  304. }
  305. // TestMultipartUploadFromFile
  306. func (s *OssProgressSuite) TestMultipartUploadFromFile(c *C) {
  307. objectName := objectNamePrefix + RandStr(8)
  308. var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
  309. fileInfo, err := os.Stat(fileName)
  310. c.Assert(err, IsNil)
  311. chunks, err := SplitFileByPartNum(fileName, 3)
  312. c.Assert(err, IsNil)
  313. // Initiate
  314. imur, err := s.bucket.InitiateMultipartUpload(objectName)
  315. c.Assert(err, IsNil)
  316. // UploadPart
  317. progressListener := OssProgressListener{}
  318. var parts []UploadPart
  319. for _, chunk := range chunks {
  320. part, err := s.bucket.UploadPartFromFile(imur, fileName, chunk.Offset, chunk.Size, chunk.Number, Progress(&progressListener))
  321. c.Assert(err, IsNil)
  322. parts = append(parts, part)
  323. }
  324. // Complete
  325. _, err = s.bucket.CompleteMultipartUpload(imur, parts)
  326. c.Assert(err, IsNil)
  327. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  328. err = s.bucket.DeleteObject(objectName)
  329. c.Assert(err, IsNil)
  330. testLogger.Println("OssProgressSuite.TestMultipartUploadFromFile")
  331. }
  332. // TestGetObject
  333. func (s *OssProgressSuite) TestGetObject(c *C) {
  334. objectName := objectNamePrefix + RandStr(8)
  335. localFile := "../sample/BingWallpaper-2015-11-07.jpg"
  336. newFile := "newpic-progress-1.jpg"
  337. fileInfo, err := os.Stat(localFile)
  338. c.Assert(err, IsNil)
  339. progressListener := OssProgressListener{}
  340. // PutObject
  341. err = s.bucket.PutObjectFromFile(objectName, localFile, Progress(&progressListener))
  342. c.Assert(err, IsNil)
  343. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  344. // GetObject
  345. progressListener.TotalRwBytes = 0
  346. body, err := s.bucket.GetObject(objectName, Progress(&progressListener))
  347. c.Assert(err, IsNil)
  348. _, err = ioutil.ReadAll(body)
  349. c.Assert(err, IsNil)
  350. body.Close()
  351. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  352. // GetObjectToFile
  353. progressListener.TotalRwBytes = 0
  354. err = s.bucket.GetObjectToFile(objectName, newFile, Progress(&progressListener))
  355. c.Assert(err, IsNil)
  356. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  357. // DoGetObject
  358. progressListener.TotalRwBytes = 0
  359. request := &GetObjectRequest{objectName}
  360. options := []Option{Progress(&progressListener)}
  361. result, err := s.bucket.DoGetObject(request, options)
  362. c.Assert(err, IsNil)
  363. _, err = ioutil.ReadAll(result.Response.Body)
  364. c.Assert(err, IsNil)
  365. result.Response.Body.Close()
  366. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  367. // GetObject with range
  368. progressListener.TotalRwBytes = 0
  369. body, err = s.bucket.GetObject(objectName, Range(1024, 4*1024), Progress(&progressListener))
  370. c.Assert(err, IsNil)
  371. text, err := ioutil.ReadAll(body)
  372. c.Assert(err, IsNil)
  373. body.Close()
  374. c.Assert(progressListener.TotalRwBytes, Equals, int64(len(text)))
  375. // PutObject size is 0
  376. progressListener.TotalRwBytes = 0
  377. err = s.bucket.PutObject(objectName, strings.NewReader(""), Progress(&progressListener))
  378. c.Assert(err, IsNil)
  379. c.Assert(progressListener.TotalRwBytes, Equals, int64(0))
  380. // GetObject size is 0
  381. progressListener.TotalRwBytes = 0
  382. body, err = s.bucket.GetObject(objectName, Progress(&progressListener))
  383. c.Assert(err, IsNil)
  384. _, err = ioutil.ReadAll(body)
  385. c.Assert(err, IsNil)
  386. body.Close()
  387. c.Assert(progressListener.TotalRwBytes, Equals, int64(0))
  388. testLogger.Println("OssProgressSuite.TestGetObject")
  389. }
  390. // TestGetObjectNegative
  391. func (s *OssProgressSuite) TestGetObjectNegative(c *C) {
  392. objectName := objectNamePrefix + RandStr(8)
  393. localFile := "../sample/BingWallpaper-2015-11-07.jpg"
  394. // PutObject
  395. err := s.bucket.PutObjectFromFile(objectName, localFile)
  396. c.Assert(err, IsNil)
  397. // GetObject
  398. body, err := s.bucket.GetObject(objectName, Progress(&OssProgressListener{}))
  399. c.Assert(err, IsNil)
  400. buf := make([]byte, 4*1024)
  401. n, err := body.Read(buf)
  402. c.Assert(err, IsNil)
  403. //time.Sleep(70 * time.Second) TODO
  404. // Read should fail
  405. for err == nil {
  406. n, err = body.Read(buf)
  407. n += n
  408. }
  409. c.Assert(err, NotNil)
  410. body.Close()
  411. testLogger.Println("OssProgressSuite.TestGetObjectNegative")
  412. }
  413. // TestUploadFile
  414. func (s *OssProgressSuite) TestUploadFile(c *C) {
  415. objectName := objectNamePrefix + RandStr(8)
  416. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  417. fileInfo, err := os.Stat(fileName)
  418. c.Assert(err, IsNil)
  419. progressListener := OssProgressListener{}
  420. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(5), Progress(&progressListener))
  421. c.Assert(err, IsNil)
  422. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  423. progressListener.TotalRwBytes = 0
  424. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), Checkpoint(true, objectName+".cp"), Progress(&progressListener))
  425. c.Assert(err, IsNil)
  426. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  427. testLogger.Println("OssProgressSuite.TestUploadFile")
  428. }
  429. // TestDownloadFile
  430. func (s *OssProgressSuite) TestDownloadFile(c *C) {
  431. objectName := objectNamePrefix + RandStr(8)
  432. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  433. newFile := "down-new-file-progress-2.jpg"
  434. fileInfo, err := os.Stat(fileName)
  435. c.Assert(err, IsNil)
  436. // Upload
  437. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  438. c.Assert(err, IsNil)
  439. progressListener := OssProgressListener{}
  440. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(5), Progress(&progressListener))
  441. c.Assert(err, IsNil)
  442. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  443. progressListener.TotalRwBytes = 0
  444. err = s.bucket.DownloadFile(objectName, newFile, 1024*1024, Routines(3), Progress(&progressListener))
  445. c.Assert(err, IsNil)
  446. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  447. progressListener.TotalRwBytes = 0
  448. err = s.bucket.DownloadFile(objectName, newFile, 50*1024, Routines(3), Checkpoint(true, ""), Progress(&progressListener))
  449. c.Assert(err, IsNil)
  450. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  451. testLogger.Println("OssProgressSuite.TestDownloadFile")
  452. }
  453. // TestCopyFile
  454. func (s *OssProgressSuite) TestCopyFile(c *C) {
  455. srcObjectName := objectNamePrefix + RandStr(8)
  456. destObjectName := srcObjectName + "-copy"
  457. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  458. fileInfo, err := os.Stat(fileName)
  459. c.Assert(err, IsNil)
  460. // Upload
  461. progressListener := OssProgressListener{}
  462. err = s.bucket.UploadFile(srcObjectName, fileName, 100*1024, Routines(3), Progress(&progressListener))
  463. c.Assert(err, IsNil)
  464. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  465. progressListener.TotalRwBytes = 0
  466. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 100*1024, Routines(5), Progress(&progressListener))
  467. c.Assert(err, IsNil)
  468. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  469. progressListener.TotalRwBytes = 0
  470. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(3), Checkpoint(true, ""), Progress(&progressListener))
  471. c.Assert(err, IsNil)
  472. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  473. testLogger.Println("OssProgressSuite.TestCopyFile")
  474. }