progress_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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) TestSignURL(c *C) {
  148. objectName := objectNamePrefix + randStr(8)
  149. filePath := randLowStr(10)
  150. content := randStr(20)
  151. createFile(filePath, content, c)
  152. // Sign URL for put
  153. progressListener := OssProgressListener{}
  154. str, err := s.bucket.SignURL(objectName, HTTPPut, 60, Progress(&progressListener))
  155. c.Assert(err, IsNil)
  156. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  157. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  158. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  159. // Put object with URL
  160. fd, err := os.Open(filePath)
  161. c.Assert(err, IsNil)
  162. defer fd.Close()
  163. err = s.bucket.PutObjectWithURL(str, fd, Progress(&progressListener))
  164. c.Assert(err, IsNil)
  165. c.Assert(progressListener.TotalRwBytes, Equals, int64(len(content)))
  166. // Put object from file with URL
  167. progressListener.TotalRwBytes = 0
  168. err = s.bucket.PutObjectFromFileWithURL(str, filePath, Progress(&progressListener))
  169. c.Assert(err, IsNil)
  170. c.Assert(progressListener.TotalRwBytes, Equals, int64(len(content)))
  171. // DoPutObject
  172. fd, err = os.Open(filePath)
  173. c.Assert(err, IsNil)
  174. defer fd.Close()
  175. progressListener.TotalRwBytes = 0
  176. options := []Option{Progress(&progressListener)}
  177. _, err = s.bucket.DoPutObjectWithURL(str, fd, options)
  178. c.Assert(err, IsNil)
  179. c.Assert(progressListener.TotalRwBytes, Equals, int64(len(content)))
  180. // Sign URL for get
  181. str, err = s.bucket.SignURL(objectName, HTTPGet, 60, Progress(&progressListener))
  182. c.Assert(err, IsNil)
  183. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  184. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  185. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  186. // Get object with URL
  187. progressListener.TotalRwBytes = 0
  188. body, err := s.bucket.GetObjectWithURL(str, Progress(&progressListener))
  189. c.Assert(err, IsNil)
  190. str, err = readBody(body)
  191. c.Assert(err, IsNil)
  192. c.Assert(str, Equals, content)
  193. c.Assert(progressListener.TotalRwBytes, Equals, int64(len(content)))
  194. // Get object to file with URL
  195. progressListener.TotalRwBytes = 0
  196. str, err = s.bucket.SignURL(objectName, HTTPGet, 10, Progress(&progressListener))
  197. c.Assert(err, IsNil)
  198. newFile := randStr(10)
  199. progressListener.TotalRwBytes = 0
  200. err = s.bucket.GetObjectToFileWithURL(str, newFile, Progress(&progressListener))
  201. c.Assert(progressListener.TotalRwBytes, Equals, int64(len(content)))
  202. c.Assert(err, IsNil)
  203. eq, err := compareFiles(filePath, newFile)
  204. c.Assert(err, IsNil)
  205. c.Assert(eq, Equals, true)
  206. os.Remove(filePath)
  207. os.Remove(newFile)
  208. err = s.bucket.DeleteObject(objectName)
  209. c.Assert(err, IsNil)
  210. testLogger.Println("OssProgressSuite.TestSignURL")
  211. }
  212. func (s *OssProgressSuite) TestPutObjectNegative(c *C) {
  213. objectName := objectNamePrefix + randStr(8)
  214. localFile := "../sample/The Go Programming Language.html"
  215. // Invalid endpoint
  216. client, err := New("http://oss-cn-taikang.aliyuncs.com", accessID, accessKey)
  217. c.Assert(err, IsNil)
  218. bucket, err := client.Bucket(bucketName)
  219. c.Assert(err, IsNil)
  220. err = bucket.PutObjectFromFile(objectName, localFile, Progress(&OssProgressListener{}))
  221. testLogger.Println(err)
  222. c.Assert(err, NotNil)
  223. testLogger.Println("OssProgressSuite.TestPutObjectNegative")
  224. }
  225. // TestAppendObject
  226. func (s *OssProgressSuite) TestAppendObject(c *C) {
  227. objectName := objectNamePrefix + randStr(8)
  228. objectValue := randStr(100)
  229. var val = []byte(objectValue)
  230. var nextPos int64
  231. var midPos = 1 + rand.Intn(len(val)-1)
  232. // AppendObject
  233. progressListener := OssProgressListener{}
  234. nextPos, err := s.bucket.AppendObject(objectName, bytes.NewReader(val[0:midPos]), nextPos, Progress(&progressListener))
  235. c.Assert(err, IsNil)
  236. c.Assert(progressListener.TotalRwBytes, Equals, nextPos)
  237. // DoAppendObject
  238. request := &AppendObjectRequest{
  239. ObjectKey: objectName,
  240. Reader: bytes.NewReader(val[midPos:]),
  241. Position: nextPos,
  242. }
  243. options := []Option{Progress(&OssProgressListener{})}
  244. _, err = s.bucket.DoAppendObject(request, options)
  245. c.Assert(err, IsNil)
  246. testLogger.Println("OssProgressSuite.TestAppendObject")
  247. }
  248. // TestMultipartUpload
  249. func (s *OssProgressSuite) TestMultipartUpload(c *C) {
  250. objectName := objectNamePrefix + randStr(8)
  251. var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
  252. fileInfo, err := os.Stat(fileName)
  253. c.Assert(err, IsNil)
  254. chunks, err := SplitFileByPartNum(fileName, 3)
  255. c.Assert(err, IsNil)
  256. testLogger.Println("chunks:", chunks)
  257. fd, err := os.Open(fileName)
  258. c.Assert(err, IsNil)
  259. defer fd.Close()
  260. // Initiate
  261. progressListener := OssProgressListener{}
  262. imur, err := s.bucket.InitiateMultipartUpload(objectName)
  263. c.Assert(err, IsNil)
  264. // UploadPart
  265. var parts []UploadPart
  266. for _, chunk := range chunks {
  267. fd.Seek(chunk.Offset, os.SEEK_SET)
  268. part, err := s.bucket.UploadPart(imur, fd, chunk.Size, chunk.Number, Progress(&progressListener))
  269. c.Assert(err, IsNil)
  270. parts = append(parts, part)
  271. }
  272. // Complete
  273. _, err = s.bucket.CompleteMultipartUpload(imur, parts)
  274. c.Assert(err, IsNil)
  275. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  276. err = s.bucket.DeleteObject(objectName)
  277. c.Assert(err, IsNil)
  278. testLogger.Println("OssProgressSuite.TestMultipartUpload")
  279. }
  280. // TestMultipartUploadFromFile
  281. func (s *OssProgressSuite) TestMultipartUploadFromFile(c *C) {
  282. objectName := objectNamePrefix + randStr(8)
  283. var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
  284. fileInfo, err := os.Stat(fileName)
  285. c.Assert(err, IsNil)
  286. chunks, err := SplitFileByPartNum(fileName, 3)
  287. c.Assert(err, IsNil)
  288. // Initiate
  289. imur, err := s.bucket.InitiateMultipartUpload(objectName)
  290. c.Assert(err, IsNil)
  291. // UploadPart
  292. progressListener := OssProgressListener{}
  293. var parts []UploadPart
  294. for _, chunk := range chunks {
  295. part, err := s.bucket.UploadPartFromFile(imur, fileName, chunk.Offset, chunk.Size, chunk.Number, Progress(&progressListener))
  296. c.Assert(err, IsNil)
  297. parts = append(parts, part)
  298. }
  299. // Complete
  300. _, err = s.bucket.CompleteMultipartUpload(imur, parts)
  301. c.Assert(err, IsNil)
  302. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  303. err = s.bucket.DeleteObject(objectName)
  304. c.Assert(err, IsNil)
  305. testLogger.Println("OssProgressSuite.TestMultipartUploadFromFile")
  306. }
  307. // TestGetObject
  308. func (s *OssProgressSuite) TestGetObject(c *C) {
  309. objectName := objectNamePrefix + randStr(8)
  310. localFile := "../sample/BingWallpaper-2015-11-07.jpg"
  311. newFile := "newpic-progress-1.jpg"
  312. fileInfo, err := os.Stat(localFile)
  313. c.Assert(err, IsNil)
  314. progressListener := OssProgressListener{}
  315. // PutObject
  316. err = s.bucket.PutObjectFromFile(objectName, localFile, Progress(&progressListener))
  317. c.Assert(err, IsNil)
  318. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  319. // GetObject
  320. progressListener.TotalRwBytes = 0
  321. body, err := s.bucket.GetObject(objectName, Progress(&progressListener))
  322. c.Assert(err, IsNil)
  323. _, err = ioutil.ReadAll(body)
  324. c.Assert(err, IsNil)
  325. body.Close()
  326. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  327. // GetObjectToFile
  328. progressListener.TotalRwBytes = 0
  329. err = s.bucket.GetObjectToFile(objectName, newFile, Progress(&progressListener))
  330. c.Assert(err, IsNil)
  331. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  332. // DoGetObject
  333. progressListener.TotalRwBytes = 0
  334. request := &GetObjectRequest{objectName}
  335. options := []Option{Progress(&progressListener)}
  336. result, err := s.bucket.DoGetObject(request, options)
  337. c.Assert(err, IsNil)
  338. _, err = ioutil.ReadAll(result.Response.Body)
  339. c.Assert(err, IsNil)
  340. result.Response.Body.Close()
  341. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  342. // GetObject with range
  343. progressListener.TotalRwBytes = 0
  344. body, err = s.bucket.GetObject(objectName, Range(1024, 4*1024), Progress(&progressListener))
  345. c.Assert(err, IsNil)
  346. text, err := ioutil.ReadAll(body)
  347. c.Assert(err, IsNil)
  348. body.Close()
  349. c.Assert(progressListener.TotalRwBytes, Equals, int64(len(text)))
  350. // PutObject size is 0
  351. progressListener.TotalRwBytes = 0
  352. err = s.bucket.PutObject(objectName, strings.NewReader(""), Progress(&progressListener))
  353. c.Assert(err, IsNil)
  354. c.Assert(progressListener.TotalRwBytes, Equals, int64(0))
  355. // GetObject size is 0
  356. progressListener.TotalRwBytes = 0
  357. body, err = s.bucket.GetObject(objectName, Progress(&progressListener))
  358. c.Assert(err, IsNil)
  359. _, err = ioutil.ReadAll(body)
  360. c.Assert(err, IsNil)
  361. body.Close()
  362. c.Assert(progressListener.TotalRwBytes, Equals, int64(0))
  363. testLogger.Println("OssProgressSuite.TestGetObject")
  364. }
  365. // TestGetObjectNegative
  366. func (s *OssProgressSuite) TestGetObjectNegative(c *C) {
  367. objectName := objectNamePrefix + randStr(8)
  368. localFile := "../sample/BingWallpaper-2015-11-07.jpg"
  369. // PutObject
  370. err := s.bucket.PutObjectFromFile(objectName, localFile)
  371. c.Assert(err, IsNil)
  372. // GetObject
  373. body, err := s.bucket.GetObject(objectName, Progress(&OssProgressListener{}))
  374. c.Assert(err, IsNil)
  375. buf := make([]byte, 4*1024)
  376. n, err := body.Read(buf)
  377. c.Assert(err, IsNil)
  378. //time.Sleep(70 * time.Second) TODO
  379. // Read should fail
  380. for err == nil {
  381. n, err = body.Read(buf)
  382. n += n
  383. }
  384. c.Assert(err, NotNil)
  385. body.Close()
  386. testLogger.Println("OssProgressSuite.TestGetObjectNegative")
  387. }
  388. // TestUploadFile
  389. func (s *OssProgressSuite) TestUploadFile(c *C) {
  390. objectName := objectNamePrefix + randStr(8)
  391. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  392. fileInfo, err := os.Stat(fileName)
  393. c.Assert(err, IsNil)
  394. progressListener := OssProgressListener{}
  395. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(5), Progress(&progressListener))
  396. c.Assert(err, IsNil)
  397. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  398. progressListener.TotalRwBytes = 0
  399. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), Checkpoint(true, objectName+".cp"), Progress(&progressListener))
  400. c.Assert(err, IsNil)
  401. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  402. testLogger.Println("OssProgressSuite.TestUploadFile")
  403. }
  404. // TestDownloadFile
  405. func (s *OssProgressSuite) TestDownloadFile(c *C) {
  406. objectName := objectNamePrefix + randStr(8)
  407. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  408. newFile := "down-new-file-progress-2.jpg"
  409. fileInfo, err := os.Stat(fileName)
  410. c.Assert(err, IsNil)
  411. // Upload
  412. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  413. c.Assert(err, IsNil)
  414. progressListener := OssProgressListener{}
  415. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(5), Progress(&progressListener))
  416. c.Assert(err, IsNil)
  417. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  418. progressListener.TotalRwBytes = 0
  419. err = s.bucket.DownloadFile(objectName, newFile, 1024*1024, Routines(3), Progress(&progressListener))
  420. c.Assert(err, IsNil)
  421. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  422. progressListener.TotalRwBytes = 0
  423. err = s.bucket.DownloadFile(objectName, newFile, 50*1024, Routines(3), Checkpoint(true, ""), Progress(&progressListener))
  424. c.Assert(err, IsNil)
  425. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  426. testLogger.Println("OssProgressSuite.TestDownloadFile")
  427. }
  428. // TestCopyFile
  429. func (s *OssProgressSuite) TestCopyFile(c *C) {
  430. srcObjectName := objectNamePrefix + randStr(8)
  431. destObjectName := srcObjectName + "-copy"
  432. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  433. fileInfo, err := os.Stat(fileName)
  434. c.Assert(err, IsNil)
  435. // Upload
  436. progressListener := OssProgressListener{}
  437. err = s.bucket.UploadFile(srcObjectName, fileName, 100*1024, Routines(3), Progress(&progressListener))
  438. c.Assert(err, IsNil)
  439. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  440. progressListener.TotalRwBytes = 0
  441. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 100*1024, Routines(5), Progress(&progressListener))
  442. c.Assert(err, IsNil)
  443. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  444. progressListener.TotalRwBytes = 0
  445. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(3), Checkpoint(true, ""), Progress(&progressListener))
  446. c.Assert(err, IsNil)
  447. c.Assert(progressListener.TotalRwBytes, Equals, fileInfo.Size())
  448. testLogger.Println("OssProgressSuite.TestCopyFile")
  449. }