progress_test.go 16 KB

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