multipart_test.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. // multipart test
  2. package oss
  3. import (
  4. "math/rand"
  5. "net/http"
  6. "os"
  7. "strconv"
  8. . "gopkg.in/check.v1"
  9. )
  10. type OssBucketMultipartSuite struct {
  11. client *Client
  12. bucket *Bucket
  13. }
  14. var _ = Suite(&OssBucketMultipartSuite{})
  15. // SetUpSuite runs once when the suite starts running
  16. func (s *OssBucketMultipartSuite) 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. // Delete part
  25. keyMarker := KeyMarker("")
  26. uploadIDMarker := UploadIDMarker("")
  27. for {
  28. lmur, err := s.bucket.ListMultipartUploads(keyMarker, uploadIDMarker)
  29. c.Assert(err, IsNil)
  30. for _, upload := range lmur.Uploads {
  31. var imur = InitiateMultipartUploadResult{Bucket: s.bucket.BucketName,
  32. Key: upload.Key, UploadID: upload.UploadID}
  33. err = s.bucket.AbortMultipartUpload(imur)
  34. c.Assert(err, IsNil)
  35. }
  36. keyMarker = KeyMarker(lmur.NextKeyMarker)
  37. uploadIDMarker = UploadIDMarker(lmur.NextUploadIDMarker)
  38. if !lmur.IsTruncated {
  39. break
  40. }
  41. }
  42. // Delete objects
  43. marker := Marker("")
  44. for {
  45. lor, err := s.bucket.ListObjects(marker)
  46. c.Assert(err, IsNil)
  47. for _, object := range lor.Objects {
  48. err = s.bucket.DeleteObject(object.Key)
  49. c.Assert(err, IsNil)
  50. }
  51. marker = Marker(lor.NextMarker)
  52. if !lor.IsTruncated {
  53. break
  54. }
  55. }
  56. testLogger.Println("test multipart started")
  57. }
  58. // TearDownSuite runs before each test or benchmark starts running
  59. func (s *OssBucketMultipartSuite) TearDownSuite(c *C) {
  60. // Delete part
  61. keyMarker := KeyMarker("")
  62. uploadIDMarker := UploadIDMarker("")
  63. for {
  64. lmur, err := s.bucket.ListMultipartUploads(keyMarker, uploadIDMarker)
  65. c.Assert(err, IsNil)
  66. for _, upload := range lmur.Uploads {
  67. var imur = InitiateMultipartUploadResult{Bucket: s.bucket.BucketName,
  68. Key: upload.Key, UploadID: upload.UploadID}
  69. err = s.bucket.AbortMultipartUpload(imur)
  70. c.Assert(err, IsNil)
  71. }
  72. keyMarker = KeyMarker(lmur.NextKeyMarker)
  73. uploadIDMarker = UploadIDMarker(lmur.NextUploadIDMarker)
  74. if !lmur.IsTruncated {
  75. break
  76. }
  77. }
  78. // Delete objects
  79. marker := Marker("")
  80. for {
  81. lor, err := s.bucket.ListObjects(marker)
  82. c.Assert(err, IsNil)
  83. for _, object := range lor.Objects {
  84. err = s.bucket.DeleteObject(object.Key)
  85. c.Assert(err, IsNil)
  86. }
  87. marker = Marker(lor.NextMarker)
  88. if !lor.IsTruncated {
  89. break
  90. }
  91. }
  92. // Delete bucket
  93. err := s.client.DeleteBucket(s.bucket.BucketName)
  94. c.Assert(err, IsNil)
  95. testLogger.Println("test multipart completed")
  96. }
  97. // SetUpTest runs after each test or benchmark runs
  98. func (s *OssBucketMultipartSuite) SetUpTest(c *C) {
  99. err := removeTempFiles("../oss", ".jpg")
  100. c.Assert(err, IsNil)
  101. }
  102. // TearDownTest runs once after all tests or benchmarks have finished running
  103. func (s *OssBucketMultipartSuite) TearDownTest(c *C) {
  104. err := removeTempFiles("../oss", ".jpg")
  105. c.Assert(err, IsNil)
  106. err = removeTempFiles("../oss", ".temp")
  107. c.Assert(err, IsNil)
  108. err = removeTempFiles("../oss", ".txt1")
  109. c.Assert(err, IsNil)
  110. err = removeTempFiles("../oss", ".txt2")
  111. c.Assert(err, IsNil)
  112. }
  113. // TestMultipartUpload
  114. func (s *OssBucketMultipartSuite) TestMultipartUpload(c *C) {
  115. objectName := objectNamePrefix + RandStr(8)
  116. var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
  117. chunks, err := SplitFileByPartNum(fileName, 3)
  118. c.Assert(err, IsNil)
  119. testLogger.Println("chunks:", chunks)
  120. options := []Option{
  121. Expires(futureDate), Meta("my", "myprop"),
  122. }
  123. fd, err := os.Open(fileName)
  124. c.Assert(err, IsNil)
  125. defer fd.Close()
  126. imur, err := s.bucket.InitiateMultipartUpload(objectName, options...)
  127. c.Assert(err, IsNil)
  128. var parts []UploadPart
  129. for _, chunk := range chunks {
  130. fd.Seek(chunk.Offset, os.SEEK_SET)
  131. part, err := s.bucket.UploadPart(imur, fd, chunk.Size, chunk.Number)
  132. c.Assert(err, IsNil)
  133. parts = append(parts, part)
  134. }
  135. cmur, err := s.bucket.CompleteMultipartUpload(imur, parts)
  136. c.Assert(err, IsNil)
  137. testLogger.Println("cmur:", cmur)
  138. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  139. c.Assert(err, IsNil)
  140. testLogger.Println("GetObjectDetailedMeta:", meta)
  141. c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
  142. c.Assert(meta.Get("Expires"), Equals, futureDate.Format(http.TimeFormat))
  143. c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Multipart")
  144. err = s.bucket.GetObjectToFile(objectName, "newpic1.jpg")
  145. c.Assert(err, IsNil)
  146. err = s.bucket.DeleteObject(objectName)
  147. c.Assert(err, IsNil)
  148. }
  149. // TestMultipartUploadFromFile
  150. func (s *OssBucketMultipartSuite) TestMultipartUploadFromFile(c *C) {
  151. objectName := objectNamePrefix + RandStr(8)
  152. var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
  153. chunks, err := SplitFileByPartNum(fileName, 3)
  154. c.Assert(err, IsNil)
  155. testLogger.Println("chunks:", chunks)
  156. options := []Option{
  157. Expires(futureDate), Meta("my", "myprop"),
  158. }
  159. imur, err := s.bucket.InitiateMultipartUpload(objectName, options...)
  160. c.Assert(err, IsNil)
  161. var parts []UploadPart
  162. for _, chunk := range chunks {
  163. part, err := s.bucket.UploadPartFromFile(imur, fileName, chunk.Offset, chunk.Size, chunk.Number)
  164. c.Assert(err, IsNil)
  165. parts = append(parts, part)
  166. }
  167. cmur, err := s.bucket.CompleteMultipartUpload(imur, parts)
  168. c.Assert(err, IsNil)
  169. testLogger.Println("cmur:", cmur)
  170. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  171. c.Assert(err, IsNil)
  172. testLogger.Println("GetObjectDetailedMeta:", meta)
  173. c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
  174. c.Assert(meta.Get("Expires"), Equals, futureDate.Format(http.TimeFormat))
  175. c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Multipart")
  176. err = s.bucket.GetObjectToFile(objectName, "newpic1.jpg")
  177. c.Assert(err, IsNil)
  178. err = s.bucket.DeleteObject(objectName)
  179. c.Assert(err, IsNil)
  180. }
  181. // TestUploadPartCopy
  182. func (s *OssBucketMultipartSuite) TestUploadPartCopy(c *C) {
  183. objectSrc := objectNamePrefix + RandStr(8) + "-src"
  184. objectDest := objectNamePrefix + RandStr(8) + "-dest"
  185. var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
  186. chunks, err := SplitFileByPartNum(fileName, 3)
  187. c.Assert(err, IsNil)
  188. testLogger.Println("chunks:", chunks)
  189. err = s.bucket.PutObjectFromFile(objectSrc, fileName)
  190. c.Assert(err, IsNil)
  191. options := []Option{
  192. Expires(futureDate), Meta("my", "myprop"),
  193. }
  194. imur, err := s.bucket.InitiateMultipartUpload(objectDest, options...)
  195. c.Assert(err, IsNil)
  196. var parts []UploadPart
  197. for _, chunk := range chunks {
  198. part, err := s.bucket.UploadPartCopy(imur, bucketName, objectSrc, chunk.Offset, chunk.Size, (int)(chunk.Number))
  199. c.Assert(err, IsNil)
  200. parts = append(parts, part)
  201. }
  202. cmur, err := s.bucket.CompleteMultipartUpload(imur, parts)
  203. c.Assert(err, IsNil)
  204. testLogger.Println("cmur:", cmur)
  205. meta, err := s.bucket.GetObjectDetailedMeta(objectDest)
  206. c.Assert(err, IsNil)
  207. testLogger.Println("GetObjectDetailedMeta:", meta)
  208. c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
  209. c.Assert(meta.Get("Expires"), Equals, futureDate.Format(http.TimeFormat))
  210. c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Multipart")
  211. err = s.bucket.GetObjectToFile(objectDest, "newpic2.jpg")
  212. c.Assert(err, IsNil)
  213. err = s.bucket.DeleteObject(objectSrc)
  214. c.Assert(err, IsNil)
  215. err = s.bucket.DeleteObject(objectDest)
  216. c.Assert(err, IsNil)
  217. }
  218. func (s *OssBucketMultipartSuite) TestListUploadedParts(c *C) {
  219. objectName := objectNamePrefix + RandStr(8)
  220. objectSrc := objectName + "-src"
  221. objectDest := objectName + "-dest"
  222. var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
  223. chunks, err := SplitFileByPartSize(fileName, 100*1024)
  224. c.Assert(err, IsNil)
  225. testLogger.Println("chunks:", chunks)
  226. err = s.bucket.PutObjectFromFile(objectSrc, fileName)
  227. c.Assert(err, IsNil)
  228. // Upload
  229. imurUpload, err := s.bucket.InitiateMultipartUpload(objectName)
  230. var partsUpload []UploadPart
  231. for _, chunk := range chunks {
  232. part, err := s.bucket.UploadPartFromFile(imurUpload, fileName, chunk.Offset, chunk.Size, (int)(chunk.Number))
  233. c.Assert(err, IsNil)
  234. partsUpload = append(partsUpload, part)
  235. }
  236. // Copy
  237. imurCopy, err := s.bucket.InitiateMultipartUpload(objectDest)
  238. var partsCopy []UploadPart
  239. for _, chunk := range chunks {
  240. part, err := s.bucket.UploadPartCopy(imurCopy, bucketName, objectSrc, chunk.Offset, chunk.Size, (int)(chunk.Number))
  241. c.Assert(err, IsNil)
  242. partsCopy = append(partsCopy, part)
  243. }
  244. // List
  245. lupr, err := s.bucket.ListUploadedParts(imurUpload)
  246. c.Assert(err, IsNil)
  247. testLogger.Println("lupr:", lupr)
  248. c.Assert(len(lupr.UploadedParts), Equals, len(chunks))
  249. lupr, err = s.bucket.ListUploadedParts(imurCopy)
  250. c.Assert(err, IsNil)
  251. testLogger.Println("lupr:", lupr)
  252. c.Assert(len(lupr.UploadedParts), Equals, len(chunks))
  253. lmur, err := s.bucket.ListMultipartUploads()
  254. c.Assert(err, IsNil)
  255. testLogger.Println("lmur:", lmur)
  256. // Complete
  257. _, err = s.bucket.CompleteMultipartUpload(imurUpload, partsUpload)
  258. c.Assert(err, IsNil)
  259. _, err = s.bucket.CompleteMultipartUpload(imurCopy, partsCopy)
  260. c.Assert(err, IsNil)
  261. // Download
  262. err = s.bucket.GetObjectToFile(objectDest, "newpic3.jpg")
  263. c.Assert(err, IsNil)
  264. err = s.bucket.GetObjectToFile(objectName, "newpic4.jpg")
  265. c.Assert(err, IsNil)
  266. err = s.bucket.DeleteObject(objectName)
  267. c.Assert(err, IsNil)
  268. err = s.bucket.DeleteObject(objectDest)
  269. c.Assert(err, IsNil)
  270. err = s.bucket.DeleteObject(objectSrc)
  271. c.Assert(err, IsNil)
  272. }
  273. func (s *OssBucketMultipartSuite) TestAbortMultipartUpload(c *C) {
  274. objectName := objectNamePrefix + RandStr(8)
  275. objectSrc := objectName + "-src"
  276. objectDest := objectName + "-dest"
  277. var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
  278. chunks, err := SplitFileByPartSize(fileName, 100*1024)
  279. c.Assert(err, IsNil)
  280. testLogger.Println("chunks:", chunks)
  281. err = s.bucket.PutObjectFromFile(objectSrc, fileName)
  282. c.Assert(err, IsNil)
  283. // Upload
  284. imurUpload, err := s.bucket.InitiateMultipartUpload(objectName)
  285. var partsUpload []UploadPart
  286. for _, chunk := range chunks {
  287. part, err := s.bucket.UploadPartFromFile(imurUpload, fileName, chunk.Offset, chunk.Size, (int)(chunk.Number))
  288. c.Assert(err, IsNil)
  289. partsUpload = append(partsUpload, part)
  290. }
  291. // Copy
  292. imurCopy, err := s.bucket.InitiateMultipartUpload(objectDest)
  293. var partsCopy []UploadPart
  294. for _, chunk := range chunks {
  295. part, err := s.bucket.UploadPartCopy(imurCopy, bucketName, objectSrc, chunk.Offset, chunk.Size, (int)(chunk.Number))
  296. c.Assert(err, IsNil)
  297. partsCopy = append(partsCopy, part)
  298. }
  299. // List
  300. lupr, err := s.bucket.ListUploadedParts(imurUpload)
  301. c.Assert(err, IsNil)
  302. testLogger.Println("lupr:", lupr)
  303. c.Assert(len(lupr.UploadedParts), Equals, len(chunks))
  304. lupr, err = s.bucket.ListUploadedParts(imurCopy)
  305. c.Assert(err, IsNil)
  306. testLogger.Println("lupr:", lupr)
  307. c.Assert(len(lupr.UploadedParts), Equals, len(chunks))
  308. lmur, err := s.bucket.ListMultipartUploads()
  309. c.Assert(err, IsNil)
  310. testLogger.Println("lmur:", lmur)
  311. c.Assert(len(lmur.Uploads), Equals, 2)
  312. // Abort
  313. err = s.bucket.AbortMultipartUpload(imurUpload)
  314. c.Assert(err, IsNil)
  315. err = s.bucket.AbortMultipartUpload(imurCopy)
  316. c.Assert(err, IsNil)
  317. lmur, err = s.bucket.ListMultipartUploads()
  318. c.Assert(err, IsNil)
  319. testLogger.Println("lmur:", lmur)
  320. c.Assert(len(lmur.Uploads), Equals, 0)
  321. // Download
  322. err = s.bucket.GetObjectToFile(objectDest, "newpic3.jpg")
  323. c.Assert(err, NotNil)
  324. err = s.bucket.GetObjectToFile(objectName, "newpic4.jpg")
  325. c.Assert(err, NotNil)
  326. }
  327. // TestUploadPartCopyWithConstraints
  328. func (s *OssBucketMultipartSuite) TestUploadPartCopyWithConstraints(c *C) {
  329. objectSrc := objectNamePrefix + RandStr(8) + "-src"
  330. objectDest := objectNamePrefix + RandStr(8) + "-dest"
  331. var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
  332. chunks, err := SplitFileByPartNum(fileName, 3)
  333. c.Assert(err, IsNil)
  334. testLogger.Println("chunks:", chunks)
  335. err = s.bucket.PutObjectFromFile(objectSrc, fileName)
  336. c.Assert(err, IsNil)
  337. imur, err := s.bucket.InitiateMultipartUpload(objectDest)
  338. var parts []UploadPart
  339. for _, chunk := range chunks {
  340. _, err = s.bucket.UploadPartCopy(imur, bucketName, objectSrc, chunk.Offset, chunk.Size, (int)(chunk.Number),
  341. CopySourceIfModifiedSince(futureDate))
  342. c.Assert(err, NotNil)
  343. }
  344. for _, chunk := range chunks {
  345. _, err = s.bucket.UploadPartCopy(imur, bucketName, objectSrc, chunk.Offset, chunk.Size, (int)(chunk.Number),
  346. CopySourceIfUnmodifiedSince(futureDate))
  347. c.Assert(err, IsNil)
  348. }
  349. meta, err := s.bucket.GetObjectDetailedMeta(objectSrc)
  350. c.Assert(err, IsNil)
  351. testLogger.Println("GetObjectDetailedMeta:", meta)
  352. for _, chunk := range chunks {
  353. _, err = s.bucket.UploadPartCopy(imur, bucketName, objectSrc, chunk.Offset, chunk.Size, (int)(chunk.Number),
  354. CopySourceIfNoneMatch(meta.Get("Etag")))
  355. c.Assert(err, NotNil)
  356. }
  357. for _, chunk := range chunks {
  358. part, err := s.bucket.UploadPartCopy(imur, bucketName, objectSrc, chunk.Offset, chunk.Size, (int)(chunk.Number),
  359. CopySourceIfMatch(meta.Get("Etag")))
  360. c.Assert(err, IsNil)
  361. parts = append(parts, part)
  362. }
  363. cmur, err := s.bucket.CompleteMultipartUpload(imur, parts)
  364. c.Assert(err, IsNil)
  365. testLogger.Println("cmur:", cmur)
  366. err = s.bucket.GetObjectToFile(objectDest, "newpic5.jpg")
  367. c.Assert(err, IsNil)
  368. err = s.bucket.DeleteObject(objectSrc)
  369. c.Assert(err, IsNil)
  370. err = s.bucket.DeleteObject(objectDest)
  371. c.Assert(err, IsNil)
  372. }
  373. // TestMultipartUploadFromFileOutofOrder
  374. func (s *OssBucketMultipartSuite) TestMultipartUploadFromFileOutofOrder(c *C) {
  375. objectName := objectNamePrefix + RandStr(8)
  376. var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
  377. chunks, err := SplitFileByPartSize(fileName, 1024*100)
  378. shuffleArray(chunks)
  379. c.Assert(err, IsNil)
  380. testLogger.Println("chunks:", chunks)
  381. imur, err := s.bucket.InitiateMultipartUpload(objectName)
  382. var parts []UploadPart
  383. for _, chunk := range chunks {
  384. _, err := s.bucket.UploadPartFromFile(imur, fileName, chunk.Offset, chunk.Size, (int)(chunk.Number))
  385. c.Assert(err, IsNil)
  386. }
  387. // Double upload
  388. for _, chunk := range chunks {
  389. part, err := s.bucket.UploadPartFromFile(imur, fileName, chunk.Offset, chunk.Size, (int)(chunk.Number))
  390. c.Assert(err, IsNil)
  391. parts = append(parts, part)
  392. }
  393. cmur, err := s.bucket.CompleteMultipartUpload(imur, parts)
  394. c.Assert(err, IsNil)
  395. testLogger.Println("cmur:", cmur)
  396. err = s.bucket.GetObjectToFile(objectName, "newpic6.jpg")
  397. c.Assert(err, IsNil)
  398. err = s.bucket.DeleteObject(objectName)
  399. c.Assert(err, IsNil)
  400. }
  401. // TestUploadPartCopyOutofOrder
  402. func (s *OssBucketMultipartSuite) TestUploadPartCopyOutofOrder(c *C) {
  403. objectSrc := objectNamePrefix + RandStr(8) + "-src"
  404. objectDest := objectNamePrefix + RandStr(8) + "-dest"
  405. var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
  406. chunks, err := SplitFileByPartSize(fileName, 1024*100)
  407. shuffleArray(chunks)
  408. c.Assert(err, IsNil)
  409. testLogger.Println("chunks:", chunks)
  410. err = s.bucket.PutObjectFromFile(objectSrc, fileName)
  411. c.Assert(err, IsNil)
  412. imur, err := s.bucket.InitiateMultipartUpload(objectDest)
  413. var parts []UploadPart
  414. for _, chunk := range chunks {
  415. _, err := s.bucket.UploadPartCopy(imur, bucketName, objectSrc, chunk.Offset, chunk.Size, (int)(chunk.Number))
  416. c.Assert(err, IsNil)
  417. }
  418. // Double copy
  419. for _, chunk := range chunks {
  420. part, err := s.bucket.UploadPartCopy(imur, bucketName, objectSrc, chunk.Offset, chunk.Size, (int)(chunk.Number))
  421. c.Assert(err, IsNil)
  422. parts = append(parts, part)
  423. }
  424. cmur, err := s.bucket.CompleteMultipartUpload(imur, parts)
  425. c.Assert(err, IsNil)
  426. testLogger.Println("cmur:", cmur)
  427. err = s.bucket.GetObjectToFile(objectDest, "newpic7.jpg")
  428. c.Assert(err, IsNil)
  429. err = s.bucket.DeleteObject(objectSrc)
  430. c.Assert(err, IsNil)
  431. err = s.bucket.DeleteObject(objectDest)
  432. c.Assert(err, IsNil)
  433. }
  434. // TestMultipartUploadFromFileType
  435. func (s *OssBucketMultipartSuite) TestMultipartUploadFromFileType(c *C) {
  436. objectName := objectNamePrefix + RandStr(8) + ".jpg"
  437. var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
  438. chunks, err := SplitFileByPartNum(fileName, 4)
  439. c.Assert(err, IsNil)
  440. testLogger.Println("chunks:", chunks)
  441. imur, err := s.bucket.InitiateMultipartUpload(objectName)
  442. var parts []UploadPart
  443. for _, chunk := range chunks {
  444. part, err := s.bucket.UploadPartFromFile(imur, fileName, chunk.Offset, chunk.Size, chunk.Number)
  445. c.Assert(err, IsNil)
  446. parts = append(parts, part)
  447. }
  448. testLogger.Println("parts:", parts)
  449. cmur, err := s.bucket.CompleteMultipartUpload(imur, parts)
  450. c.Assert(err, IsNil)
  451. testLogger.Println("cmur:", cmur)
  452. err = s.bucket.GetObjectToFile(objectName, "newpic8.jpg")
  453. c.Assert(err, IsNil)
  454. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  455. c.Assert(err, IsNil)
  456. c.Assert(meta.Get("Content-Type"), Equals, "image/jpeg")
  457. err = s.bucket.DeleteObject(objectName)
  458. c.Assert(err, IsNil)
  459. }
  460. func (s *OssBucketMultipartSuite) TestListMultipartUploads(c *C) {
  461. objectName := objectNamePrefix + RandStr(8)
  462. imurs := []InitiateMultipartUploadResult{}
  463. for i := 0; i < 20; i++ {
  464. imur, err := s.bucket.InitiateMultipartUpload(objectName + strconv.Itoa(i))
  465. c.Assert(err, IsNil)
  466. imurs = append(imurs, imur)
  467. }
  468. lmpu, err := s.bucket.ListMultipartUploads()
  469. c.Assert(err, IsNil)
  470. c.Assert(len(lmpu.Uploads), Equals, 20)
  471. lmpu, err = s.bucket.ListMultipartUploads(MaxUploads(3))
  472. c.Assert(err, IsNil)
  473. c.Assert(len(lmpu.Uploads), Equals, 3)
  474. lmpu, err = s.bucket.ListMultipartUploads(Prefix(objectName))
  475. c.Assert(err, IsNil)
  476. c.Assert(len(lmpu.Uploads), Equals, 20)
  477. lmpu, err = s.bucket.ListMultipartUploads(Prefix(objectName + "1"))
  478. c.Assert(err, IsNil)
  479. c.Assert(len(lmpu.Uploads), Equals, 11)
  480. lmpu, err = s.bucket.ListMultipartUploads(Prefix(objectName + "22"))
  481. c.Assert(err, IsNil)
  482. c.Assert(len(lmpu.Uploads), Equals, 0)
  483. lmpu, err = s.bucket.ListMultipartUploads(KeyMarker(objectName + "10"))
  484. c.Assert(err, IsNil)
  485. c.Assert(len(lmpu.Uploads), Equals, 17)
  486. lmpu, err = s.bucket.ListMultipartUploads(KeyMarker(objectName+"10"), MaxUploads(3))
  487. c.Assert(err, IsNil)
  488. c.Assert(len(lmpu.Uploads), Equals, 3)
  489. lmpu, err = s.bucket.ListMultipartUploads(Prefix(objectName), Delimiter("4"))
  490. c.Assert(err, IsNil)
  491. c.Assert(len(lmpu.Uploads), Equals, 18)
  492. c.Assert(len(lmpu.CommonPrefixes), Equals, 2)
  493. upLoadIDStr := RandStr(3)
  494. lmpu, err = s.bucket.ListMultipartUploads(KeyMarker(objectName+"12"), UploadIDMarker(upLoadIDStr))
  495. c.Assert(err, IsNil)
  496. checkNum := 15
  497. for _, im := range imurs {
  498. if im.Key == objectName+"12" && im.UploadID > upLoadIDStr {
  499. checkNum = 16
  500. break
  501. }
  502. }
  503. c.Assert(len(lmpu.Uploads), Equals, checkNum)
  504. //testLogger.Println("UploadIDMarker", lmpu.Uploads)
  505. for _, imur := range imurs {
  506. err = s.bucket.AbortMultipartUpload(imur)
  507. c.Assert(err, IsNil)
  508. }
  509. }
  510. func (s *OssBucketMultipartSuite) TestListMultipartUploadsEncodingKey(c *C) {
  511. prefix := objectNamePrefix + "让你任性让你狂" + RandStr(8)
  512. imurs := []InitiateMultipartUploadResult{}
  513. for i := 0; i < 3; i++ {
  514. imur, err := s.bucket.InitiateMultipartUpload(prefix + strconv.Itoa(i))
  515. c.Assert(err, IsNil)
  516. imurs = append(imurs, imur)
  517. }
  518. lmpu, err := s.bucket.ListMultipartUploads()
  519. c.Assert(err, IsNil)
  520. c.Assert(len(lmpu.Uploads), Equals, 3)
  521. lmpu, err = s.bucket.ListMultipartUploads(Prefix(prefix + "1"))
  522. c.Assert(err, IsNil)
  523. c.Assert(len(lmpu.Uploads), Equals, 1)
  524. lmpu, err = s.bucket.ListMultipartUploads(KeyMarker(prefix + "1"))
  525. c.Assert(err, IsNil)
  526. c.Assert(len(lmpu.Uploads), Equals, 1)
  527. lmpu, err = s.bucket.ListMultipartUploads(EncodingType("url"))
  528. c.Assert(err, IsNil)
  529. for i, upload := range lmpu.Uploads {
  530. c.Assert(upload.Key, Equals, prefix+strconv.Itoa(i))
  531. }
  532. for _, imur := range imurs {
  533. err = s.bucket.AbortMultipartUpload(imur)
  534. c.Assert(err, IsNil)
  535. }
  536. }
  537. func (s *OssBucketMultipartSuite) TestMultipartNegative(c *C) {
  538. objectName := objectNamePrefix + RandStr(8)
  539. // Key tool long
  540. data := make([]byte, 100*1024)
  541. imur, err := s.bucket.InitiateMultipartUpload(string(data))
  542. c.Assert(err, NotNil)
  543. // Invalid imur
  544. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  545. fd, err := os.Open(fileName)
  546. c.Assert(err, IsNil)
  547. defer fd.Close()
  548. _, err = s.bucket.UploadPart(imur, fd, 1024, 1)
  549. c.Assert(err, NotNil)
  550. _, err = s.bucket.UploadPartFromFile(imur, fileName, 0, 1024, 1)
  551. c.Assert(err, NotNil)
  552. _, err = s.bucket.UploadPartCopy(imur, bucketName, fileName, 0, 1024, 1)
  553. c.Assert(err, NotNil)
  554. err = s.bucket.AbortMultipartUpload(imur)
  555. c.Assert(err, NotNil)
  556. _, err = s.bucket.ListUploadedParts(imur)
  557. c.Assert(err, NotNil)
  558. // Invalid exist
  559. imur, err = s.bucket.InitiateMultipartUpload(objectName)
  560. c.Assert(err, IsNil)
  561. _, err = s.bucket.UploadPart(imur, fd, 1024, 1)
  562. c.Assert(err, IsNil)
  563. _, err = s.bucket.UploadPart(imur, fd, 102400, 10001)
  564. c.Assert(err, NotNil)
  565. // _, err = s.bucket.UploadPartFromFile(imur, fileName, 0, 1024, 1)
  566. // c.Assert(err, IsNil)
  567. _, err = s.bucket.UploadPartFromFile(imur, fileName, 0, 102400, 10001)
  568. c.Assert(err, NotNil)
  569. _, err = s.bucket.UploadPartCopy(imur, bucketName, fileName, 0, 1024, 1)
  570. c.Assert(err, NotNil)
  571. _, err = s.bucket.UploadPartCopy(imur, bucketName, fileName, 0, 1024, 1000)
  572. c.Assert(err, NotNil)
  573. err = s.bucket.AbortMultipartUpload(imur)
  574. c.Assert(err, IsNil)
  575. // Invalid option
  576. _, err = s.bucket.InitiateMultipartUpload(objectName, IfModifiedSince(futureDate))
  577. c.Assert(err, IsNil)
  578. }
  579. func (s *OssBucketMultipartSuite) TestMultipartUploadFromFileBigFile(c *C) {
  580. objectName := objectNamePrefix + RandStr(8)
  581. bigFile := "D:\\tmp\\bigfile.zip"
  582. newFile := "D:\\tmp\\newbigfile.zip"
  583. exist, err := isFileExist(bigFile)
  584. c.Assert(err, IsNil)
  585. if !exist {
  586. return
  587. }
  588. chunks, err := SplitFileByPartNum(bigFile, 64)
  589. c.Assert(err, IsNil)
  590. testLogger.Println("chunks:", chunks)
  591. imur, err := s.bucket.InitiateMultipartUpload(objectName)
  592. var parts []UploadPart
  593. start := GetNowSec()
  594. for _, chunk := range chunks {
  595. part, err := s.bucket.UploadPartFromFile(imur, bigFile, chunk.Offset, chunk.Size, (int)(chunk.Number))
  596. c.Assert(err, IsNil)
  597. parts = append(parts, part)
  598. }
  599. end := GetNowSec()
  600. testLogger.Println("Uplaod big file:", bigFile, "use sec:", end-start)
  601. testLogger.Println("parts:", parts)
  602. _, err = s.bucket.CompleteMultipartUpload(imur, parts)
  603. c.Assert(err, IsNil)
  604. start = GetNowSec()
  605. err = s.bucket.GetObjectToFile(objectName, newFile)
  606. c.Assert(err, IsNil)
  607. end = GetNowSec()
  608. testLogger.Println("Download big file:", bigFile, "use sec:", end-start)
  609. start = GetNowSec()
  610. eq, err := compareFiles(bigFile, newFile)
  611. c.Assert(err, IsNil)
  612. c.Assert(eq, Equals, true)
  613. end = GetNowSec()
  614. testLogger.Println("Compare big file:", bigFile, "use sec:", end-start)
  615. err = s.bucket.DeleteObject(objectName)
  616. c.Assert(err, IsNil)
  617. }
  618. // TestUploadFile
  619. func (s *OssBucketMultipartSuite) TestUploadFile(c *C) {
  620. objectName := objectNamePrefix + RandStr(8)
  621. var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
  622. newFile := RandStr(8) + ".jpg"
  623. // Upload with 100K part size
  624. err := s.bucket.UploadFile(objectName, fileName, 100*1024)
  625. c.Assert(err, IsNil)
  626. os.Remove(newFile)
  627. err = s.bucket.GetObjectToFile(objectName, newFile)
  628. c.Assert(err, IsNil)
  629. eq, err := compareFiles(fileName, newFile)
  630. c.Assert(err, IsNil)
  631. c.Assert(eq, Equals, true)
  632. err = s.bucket.DeleteObject(objectName)
  633. c.Assert(err, IsNil)
  634. // Upload with part size equals to 1/4 of the file size
  635. err = s.bucket.UploadFile(objectName, fileName, 482048/4)
  636. c.Assert(err, IsNil)
  637. os.Remove(newFile)
  638. err = s.bucket.GetObjectToFile(objectName, newFile)
  639. c.Assert(err, IsNil)
  640. eq, err = compareFiles(fileName, newFile)
  641. c.Assert(err, IsNil)
  642. c.Assert(eq, Equals, true)
  643. err = s.bucket.DeleteObject(objectName)
  644. c.Assert(err, IsNil)
  645. // Upload with part size equals to the file size
  646. err = s.bucket.UploadFile(objectName, fileName, 482048)
  647. c.Assert(err, IsNil)
  648. os.Remove(newFile)
  649. err = s.bucket.GetObjectToFile(objectName, newFile)
  650. c.Assert(err, IsNil)
  651. eq, err = compareFiles(fileName, newFile)
  652. c.Assert(err, IsNil)
  653. c.Assert(eq, Equals, true)
  654. err = s.bucket.DeleteObject(objectName)
  655. c.Assert(err, IsNil)
  656. // Upload with part size is bigger than the file size
  657. err = s.bucket.UploadFile(objectName, fileName, 482049)
  658. c.Assert(err, IsNil)
  659. os.Remove(newFile)
  660. err = s.bucket.GetObjectToFile(objectName, newFile)
  661. c.Assert(err, IsNil)
  662. eq, err = compareFiles(fileName, newFile)
  663. c.Assert(err, IsNil)
  664. c.Assert(eq, Equals, true)
  665. err = s.bucket.DeleteObject(objectName)
  666. c.Assert(err, IsNil)
  667. // Option
  668. options := []Option{
  669. Expires(futureDate),
  670. ObjectACL(ACLPublicRead),
  671. Meta("myprop", "mypropval")}
  672. err = s.bucket.UploadFile(objectName, fileName, 482049, options...)
  673. c.Assert(err, IsNil)
  674. // Check
  675. os.Remove(newFile)
  676. err = s.bucket.GetObjectToFile(objectName, newFile)
  677. c.Assert(err, IsNil)
  678. eq, err = compareFiles(fileName, newFile)
  679. c.Assert(err, IsNil)
  680. c.Assert(eq, Equals, true)
  681. acl, err := s.bucket.GetObjectACL(objectName)
  682. c.Assert(err, IsNil)
  683. testLogger.Println("GetObjectAcl:", acl)
  684. c.Assert(acl.ACL, Equals, "public-read")
  685. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  686. c.Assert(err, IsNil)
  687. testLogger.Println("GetObjectDetailedMeta:", meta)
  688. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  689. }
  690. func (s *OssBucketMultipartSuite) TestUploadFileNegative(c *C) {
  691. objectName := objectNamePrefix + RandStr(8)
  692. var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
  693. // Smaller than the required minimal part size (100KB)
  694. err := s.bucket.UploadFile(objectName, fileName, 100*1024-1)
  695. c.Assert(err, NotNil)
  696. // Bigger than the max part size (5G)
  697. err = s.bucket.UploadFile(objectName, fileName, 1024*1024*1024*5+1)
  698. c.Assert(err, NotNil)
  699. // File does not exist
  700. err = s.bucket.UploadFile(objectName, "/root1/123abc9874", 1024*1024*1024)
  701. c.Assert(err, NotNil)
  702. // Invalid key , key is empty.
  703. err = s.bucket.UploadFile("", fileName, 100*1024)
  704. c.Assert(err, NotNil)
  705. }
  706. // TestDownloadFile
  707. func (s *OssBucketMultipartSuite) TestDownloadFile(c *C) {
  708. objectName := objectNamePrefix + RandStr(8)
  709. var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
  710. newFile := RandStr(8) + ".jpg"
  711. err := s.bucket.UploadFile(objectName, fileName, 100*1024)
  712. c.Assert(err, IsNil)
  713. // Download file with part size of 100K
  714. err = s.bucket.DownloadFile(objectName, newFile, 100*1024)
  715. c.Assert(err, IsNil)
  716. os.Remove(newFile)
  717. err = s.bucket.GetObjectToFile(objectName, newFile)
  718. c.Assert(err, IsNil)
  719. eq, err := compareFiles(fileName, newFile)
  720. c.Assert(err, IsNil)
  721. c.Assert(eq, Equals, true)
  722. // Download the file with part size equals to 1/4 of the file size
  723. err = s.bucket.DownloadFile(objectName, newFile, 482048/4)
  724. c.Assert(err, IsNil)
  725. os.Remove(newFile)
  726. err = s.bucket.GetObjectToFile(objectName, newFile)
  727. c.Assert(err, IsNil)
  728. eq, err = compareFiles(fileName, newFile)
  729. c.Assert(err, IsNil)
  730. c.Assert(eq, Equals, true)
  731. // Download the file with part size same as the file size
  732. err = s.bucket.DownloadFile(objectName, newFile, 482048)
  733. c.Assert(err, IsNil)
  734. os.Remove(newFile)
  735. err = s.bucket.GetObjectToFile(objectName, newFile)
  736. c.Assert(err, IsNil)
  737. eq, err = compareFiles(fileName, newFile)
  738. c.Assert(err, IsNil)
  739. c.Assert(eq, Equals, true)
  740. // Download the file with part size bigger than the file size
  741. err = s.bucket.DownloadFile(objectName, newFile, 482049)
  742. c.Assert(err, IsNil)
  743. os.Remove(newFile)
  744. err = s.bucket.GetObjectToFile(objectName, newFile)
  745. c.Assert(err, IsNil)
  746. eq, err = compareFiles(fileName, newFile)
  747. c.Assert(err, IsNil)
  748. c.Assert(eq, Equals, true)
  749. // Option
  750. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  751. c.Assert(err, IsNil)
  752. testLogger.Println("GetObjectDetailedMeta:", meta)
  753. // If-Match
  754. err = s.bucket.DownloadFile(objectName, newFile, 482048/4, IfMatch(meta.Get("Etag")))
  755. c.Assert(err, IsNil)
  756. os.Remove(newFile)
  757. err = s.bucket.GetObjectToFile(objectName, newFile)
  758. c.Assert(err, IsNil)
  759. eq, err = compareFiles(fileName, newFile)
  760. c.Assert(err, IsNil)
  761. c.Assert(eq, Equals, true)
  762. // If-None-Match
  763. err = s.bucket.DownloadFile(objectName, newFile, 482048, IfNoneMatch(meta.Get("Etag")))
  764. c.Assert(err, NotNil)
  765. os.Remove(newFile)
  766. err = s.bucket.DeleteObject(objectName)
  767. c.Assert(err, IsNil)
  768. }
  769. func (s *OssBucketMultipartSuite) TestDownloadFileNegative(c *C) {
  770. objectName := objectNamePrefix + RandStr(8)
  771. newFile := RandStr(8) + ".jpg"
  772. // Smaller than the required minimal part size (100KB)
  773. err := s.bucket.DownloadFile(objectName, newFile, 100*1024-1)
  774. c.Assert(err, NotNil)
  775. // Bigger than the required max part size (5G)
  776. err = s.bucket.DownloadFile(objectName, newFile, 1024*1024*1024+1)
  777. c.Assert(err, NotNil)
  778. // File does not exist
  779. err = s.bucket.DownloadFile(objectName, "/OSS/TEMP/ZIBI/QUQU/BALA", 1024*1024*1024+1)
  780. c.Assert(err, NotNil)
  781. // Key does not exist
  782. err = s.bucket.DownloadFile(objectName, newFile, 100*1024)
  783. c.Assert(err, NotNil)
  784. }
  785. // Private
  786. func shuffleArray(chunks []FileChunk) []FileChunk {
  787. for i := range chunks {
  788. j := rand.Intn(i + 1)
  789. chunks[i], chunks[j] = chunks[j], chunks[i]
  790. }
  791. return chunks
  792. }