multipart_test.go 27 KB

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