upload_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. // SetUpSuite runs 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. // TearDownSuite runs 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. // SetUpTest runs 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. // TearDownTest runs 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 tests multiroutineed upload without checkpoint
  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 is not specified, by default single routine
  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. // Specify routine count as 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. // Specify routine count as 3, which is smaller than parts count 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. // Specify routine count as 5, which is same as the part count 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. // Specify routine count as 10, which is bigger than the part count 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. // Invalid routine count, it will use 1 automatically.
  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. // Invalid routine count, it will use 1 automatically
  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 is a UploadPart hook---it will fail the 5th part's upload.
  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. // TestUploadRoutineWithoutRecoveryNegative is multiroutineed upload without checkpoint
  159. func (s *OssUploadSuite) TestUploadRoutineWithoutRecoveryNegative(c *C) {
  160. objectName := objectNamePrefix + "turwrn"
  161. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  162. uploadPartHooker = ErrorHooker
  163. // Worker routine error
  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. // Local file does not exist
  169. err = s.bucket.UploadFile(objectName, "NotExist", 100*1024, Routines(2))
  170. c.Assert(err, NotNil)
  171. // The part size is invalid
  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 is multi-routine upload with resumable recovery
  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. // Use default routines and default CP file path (fileName+.cp)
  183. // First upload for 4 parts
  184. uploadPartHooker = ErrorHooker
  185. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Checkpoint(true, fileName+".cp"))
  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. // Second upload, finish the remaining part
  205. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Checkpoint(true, fileName+".cp"))
  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. // Resumable upload with empty checkpoint path
  218. uploadPartHooker = ErrorHooker
  219. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), CheckpointDir(true, ""))
  220. c.Assert(err, NotNil)
  221. c.Assert(err.Error(), Equals, "ErrorHooker")
  222. uploadPartHooker = defaultUploadPart
  223. ucp = uploadCheckpoint{}
  224. err = ucp.load(fileName + ".cp")
  225. c.Assert(err, NotNil)
  226. // Resumable upload with checkpoint dir
  227. uploadPartHooker = ErrorHooker
  228. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), CheckpointDir(true, "./"))
  229. c.Assert(err, NotNil)
  230. c.Assert(err.Error(), Equals, "ErrorHooker")
  231. uploadPartHooker = defaultUploadPart
  232. // Check CP
  233. ucp = uploadCheckpoint{}
  234. cpConf := cpConfig{IsEnable: true, DirPath: "./"}
  235. cpFilePath := getUploadCpFilePath(&cpConf, fileName, s.bucket.BucketName, objectName)
  236. err = ucp.load(cpFilePath)
  237. c.Assert(err, IsNil)
  238. c.Assert(ucp.Magic, Equals, uploadCpMagic)
  239. c.Assert(len(ucp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
  240. c.Assert(ucp.FilePath, Equals, fileName)
  241. c.Assert(ucp.FileStat.Size, Equals, int64(482048))
  242. c.Assert(len(ucp.FileStat.LastModified.String()) > 0, Equals, true)
  243. c.Assert(ucp.FileStat.MD5, Equals, "")
  244. c.Assert(ucp.ObjectKey, Equals, objectName)
  245. c.Assert(len(ucp.UploadID), Equals, len("3F79722737D1469980DACEDCA325BB52"))
  246. c.Assert(len(ucp.Parts), Equals, 5)
  247. c.Assert(len(ucp.todoParts()), Equals, 1)
  248. c.Assert(len(ucp.allParts()), Equals, 5)
  249. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), CheckpointDir(true, "./"))
  250. c.Assert(err, IsNil)
  251. os.Remove(newFile)
  252. err = s.bucket.GetObjectToFile(objectName, newFile)
  253. c.Assert(err, IsNil)
  254. eq, err = compareFiles(fileName, newFile)
  255. c.Assert(err, IsNil)
  256. c.Assert(eq, Equals, true)
  257. err = s.bucket.DeleteObject(objectName)
  258. c.Assert(err, IsNil)
  259. err = ucp.load(cpFilePath)
  260. c.Assert(err, NotNil)
  261. // Upload all 5 parts without error
  262. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), Checkpoint(true, objectName+".cp"))
  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. // Upload all 5 parts with 10 routines without error
  273. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(10), Checkpoint(true, objectName+".cp"))
  274. c.Assert(err, IsNil)
  275. os.Remove(newFile)
  276. err = s.bucket.GetObjectToFile(objectName, newFile)
  277. c.Assert(err, IsNil)
  278. eq, err = compareFiles(fileName, newFile)
  279. c.Assert(err, IsNil)
  280. c.Assert(eq, Equals, true)
  281. err = s.bucket.DeleteObject(objectName)
  282. c.Assert(err, IsNil)
  283. // Option
  284. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), Checkpoint(true, objectName+".cp"), Meta("myprop", "mypropval"))
  285. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  286. c.Assert(err, IsNil)
  287. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  288. os.Remove(newFile)
  289. err = s.bucket.GetObjectToFile(objectName, newFile)
  290. c.Assert(err, IsNil)
  291. eq, err = compareFiles(fileName, newFile)
  292. c.Assert(err, IsNil)
  293. c.Assert(eq, Equals, true)
  294. err = s.bucket.DeleteObject(objectName)
  295. c.Assert(err, IsNil)
  296. }
  297. // TestUploadRoutineWithRecoveryNegative is multiroutineed upload without checkpoint
  298. func (s *OssUploadSuite) TestUploadRoutineWithRecoveryNegative(c *C) {
  299. objectName := objectNamePrefix + "turrn"
  300. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  301. // The local file does not exist
  302. err := s.bucket.UploadFile(objectName, "NotExist", 100*1024, Checkpoint(true, "NotExist.cp"))
  303. c.Assert(err, NotNil)
  304. err = s.bucket.UploadFile(objectName, "NotExist", 100*1024, Routines(2), Checkpoint(true, "NotExist.cp"))
  305. c.Assert(err, NotNil)
  306. // Specified part size is invalid
  307. err = s.bucket.UploadFile(objectName, fileName, 1024, Checkpoint(true, fileName+".cp"))
  308. c.Assert(err, NotNil)
  309. err = s.bucket.UploadFile(objectName, fileName, 1024, Routines(2), Checkpoint(true, fileName+".cp"))
  310. c.Assert(err, NotNil)
  311. err = s.bucket.UploadFile(objectName, fileName, 1024*1024*1024*100, Checkpoint(true, fileName+".cp"))
  312. c.Assert(err, NotNil)
  313. err = s.bucket.UploadFile(objectName, fileName, 1024*1024*1024*100, Routines(2), Checkpoint(true, fileName+".cp"))
  314. c.Assert(err, NotNil)
  315. }
  316. // TestUploadLocalFileChange tests the file is updated while being uploaded
  317. func (s *OssUploadSuite) TestUploadLocalFileChange(c *C) {
  318. objectName := objectNamePrefix + "tulfc"
  319. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  320. localFile := "BingWallpaper-2015-11-07.jpg"
  321. newFile := "upload-new-file-3.jpg"
  322. os.Remove(localFile)
  323. err := copyFile(fileName, localFile)
  324. c.Assert(err, IsNil)
  325. // First upload for 4 parts
  326. uploadPartHooker = ErrorHooker
  327. err = s.bucket.UploadFile(objectName, localFile, 100*1024, Checkpoint(true, localFile+".cp"))
  328. c.Assert(err, NotNil)
  329. c.Assert(err.Error(), Equals, "ErrorHooker")
  330. uploadPartHooker = defaultUploadPart
  331. os.Remove(localFile)
  332. err = copyFile(fileName, localFile)
  333. c.Assert(err, IsNil)
  334. // Updating the file. The second upload will re-upload all 5 parts.
  335. err = s.bucket.UploadFile(objectName, localFile, 100*1024, Checkpoint(true, localFile+".cp"))
  336. c.Assert(err, IsNil)
  337. os.Remove(newFile)
  338. err = s.bucket.GetObjectToFile(objectName, newFile)
  339. c.Assert(err, IsNil)
  340. eq, err := compareFiles(fileName, newFile)
  341. c.Assert(err, IsNil)
  342. c.Assert(eq, Equals, true)
  343. err = s.bucket.DeleteObject(objectName)
  344. c.Assert(err, IsNil)
  345. }
  346. func copyFile(src, dst string) error {
  347. srcFile, err := os.Open(src)
  348. if err != nil {
  349. return err
  350. }
  351. defer srcFile.Close()
  352. dstFile, err := os.Create(dst)
  353. if err != nil {
  354. return err
  355. }
  356. defer dstFile.Close()
  357. _, err = io.Copy(dstFile, srcFile)
  358. return err
  359. }