upload_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. package oss
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "time"
  7. . "gopkg.in/check.v1"
  8. )
  9. type OssUploadSuite struct {
  10. client *Client
  11. bucket *Bucket
  12. }
  13. var _ = Suite(&OssUploadSuite{})
  14. // Run once when the suite starts running
  15. func (s *OssUploadSuite) SetUpSuite(c *C) {
  16. client, err := New(endpoint, accessID, accessKey)
  17. c.Assert(err, IsNil)
  18. s.client = client
  19. s.client.CreateBucket(bucketName)
  20. time.Sleep(5 * time.Second)
  21. bucket, err := s.client.Bucket(bucketName)
  22. c.Assert(err, IsNil)
  23. s.bucket = bucket
  24. testLogger.Println("test upload started")
  25. }
  26. // Run before each test or benchmark starts running
  27. func (s *OssUploadSuite) TearDownSuite(c *C) {
  28. // Delete Part
  29. lmur, err := s.bucket.ListMultipartUploads()
  30. c.Assert(err, IsNil)
  31. for _, upload := range lmur.Uploads {
  32. var imur = InitiateMultipartUploadResult{Bucket: s.bucket.BucketName,
  33. Key: upload.Key, UploadID: upload.UploadID}
  34. err = s.bucket.AbortMultipartUpload(imur)
  35. c.Assert(err, IsNil)
  36. }
  37. // Delete Objects
  38. lor, err := s.bucket.ListObjects()
  39. c.Assert(err, IsNil)
  40. for _, object := range lor.Objects {
  41. err = s.bucket.DeleteObject(object.Key)
  42. c.Assert(err, IsNil)
  43. }
  44. testLogger.Println("test upload completed")
  45. }
  46. // Run after each test or benchmark runs
  47. func (s *OssUploadSuite) SetUpTest(c *C) {
  48. err := removeTempFiles("../oss", ".jpg")
  49. c.Assert(err, IsNil)
  50. }
  51. // Run once after all tests or benchmarks have finished running
  52. func (s *OssUploadSuite) TearDownTest(c *C) {
  53. err := removeTempFiles("../oss", ".jpg")
  54. c.Assert(err, IsNil)
  55. }
  56. // TestUploadRoutineWithoutRecovery 多线程无断点恢复的上传
  57. func (s *OssUploadSuite) TestUploadRoutineWithoutRecovery(c *C) {
  58. objectName := objectNamePrefix + "turwr"
  59. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  60. newFile := "upload-new-file.jpg"
  61. // 不指定Routines,默认单线程
  62. err := s.bucket.UploadFile(objectName, fileName, 100*1024)
  63. c.Assert(err, IsNil)
  64. os.Remove(newFile)
  65. err = s.bucket.GetObjectToFile(objectName, newFile)
  66. c.Assert(err, IsNil)
  67. eq, err := compareFiles(fileName, newFile)
  68. c.Assert(err, IsNil)
  69. c.Assert(eq, Equals, true)
  70. err = s.bucket.DeleteObject(objectName)
  71. c.Assert(err, IsNil)
  72. // 指定线程数1
  73. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(1))
  74. c.Assert(err, IsNil)
  75. os.Remove(newFile)
  76. err = s.bucket.GetObjectToFile(objectName, newFile)
  77. c.Assert(err, IsNil)
  78. eq, err = compareFiles(fileName, newFile)
  79. c.Assert(err, IsNil)
  80. c.Assert(eq, Equals, true)
  81. err = s.bucket.DeleteObject(objectName)
  82. c.Assert(err, IsNil)
  83. // 指定线程数3,小于分片数5
  84. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  85. c.Assert(err, IsNil)
  86. os.Remove(newFile)
  87. err = s.bucket.GetObjectToFile(objectName, newFile)
  88. c.Assert(err, IsNil)
  89. eq, err = compareFiles(fileName, newFile)
  90. c.Assert(err, IsNil)
  91. c.Assert(eq, Equals, true)
  92. err = s.bucket.DeleteObject(objectName)
  93. c.Assert(err, IsNil)
  94. // 指定线程数5,等于分片数
  95. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(5))
  96. c.Assert(err, IsNil)
  97. os.Remove(newFile)
  98. err = s.bucket.GetObjectToFile(objectName, newFile)
  99. c.Assert(err, IsNil)
  100. eq, err = compareFiles(fileName, newFile)
  101. c.Assert(err, IsNil)
  102. c.Assert(eq, Equals, true)
  103. err = s.bucket.DeleteObject(objectName)
  104. c.Assert(err, IsNil)
  105. // 指定线程数10,大于分片数5
  106. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(10))
  107. c.Assert(err, IsNil)
  108. os.Remove(newFile)
  109. err = s.bucket.GetObjectToFile(objectName, newFile)
  110. c.Assert(err, IsNil)
  111. eq, err = compareFiles(fileName, newFile)
  112. c.Assert(err, IsNil)
  113. c.Assert(eq, Equals, true)
  114. err = s.bucket.DeleteObject(objectName)
  115. c.Assert(err, IsNil)
  116. // 线程值无效自动变成1
  117. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(0))
  118. os.Remove(newFile)
  119. err = s.bucket.GetObjectToFile(objectName, newFile)
  120. c.Assert(err, IsNil)
  121. eq, err = compareFiles(fileName, newFile)
  122. c.Assert(err, IsNil)
  123. c.Assert(eq, Equals, true)
  124. err = s.bucket.DeleteObject(objectName)
  125. c.Assert(err, IsNil)
  126. // 线程值无效自动变成1
  127. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(-1))
  128. os.Remove(newFile)
  129. err = s.bucket.GetObjectToFile(objectName, newFile)
  130. c.Assert(err, IsNil)
  131. eq, err = compareFiles(fileName, newFile)
  132. c.Assert(err, IsNil)
  133. c.Assert(eq, Equals, true)
  134. err = s.bucket.DeleteObject(objectName)
  135. c.Assert(err, IsNil)
  136. // option
  137. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), Meta("myprop", "mypropval"))
  138. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  139. c.Assert(err, IsNil)
  140. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  141. os.Remove(newFile)
  142. err = s.bucket.GetObjectToFile(objectName, newFile)
  143. c.Assert(err, IsNil)
  144. eq, err = compareFiles(fileName, newFile)
  145. c.Assert(err, IsNil)
  146. c.Assert(eq, Equals, true)
  147. err = s.bucket.DeleteObject(objectName)
  148. c.Assert(err, IsNil)
  149. }
  150. // ErrorHooker UploadPart请求Hook
  151. func ErrorHooker(id int, chunk FileChunk) error {
  152. if chunk.Number == 5 {
  153. time.Sleep(time.Second)
  154. return fmt.Errorf("ErrorHooker")
  155. }
  156. return nil
  157. }
  158. // TestUploadRoutineWithoutRecovery 多线程无断点恢复的上传
  159. func (s *OssUploadSuite) TestUploadRoutineWithoutRecoveryNegative(c *C) {
  160. objectName := objectNamePrefix + "turwrn"
  161. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  162. uploadPartHooker = ErrorHooker
  163. // worker线程错误
  164. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(2))
  165. c.Assert(err, NotNil)
  166. c.Assert(err.Error(), Equals, "ErrorHooker")
  167. uploadPartHooker = defaultUploadPart
  168. // 本地文件不存在
  169. err = s.bucket.UploadFile(objectName, "NotExist", 100*1024, Routines(2))
  170. c.Assert(err, NotNil)
  171. // 指定的分片大小无效
  172. err = s.bucket.UploadFile(objectName, fileName, 1024, Routines(2))
  173. c.Assert(err, NotNil)
  174. err = s.bucket.UploadFile(objectName, fileName, 1024*1024*1024*100, Routines(2))
  175. c.Assert(err, NotNil)
  176. }
  177. // TestUploadRoutineWithRecovery 多线程且有断点恢复的上传
  178. func (s *OssUploadSuite) TestUploadRoutineWithRecovery(c *C) {
  179. objectName := objectNamePrefix + "turtr"
  180. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  181. newFile := "upload-new-file-2.jpg"
  182. // Routines默认值,CP开启默认路径是fileName+.cp
  183. // 第一次上传,上传4片
  184. uploadPartHooker = ErrorHooker
  185. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Checkpoint(true, ""))
  186. c.Assert(err, NotNil)
  187. c.Assert(err.Error(), Equals, "ErrorHooker")
  188. uploadPartHooker = defaultUploadPart
  189. // check cp
  190. ucp := uploadCheckpoint{}
  191. err = ucp.load(fileName + ".cp")
  192. c.Assert(err, IsNil)
  193. c.Assert(ucp.Magic, Equals, uploadCpMagic)
  194. c.Assert(len(ucp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
  195. c.Assert(ucp.FilePath, Equals, fileName)
  196. c.Assert(ucp.FileStat.Size, Equals, int64(482048))
  197. c.Assert(len(ucp.FileStat.LastModified.String()) > 0, Equals, true)
  198. c.Assert(ucp.FileStat.MD5, Equals, "")
  199. c.Assert(ucp.ObjectKey, Equals, objectName)
  200. c.Assert(len(ucp.UploadID), Equals, len("3F79722737D1469980DACEDCA325BB52"))
  201. c.Assert(len(ucp.Parts), Equals, 5)
  202. c.Assert(len(ucp.todoParts()), Equals, 1)
  203. c.Assert(len(ucp.allParts()), Equals, 5)
  204. // 第二次上传,完成剩余的一片
  205. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Checkpoint(true, ""))
  206. c.Assert(err, IsNil)
  207. os.Remove(newFile)
  208. err = s.bucket.GetObjectToFile(objectName, newFile)
  209. c.Assert(err, IsNil)
  210. eq, err := compareFiles(fileName, newFile)
  211. c.Assert(err, IsNil)
  212. c.Assert(eq, Equals, true)
  213. err = s.bucket.DeleteObject(objectName)
  214. c.Assert(err, IsNil)
  215. err = ucp.load(fileName + ".cp")
  216. c.Assert(err, NotNil)
  217. // Routines指定,CP指定
  218. uploadPartHooker = ErrorHooker
  219. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(2), Checkpoint(true, objectName+".cp"))
  220. c.Assert(err, NotNil)
  221. c.Assert(err.Error(), Equals, "ErrorHooker")
  222. uploadPartHooker = defaultUploadPart
  223. // check cp
  224. ucp = uploadCheckpoint{}
  225. err = ucp.load(objectName + ".cp")
  226. c.Assert(err, IsNil)
  227. c.Assert(ucp.Magic, Equals, uploadCpMagic)
  228. c.Assert(len(ucp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
  229. c.Assert(ucp.FilePath, Equals, fileName)
  230. c.Assert(ucp.FileStat.Size, Equals, int64(482048))
  231. c.Assert(len(ucp.FileStat.LastModified.String()) > 0, Equals, true)
  232. c.Assert(ucp.FileStat.MD5, Equals, "")
  233. c.Assert(ucp.ObjectKey, Equals, objectName)
  234. c.Assert(len(ucp.UploadID), Equals, len("3F79722737D1469980DACEDCA325BB52"))
  235. c.Assert(len(ucp.Parts), Equals, 5)
  236. c.Assert(len(ucp.todoParts()), Equals, 1)
  237. c.Assert(len(ucp.allParts()), Equals, 5)
  238. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), Checkpoint(true, objectName+".cp"))
  239. c.Assert(err, IsNil)
  240. os.Remove(newFile)
  241. err = s.bucket.GetObjectToFile(objectName, 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(objectName)
  247. c.Assert(err, IsNil)
  248. err = ucp.load(objectName + ".cp")
  249. c.Assert(err, NotNil)
  250. // 一次完成上传,中间没有错误
  251. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), Checkpoint(true, ""))
  252. c.Assert(err, IsNil)
  253. os.Remove(newFile)
  254. err = s.bucket.GetObjectToFile(objectName, newFile)
  255. c.Assert(err, IsNil)
  256. eq, err = compareFiles(fileName, newFile)
  257. c.Assert(err, IsNil)
  258. c.Assert(eq, Equals, true)
  259. err = s.bucket.DeleteObject(objectName)
  260. c.Assert(err, IsNil)
  261. // 用多协程下载,中间没有错误
  262. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(10), Checkpoint(true, ""))
  263. c.Assert(err, IsNil)
  264. os.Remove(newFile)
  265. err = s.bucket.GetObjectToFile(objectName, newFile)
  266. c.Assert(err, IsNil)
  267. eq, err = compareFiles(fileName, newFile)
  268. c.Assert(err, IsNil)
  269. c.Assert(eq, Equals, true)
  270. err = s.bucket.DeleteObject(objectName)
  271. c.Assert(err, IsNil)
  272. // option
  273. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), Checkpoint(true, ""), Meta("myprop", "mypropval"))
  274. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  275. c.Assert(err, IsNil)
  276. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  277. os.Remove(newFile)
  278. err = s.bucket.GetObjectToFile(objectName, newFile)
  279. c.Assert(err, IsNil)
  280. eq, err = compareFiles(fileName, newFile)
  281. c.Assert(err, IsNil)
  282. c.Assert(eq, Equals, true)
  283. err = s.bucket.DeleteObject(objectName)
  284. c.Assert(err, IsNil)
  285. }
  286. // TestUploadRoutineWithoutRecovery 多线程无断点恢复的上传
  287. func (s *OssUploadSuite) TestUploadRoutineWithRecoveryNegative(c *C) {
  288. objectName := objectNamePrefix + "turrn"
  289. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  290. // 本地文件不存在
  291. err := s.bucket.UploadFile(objectName, "NotExist", 100*1024, Checkpoint(true, ""))
  292. c.Assert(err, NotNil)
  293. err = s.bucket.UploadFile(objectName, "NotExist", 100*1024, Routines(2), Checkpoint(true, ""))
  294. c.Assert(err, NotNil)
  295. // 指定的分片大小无效
  296. err = s.bucket.UploadFile(objectName, fileName, 1024, Checkpoint(true, ""))
  297. c.Assert(err, NotNil)
  298. err = s.bucket.UploadFile(objectName, fileName, 1024, Routines(2), Checkpoint(true, ""))
  299. c.Assert(err, NotNil)
  300. err = s.bucket.UploadFile(objectName, fileName, 1024*1024*1024*100, Checkpoint(true, ""))
  301. c.Assert(err, NotNil)
  302. err = s.bucket.UploadFile(objectName, fileName, 1024*1024*1024*100, Routines(2), Checkpoint(true, ""))
  303. c.Assert(err, NotNil)
  304. }
  305. // TestUploadLocalFileChange 上传过程中文件修改了
  306. func (s *OssUploadSuite) TestUploadLocalFileChange(c *C) {
  307. objectName := objectNamePrefix + "tulfc"
  308. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  309. localFile := "BingWallpaper-2015-11-07.jpg"
  310. newFile := "upload-new-file-3.jpg"
  311. os.Remove(localFile)
  312. err := copyFile(fileName, localFile)
  313. c.Assert(err, IsNil)
  314. // 第一次上传,上传4片
  315. uploadPartHooker = ErrorHooker
  316. err = s.bucket.UploadFile(objectName, localFile, 100*1024, Checkpoint(true, ""))
  317. c.Assert(err, NotNil)
  318. c.Assert(err.Error(), Equals, "ErrorHooker")
  319. uploadPartHooker = defaultUploadPart
  320. os.Remove(localFile)
  321. err = copyFile(fileName, localFile)
  322. c.Assert(err, IsNil)
  323. // 文件修改,第二次上传全部分片重新上传
  324. err = s.bucket.UploadFile(objectName, localFile, 100*1024, Checkpoint(true, ""))
  325. c.Assert(err, IsNil)
  326. os.Remove(newFile)
  327. err = s.bucket.GetObjectToFile(objectName, newFile)
  328. c.Assert(err, IsNil)
  329. eq, err := compareFiles(fileName, newFile)
  330. c.Assert(err, IsNil)
  331. c.Assert(eq, Equals, true)
  332. err = s.bucket.DeleteObject(objectName)
  333. c.Assert(err, IsNil)
  334. }
  335. func copyFile(src, dst string) error {
  336. srcFile, err := os.Open(src)
  337. if err != nil {
  338. return err
  339. }
  340. defer srcFile.Close()
  341. dstFile, err := os.Create(dst)
  342. if err != nil {
  343. return err
  344. }
  345. defer dstFile.Close()
  346. _, err = io.Copy(dstFile, srcFile)
  347. return err
  348. }