123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758 |
- // multipart test
- package osscrypto
- import (
- "io/ioutil"
- "os"
- "github.com/aliyun/aliyun-oss-go-sdk/oss"
- . "gopkg.in/check.v1"
- )
- func (s *OssCryptoBucketSuite) TestMultipartUpload(c *C) {
- // create a bucket with default proprety
- client, err := oss.New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(6)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- // crypto bucket
- masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
- contentProvider := CreateAesCtrCipher(masterRsaCipher)
- bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
- objectName := objectNamePrefix + RandStr(8)
- fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
- srcMD5, err := GetFileMD5(fileName)
- c.Assert(err, IsNil)
- fileInfo, err := os.Stat(fileName)
- dataSize := fileInfo.Size()
- c.Assert(err, IsNil)
- options := []oss.Option{oss.Meta("my", "myprop")}
- var cryptoContext PartCryptoContext
- cryptoContext.DataSize = dataSize
- cryptoContext.PartSize = (dataSize / 16 / 3) * 16
- imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext, options...)
- c.Assert(err, IsNil)
- chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
- c.Assert(err, IsNil)
- fd, err := os.Open(fileName)
- c.Assert(err, IsNil)
- defer fd.Close()
- var parts []oss.UploadPart
- for _, chunk := range chunks {
- fd.Seek(chunk.Offset, os.SEEK_SET)
- part, err := bucket.UploadPart(imur, fd, chunk.Size, chunk.Number, cryptoContext)
- c.Assert(err, IsNil)
- parts = append(parts, part)
- }
- _, err = bucket.CompleteMultipartUpload(imur, parts)
- c.Assert(err, IsNil)
- meta, err := bucket.GetObjectDetailedMeta(objectName)
- c.Assert(err, IsNil)
- c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
- c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Multipart")
- downfileName := "test-go-sdk-file-" + RandLowStr(5)
- err = bucket.GetObjectToFile(objectName, downfileName)
- c.Assert(err, IsNil)
- downFileMD5, err := GetFileMD5(downfileName)
- c.Assert(err, IsNil)
- c.Assert(downFileMD5, Equals, srcMD5)
- os.Remove(downfileName)
- ForceDeleteBucket(client, bucketName, c)
- }
- func (s *OssCryptoBucketSuite) TestMultipartUploadFromFile(c *C) {
- // create a bucket with default proprety
- client, err := oss.New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(6)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- // crypto bucket
- masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
- contentProvider := CreateAesCtrCipher(masterRsaCipher)
- bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
- objectName := objectNamePrefix + RandStr(8)
- fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
- srcMD5, err := GetFileMD5(fileName)
- c.Assert(err, IsNil)
- fileInfo, err := os.Stat(fileName)
- dataSize := fileInfo.Size()
- c.Assert(err, IsNil)
- options := []oss.Option{oss.Meta("my", "myprop")}
- var cryptoContext PartCryptoContext
- cryptoContext.DataSize = dataSize
- cryptoContext.PartSize = (dataSize / 16 / 3) * 16
- imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext, options...)
- c.Assert(err, IsNil)
- chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
- c.Assert(err, IsNil)
- var parts []oss.UploadPart
- for _, chunk := range chunks {
- part, err := bucket.UploadPartFromFile(imur, fileName, chunk.Offset, chunk.Size, chunk.Number, cryptoContext)
- c.Assert(err, IsNil)
- parts = append(parts, part)
- }
- _, err = bucket.CompleteMultipartUpload(imur, parts)
- c.Assert(err, IsNil)
- meta, err := bucket.GetObjectDetailedMeta(objectName)
- c.Assert(err, IsNil)
- c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
- c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Multipart")
- downfileName := "test-go-sdk-file-" + RandLowStr(5) + ".jpg"
- err = bucket.GetObjectToFile(objectName, downfileName)
- c.Assert(err, IsNil)
- downFileMD5, err := GetFileMD5(downfileName)
- c.Assert(err, IsNil)
- c.Assert(downFileMD5, Equals, srcMD5)
- os.Remove(downfileName)
- ForceDeleteBucket(client, bucketName, c)
- }
- func (s *OssCryptoBucketSuite) TestMultipartUploadFromSmallSizeFile(c *C) {
- // create a bucket with default proprety
- client, err := oss.New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(6)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- // crypto bucket
- masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
- contentProvider := CreateAesCtrCipher(masterRsaCipher)
- bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
- objectName := objectNamePrefix + RandStr(8)
- fileName := "oss-go-sdk-test-file-" + RandStr(5)
- fo, err := os.Create(fileName)
- c.Assert(err, IsNil)
- _, err = fo.Write([]byte("123"))
- c.Assert(err, IsNil)
- fo.Close()
- srcMD5, err := GetFileMD5(fileName)
- c.Assert(err, IsNil)
- fileInfo, err := os.Stat(fileName)
- dataSize := fileInfo.Size()
- c.Assert(err, IsNil)
- options := []oss.Option{oss.Meta("my", "myprop")}
- var cryptoContext PartCryptoContext
- cryptoContext.DataSize = dataSize
- cryptoContext.PartSize = 16
- imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext, options...)
- c.Assert(err, IsNil)
- chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
- c.Assert(err, IsNil)
- var parts []oss.UploadPart
- for _, chunk := range chunks {
- part, err := bucket.UploadPartFromFile(imur, fileName, chunk.Offset, chunk.Size, chunk.Number, cryptoContext)
- c.Assert(err, IsNil)
- parts = append(parts, part)
- }
- _, err = bucket.CompleteMultipartUpload(imur, parts)
- c.Assert(err, IsNil)
- meta, err := bucket.GetObjectDetailedMeta(objectName)
- c.Assert(err, IsNil)
- c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
- c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Multipart")
- downfileName := "test-go-sdk-file-" + RandLowStr(5) + ".jpg"
- err = bucket.GetObjectToFile(objectName, downfileName)
- c.Assert(err, IsNil)
- downFileMD5, err := GetFileMD5(downfileName)
- c.Assert(err, IsNil)
- c.Assert(downFileMD5, Equals, srcMD5)
- os.Remove(fileName)
- os.Remove(downfileName)
- ForceDeleteBucket(client, bucketName, c)
- }
- func (s *OssCryptoBucketSuite) TestListUploadedPartsNormal(c *C) {
- // create a bucket with default proprety
- client, err := oss.New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(6)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- // crypto bucket
- masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
- contentProvider := CreateAesCtrCipher(masterRsaCipher)
- bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
- objectName := objectNamePrefix + RandStr(8)
- fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
- srcMD5, err := GetFileMD5(fileName)
- c.Assert(err, IsNil)
- fileInfo, err := os.Stat(fileName)
- dataSize := fileInfo.Size()
- c.Assert(err, IsNil)
- // Upload
- var cryptoContext PartCryptoContext
- cryptoContext.DataSize = dataSize
- cryptoContext.PartSize = (dataSize / 16 / 3) * 16
- imurUpload, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
- chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
- c.Assert(err, IsNil)
- var partsUpload []oss.UploadPart
- for _, chunk := range chunks {
- part, err := bucket.UploadPartFromFile(imurUpload, fileName, chunk.Offset, chunk.Size, (int)(chunk.Number), cryptoContext)
- c.Assert(err, IsNil)
- partsUpload = append(partsUpload, part)
- }
- // List
- lupr, err := bucket.ListUploadedParts(imurUpload)
- c.Assert(err, IsNil)
- c.Assert(len(lupr.UploadedParts), Equals, len(chunks))
- // Complete
- _, err = bucket.CompleteMultipartUpload(imurUpload, partsUpload)
- c.Assert(err, IsNil)
- // Download
- downfileName := "test-go-sdk-file-" + RandLowStr(5) + ".jpg"
- err = bucket.GetObjectToFile(objectName, downfileName)
- downFileMD5, err := GetFileMD5(downfileName)
- c.Assert(err, IsNil)
- c.Assert(downFileMD5, Equals, srcMD5)
- os.Remove(downfileName)
- ForceDeleteBucket(client, bucketName, c)
- }
- func (s *OssCryptoBucketSuite) TestListUploadedPartsComplete(c *C) {
- // create a bucket with default proprety
- client, err := oss.New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(6)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- // crypto bucket
- masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
- contentProvider := CreateAesCtrCipher(masterRsaCipher)
- bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
- objectName := objectNamePrefix + RandStr(8)
- fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
- srcMD5, err := GetFileMD5(fileName)
- c.Assert(err, IsNil)
- fileInfo, err := os.Stat(fileName)
- dataSize := fileInfo.Size()
- c.Assert(err, IsNil)
- // Upload
- var cryptoContext PartCryptoContext
- cryptoContext.DataSize = dataSize
- cryptoContext.PartSize = (dataSize / 16 / 3) * 16
- imurUpload, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
- chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
- c.Assert(err, IsNil)
- var partsUpload []oss.UploadPart
- i := 0
- // upload excepted the last part
- for ; i < len(chunks)-1; i++ {
- part, err := bucket.UploadPartFromFile(imurUpload, fileName, chunks[i].Offset, chunks[i].Size, (int)(chunks[i].Number), cryptoContext)
- c.Assert(err, IsNil)
- partsUpload = append(partsUpload, part)
- }
- // List
- lupr, err := bucket.ListUploadedParts(imurUpload)
- c.Assert(err, IsNil)
- c.Assert(len(lupr.UploadedParts), Equals, len(chunks)-1)
- lmur, err := bucket.ListMultipartUploads()
- c.Assert(err, IsNil)
- c.Assert(len(lmur.Uploads), Equals, 1)
- // upload the last part with list part result
- part, err := bucket.UploadPartFromFile(imurUpload, fileName, chunks[i].Offset, chunks[i].Size, (int)(chunks[i].Number), cryptoContext)
- c.Assert(err, IsNil)
- partsUpload = append(partsUpload, part)
- // Complete
- _, err = bucket.CompleteMultipartUpload(imurUpload, partsUpload)
- c.Assert(err, IsNil)
- // Download
- downfileName := "test-go-sdk-file-" + RandLowStr(5) + ".jpg"
- err = bucket.GetObjectToFile(objectName, downfileName)
- downFileMD5, err := GetFileMD5(downfileName)
- c.Assert(err, IsNil)
- c.Assert(downFileMD5, Equals, srcMD5)
- os.Remove(downfileName)
- ForceDeleteBucket(client, bucketName, c)
- }
- func (s *OssCryptoBucketSuite) TestListUploadedPartsAbortUseInit(c *C) {
- // create a bucket with default proprety
- client, err := oss.New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(6)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- // crypto bucket
- masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
- contentProvider := CreateAesCtrCipher(masterRsaCipher)
- bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
- objectName := objectNamePrefix + RandStr(8)
- fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
- fileInfo, err := os.Stat(fileName)
- dataSize := fileInfo.Size()
- c.Assert(err, IsNil)
- // Upload
- var cryptoContext PartCryptoContext
- cryptoContext.DataSize = dataSize
- cryptoContext.PartSize = (dataSize / 16 / 3) * 16
- imurUpload, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
- chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
- c.Assert(err, IsNil)
- c.Assert(cryptoContext.Valid(), Equals, true)
- var partsUpload []oss.UploadPart
- i := 0
- // upload excepted the last part
- for ; i < len(chunks)-1; i++ {
- part, err := bucket.UploadPartFromFile(imurUpload, fileName, chunks[i].Offset, chunks[i].Size, (int)(chunks[i].Number), cryptoContext)
- c.Assert(err, IsNil)
- partsUpload = append(partsUpload, part)
- }
- // List
- lupr, err := bucket.ListUploadedParts(imurUpload)
- c.Assert(err, IsNil)
- c.Assert(len(lupr.UploadedParts), Equals, len(chunks)-1)
- lmur, err := bucket.ListMultipartUploads()
- c.Assert(err, IsNil)
- c.Assert(len(lmur.Uploads), Equals, 1)
- // abort upload
- err = bucket.AbortMultipartUpload(imurUpload)
- c.Assert(err, IsNil)
- // list again
- lupr, err = bucket.ListUploadedParts(imurUpload)
- c.Assert(err, NotNil)
- ForceDeleteBucket(client, bucketName, c)
- }
- func (s *OssCryptoBucketSuite) TestListUploadedPartsAbortUseList(c *C) {
- // create a bucket with default proprety
- client, err := oss.New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(6)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- // crypto bucket
- masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
- contentProvider := CreateAesCtrCipher(masterRsaCipher)
- bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
- objectName := objectNamePrefix + RandStr(8)
- fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
- fileInfo, err := os.Stat(fileName)
- dataSize := fileInfo.Size()
- c.Assert(err, IsNil)
- // Upload
- var cryptoContext PartCryptoContext
- cryptoContext.DataSize = dataSize
- cryptoContext.PartSize = (dataSize / 16 / 3) * 16
- imurUpload, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
- chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
- c.Assert(err, IsNil)
- var partsUpload []oss.UploadPart
- i := 0
- // upload excepted the last part
- for ; i < len(chunks)-1; i++ {
- part, err := bucket.UploadPartFromFile(imurUpload, fileName, chunks[i].Offset, chunks[i].Size, (int)(chunks[i].Number), cryptoContext)
- c.Assert(err, IsNil)
- partsUpload = append(partsUpload, part)
- }
- // List
- lupr, err := bucket.ListUploadedParts(imurUpload)
- c.Assert(err, IsNil)
- c.Assert(len(lupr.UploadedParts), Equals, len(chunks)-1)
- // abort upload
- err = bucket.AbortMultipartUpload(imurUpload)
- c.Assert(err, IsNil)
- // list again
- lupr, err = bucket.ListUploadedParts(imurUpload)
- c.Assert(err, NotNil)
- ForceDeleteBucket(client, bucketName, c)
- }
- func (s *OssCryptoBucketSuite) TestInitiateMultipartUpload(c *C) {
- // create a bucket with default proprety
- client, err := oss.New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(6)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- // crypto bucket
- masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
- contentProvider := CreateAesCtrCipher(masterRsaCipher)
- bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
- objectName := objectNamePrefix + RandStr(8)
- context := RandStr(ivSize * 1024 * 10)
- fileName := "test-go-sdk-file-" + RandStr(5)
- err = ioutil.WriteFile(fileName, []byte(context), 0666)
- c.Assert(err, IsNil)
- fileInfo, err := os.Stat(fileName)
- dataSize := fileInfo.Size()
- c.Assert(err, IsNil)
- var cryptoContext PartCryptoContext
- cryptoContext.DataSize = dataSize
- cryptoContext.PartSize = ivSize * 1024
- imurUpload, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
- c.Assert(err, IsNil)
- err = bucket.AbortMultipartUpload(imurUpload)
- c.Assert(err, IsNil)
- cryptoContext.DataSize = dataSize
- cryptoContext.PartSize = ivSize / 2
- imurUpload, err = bucket.InitiateMultipartUpload(objectName, &cryptoContext)
- c.Assert(err, NotNil)
- os.Remove(fileName)
- ForceDeleteBucket(client, bucketName, c)
- }
- func (s *OssCryptoBucketSuite) TestUploadPartError(c *C) {
- // create a bucket with default proprety
- client, err := oss.New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(6)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- // crypto bucket
- masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
- contentProvider := CreateAesCtrCipher(masterRsaCipher)
- bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
- objectName := objectNamePrefix + RandStr(8)
- context := RandStr(ivSize * 1024 * 10)
- fileName := "test-go-sdk-file-" + RandStr(5)
- err = ioutil.WriteFile(fileName, []byte(context), 0666)
- c.Assert(err, IsNil)
- fileInfo, err := os.Stat(fileName)
- dataSize := fileInfo.Size()
- c.Assert(err, IsNil)
- var cryptoContext PartCryptoContext
- cryptoContext.DataSize = dataSize
- cryptoContext.PartSize = ivSize * 1024
- imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
- c.Assert(err, IsNil)
- chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
- c.Assert(err, IsNil)
- fd, err := os.Open(fileName)
- c.Assert(err, IsNil)
- for _, chunk := range chunks {
- fd.Seek(chunk.Offset, os.SEEK_SET)
- _, err := bucket.UploadPart(imur, fd, chunk.Size+1, chunk.Number, cryptoContext)
- c.Assert(err, NotNil)
- }
- for _, chunk := range chunks {
- fd.Seek(chunk.Offset, os.SEEK_SET)
- _, err := bucket.UploadPart(imur, fd, chunk.Size, 0, cryptoContext)
- c.Assert(err, NotNil)
- }
- fd.Close()
- err = bucket.AbortMultipartUpload(imur)
- c.Assert(err, IsNil)
- os.Remove(fileName)
- ForceDeleteBucket(client, bucketName, c)
- }
- func (s *OssCryptoBucketSuite) TestUploadPartFromFileError(c *C) {
- // create a bucket with default proprety
- client, err := oss.New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(6)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- // crypto bucket
- masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
- contentProvider := CreateAesCtrCipher(masterRsaCipher)
- bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
- objectName := objectNamePrefix + RandStr(8)
- context := RandStr(ivSize * 1024 * 10)
- fileName := "test-go-sdk-file-" + RandStr(5)
- err = ioutil.WriteFile(fileName, []byte(context), 0666)
- c.Assert(err, IsNil)
- fileInfo, err := os.Stat(fileName)
- dataSize := fileInfo.Size()
- c.Assert(err, IsNil)
- var cryptoContext PartCryptoContext
- cryptoContext.DataSize = dataSize
- cryptoContext.PartSize = ivSize * 1024
- imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
- c.Assert(err, IsNil)
- chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
- c.Assert(err, IsNil)
- for _, chunk := range chunks {
- _, err := bucket.UploadPartFromFile(imur, fileName+".test", chunk.Offset, chunk.Size, chunk.Number, cryptoContext)
- c.Assert(err, NotNil)
- }
- _, err = bucket.UploadPartFromFile(imur, fileName+".test", chunks[0].Offset, chunks[0].Size+1, chunks[0].Number, cryptoContext)
- c.Assert(err, NotNil)
- _, err = bucket.UploadPartFromFile(imur, fileName+".test", chunks[0].Offset, chunks[0].Size, 0, cryptoContext)
- c.Assert(err, NotNil)
- err = bucket.AbortMultipartUpload(imur)
- c.Assert(err, IsNil)
- os.Remove(fileName)
- ForceDeleteBucket(client, bucketName, c)
- }
- func (s *OssCryptoBucketSuite) TestUploadPartCopyError(c *C) {
- // create a bucket with default proprety
- client, err := oss.New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(6)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- // crypto bucket
- masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
- contentProvider := CreateAesCtrCipher(masterRsaCipher)
- bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
- objectName := objectNamePrefix + RandStr(8)
- context := RandStr(ivSize * 1024 * 10)
- fileName := "test-go-sdk-file-" + RandStr(5)
- err = ioutil.WriteFile(fileName, []byte(context), 0666)
- c.Assert(err, IsNil)
- fileInfo, err := os.Stat(fileName)
- dataSize := fileInfo.Size()
- c.Assert(err, IsNil)
- var cryptoContext PartCryptoContext
- cryptoContext.DataSize = dataSize
- cryptoContext.PartSize = ivSize * 1024
- imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
- c.Assert(err, IsNil)
- chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
- c.Assert(err, IsNil)
- _, err = bucket.UploadPartCopy(imur, bucketName, objectName, 0, chunks[0].Size, 1, cryptoContext)
- c.Assert(err, NotNil)
- err = bucket.AbortMultipartUpload(imur)
- c.Assert(err, IsNil)
- os.Remove(fileName)
- ForceDeleteBucket(client, bucketName, c)
- }
- func (s *OssCryptoBucketSuite) TestMultipartUploadFromFileError(c *C) {
- // create a bucket with default proprety
- client, err := oss.New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(6)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- // crypto bucket
- masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
- contentProvider := CreateAesCtrCipher(masterRsaCipher)
- bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
- objectName := objectNamePrefix + RandStr(8)
- fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
- fileInfo, err := os.Stat(fileName)
- dataSize := fileInfo.Size()
- c.Assert(err, IsNil)
- options := []oss.Option{oss.Meta("my", "myprop")}
- var cryptoContext PartCryptoContext
- cryptoContext.DataSize = dataSize
- cryptoContext.PartSize = -1
- _, err = bucket.InitiateMultipartUpload(objectName, nil, options...)
- c.Assert(err, NotNil)
- _, err = bucket.InitiateMultipartUpload(objectName, &cryptoContext, options...)
- c.Assert(err, NotNil)
- cryptoContext.PartSize = (dataSize / 16 / 3) * 16
- imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext, options...)
- c.Assert(err, IsNil)
- chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
- c.Assert(err, IsNil)
- bakCC := cryptoContext.ContentCipher
- cryptoContext.ContentCipher = nil
- i := 0
- // upload excepted the last part
- for ; i < len(chunks); i++ {
- _, err = bucket.UploadPartFromFile(imur, fileName, chunks[i].Offset, chunks[i].Size, (int)(chunks[i].Number), cryptoContext)
- c.Assert(err, NotNil)
- }
- i = 0
- cryptoContext.ContentCipher = bakCC
- cryptoContext.PartSize -= 1
- for ; i < len(chunks); i++ {
- _, err = bucket.UploadPartFromFile(imur, fileName, chunks[i].Offset, chunks[i].Size, (int)(chunks[i].Number), cryptoContext)
- c.Assert(err, NotNil)
- }
- ForceDeleteBucket(client, bucketName, c)
- }
- func (s *OssCryptoBucketSuite) TestMultipartUploadPartError(c *C) {
- // create a bucket with default proprety
- client, err := oss.New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- bucketName := bucketNamePrefix + RandLowStr(6)
- err = client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- // crypto bucket
- masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
- contentProvider := CreateAesCtrCipher(masterRsaCipher)
- bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
- objectName := objectNamePrefix + RandStr(8)
- fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
- fileInfo, err := os.Stat(fileName)
- dataSize := fileInfo.Size()
- c.Assert(err, IsNil)
- options := []oss.Option{oss.Meta("my", "myprop")}
- var cryptoContext PartCryptoContext
- cryptoContext.DataSize = dataSize
- cryptoContext.PartSize = (dataSize / 16 / 3) * 16
- imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext, options...)
- c.Assert(err, IsNil)
- chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
- c.Assert(err, IsNil)
- bakCC := cryptoContext.ContentCipher
- cryptoContext.ContentCipher = nil
- fd, err := os.Open(fileName)
- c.Assert(err, IsNil)
- for _, chunk := range chunks {
- fd.Seek(chunk.Offset, os.SEEK_SET)
- _, err = bucket.UploadPart(imur, fd, chunk.Size, chunk.Number, cryptoContext)
- c.Assert(err, NotNil)
- }
- cryptoContext.ContentCipher = bakCC
- cryptoContext.PartSize -= 1
- for _, chunk := range chunks {
- _, err = bucket.UploadPart(imur, fd, chunk.Size, chunk.Number, cryptoContext)
- c.Assert(err, NotNil)
- }
- ForceDeleteBucket(client, bucketName, c)
- }
|