upload_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. package oss
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "os"
  7. "time"
  8. . "gopkg.in/check.v1"
  9. )
  10. type OssUploadSuite struct {
  11. client *Client
  12. bucket *Bucket
  13. }
  14. var _ = Suite(&OssUploadSuite{})
  15. // SetUpSuite runs once when the suite starts running
  16. func (s *OssUploadSuite) SetUpSuite(c *C) {
  17. client, err := New(endpoint, accessID, accessKey)
  18. c.Assert(err, IsNil)
  19. s.client = client
  20. s.client.CreateBucket(bucketName)
  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. keyMarker := KeyMarker("")
  30. uploadIDMarker := UploadIDMarker("")
  31. for {
  32. lmur, err := s.bucket.ListMultipartUploads(keyMarker, uploadIDMarker)
  33. c.Assert(err, IsNil)
  34. for _, upload := range lmur.Uploads {
  35. var imur = InitiateMultipartUploadResult{Bucket: s.bucket.BucketName,
  36. Key: upload.Key, UploadID: upload.UploadID}
  37. err = s.bucket.AbortMultipartUpload(imur)
  38. c.Assert(err, IsNil)
  39. }
  40. keyMarker = KeyMarker(lmur.NextKeyMarker)
  41. uploadIDMarker = UploadIDMarker(lmur.NextUploadIDMarker)
  42. if !lmur.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 upload completed")
  64. }
  65. // SetUpTest runs after each test or benchmark runs
  66. func (s *OssUploadSuite) SetUpTest(c *C) {
  67. err := removeTempFiles("../oss", ".jpg")
  68. c.Assert(err, IsNil)
  69. }
  70. // TearDownTest runs once after all tests or benchmarks have finished running
  71. func (s *OssUploadSuite) TearDownTest(c *C) {
  72. err := removeTempFiles("../oss", ".jpg")
  73. c.Assert(err, IsNil)
  74. }
  75. // TestUploadRoutineWithoutRecovery tests multiroutineed upload without checkpoint
  76. func (s *OssUploadSuite) TestUploadRoutineWithoutRecovery(c *C) {
  77. objectName := objectNamePrefix + randStr(8)
  78. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  79. newFile := randStr(8) + ".jpg"
  80. // Routines is not specified, by default single routine
  81. err := s.bucket.UploadFile(objectName, fileName, 100*1024)
  82. c.Assert(err, IsNil)
  83. os.Remove(newFile)
  84. err = s.bucket.GetObjectToFile(objectName, newFile)
  85. c.Assert(err, IsNil)
  86. eq, err := compareFiles(fileName, newFile)
  87. c.Assert(err, IsNil)
  88. c.Assert(eq, Equals, true)
  89. err = s.bucket.DeleteObject(objectName)
  90. c.Assert(err, IsNil)
  91. // Specify routine count as 1
  92. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(1))
  93. c.Assert(err, IsNil)
  94. os.Remove(newFile)
  95. err = s.bucket.GetObjectToFile(objectName, newFile)
  96. c.Assert(err, IsNil)
  97. eq, err = compareFiles(fileName, newFile)
  98. c.Assert(err, IsNil)
  99. c.Assert(eq, Equals, true)
  100. err = s.bucket.DeleteObject(objectName)
  101. c.Assert(err, IsNil)
  102. // Specify routine count as 3, which is smaller than parts count 5
  103. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  104. c.Assert(err, IsNil)
  105. os.Remove(newFile)
  106. err = s.bucket.GetObjectToFile(objectName, newFile)
  107. c.Assert(err, IsNil)
  108. eq, err = compareFiles(fileName, newFile)
  109. c.Assert(err, IsNil)
  110. c.Assert(eq, Equals, true)
  111. err = s.bucket.DeleteObject(objectName)
  112. c.Assert(err, IsNil)
  113. // Specify routine count as 5, which is same as the part count 5
  114. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(5))
  115. c.Assert(err, IsNil)
  116. os.Remove(newFile)
  117. err = s.bucket.GetObjectToFile(objectName, newFile)
  118. c.Assert(err, IsNil)
  119. eq, err = compareFiles(fileName, newFile)
  120. c.Assert(err, IsNil)
  121. c.Assert(eq, Equals, true)
  122. err = s.bucket.DeleteObject(objectName)
  123. c.Assert(err, IsNil)
  124. // Specify routine count as 10, which is bigger than the part count 5.
  125. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(10))
  126. c.Assert(err, IsNil)
  127. os.Remove(newFile)
  128. err = s.bucket.GetObjectToFile(objectName, newFile)
  129. c.Assert(err, IsNil)
  130. eq, err = compareFiles(fileName, newFile)
  131. c.Assert(err, IsNil)
  132. c.Assert(eq, Equals, true)
  133. err = s.bucket.DeleteObject(objectName)
  134. c.Assert(err, IsNil)
  135. // Invalid routine count, it will use 1 automatically.
  136. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(0))
  137. os.Remove(newFile)
  138. err = s.bucket.GetObjectToFile(objectName, newFile)
  139. c.Assert(err, IsNil)
  140. eq, err = compareFiles(fileName, newFile)
  141. c.Assert(err, IsNil)
  142. c.Assert(eq, Equals, true)
  143. err = s.bucket.DeleteObject(objectName)
  144. c.Assert(err, IsNil)
  145. // Invalid routine count, it will use 1 automatically
  146. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(-1))
  147. os.Remove(newFile)
  148. err = s.bucket.GetObjectToFile(objectName, newFile)
  149. c.Assert(err, IsNil)
  150. eq, err = compareFiles(fileName, newFile)
  151. c.Assert(err, IsNil)
  152. c.Assert(eq, Equals, true)
  153. err = s.bucket.DeleteObject(objectName)
  154. c.Assert(err, IsNil)
  155. // Option
  156. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), Meta("myprop", "mypropval"))
  157. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  158. c.Assert(err, IsNil)
  159. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  160. os.Remove(newFile)
  161. err = s.bucket.GetObjectToFile(objectName, newFile)
  162. c.Assert(err, IsNil)
  163. eq, err = compareFiles(fileName, newFile)
  164. c.Assert(err, IsNil)
  165. c.Assert(eq, Equals, true)
  166. err = s.bucket.DeleteObject(objectName)
  167. c.Assert(err, IsNil)
  168. }
  169. // ErrorHooker is a UploadPart hook---it will fail the 5th part's upload.
  170. func ErrorHooker(id int, chunk FileChunk) error {
  171. if chunk.Number == 5 {
  172. time.Sleep(time.Second)
  173. return fmt.Errorf("ErrorHooker")
  174. }
  175. return nil
  176. }
  177. // TestUploadRoutineWithoutRecoveryNegative is multiroutineed upload without checkpoint
  178. func (s *OssUploadSuite) TestUploadRoutineWithoutRecoveryNegative(c *C) {
  179. objectName := objectNamePrefix + randStr(8)
  180. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  181. uploadPartHooker = ErrorHooker
  182. // Worker routine error
  183. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(2))
  184. c.Assert(err, NotNil)
  185. c.Assert(err.Error(), Equals, "ErrorHooker")
  186. uploadPartHooker = defaultUploadPart
  187. // Local file does not exist
  188. err = s.bucket.UploadFile(objectName, "NotExist", 100*1024, Routines(2))
  189. c.Assert(err, NotNil)
  190. // The part size is invalid
  191. err = s.bucket.UploadFile(objectName, fileName, 1024, Routines(2))
  192. c.Assert(err, NotNil)
  193. err = s.bucket.UploadFile(objectName, fileName, 1024*1024*1024*100, Routines(2))
  194. c.Assert(err, NotNil)
  195. }
  196. // TestUploadRoutineWithRecovery is multi-routine upload with resumable recovery
  197. func (s *OssUploadSuite) TestUploadRoutineWithRecovery(c *C) {
  198. objectName := objectNamePrefix + randStr(8)
  199. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  200. newFile := "upload-new-file-2.jpg"
  201. // Use default routines and default CP file path (fileName+.cp)
  202. // First upload for 4 parts
  203. uploadPartHooker = ErrorHooker
  204. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Checkpoint(true, fileName+".cp"))
  205. c.Assert(err, NotNil)
  206. c.Assert(err.Error(), Equals, "ErrorHooker")
  207. uploadPartHooker = defaultUploadPart
  208. // Check CP
  209. ucp := uploadCheckpoint{}
  210. err = ucp.load(fileName + ".cp")
  211. c.Assert(err, IsNil)
  212. c.Assert(ucp.Magic, Equals, uploadCpMagic)
  213. c.Assert(len(ucp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
  214. c.Assert(ucp.FilePath, Equals, fileName)
  215. c.Assert(ucp.FileStat.Size, Equals, int64(482048))
  216. c.Assert(len(ucp.FileStat.LastModified.String()) > 0, Equals, true)
  217. c.Assert(ucp.FileStat.MD5, Equals, "")
  218. c.Assert(ucp.ObjectKey, Equals, objectName)
  219. c.Assert(len(ucp.UploadID), Equals, len("3F79722737D1469980DACEDCA325BB52"))
  220. c.Assert(len(ucp.Parts), Equals, 5)
  221. c.Assert(len(ucp.todoParts()), Equals, 1)
  222. c.Assert(len(ucp.allParts()), Equals, 5)
  223. // Second upload, finish the remaining part
  224. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Checkpoint(true, fileName+".cp"))
  225. c.Assert(err, IsNil)
  226. os.Remove(newFile)
  227. err = s.bucket.GetObjectToFile(objectName, newFile)
  228. c.Assert(err, IsNil)
  229. eq, err := compareFiles(fileName, newFile)
  230. c.Assert(err, IsNil)
  231. c.Assert(eq, Equals, true)
  232. err = s.bucket.DeleteObject(objectName)
  233. c.Assert(err, IsNil)
  234. err = ucp.load(fileName + ".cp")
  235. c.Assert(err, NotNil)
  236. // Resumable upload with empty checkpoint path
  237. uploadPartHooker = ErrorHooker
  238. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), CheckpointDir(true, ""))
  239. c.Assert(err, NotNil)
  240. c.Assert(err.Error(), Equals, "ErrorHooker")
  241. uploadPartHooker = defaultUploadPart
  242. ucp = uploadCheckpoint{}
  243. err = ucp.load(fileName + ".cp")
  244. c.Assert(err, NotNil)
  245. // Resumable upload with checkpoint dir
  246. uploadPartHooker = ErrorHooker
  247. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), CheckpointDir(true, "./"))
  248. c.Assert(err, NotNil)
  249. c.Assert(err.Error(), Equals, "ErrorHooker")
  250. uploadPartHooker = defaultUploadPart
  251. // Check CP
  252. ucp = uploadCheckpoint{}
  253. cpConf := cpConfig{IsEnable: true, DirPath: "./"}
  254. cpFilePath := getUploadCpFilePath(&cpConf, fileName, s.bucket.BucketName, objectName)
  255. err = ucp.load(cpFilePath)
  256. c.Assert(err, IsNil)
  257. c.Assert(ucp.Magic, Equals, uploadCpMagic)
  258. c.Assert(len(ucp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
  259. c.Assert(ucp.FilePath, Equals, fileName)
  260. c.Assert(ucp.FileStat.Size, Equals, int64(482048))
  261. c.Assert(len(ucp.FileStat.LastModified.String()) > 0, Equals, true)
  262. c.Assert(ucp.FileStat.MD5, Equals, "")
  263. c.Assert(ucp.ObjectKey, Equals, objectName)
  264. c.Assert(len(ucp.UploadID), Equals, len("3F79722737D1469980DACEDCA325BB52"))
  265. c.Assert(len(ucp.Parts), Equals, 5)
  266. c.Assert(len(ucp.todoParts()), Equals, 1)
  267. c.Assert(len(ucp.allParts()), Equals, 5)
  268. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), CheckpointDir(true, "./"))
  269. c.Assert(err, IsNil)
  270. os.Remove(newFile)
  271. err = s.bucket.GetObjectToFile(objectName, newFile)
  272. c.Assert(err, IsNil)
  273. eq, err = compareFiles(fileName, newFile)
  274. c.Assert(err, IsNil)
  275. c.Assert(eq, Equals, true)
  276. err = s.bucket.DeleteObject(objectName)
  277. c.Assert(err, IsNil)
  278. err = ucp.load(cpFilePath)
  279. c.Assert(err, NotNil)
  280. // Upload all 5 parts without error
  281. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), Checkpoint(true, objectName+".cp"))
  282. c.Assert(err, IsNil)
  283. os.Remove(newFile)
  284. err = s.bucket.GetObjectToFile(objectName, newFile)
  285. c.Assert(err, IsNil)
  286. eq, err = compareFiles(fileName, newFile)
  287. c.Assert(err, IsNil)
  288. c.Assert(eq, Equals, true)
  289. err = s.bucket.DeleteObject(objectName)
  290. c.Assert(err, IsNil)
  291. // Upload all 5 parts with 10 routines without error
  292. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(10), Checkpoint(true, objectName+".cp"))
  293. c.Assert(err, IsNil)
  294. os.Remove(newFile)
  295. err = s.bucket.GetObjectToFile(objectName, newFile)
  296. c.Assert(err, IsNil)
  297. eq, err = compareFiles(fileName, newFile)
  298. c.Assert(err, IsNil)
  299. c.Assert(eq, Equals, true)
  300. err = s.bucket.DeleteObject(objectName)
  301. c.Assert(err, IsNil)
  302. // Option
  303. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), Checkpoint(true, objectName+".cp"), Meta("myprop", "mypropval"))
  304. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  305. c.Assert(err, IsNil)
  306. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  307. os.Remove(newFile)
  308. err = s.bucket.GetObjectToFile(objectName, newFile)
  309. c.Assert(err, IsNil)
  310. eq, err = compareFiles(fileName, newFile)
  311. c.Assert(err, IsNil)
  312. c.Assert(eq, Equals, true)
  313. err = s.bucket.DeleteObject(objectName)
  314. c.Assert(err, IsNil)
  315. }
  316. // TestUploadRoutineWithRecoveryNegative is multiroutineed upload without checkpoint
  317. func (s *OssUploadSuite) TestUploadRoutineWithRecoveryNegative(c *C) {
  318. objectName := objectNamePrefix + randStr(8)
  319. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  320. // The local file does not exist
  321. err := s.bucket.UploadFile(objectName, "NotExist", 100*1024, Checkpoint(true, "NotExist.cp"))
  322. c.Assert(err, NotNil)
  323. err = s.bucket.UploadFile(objectName, "NotExist", 100*1024, Routines(2), Checkpoint(true, "NotExist.cp"))
  324. c.Assert(err, NotNil)
  325. // Specified part size is invalid
  326. err = s.bucket.UploadFile(objectName, fileName, 1024, Checkpoint(true, fileName+".cp"))
  327. c.Assert(err, NotNil)
  328. err = s.bucket.UploadFile(objectName, fileName, 1024, Routines(2), Checkpoint(true, fileName+".cp"))
  329. c.Assert(err, NotNil)
  330. err = s.bucket.UploadFile(objectName, fileName, 1024*1024*1024*100, Checkpoint(true, fileName+".cp"))
  331. c.Assert(err, NotNil)
  332. err = s.bucket.UploadFile(objectName, fileName, 1024*1024*1024*100, Routines(2), Checkpoint(true, fileName+".cp"))
  333. c.Assert(err, NotNil)
  334. }
  335. // TestUploadLocalFileChange tests the file is updated while being uploaded
  336. func (s *OssUploadSuite) TestUploadLocalFileChange(c *C) {
  337. objectName := objectNamePrefix + randStr(8)
  338. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  339. localFile := randStr(8) + ".jpg"
  340. newFile := randStr(8) + ".jpg"
  341. os.Remove(localFile)
  342. err := copyFile(fileName, localFile)
  343. c.Assert(err, IsNil)
  344. // First upload for 4 parts
  345. uploadPartHooker = ErrorHooker
  346. err = s.bucket.UploadFile(objectName, localFile, 100*1024, Checkpoint(true, localFile+".cp"))
  347. c.Assert(err, NotNil)
  348. c.Assert(err.Error(), Equals, "ErrorHooker")
  349. uploadPartHooker = defaultUploadPart
  350. os.Remove(localFile)
  351. err = copyFile(fileName, localFile)
  352. c.Assert(err, IsNil)
  353. // Updating the file. The second upload will re-upload all 5 parts.
  354. err = s.bucket.UploadFile(objectName, localFile, 100*1024, Checkpoint(true, localFile+".cp"))
  355. c.Assert(err, IsNil)
  356. os.Remove(newFile)
  357. err = s.bucket.GetObjectToFile(objectName, newFile)
  358. c.Assert(err, IsNil)
  359. eq, err := compareFiles(fileName, newFile)
  360. c.Assert(err, IsNil)
  361. c.Assert(eq, Equals, true)
  362. err = s.bucket.DeleteObject(objectName)
  363. c.Assert(err, IsNil)
  364. }
  365. func copyFile(src, dst string) error {
  366. srcFile, err := os.Open(src)
  367. if err != nil {
  368. return err
  369. }
  370. defer srcFile.Close()
  371. dstFile, err := os.Create(dst)
  372. if err != nil {
  373. return err
  374. }
  375. defer dstFile.Close()
  376. _, err = io.Copy(dstFile, srcFile)
  377. return err
  378. }
  379. func (s *OssUploadSuite) TestVersioningUploadRoutineWithRecovery(c *C) {
  380. // create a bucket with default proprety
  381. client, err := New(endpoint, accessID, accessKey)
  382. c.Assert(err, IsNil)
  383. bucketName := bucketNamePrefix + randLowStr(6)
  384. err = client.CreateBucket(bucketName)
  385. c.Assert(err, IsNil)
  386. bucket, err := client.Bucket(bucketName)
  387. // put bucket version:enabled
  388. var versioningConfig VersioningConfig
  389. versioningConfig.Status = string(VersionEnabled)
  390. err = client.SetBucketVersioning(bucketName, versioningConfig)
  391. c.Assert(err, IsNil)
  392. // begin test
  393. objectName := objectNamePrefix + randStr(8)
  394. fileName := "test-file-" + randStr(8)
  395. fileData := randStr(500 * 1024)
  396. createFile(fileName, fileData, c)
  397. newFile := "test-file-" + randStr(8)
  398. // Use default routines and default CP file path (fileName+.cp)Header
  399. // First upload for 4 parts
  400. var respHeader http.Header
  401. uploadPartHooker = ErrorHooker
  402. options := []Option{Checkpoint(true, fileName+".cp"), GetResponseHeader(&respHeader)}
  403. err = bucket.UploadFile(objectName, fileName, 100*1024, options...)
  404. c.Assert(err, NotNil)
  405. c.Assert(err.Error(), Equals, "ErrorHooker")
  406. c.Assert(GetVersionId(respHeader), Equals, "")
  407. uploadPartHooker = defaultUploadPart
  408. // Second upload, finish the remaining part
  409. options = []Option{Checkpoint(true, fileName+".cp"), GetResponseHeader(&respHeader)}
  410. err = bucket.UploadFile(objectName, fileName, 100*1024, options...)
  411. c.Assert(err, IsNil)
  412. versionIdUp := GetVersionId(respHeader)
  413. c.Assert(len(versionIdUp) > 0, Equals, true)
  414. os.Remove(newFile)
  415. var respHeaderDown http.Header
  416. err = bucket.GetObjectToFile(objectName, newFile, GetResponseHeader(&respHeaderDown))
  417. versionIdDown := GetVersionId(respHeaderDown)
  418. c.Assert(err, IsNil)
  419. c.Assert(versionIdUp, Equals, versionIdDown)
  420. eq, err := compareFiles(fileName, newFile)
  421. c.Assert(err, IsNil)
  422. c.Assert(eq, Equals, true)
  423. os.Remove(fileName)
  424. os.Remove(newFile)
  425. bucket.DeleteObject(objectName)
  426. forceDeleteBucket(client, bucketName, c)
  427. }