crypto_multipart_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. // multipart test
  2. package osscrypto
  3. import (
  4. "io/ioutil"
  5. "os"
  6. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  7. . "gopkg.in/check.v1"
  8. )
  9. func (s *OssCryptoBucketSuite) TestMultipartUpload(c *C) {
  10. // create a bucket with default proprety
  11. client, err := oss.New(endpoint, accessID, accessKey)
  12. c.Assert(err, IsNil)
  13. bucketName := bucketNamePrefix + RandLowStr(6)
  14. err = client.CreateBucket(bucketName)
  15. c.Assert(err, IsNil)
  16. // crypto bucket
  17. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  18. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  19. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  20. objectName := objectNamePrefix + RandStr(8)
  21. fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
  22. srcMD5, err := GetFileMD5(fileName)
  23. c.Assert(err, IsNil)
  24. fileInfo, err := os.Stat(fileName)
  25. dataSize := fileInfo.Size()
  26. c.Assert(err, IsNil)
  27. options := []oss.Option{oss.Meta("my", "myprop")}
  28. var cryptoContext PartCryptoContext
  29. cryptoContext.DataSize = dataSize
  30. cryptoContext.PartSize = (dataSize / 16 / 3) * 16
  31. imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext, options...)
  32. c.Assert(err, IsNil)
  33. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  34. c.Assert(err, IsNil)
  35. fd, err := os.Open(fileName)
  36. c.Assert(err, IsNil)
  37. defer fd.Close()
  38. var parts []oss.UploadPart
  39. for _, chunk := range chunks {
  40. fd.Seek(chunk.Offset, os.SEEK_SET)
  41. part, err := bucket.UploadPart(imur, fd, chunk.Size, chunk.Number, cryptoContext)
  42. c.Assert(err, IsNil)
  43. parts = append(parts, part)
  44. }
  45. _, err = bucket.CompleteMultipartUpload(imur, parts)
  46. c.Assert(err, IsNil)
  47. meta, err := bucket.GetObjectDetailedMeta(objectName)
  48. c.Assert(err, IsNil)
  49. c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
  50. c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Multipart")
  51. downfileName := "test-go-sdk-file-" + RandLowStr(5)
  52. err = bucket.GetObjectToFile(objectName, downfileName)
  53. c.Assert(err, IsNil)
  54. downFileMD5, err := GetFileMD5(downfileName)
  55. c.Assert(err, IsNil)
  56. c.Assert(downFileMD5, Equals, srcMD5)
  57. os.Remove(downfileName)
  58. ForceDeleteBucket(client, bucketName, c)
  59. }
  60. func (s *OssCryptoBucketSuite) TestMultipartUploadFromFile(c *C) {
  61. // create a bucket with default proprety
  62. client, err := oss.New(endpoint, accessID, accessKey)
  63. c.Assert(err, IsNil)
  64. bucketName := bucketNamePrefix + RandLowStr(6)
  65. err = client.CreateBucket(bucketName)
  66. c.Assert(err, IsNil)
  67. // crypto bucket
  68. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  69. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  70. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  71. objectName := objectNamePrefix + RandStr(8)
  72. fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
  73. srcMD5, err := GetFileMD5(fileName)
  74. c.Assert(err, IsNil)
  75. fileInfo, err := os.Stat(fileName)
  76. dataSize := fileInfo.Size()
  77. c.Assert(err, IsNil)
  78. options := []oss.Option{oss.Meta("my", "myprop")}
  79. var cryptoContext PartCryptoContext
  80. cryptoContext.DataSize = dataSize
  81. cryptoContext.PartSize = (dataSize / 16 / 3) * 16
  82. imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext, options...)
  83. c.Assert(err, IsNil)
  84. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  85. c.Assert(err, IsNil)
  86. var parts []oss.UploadPart
  87. for _, chunk := range chunks {
  88. part, err := bucket.UploadPartFromFile(imur, fileName, chunk.Offset, chunk.Size, chunk.Number, cryptoContext)
  89. c.Assert(err, IsNil)
  90. parts = append(parts, part)
  91. }
  92. _, err = bucket.CompleteMultipartUpload(imur, parts)
  93. c.Assert(err, IsNil)
  94. meta, err := bucket.GetObjectDetailedMeta(objectName)
  95. c.Assert(err, IsNil)
  96. c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
  97. c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Multipart")
  98. downfileName := "test-go-sdk-file-" + RandLowStr(5) + ".jpg"
  99. err = bucket.GetObjectToFile(objectName, downfileName)
  100. c.Assert(err, IsNil)
  101. downFileMD5, err := GetFileMD5(downfileName)
  102. c.Assert(err, IsNil)
  103. c.Assert(downFileMD5, Equals, srcMD5)
  104. os.Remove(downfileName)
  105. ForceDeleteBucket(client, bucketName, c)
  106. }
  107. func (s *OssCryptoBucketSuite) TestMultipartUploadFromSmallSizeFile(c *C) {
  108. // create a bucket with default proprety
  109. client, err := oss.New(endpoint, accessID, accessKey)
  110. c.Assert(err, IsNil)
  111. bucketName := bucketNamePrefix + RandLowStr(6)
  112. err = client.CreateBucket(bucketName)
  113. c.Assert(err, IsNil)
  114. // crypto bucket
  115. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  116. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  117. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  118. objectName := objectNamePrefix + RandStr(8)
  119. fileName := "oss-go-sdk-test-file-" + RandStr(5)
  120. fo, err := os.Create(fileName)
  121. c.Assert(err, IsNil)
  122. _, err = fo.Write([]byte("123"))
  123. c.Assert(err, IsNil)
  124. fo.Close()
  125. srcMD5, err := GetFileMD5(fileName)
  126. c.Assert(err, IsNil)
  127. fileInfo, err := os.Stat(fileName)
  128. dataSize := fileInfo.Size()
  129. c.Assert(err, IsNil)
  130. options := []oss.Option{oss.Meta("my", "myprop")}
  131. var cryptoContext PartCryptoContext
  132. cryptoContext.DataSize = dataSize
  133. cryptoContext.PartSize = 16
  134. imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext, options...)
  135. c.Assert(err, IsNil)
  136. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  137. c.Assert(err, IsNil)
  138. var parts []oss.UploadPart
  139. for _, chunk := range chunks {
  140. part, err := bucket.UploadPartFromFile(imur, fileName, chunk.Offset, chunk.Size, chunk.Number, cryptoContext)
  141. c.Assert(err, IsNil)
  142. parts = append(parts, part)
  143. }
  144. _, err = bucket.CompleteMultipartUpload(imur, parts)
  145. c.Assert(err, IsNil)
  146. meta, err := bucket.GetObjectDetailedMeta(objectName)
  147. c.Assert(err, IsNil)
  148. c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
  149. c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Multipart")
  150. downfileName := "test-go-sdk-file-" + RandLowStr(5) + ".jpg"
  151. err = bucket.GetObjectToFile(objectName, downfileName)
  152. c.Assert(err, IsNil)
  153. downFileMD5, err := GetFileMD5(downfileName)
  154. c.Assert(err, IsNil)
  155. c.Assert(downFileMD5, Equals, srcMD5)
  156. os.Remove(fileName)
  157. os.Remove(downfileName)
  158. ForceDeleteBucket(client, bucketName, c)
  159. }
  160. func (s *OssCryptoBucketSuite) TestListUploadedPartsNormal(c *C) {
  161. // create a bucket with default proprety
  162. client, err := oss.New(endpoint, accessID, accessKey)
  163. c.Assert(err, IsNil)
  164. bucketName := bucketNamePrefix + RandLowStr(6)
  165. err = client.CreateBucket(bucketName)
  166. c.Assert(err, IsNil)
  167. // crypto bucket
  168. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  169. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  170. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  171. objectName := objectNamePrefix + RandStr(8)
  172. fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
  173. srcMD5, err := GetFileMD5(fileName)
  174. c.Assert(err, IsNil)
  175. fileInfo, err := os.Stat(fileName)
  176. dataSize := fileInfo.Size()
  177. c.Assert(err, IsNil)
  178. // Upload
  179. var cryptoContext PartCryptoContext
  180. cryptoContext.DataSize = dataSize
  181. cryptoContext.PartSize = (dataSize / 16 / 3) * 16
  182. imurUpload, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
  183. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  184. c.Assert(err, IsNil)
  185. var partsUpload []oss.UploadPart
  186. for _, chunk := range chunks {
  187. part, err := bucket.UploadPartFromFile(imurUpload, fileName, chunk.Offset, chunk.Size, (int)(chunk.Number), cryptoContext)
  188. c.Assert(err, IsNil)
  189. partsUpload = append(partsUpload, part)
  190. }
  191. // List
  192. lupr, err := bucket.ListUploadedParts(imurUpload)
  193. c.Assert(err, IsNil)
  194. c.Assert(len(lupr.UploadedParts), Equals, len(chunks))
  195. // Complete
  196. _, err = bucket.CompleteMultipartUpload(imurUpload, partsUpload)
  197. c.Assert(err, IsNil)
  198. // Download
  199. downfileName := "test-go-sdk-file-" + RandLowStr(5) + ".jpg"
  200. err = bucket.GetObjectToFile(objectName, downfileName)
  201. downFileMD5, err := GetFileMD5(downfileName)
  202. c.Assert(err, IsNil)
  203. c.Assert(downFileMD5, Equals, srcMD5)
  204. os.Remove(downfileName)
  205. ForceDeleteBucket(client, bucketName, c)
  206. }
  207. func (s *OssCryptoBucketSuite) TestListUploadedPartsComplete(c *C) {
  208. // create a bucket with default proprety
  209. client, err := oss.New(endpoint, accessID, accessKey)
  210. c.Assert(err, IsNil)
  211. bucketName := bucketNamePrefix + RandLowStr(6)
  212. err = client.CreateBucket(bucketName)
  213. c.Assert(err, IsNil)
  214. // crypto bucket
  215. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  216. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  217. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  218. objectName := objectNamePrefix + RandStr(8)
  219. fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
  220. srcMD5, err := GetFileMD5(fileName)
  221. c.Assert(err, IsNil)
  222. fileInfo, err := os.Stat(fileName)
  223. dataSize := fileInfo.Size()
  224. c.Assert(err, IsNil)
  225. // Upload
  226. var cryptoContext PartCryptoContext
  227. cryptoContext.DataSize = dataSize
  228. cryptoContext.PartSize = (dataSize / 16 / 3) * 16
  229. imurUpload, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
  230. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  231. c.Assert(err, IsNil)
  232. var partsUpload []oss.UploadPart
  233. i := 0
  234. // upload excepted the last part
  235. for ; i < len(chunks)-1; i++ {
  236. part, err := bucket.UploadPartFromFile(imurUpload, fileName, chunks[i].Offset, chunks[i].Size, (int)(chunks[i].Number), cryptoContext)
  237. c.Assert(err, IsNil)
  238. partsUpload = append(partsUpload, part)
  239. }
  240. // List
  241. lupr, err := bucket.ListUploadedParts(imurUpload)
  242. c.Assert(err, IsNil)
  243. c.Assert(len(lupr.UploadedParts), Equals, len(chunks)-1)
  244. lmur, err := bucket.ListMultipartUploads()
  245. c.Assert(err, IsNil)
  246. c.Assert(len(lmur.Uploads), Equals, 1)
  247. // upload the last part with list part result
  248. part, err := bucket.UploadPartFromFile(imurUpload, fileName, chunks[i].Offset, chunks[i].Size, (int)(chunks[i].Number), cryptoContext)
  249. c.Assert(err, IsNil)
  250. partsUpload = append(partsUpload, part)
  251. // Complete
  252. _, err = bucket.CompleteMultipartUpload(imurUpload, partsUpload)
  253. c.Assert(err, IsNil)
  254. // Download
  255. downfileName := "test-go-sdk-file-" + RandLowStr(5) + ".jpg"
  256. err = bucket.GetObjectToFile(objectName, downfileName)
  257. downFileMD5, err := GetFileMD5(downfileName)
  258. c.Assert(err, IsNil)
  259. c.Assert(downFileMD5, Equals, srcMD5)
  260. os.Remove(downfileName)
  261. ForceDeleteBucket(client, bucketName, c)
  262. }
  263. func (s *OssCryptoBucketSuite) TestListUploadedPartsAbortUseInit(c *C) {
  264. // create a bucket with default proprety
  265. client, err := oss.New(endpoint, accessID, accessKey)
  266. c.Assert(err, IsNil)
  267. bucketName := bucketNamePrefix + RandLowStr(6)
  268. err = client.CreateBucket(bucketName)
  269. c.Assert(err, IsNil)
  270. // crypto bucket
  271. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  272. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  273. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  274. objectName := objectNamePrefix + RandStr(8)
  275. fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
  276. fileInfo, err := os.Stat(fileName)
  277. dataSize := fileInfo.Size()
  278. c.Assert(err, IsNil)
  279. // Upload
  280. var cryptoContext PartCryptoContext
  281. cryptoContext.DataSize = dataSize
  282. cryptoContext.PartSize = (dataSize / 16 / 3) * 16
  283. imurUpload, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
  284. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  285. c.Assert(err, IsNil)
  286. c.Assert(cryptoContext.Valid(), Equals, true)
  287. var partsUpload []oss.UploadPart
  288. i := 0
  289. // upload excepted the last part
  290. for ; i < len(chunks)-1; i++ {
  291. part, err := bucket.UploadPartFromFile(imurUpload, fileName, chunks[i].Offset, chunks[i].Size, (int)(chunks[i].Number), cryptoContext)
  292. c.Assert(err, IsNil)
  293. partsUpload = append(partsUpload, part)
  294. }
  295. // List
  296. lupr, err := bucket.ListUploadedParts(imurUpload)
  297. c.Assert(err, IsNil)
  298. c.Assert(len(lupr.UploadedParts), Equals, len(chunks)-1)
  299. lmur, err := bucket.ListMultipartUploads()
  300. c.Assert(err, IsNil)
  301. c.Assert(len(lmur.Uploads), Equals, 1)
  302. // abort upload
  303. err = bucket.AbortMultipartUpload(imurUpload)
  304. c.Assert(err, IsNil)
  305. // list again
  306. lupr, err = bucket.ListUploadedParts(imurUpload)
  307. c.Assert(err, NotNil)
  308. ForceDeleteBucket(client, bucketName, c)
  309. }
  310. func (s *OssCryptoBucketSuite) TestListUploadedPartsAbortUseList(c *C) {
  311. // create a bucket with default proprety
  312. client, err := oss.New(endpoint, accessID, accessKey)
  313. c.Assert(err, IsNil)
  314. bucketName := bucketNamePrefix + RandLowStr(6)
  315. err = client.CreateBucket(bucketName)
  316. c.Assert(err, IsNil)
  317. // crypto bucket
  318. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  319. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  320. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  321. objectName := objectNamePrefix + RandStr(8)
  322. fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
  323. fileInfo, err := os.Stat(fileName)
  324. dataSize := fileInfo.Size()
  325. c.Assert(err, IsNil)
  326. // Upload
  327. var cryptoContext PartCryptoContext
  328. cryptoContext.DataSize = dataSize
  329. cryptoContext.PartSize = (dataSize / 16 / 3) * 16
  330. imurUpload, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
  331. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  332. c.Assert(err, IsNil)
  333. var partsUpload []oss.UploadPart
  334. i := 0
  335. // upload excepted the last part
  336. for ; i < len(chunks)-1; i++ {
  337. part, err := bucket.UploadPartFromFile(imurUpload, fileName, chunks[i].Offset, chunks[i].Size, (int)(chunks[i].Number), cryptoContext)
  338. c.Assert(err, IsNil)
  339. partsUpload = append(partsUpload, part)
  340. }
  341. // List
  342. lupr, err := bucket.ListUploadedParts(imurUpload)
  343. c.Assert(err, IsNil)
  344. c.Assert(len(lupr.UploadedParts), Equals, len(chunks)-1)
  345. // abort upload
  346. err = bucket.AbortMultipartUpload(imurUpload)
  347. c.Assert(err, IsNil)
  348. // list again
  349. lupr, err = bucket.ListUploadedParts(imurUpload)
  350. c.Assert(err, NotNil)
  351. ForceDeleteBucket(client, bucketName, c)
  352. }
  353. func (s *OssCryptoBucketSuite) TestInitiateMultipartUpload(c *C) {
  354. // create a bucket with default proprety
  355. client, err := oss.New(endpoint, accessID, accessKey)
  356. c.Assert(err, IsNil)
  357. bucketName := bucketNamePrefix + RandLowStr(6)
  358. err = client.CreateBucket(bucketName)
  359. c.Assert(err, IsNil)
  360. // crypto bucket
  361. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  362. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  363. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  364. objectName := objectNamePrefix + RandStr(8)
  365. context := RandStr(ivSize * 1024 * 10)
  366. fileName := "test-go-sdk-file-" + RandStr(5)
  367. err = ioutil.WriteFile(fileName, []byte(context), 0666)
  368. c.Assert(err, IsNil)
  369. fileInfo, err := os.Stat(fileName)
  370. dataSize := fileInfo.Size()
  371. c.Assert(err, IsNil)
  372. var cryptoContext PartCryptoContext
  373. cryptoContext.DataSize = -1
  374. cryptoContext.PartSize = (dataSize / 16 / 3) * 16
  375. imurUpload, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
  376. c.Assert(err, IsNil)
  377. cryptoContext.DataSize = dataSize
  378. cryptoContext.PartSize = ivSize * 1024
  379. imurUpload, err = bucket.InitiateMultipartUpload(objectName, &cryptoContext)
  380. c.Assert(err, IsNil)
  381. err = bucket.AbortMultipartUpload(imurUpload)
  382. c.Assert(err, IsNil)
  383. cryptoContext.DataSize = dataSize
  384. cryptoContext.PartSize = ivSize / 2
  385. imurUpload, err = bucket.InitiateMultipartUpload(objectName, &cryptoContext)
  386. c.Assert(err, NotNil)
  387. os.Remove(fileName)
  388. ForceDeleteBucket(client, bucketName, c)
  389. }
  390. func (s *OssCryptoBucketSuite) TestUploadPartError(c *C) {
  391. // create a bucket with default proprety
  392. client, err := oss.New(endpoint, accessID, accessKey)
  393. c.Assert(err, IsNil)
  394. bucketName := bucketNamePrefix + RandLowStr(6)
  395. err = client.CreateBucket(bucketName)
  396. c.Assert(err, IsNil)
  397. // crypto bucket
  398. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  399. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  400. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  401. objectName := objectNamePrefix + RandStr(8)
  402. context := RandStr(ivSize * 1024 * 10)
  403. fileName := "test-go-sdk-file-" + RandStr(5)
  404. err = ioutil.WriteFile(fileName, []byte(context), 0666)
  405. c.Assert(err, IsNil)
  406. fileInfo, err := os.Stat(fileName)
  407. dataSize := fileInfo.Size()
  408. c.Assert(err, IsNil)
  409. var cryptoContext PartCryptoContext
  410. cryptoContext.DataSize = dataSize
  411. cryptoContext.PartSize = ivSize * 1024
  412. imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
  413. c.Assert(err, IsNil)
  414. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  415. c.Assert(err, IsNil)
  416. fd, err := os.Open(fileName)
  417. c.Assert(err, IsNil)
  418. partCount := len(chunks)
  419. for _, chunk := range chunks {
  420. fd.Seek(chunk.Offset, os.SEEK_SET)
  421. _, err := bucket.UploadPart(imur, fd, chunk.Size+1, chunk.Number, cryptoContext)
  422. if chunk.Number < partCount {
  423. c.Assert(err, IsNil)
  424. } else {
  425. c.Assert(err, NotNil)
  426. }
  427. }
  428. for _, chunk := range chunks {
  429. fd.Seek(chunk.Offset, os.SEEK_SET)
  430. _, err := bucket.UploadPart(imur, fd, chunk.Size, 0, cryptoContext)
  431. c.Assert(err, NotNil)
  432. }
  433. fd.Close()
  434. err = bucket.AbortMultipartUpload(imur)
  435. c.Assert(err, IsNil)
  436. os.Remove(fileName)
  437. ForceDeleteBucket(client, bucketName, c)
  438. }
  439. func (s *OssCryptoBucketSuite) TestUploadPartFromFileError(c *C) {
  440. // create a bucket with default proprety
  441. client, err := oss.New(endpoint, accessID, accessKey)
  442. c.Assert(err, IsNil)
  443. bucketName := bucketNamePrefix + RandLowStr(6)
  444. err = client.CreateBucket(bucketName)
  445. c.Assert(err, IsNil)
  446. // crypto bucket
  447. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  448. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  449. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  450. objectName := objectNamePrefix + RandStr(8)
  451. context := RandStr(ivSize * 1024 * 10)
  452. fileName := "test-go-sdk-file-" + RandStr(5)
  453. err = ioutil.WriteFile(fileName, []byte(context), 0666)
  454. c.Assert(err, IsNil)
  455. fileInfo, err := os.Stat(fileName)
  456. dataSize := fileInfo.Size()
  457. c.Assert(err, IsNil)
  458. var cryptoContext PartCryptoContext
  459. cryptoContext.DataSize = dataSize
  460. cryptoContext.PartSize = ivSize * 1024
  461. imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
  462. c.Assert(err, IsNil)
  463. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  464. c.Assert(err, IsNil)
  465. for _, chunk := range chunks {
  466. _, err := bucket.UploadPartFromFile(imur, fileName+".test", chunk.Offset, chunk.Size, chunk.Number, cryptoContext)
  467. c.Assert(err, NotNil)
  468. }
  469. _, err = bucket.UploadPartFromFile(imur, fileName+".test", chunks[0].Offset, chunks[0].Size+1, chunks[0].Number, cryptoContext)
  470. c.Assert(err, NotNil)
  471. _, err = bucket.UploadPartFromFile(imur, fileName+".test", chunks[0].Offset, chunks[0].Size, 0, cryptoContext)
  472. c.Assert(err, NotNil)
  473. err = bucket.AbortMultipartUpload(imur)
  474. c.Assert(err, IsNil)
  475. os.Remove(fileName)
  476. ForceDeleteBucket(client, bucketName, c)
  477. }
  478. func (s *OssCryptoBucketSuite) TestUploadPartCopyError(c *C) {
  479. // create a bucket with default proprety
  480. client, err := oss.New(endpoint, accessID, accessKey)
  481. c.Assert(err, IsNil)
  482. bucketName := bucketNamePrefix + RandLowStr(6)
  483. err = client.CreateBucket(bucketName)
  484. c.Assert(err, IsNil)
  485. // crypto bucket
  486. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  487. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  488. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  489. objectName := objectNamePrefix + RandStr(8)
  490. context := RandStr(ivSize * 1024 * 10)
  491. fileName := "test-go-sdk-file-" + RandStr(5)
  492. err = ioutil.WriteFile(fileName, []byte(context), 0666)
  493. c.Assert(err, IsNil)
  494. fileInfo, err := os.Stat(fileName)
  495. dataSize := fileInfo.Size()
  496. c.Assert(err, IsNil)
  497. var cryptoContext PartCryptoContext
  498. cryptoContext.DataSize = dataSize
  499. cryptoContext.PartSize = ivSize * 1024
  500. imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
  501. c.Assert(err, IsNil)
  502. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  503. c.Assert(err, IsNil)
  504. _, err = bucket.UploadPartCopy(imur, bucketName, objectName, 0, chunks[0].Size, 1, cryptoContext)
  505. c.Assert(err, NotNil)
  506. err = bucket.AbortMultipartUpload(imur)
  507. c.Assert(err, IsNil)
  508. os.Remove(fileName)
  509. ForceDeleteBucket(client, bucketName, c)
  510. }
  511. func (s *OssCryptoBucketSuite) TestMultipartUploadFromFileError(c *C) {
  512. // create a bucket with default proprety
  513. client, err := oss.New(endpoint, accessID, accessKey)
  514. c.Assert(err, IsNil)
  515. bucketName := bucketNamePrefix + RandLowStr(6)
  516. err = client.CreateBucket(bucketName)
  517. c.Assert(err, IsNil)
  518. // crypto bucket
  519. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  520. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  521. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  522. objectName := objectNamePrefix + RandStr(8)
  523. fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
  524. fileInfo, err := os.Stat(fileName)
  525. dataSize := fileInfo.Size()
  526. c.Assert(err, IsNil)
  527. options := []oss.Option{oss.Meta("my", "myprop")}
  528. var cryptoContext PartCryptoContext
  529. cryptoContext.DataSize = dataSize
  530. cryptoContext.PartSize = -1
  531. _, err = bucket.InitiateMultipartUpload(objectName, nil, options...)
  532. c.Assert(err, NotNil)
  533. _, err = bucket.InitiateMultipartUpload(objectName, &cryptoContext, options...)
  534. c.Assert(err, NotNil)
  535. cryptoContext.PartSize = (dataSize / 16 / 3) * 16
  536. imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext, options...)
  537. c.Assert(err, IsNil)
  538. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  539. c.Assert(err, IsNil)
  540. bakCC := cryptoContext.ContentCipher
  541. cryptoContext.ContentCipher = nil
  542. i := 0
  543. // upload excepted the last part
  544. for ; i < len(chunks); i++ {
  545. _, err = bucket.UploadPartFromFile(imur, fileName, chunks[i].Offset, chunks[i].Size, (int)(chunks[i].Number), cryptoContext)
  546. c.Assert(err, NotNil)
  547. }
  548. i = 0
  549. cryptoContext.ContentCipher = bakCC
  550. cryptoContext.PartSize -= 1
  551. for ; i < len(chunks); i++ {
  552. _, err = bucket.UploadPartFromFile(imur, fileName, chunks[i].Offset, chunks[i].Size, (int)(chunks[i].Number), cryptoContext)
  553. c.Assert(err, NotNil)
  554. }
  555. ForceDeleteBucket(client, bucketName, c)
  556. }
  557. func (s *OssCryptoBucketSuite) TestMultipartUploadPartError(c *C) {
  558. // create a bucket with default proprety
  559. client, err := oss.New(endpoint, accessID, accessKey)
  560. c.Assert(err, IsNil)
  561. bucketName := bucketNamePrefix + RandLowStr(6)
  562. err = client.CreateBucket(bucketName)
  563. c.Assert(err, IsNil)
  564. // crypto bucket
  565. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  566. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  567. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  568. objectName := objectNamePrefix + RandStr(8)
  569. fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
  570. fileInfo, err := os.Stat(fileName)
  571. dataSize := fileInfo.Size()
  572. c.Assert(err, IsNil)
  573. options := []oss.Option{oss.Meta("my", "myprop")}
  574. var cryptoContext PartCryptoContext
  575. cryptoContext.DataSize = dataSize
  576. cryptoContext.PartSize = (dataSize / 16 / 3) * 16
  577. imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext, options...)
  578. c.Assert(err, IsNil)
  579. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  580. c.Assert(err, IsNil)
  581. bakCC := cryptoContext.ContentCipher
  582. cryptoContext.ContentCipher = nil
  583. fd, err := os.Open(fileName)
  584. c.Assert(err, IsNil)
  585. for _, chunk := range chunks {
  586. fd.Seek(chunk.Offset, os.SEEK_SET)
  587. _, err = bucket.UploadPart(imur, fd, chunk.Size, chunk.Number, cryptoContext)
  588. c.Assert(err, NotNil)
  589. }
  590. cryptoContext.ContentCipher = bakCC
  591. cryptoContext.PartSize -= 1
  592. for _, chunk := range chunks {
  593. _, err = bucket.UploadPart(imur, fd, chunk.Size, chunk.Number, cryptoContext)
  594. c.Assert(err, NotNil)
  595. }
  596. ForceDeleteBucket(client, bucketName, c)
  597. }