progress_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // bucket test
  2. package oss
  3. import (
  4. "bytes"
  5. "io/ioutil"
  6. "math/rand"
  7. "os"
  8. "strings"
  9. "time"
  10. . "gopkg.in/check.v1"
  11. )
  12. type OssProgressSuite struct {
  13. client *Client
  14. bucket *Bucket
  15. }
  16. var _ = Suite(&OssProgressSuite{})
  17. // Run 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. time.Sleep(5 * time.Second)
  24. bucket, err := s.client.Bucket(bucketName)
  25. c.Assert(err, IsNil)
  26. s.bucket = bucket
  27. testLogger.Println("test progress started")
  28. }
  29. // Run before each test or benchmark starts running
  30. func (s *OssProgressSuite) TearDownSuite(c *C) {
  31. // Delete Multipart
  32. lmu, err := s.bucket.ListMultipartUploads()
  33. c.Assert(err, IsNil)
  34. for _, upload := range lmu.Uploads {
  35. imur := InitiateMultipartUploadResult{Bucket: bucketName, Key: upload.Key, UploadID: upload.UploadID}
  36. err = s.bucket.AbortMultipartUpload(imur)
  37. c.Assert(err, IsNil)
  38. }
  39. // Delete Objects
  40. lor, err := s.bucket.ListObjects()
  41. c.Assert(err, IsNil)
  42. for _, object := range lor.Objects {
  43. err = s.bucket.DeleteObject(object.Key)
  44. c.Assert(err, IsNil)
  45. }
  46. testLogger.Println("test progress completed")
  47. }
  48. // Run after each test or benchmark runs
  49. func (s *OssProgressSuite) SetUpTest(c *C) {
  50. err := removeTempFiles("../oss", ".jpg")
  51. c.Assert(err, IsNil)
  52. err = removeTempFiles("../oss", ".txt")
  53. c.Assert(err, IsNil)
  54. err = removeTempFiles("../oss", ".html")
  55. c.Assert(err, IsNil)
  56. }
  57. // Run once after all tests or benchmarks have finished running
  58. func (s *OssProgressSuite) TearDownTest(c *C) {
  59. err := removeTempFiles("../oss", ".jpg")
  60. c.Assert(err, IsNil)
  61. err = removeTempFiles("../oss", ".txt")
  62. c.Assert(err, IsNil)
  63. err = removeTempFiles("../oss", ".html")
  64. c.Assert(err, IsNil)
  65. }
  66. // OssProgressListener progress listener
  67. type OssProgressListener struct {
  68. }
  69. // ProgressChanged handle progress event
  70. func (listener *OssProgressListener) ProgressChanged(event *ProgressEvent) {
  71. switch event.EventType {
  72. case TransferStartedEvent:
  73. testLogger.Printf("Transfer Started, ConsumedBytes: %d, TotalBytes %d.\n",
  74. event.ConsumedBytes, event.TotalBytes)
  75. case TransferDataEvent:
  76. testLogger.Printf("Transfer Data, ConsumedBytes: %d, TotalBytes %d, %d%%.\n",
  77. event.ConsumedBytes, event.TotalBytes, event.ConsumedBytes*100/event.TotalBytes)
  78. case TransferCompletedEvent:
  79. testLogger.Printf("Transfer Completed, ConsumedBytes: %d, TotalBytes %d.\n",
  80. event.ConsumedBytes, event.TotalBytes)
  81. case TransferFailedEvent:
  82. testLogger.Printf("Transfer Failed, ConsumedBytes: %d, TotalBytes %d.\n",
  83. event.ConsumedBytes, event.TotalBytes)
  84. default:
  85. }
  86. }
  87. // TestPutObject
  88. func (s *OssProgressSuite) TestPutObject(c *C) {
  89. objectName := objectNamePrefix + "tpo.html"
  90. localFile := "../sample/The Go Programming Language.html"
  91. // PutObject
  92. fd, err := os.Open(localFile)
  93. c.Assert(err, IsNil)
  94. defer fd.Close()
  95. err = s.bucket.PutObject(objectName, fd, Progress(&OssProgressListener{}))
  96. c.Assert(err, IsNil)
  97. // PutObjectFromFile
  98. err = s.bucket.PutObjectFromFile(objectName, localFile, Progress(&OssProgressListener{}))
  99. c.Assert(err, IsNil)
  100. // DoPutObject
  101. fd, err = os.Open(localFile)
  102. c.Assert(err, IsNil)
  103. defer fd.Close()
  104. request := &PutObjectRequest{
  105. ObjectKey: objectName,
  106. Reader: fd,
  107. }
  108. options := []Option{Progress(&OssProgressListener{})}
  109. _, err = s.bucket.DoPutObject(request, options)
  110. c.Assert(err, IsNil)
  111. // PutObject size is 0
  112. err = s.bucket.PutObject(objectName, strings.NewReader(""), Progress(&OssProgressListener{}))
  113. c.Assert(err, IsNil)
  114. testLogger.Println("OssProgressSuite.TestPutObject")
  115. }
  116. // Test SignURL
  117. func (s *OssProgressSuite) TestSignURL(c *C) {
  118. objectName := objectNamePrefix + randStr(5)
  119. filePath := randLowStr(10)
  120. content := randStr(20)
  121. createFile(filePath, content, c)
  122. // sign url for put
  123. str, err := s.bucket.SignURL(objectName, HTTPPut, 60, Progress(&OssProgressListener{}))
  124. c.Assert(err, IsNil)
  125. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  126. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  127. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  128. // put object with url
  129. fd, err := os.Open(filePath)
  130. c.Assert(err, IsNil)
  131. defer fd.Close()
  132. err = s.bucket.PutObjectWithURL(str, fd, Progress(&OssProgressListener{}))
  133. c.Assert(err, IsNil)
  134. // put object from file with url
  135. err = s.bucket.PutObjectFromFileWithURL(str, filePath, Progress(&OssProgressListener{}))
  136. c.Assert(err, IsNil)
  137. // DoPutObject
  138. fd, err = os.Open(filePath)
  139. c.Assert(err, IsNil)
  140. defer fd.Close()
  141. options := []Option{Progress(&OssProgressListener{})}
  142. _, err = s.bucket.DoPutObjectWithURL(str, fd, options)
  143. c.Assert(err, IsNil)
  144. // sign url for get
  145. str, err = s.bucket.SignURL(objectName, HTTPGet, 60, Progress(&OssProgressListener{}))
  146. c.Assert(err, IsNil)
  147. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  148. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  149. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  150. // get object with url
  151. body, err := s.bucket.GetObjectWithURL(str, Progress(&OssProgressListener{}))
  152. c.Assert(err, IsNil)
  153. str, err = readBody(body)
  154. c.Assert(err, IsNil)
  155. c.Assert(str, Equals, content)
  156. // get object to file with url
  157. str, err = s.bucket.SignURL(objectName, HTTPGet, 10, Progress(&OssProgressListener{}))
  158. c.Assert(err, IsNil)
  159. newFile := randStr(10)
  160. err = s.bucket.GetObjectToFileWithURL(str, newFile, Progress(&OssProgressListener{}))
  161. c.Assert(err, IsNil)
  162. eq, err := compareFiles(filePath, newFile)
  163. c.Assert(err, IsNil)
  164. c.Assert(eq, Equals, true)
  165. os.Remove(filePath)
  166. os.Remove(newFile)
  167. err = s.bucket.DeleteObject(objectName)
  168. c.Assert(err, IsNil)
  169. testLogger.Println("OssProgressSuite.TestSignURL")
  170. }
  171. func (s *OssProgressSuite) TestPutObjectNegative(c *C) {
  172. objectName := objectNamePrefix + "tpon.html"
  173. localFile := "../sample/The Go Programming Language.html"
  174. // invalid endpoint
  175. client, err := New("http://oss-cn-taikang.aliyuncs.com", accessID, accessKey)
  176. c.Assert(err, IsNil)
  177. bucket, err := client.Bucket(bucketName)
  178. c.Assert(err, IsNil)
  179. err = bucket.PutObjectFromFile(objectName, localFile, Progress(&OssProgressListener{}))
  180. testLogger.Println(err)
  181. c.Assert(err, NotNil)
  182. testLogger.Println("OssProgressSuite.TestPutObjectNegative")
  183. }
  184. // TestAppendObject
  185. func (s *OssProgressSuite) TestAppendObject(c *C) {
  186. objectName := objectNamePrefix + "tao"
  187. objectValue := "昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。"
  188. var val = []byte(objectValue)
  189. var nextPos int64
  190. var midPos = 1 + rand.Intn(len(val)-1)
  191. // AppendObject
  192. nextPos, err := s.bucket.AppendObject(objectName, bytes.NewReader(val[0:midPos]), nextPos, Progress(&OssProgressListener{}))
  193. c.Assert(err, IsNil)
  194. // DoAppendObject
  195. request := &AppendObjectRequest{
  196. ObjectKey: objectName,
  197. Reader: bytes.NewReader(val[midPos:]),
  198. Position: nextPos,
  199. }
  200. options := []Option{Progress(&OssProgressListener{})}
  201. _, err = s.bucket.DoAppendObject(request, options)
  202. c.Assert(err, IsNil)
  203. testLogger.Println("OssProgressSuite.TestAppendObject")
  204. }
  205. // TestMultipartUpload
  206. func (s *OssProgressSuite) TestMultipartUpload(c *C) {
  207. objectName := objectNamePrefix + "tmu.jpg"
  208. var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
  209. chunks, err := SplitFileByPartNum(fileName, 3)
  210. c.Assert(err, IsNil)
  211. testLogger.Println("chunks:", chunks)
  212. fd, err := os.Open(fileName)
  213. c.Assert(err, IsNil)
  214. defer fd.Close()
  215. // Initiate
  216. imur, err := s.bucket.InitiateMultipartUpload(objectName)
  217. c.Assert(err, IsNil)
  218. // UploadPart
  219. var parts []UploadPart
  220. for _, chunk := range chunks {
  221. fd.Seek(chunk.Offset, os.SEEK_SET)
  222. part, err := s.bucket.UploadPart(imur, fd, chunk.Size, chunk.Number, Progress(&OssProgressListener{}))
  223. c.Assert(err, IsNil)
  224. parts = append(parts, part)
  225. }
  226. // Complete
  227. _, err = s.bucket.CompleteMultipartUpload(imur, parts)
  228. c.Assert(err, IsNil)
  229. err = s.bucket.DeleteObject(objectName)
  230. c.Assert(err, IsNil)
  231. testLogger.Println("OssProgressSuite.TestMultipartUpload")
  232. }
  233. // TestMultipartUploadFromFile
  234. func (s *OssProgressSuite) TestMultipartUploadFromFile(c *C) {
  235. objectName := objectNamePrefix + "tmuff.jpg"
  236. var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
  237. chunks, err := SplitFileByPartNum(fileName, 3)
  238. c.Assert(err, IsNil)
  239. // Initiate
  240. imur, err := s.bucket.InitiateMultipartUpload(objectName)
  241. c.Assert(err, IsNil)
  242. // UploadPart
  243. var parts []UploadPart
  244. for _, chunk := range chunks {
  245. part, err := s.bucket.UploadPartFromFile(imur, fileName, chunk.Offset, chunk.Size, chunk.Number, Progress(&OssProgressListener{}))
  246. c.Assert(err, IsNil)
  247. parts = append(parts, part)
  248. }
  249. // Complete
  250. _, err = s.bucket.CompleteMultipartUpload(imur, parts)
  251. c.Assert(err, IsNil)
  252. err = s.bucket.DeleteObject(objectName)
  253. c.Assert(err, IsNil)
  254. testLogger.Println("OssProgressSuite.TestMultipartUploadFromFile")
  255. }
  256. // TestGetObject
  257. func (s *OssProgressSuite) TestGetObject(c *C) {
  258. objectName := objectNamePrefix + "tgo.jpg"
  259. localFile := "../sample/BingWallpaper-2015-11-07.jpg"
  260. newFile := "newpic-progress-1.jpg"
  261. // PutObject
  262. err := s.bucket.PutObjectFromFile(objectName, localFile, Progress(&OssProgressListener{}))
  263. c.Assert(err, IsNil)
  264. // GetObject
  265. body, err := s.bucket.GetObject(objectName, Progress(&OssProgressListener{}))
  266. c.Assert(err, IsNil)
  267. _, err = ioutil.ReadAll(body)
  268. c.Assert(err, IsNil)
  269. body.Close()
  270. // GetObjectToFile
  271. err = s.bucket.GetObjectToFile(objectName, newFile, Progress(&OssProgressListener{}))
  272. c.Assert(err, IsNil)
  273. // DoGetObject
  274. request := &GetObjectRequest{objectName}
  275. options := []Option{Progress(&OssProgressListener{})}
  276. result, err := s.bucket.DoGetObject(request, options)
  277. c.Assert(err, IsNil)
  278. _, err = ioutil.ReadAll(result.Response.Body)
  279. c.Assert(err, IsNil)
  280. result.Response.Body.Close()
  281. // GetObject with range
  282. body, err = s.bucket.GetObject(objectName, Range(1024, 4*1024), Progress(&OssProgressListener{}))
  283. c.Assert(err, IsNil)
  284. _, err = ioutil.ReadAll(body)
  285. c.Assert(err, IsNil)
  286. body.Close()
  287. // PutObject size is 0
  288. err = s.bucket.PutObject(objectName, strings.NewReader(""), Progress(&OssProgressListener{}))
  289. c.Assert(err, IsNil)
  290. // GetObject size is 0
  291. body, err = s.bucket.GetObject(objectName, Progress(&OssProgressListener{}))
  292. c.Assert(err, IsNil)
  293. _, err = ioutil.ReadAll(body)
  294. c.Assert(err, IsNil)
  295. body.Close()
  296. testLogger.Println("OssProgressSuite.TestGetObject")
  297. }
  298. // TestGetObjectNegative
  299. func (s *OssProgressSuite) TestGetObjectNegative(c *C) {
  300. objectName := objectNamePrefix + "tgon.jpg"
  301. localFile := "../sample/BingWallpaper-2015-11-07.jpg"
  302. // PutObject
  303. err := s.bucket.PutObjectFromFile(objectName, localFile)
  304. c.Assert(err, IsNil)
  305. // GetObject
  306. body, err := s.bucket.GetObject(objectName, Progress(&OssProgressListener{}))
  307. c.Assert(err, IsNil)
  308. buf := make([]byte, 4*1024)
  309. n, err := body.Read(buf)
  310. c.Assert(err, IsNil)
  311. //time.Sleep(70 * time.Second) TODO
  312. // read should fail
  313. for err == nil {
  314. n, err = body.Read(buf)
  315. n += n
  316. }
  317. c.Assert(err, NotNil)
  318. body.Close()
  319. testLogger.Println("OssProgressSuite.TestGetObjectNegative")
  320. }
  321. // TestUploadFile
  322. func (s *OssProgressSuite) TestUploadFile(c *C) {
  323. objectName := objectNamePrefix + "tuf.jpg"
  324. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  325. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(5), Progress(&OssProgressListener{}))
  326. c.Assert(err, IsNil)
  327. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), Checkpoint(true, objectName+".cp"), Progress(&OssProgressListener{}))
  328. c.Assert(err, IsNil)
  329. testLogger.Println("OssProgressSuite.TestUploadFile")
  330. }
  331. // TestDownloadFile
  332. func (s *OssProgressSuite) TestDownloadFile(c *C) {
  333. objectName := objectNamePrefix + "tdf.jpg"
  334. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  335. newFile := "down-new-file-progress-2.jpg"
  336. // upload
  337. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  338. c.Assert(err, IsNil)
  339. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(5), Progress(&OssProgressListener{}))
  340. c.Assert(err, IsNil)
  341. err = s.bucket.DownloadFile(objectName, newFile, 1024*1024, Routines(3), Progress(&OssProgressListener{}))
  342. c.Assert(err, IsNil)
  343. err = s.bucket.DownloadFile(objectName, newFile, 50*1024, Routines(3), Checkpoint(true, ""), Progress(&OssProgressListener{}))
  344. c.Assert(err, IsNil)
  345. testLogger.Println("OssProgressSuite.TestDownloadFile")
  346. }
  347. // TestCopyFile
  348. func (s *OssProgressSuite) TestCopyFile(c *C) {
  349. srcObjectName := objectNamePrefix + "tcf.jpg"
  350. destObjectName := srcObjectName + "-copy"
  351. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  352. // upload
  353. err := s.bucket.UploadFile(srcObjectName, fileName, 100*1024, Routines(3))
  354. c.Assert(err, IsNil)
  355. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 100*1024, Routines(5), Progress(&OssProgressListener{}))
  356. c.Assert(err, IsNil)
  357. err = s.bucket.CopyFile(bucketName, srcObjectName, destObjectName, 1024*100, Routines(3), Checkpoint(true, ""), Progress(&OssProgressListener{}))
  358. c.Assert(err, IsNil)
  359. testLogger.Println("OssProgressSuite.TestCopyFile")
  360. }