upload_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. // TestUploadPartArchiveObject
  366. func (s *OssUploadSuite) TestUploadPartArchiveObject(c *C) {
  367. // create archive bucket
  368. client, err := New(endpoint, accessID, accessKey)
  369. c.Assert(err, IsNil)
  370. bucketName := bucketNamePrefix + RandLowStr(6)
  371. err = client.CreateBucket(bucketName, StorageClass(StorageArchive))
  372. c.Assert(err, IsNil)
  373. bucket, err := client.Bucket(bucketName)
  374. objectName := objectNamePrefix + RandStr(8)
  375. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  376. fileInfo, err := os.Stat(fileName)
  377. c.Assert(err, IsNil)
  378. // Updating the file,archive object
  379. err = bucket.UploadFile(objectName, fileName, fileInfo.Size()/2, ObjectStorageClass(StorageArchive))
  380. c.Assert(err, IsNil)
  381. // Updating the file,archive object,checkpoint
  382. err = bucket.UploadFile(objectName, fileName, fileInfo.Size()/2, ObjectStorageClass(StorageArchive), Checkpoint(true, fileName+".cp"))
  383. c.Assert(err, IsNil)
  384. ForceDeleteBucket(client, bucketName, c)
  385. }
  386. func copyFile(src, dst string) error {
  387. srcFile, err := os.Open(src)
  388. if err != nil {
  389. return err
  390. }
  391. defer srcFile.Close()
  392. dstFile, err := os.Create(dst)
  393. if err != nil {
  394. return err
  395. }
  396. defer dstFile.Close()
  397. _, err = io.Copy(dstFile, srcFile)
  398. return err
  399. }
  400. func (s *OssUploadSuite) TestVersioningUploadRoutineWithRecovery(c *C) {
  401. // create a bucket with default proprety
  402. client, err := New(endpoint, accessID, accessKey)
  403. c.Assert(err, IsNil)
  404. bucketName := bucketNamePrefix + RandLowStr(6)
  405. err = client.CreateBucket(bucketName)
  406. c.Assert(err, IsNil)
  407. bucket, err := client.Bucket(bucketName)
  408. // put bucket version:enabled
  409. var versioningConfig VersioningConfig
  410. versioningConfig.Status = string(VersionEnabled)
  411. err = client.SetBucketVersioning(bucketName, versioningConfig)
  412. c.Assert(err, IsNil)
  413. // begin test
  414. objectName := objectNamePrefix + RandStr(8)
  415. fileName := "test-file-" + RandStr(8)
  416. fileData := RandStr(500 * 1024)
  417. CreateFile(fileName, fileData, c)
  418. newFile := "test-file-" + RandStr(8)
  419. // Use default routines and default CP file path (fileName+.cp)Header
  420. // First upload for 4 parts
  421. var respHeader http.Header
  422. uploadPartHooker = ErrorHooker
  423. options := []Option{Checkpoint(true, fileName+".cp"), GetResponseHeader(&respHeader)}
  424. err = bucket.UploadFile(objectName, fileName, 100*1024, options...)
  425. c.Assert(err, NotNil)
  426. c.Assert(err.Error(), Equals, "ErrorHooker")
  427. c.Assert(GetVersionId(respHeader), Equals, "")
  428. uploadPartHooker = defaultUploadPart
  429. // Second upload, finish the remaining part
  430. options = []Option{Checkpoint(true, fileName+".cp"), GetResponseHeader(&respHeader)}
  431. err = bucket.UploadFile(objectName, fileName, 100*1024, options...)
  432. c.Assert(err, IsNil)
  433. versionIdUp := GetVersionId(respHeader)
  434. c.Assert(len(versionIdUp) > 0, Equals, true)
  435. os.Remove(newFile)
  436. var respHeaderDown http.Header
  437. err = bucket.GetObjectToFile(objectName, newFile, GetResponseHeader(&respHeaderDown))
  438. versionIdDown := GetVersionId(respHeaderDown)
  439. c.Assert(err, IsNil)
  440. c.Assert(versionIdUp, Equals, versionIdDown)
  441. eq, err := compareFiles(fileName, newFile)
  442. c.Assert(err, IsNil)
  443. c.Assert(eq, Equals, true)
  444. os.Remove(fileName)
  445. os.Remove(newFile)
  446. bucket.DeleteObject(objectName)
  447. ForceDeleteBucket(client, bucketName, c)
  448. }
  449. // TestUploadFileChoiceOptions
  450. func (s *OssUploadSuite) TestUploadFileChoiceOptions(c *C) {
  451. // create a bucket with default proprety
  452. client, err := New(endpoint, accessID, accessKey)
  453. c.Assert(err, IsNil)
  454. bucketName := bucketNamePrefix + RandLowStr(6)
  455. err = client.CreateBucket(bucketName)
  456. c.Assert(err, IsNil)
  457. bucket, err := client.Bucket(bucketName)
  458. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  459. fileInfo, err := os.Stat(fileName)
  460. c.Assert(err, IsNil)
  461. objectName := objectNamePrefix + RandStr(8)
  462. // UploadFile with properties
  463. options := []Option{
  464. ObjectACL(ACLPublicRead),
  465. RequestPayer(Requester),
  466. TrafficLimitHeader(1024 * 1024 * 8),
  467. ServerSideEncryption("AES256"),
  468. ObjectStorageClass(StorageArchive),
  469. }
  470. // Updating the file
  471. err = bucket.UploadFile(objectName, fileName, fileInfo.Size()/2, options...)
  472. c.Assert(err, IsNil)
  473. // GetMetaDetail
  474. headerResp, err := bucket.GetObjectDetailedMeta(objectName)
  475. c.Assert(err, IsNil)
  476. c.Assert(headerResp.Get("X-Oss-Server-Side-Encryption"), Equals, "AES256")
  477. aclResult, err := bucket.GetObjectACL(objectName)
  478. c.Assert(aclResult.ACL, Equals, "public-read")
  479. c.Assert(err, IsNil)
  480. ForceDeleteBucket(client, bucketName, c)
  481. }
  482. // TestUploadFileWithCpChoiceOptions
  483. func (s *OssUploadSuite) TestUploadFileWithCpChoiceOptions(c *C) {
  484. // create a bucket with default proprety
  485. client, err := New(endpoint, accessID, accessKey)
  486. c.Assert(err, IsNil)
  487. bucketName := bucketNamePrefix + RandLowStr(6)
  488. err = client.CreateBucket(bucketName)
  489. c.Assert(err, IsNil)
  490. bucket, err := client.Bucket(bucketName)
  491. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  492. fileInfo, err := os.Stat(fileName)
  493. c.Assert(err, IsNil)
  494. objectName := objectNamePrefix + RandStr(8)
  495. // UploadFile with properties
  496. options := []Option{
  497. ObjectACL(ACLPublicRead),
  498. RequestPayer(Requester),
  499. TrafficLimitHeader(1024 * 1024 * 8),
  500. ServerSideEncryption("AES256"),
  501. ObjectStorageClass(StorageArchive),
  502. Checkpoint(true, fileName+".cp"), // with checkpoint
  503. }
  504. // Updating the file
  505. err = bucket.UploadFile(objectName, fileName, fileInfo.Size()/2, options...)
  506. c.Assert(err, IsNil)
  507. // GetMetaDetail
  508. headerResp, err := bucket.GetObjectDetailedMeta(objectName)
  509. c.Assert(err, IsNil)
  510. c.Assert(headerResp.Get("X-Oss-Server-Side-Encryption"), Equals, "AES256")
  511. c.Assert(headerResp.Get("X-Oss-Storage-Class"), Equals, "Archive")
  512. aclResult, err := bucket.GetObjectACL(objectName)
  513. c.Assert(aclResult.ACL, Equals, "public-read")
  514. c.Assert(err, IsNil)
  515. ForceDeleteBucket(client, bucketName, c)
  516. }
  517. // TestUploadFileWithForbidOverWrite
  518. func (s *OssUploadSuite) TestUploadFileWithForbidOverWrite(c *C) {
  519. // create a bucket with default proprety
  520. client, err := New(endpoint, accessID, accessKey)
  521. c.Assert(err, IsNil)
  522. bucketName := bucketNamePrefix + RandLowStr(6)
  523. err = client.CreateBucket(bucketName)
  524. c.Assert(err, IsNil)
  525. bucket, err := client.Bucket(bucketName)
  526. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  527. fileInfo, err := os.Stat(fileName)
  528. c.Assert(err, IsNil)
  529. objectName := objectNamePrefix + RandStr(8)
  530. // UploadFile with properties
  531. options := []Option{
  532. ObjectACL(ACLPublicRead),
  533. RequestPayer(Requester),
  534. TrafficLimitHeader(1024 * 1024 * 8),
  535. ServerSideEncryption("AES256"),
  536. ObjectStorageClass(StorageArchive),
  537. ForbidOverWrite(true),
  538. Checkpoint(true, fileName+".cp"),
  539. }
  540. // Updating the file
  541. err = bucket.UploadFile(objectName, fileName, fileInfo.Size()/2, options...)
  542. c.Assert(err, IsNil)
  543. // Updating the file with ForbidOverWrite(true)
  544. err = bucket.UploadFile(objectName, fileName, fileInfo.Size()/2, options...)
  545. c.Assert(err, NotNil)
  546. // without Checkpoint
  547. options = []Option{
  548. ObjectACL(ACLPublicRead),
  549. RequestPayer(Requester),
  550. TrafficLimitHeader(1024 * 1024 * 8),
  551. ServerSideEncryption("AES256"),
  552. ObjectStorageClass(StorageArchive),
  553. ForbidOverWrite(true),
  554. }
  555. // Updating the file with ForbidOverWrite(true)
  556. err = bucket.UploadFile(objectName, fileName, fileInfo.Size()/2, options...)
  557. c.Assert(err, NotNil)
  558. ForceDeleteBucket(client, bucketName, c)
  559. }
  560. // TestUploadFileWithSequential
  561. func (s *OssUploadSuite) TestUploadFileWithSequential(c *C) {
  562. // create a bucket with default proprety
  563. client, err := New(endpoint, accessID, accessKey)
  564. c.Assert(err, IsNil)
  565. bucketName := bucketNamePrefix + RandLowStr(6)
  566. err = client.CreateBucket(bucketName)
  567. c.Assert(err, IsNil)
  568. bucket, err := client.Bucket(bucketName)
  569. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  570. fileInfo, err := os.Stat(fileName)
  571. c.Assert(err, IsNil)
  572. objectName := objectNamePrefix + RandStr(8)
  573. var respHeader http.Header
  574. // UploadFile with properties
  575. options := []Option{
  576. Sequential(),
  577. GetResponseHeader(&respHeader),
  578. Checkpoint(true, fileName+".cp"),
  579. }
  580. // Updating the file
  581. err = bucket.UploadFile(objectName, fileName, fileInfo.Size()/2, options...)
  582. c.Assert(err, IsNil)
  583. respHeader, err = bucket.GetObjectDetailedMeta(objectName)
  584. c.Assert(err, IsNil)
  585. strMD5 := respHeader.Get("Content-MD5")
  586. c.Assert(len(strMD5) > 0, Equals, true)
  587. ForceDeleteBucket(client, bucketName, c)
  588. }