123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986 |
- // multipart test
- package oss
- import (
- "math/rand"
- "net/http"
- "os"
- "strconv"
- . "gopkg.in/check.v1"
- )
- type OssBucketMultipartSuite struct {
- client *Client
- bucket *Bucket
- }
- var _ = Suite(&OssBucketMultipartSuite{})
- // SetUpSuite runs once when the suite starts running
- func (s *OssBucketMultipartSuite) SetUpSuite(c *C) {
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- s.client = client
- s.client.CreateBucket(bucketName)
- bucket, err := s.client.Bucket(bucketName)
- c.Assert(err, IsNil)
- s.bucket = bucket
- // Delete part
- keyMarker := KeyMarker("")
- uploadIDMarker := UploadIDMarker("")
- for {
- lmur, err := s.bucket.ListMultipartUploads(keyMarker, uploadIDMarker)
- c.Assert(err, IsNil)
- for _, upload := range lmur.Uploads {
- var imur = InitiateMultipartUploadResult{Bucket: s.bucket.BucketName,
- Key: upload.Key, UploadID: upload.UploadID}
- err = s.bucket.AbortMultipartUpload(imur)
- c.Assert(err, IsNil)
- }
- keyMarker = KeyMarker(lmur.NextKeyMarker)
- uploadIDMarker = UploadIDMarker(lmur.NextUploadIDMarker)
- if !lmur.IsTruncated {
- break
- }
- }
- // Delete objects
- marker := Marker("")
- for {
- lor, err := s.bucket.ListObjects(marker)
- c.Assert(err, IsNil)
- for _, object := range lor.Objects {
- err = s.bucket.DeleteObject(object.Key)
- c.Assert(err, IsNil)
- }
- marker = Marker(lor.NextMarker)
- if !lor.IsTruncated {
- break
- }
- }
- testLogger.Println("test multipart started")
- }
- // TearDownSuite runs before each test or benchmark starts running
- func (s *OssBucketMultipartSuite) TearDownSuite(c *C) {
- // Delete part
- keyMarker := KeyMarker("")
- uploadIDMarker := UploadIDMarker("")
- for {
- lmur, err := s.bucket.ListMultipartUploads(keyMarker, uploadIDMarker)
- c.Assert(err, IsNil)
- for _, upload := range lmur.Uploads {
- var imur = InitiateMultipartUploadResult{Bucket: s.bucket.BucketName,
- Key: upload.Key, UploadID: upload.UploadID}
- err = s.bucket.AbortMultipartUpload(imur)
- c.Assert(err, IsNil)
- }
- keyMarker = KeyMarker(lmur.NextKeyMarker)
- uploadIDMarker = UploadIDMarker(lmur.NextUploadIDMarker)
- if !lmur.IsTruncated {
- break
- }
- }
- // Delete objects
- marker := Marker("")
- for {
- lor, err := s.bucket.ListObjects(marker)
- c.Assert(err, IsNil)
- for _, object := range lor.Objects {
- err = s.bucket.DeleteObject(object.Key)
- c.Assert(err, IsNil)
- }
- marker = Marker(lor.NextMarker)
- if !lor.IsTruncated {
- break
- }
- }
- // Delete bucket
- err := s.client.DeleteBucket(s.bucket.BucketName)
- c.Assert(err, IsNil)
- testLogger.Println("test multipart completed")
- }
- // SetUpTest runs after each test or benchmark runs
- func (s *OssBucketMultipartSuite) SetUpTest(c *C) {
- err := removeTempFiles("../oss", ".jpg")
- c.Assert(err, IsNil)
- }
- // TearDownTest runs once after all tests or benchmarks have finished running
- func (s *OssBucketMultipartSuite) TearDownTest(c *C) {
- err := removeTempFiles("../oss", ".jpg")
- c.Assert(err, IsNil)
- err = removeTempFiles("../oss", ".temp")
- c.Assert(err, IsNil)
- err = removeTempFiles("../oss", ".txt1")
- c.Assert(err, IsNil)
- err = removeTempFiles("../oss", ".txt2")
- c.Assert(err, IsNil)
- }
- // TestMultipartUpload
- func (s *OssBucketMultipartSuite) TestMultipartUpload(c *C) {
- objectName := objectNamePrefix + RandStr(8)
- var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
- chunks, err := SplitFileByPartNum(fileName, 3)
- c.Assert(err, IsNil)
- testLogger.Println("chunks:", chunks)
- options := []Option{
- Expires(futureDate), Meta("my", "myprop"),
- }
- fd, err := os.Open(fileName)
- c.Assert(err, IsNil)
- defer fd.Close()
- imur, err := s.bucket.InitiateMultipartUpload(objectName, options...)
- c.Assert(err, IsNil)
- var parts []UploadPart
- for _, chunk := range chunks {
- fd.Seek(chunk.Offset, os.SEEK_SET)
- part, err := s.bucket.UploadPart(imur, fd, chunk.Size, chunk.Number)
- c.Assert(err, IsNil)
- parts = append(parts, part)
- }
- cmur, err := s.bucket.CompleteMultipartUpload(imur, parts)
- c.Assert(err, IsNil)
- testLogger.Println("cmur:", cmur)
- meta, err := s.bucket.GetObjectDetailedMeta(objectName)
- c.Assert(err, IsNil)
- testLogger.Println("GetObjectDetailedMeta:", meta)
- c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
- c.Assert(meta.Get("Expires"), Equals, futureDate.Format(http.TimeFormat))
- c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Multipart")
- err = s.bucket.GetObjectToFile(objectName, "newpic1.jpg")
- c.Assert(err, IsNil)
- err = s.bucket.DeleteObject(objectName)
- c.Assert(err, IsNil)
- }
- // TestMultipartUploadFromFile
- func (s *OssBucketMultipartSuite) TestMultipartUploadFromFile(c *C) {
- objectName := objectNamePrefix + RandStr(8)
- var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
- chunks, err := SplitFileByPartNum(fileName, 3)
- c.Assert(err, IsNil)
- testLogger.Println("chunks:", chunks)
- options := []Option{
- Expires(futureDate), Meta("my", "myprop"),
- }
- imur, err := s.bucket.InitiateMultipartUpload(objectName, options...)
- c.Assert(err, IsNil)
- var parts []UploadPart
- for _, chunk := range chunks {
- part, err := s.bucket.UploadPartFromFile(imur, fileName, chunk.Offset, chunk.Size, chunk.Number)
- c.Assert(err, IsNil)
- parts = append(parts, part)
- }
- cmur, err := s.bucket.CompleteMultipartUpload(imur, parts)
- c.Assert(err, IsNil)
- testLogger.Println("cmur:", cmur)
- meta, err := s.bucket.GetObjectDetailedMeta(objectName)
- c.Assert(err, IsNil)
- testLogger.Println("GetObjectDetailedMeta:", meta)
- c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
- c.Assert(meta.Get("Expires"), Equals, futureDate.Format(http.TimeFormat))
- c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Multipart")
- err = s.bucket.GetObjectToFile(objectName, "newpic1.jpg")
- c.Assert(err, IsNil)
- err = s.bucket.DeleteObject(objectName)
- c.Assert(err, IsNil)
- }
- // TestUploadPartCopy
- func (s *OssBucketMultipartSuite) TestUploadPartCopy(c *C) {
- objectSrc := objectNamePrefix + RandStr(8) + "-src"
- objectDest := objectNamePrefix + RandStr(8) + "-dest"
- var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
- chunks, err := SplitFileByPartNum(fileName, 3)
- c.Assert(err, IsNil)
- testLogger.Println("chunks:", chunks)
- err = s.bucket.PutObjectFromFile(objectSrc, fileName)
- c.Assert(err, IsNil)
- options := []Option{
- Expires(futureDate), Meta("my", "myprop"),
- }
- imur, err := s.bucket.InitiateMultipartUpload(objectDest, options...)
- c.Assert(err, IsNil)
- var parts []UploadPart
- for _, chunk := range chunks {
- part, err := s.bucket.UploadPartCopy(imur, bucketName, objectSrc, chunk.Offset, chunk.Size, (int)(chunk.Number))
- c.Assert(err, IsNil)
- parts = append(parts, part)
- }
- cmur, err := s.bucket.CompleteMultipartUpload(imur, parts)
- c.Assert(err, IsNil)
- testLogger.Println("cmur:", cmur)
- meta, err := s.bucket.GetObjectDetailedMeta(objectDest)
- c.Assert(err, IsNil)
- testLogger.Println("GetObjectDetailedMeta:", meta)
- c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
- c.Assert(meta.Get("Expires"), Equals, futureDate.Format(http.TimeFormat))
- c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Multipart")
- err = s.bucket.GetObjectToFile(objectDest, "newpic2.jpg")
- c.Assert(err, IsNil)
- err = s.bucket.DeleteObject(objectSrc)
- c.Assert(err, IsNil)
- err = s.bucket.DeleteObject(objectDest)
- c.Assert(err, IsNil)
- }
- func (s *OssBucketMultipartSuite) TestListUploadedParts(c *C) {
- objectName := objectNamePrefix + RandStr(8)
- objectSrc := objectName + "-src"
- objectDest := objectName + "-dest"
- var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
- chunks, err := SplitFileByPartSize(fileName, 100*1024)
- c.Assert(err, IsNil)
- testLogger.Println("chunks:", chunks)
- err = s.bucket.PutObjectFromFile(objectSrc, fileName)
- c.Assert(err, IsNil)
- // Upload
- imurUpload, err := s.bucket.InitiateMultipartUpload(objectName)
- var partsUpload []UploadPart
- for _, chunk := range chunks {
- part, err := s.bucket.UploadPartFromFile(imurUpload, fileName, chunk.Offset, chunk.Size, (int)(chunk.Number))
- c.Assert(err, IsNil)
- partsUpload = append(partsUpload, part)
- }
- // Copy
- imurCopy, err := s.bucket.InitiateMultipartUpload(objectDest)
- var partsCopy []UploadPart
- for _, chunk := range chunks {
- part, err := s.bucket.UploadPartCopy(imurCopy, bucketName, objectSrc, chunk.Offset, chunk.Size, (int)(chunk.Number))
- c.Assert(err, IsNil)
- partsCopy = append(partsCopy, part)
- }
- // List
- lupr, err := s.bucket.ListUploadedParts(imurUpload)
- c.Assert(err, IsNil)
- testLogger.Println("lupr:", lupr)
- c.Assert(len(lupr.UploadedParts), Equals, len(chunks))
- lupr, err = s.bucket.ListUploadedParts(imurCopy)
- c.Assert(err, IsNil)
- testLogger.Println("lupr:", lupr)
- c.Assert(len(lupr.UploadedParts), Equals, len(chunks))
- lmur, err := s.bucket.ListMultipartUploads()
- c.Assert(err, IsNil)
- testLogger.Println("lmur:", lmur)
- // Complete
- _, err = s.bucket.CompleteMultipartUpload(imurUpload, partsUpload)
- c.Assert(err, IsNil)
- _, err = s.bucket.CompleteMultipartUpload(imurCopy, partsCopy)
- c.Assert(err, IsNil)
- // Download
- err = s.bucket.GetObjectToFile(objectDest, "newpic3.jpg")
- c.Assert(err, IsNil)
- err = s.bucket.GetObjectToFile(objectName, "newpic4.jpg")
- c.Assert(err, IsNil)
- err = s.bucket.DeleteObject(objectName)
- c.Assert(err, IsNil)
- err = s.bucket.DeleteObject(objectDest)
- c.Assert(err, IsNil)
- err = s.bucket.DeleteObject(objectSrc)
- c.Assert(err, IsNil)
- }
- func (s *OssBucketMultipartSuite) TestAbortMultipartUpload(c *C) {
- objectName := objectNamePrefix + RandStr(8)
- objectSrc := objectName + "-src"
- objectDest := objectName + "-dest"
- var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
- chunks, err := SplitFileByPartSize(fileName, 100*1024)
- c.Assert(err, IsNil)
- testLogger.Println("chunks:", chunks)
- err = s.bucket.PutObjectFromFile(objectSrc, fileName)
- c.Assert(err, IsNil)
- // Upload
- imurUpload, err := s.bucket.InitiateMultipartUpload(objectName)
- var partsUpload []UploadPart
- for _, chunk := range chunks {
- part, err := s.bucket.UploadPartFromFile(imurUpload, fileName, chunk.Offset, chunk.Size, (int)(chunk.Number))
- c.Assert(err, IsNil)
- partsUpload = append(partsUpload, part)
- }
- // Copy
- imurCopy, err := s.bucket.InitiateMultipartUpload(objectDest)
- var partsCopy []UploadPart
- for _, chunk := range chunks {
- part, err := s.bucket.UploadPartCopy(imurCopy, bucketName, objectSrc, chunk.Offset, chunk.Size, (int)(chunk.Number))
- c.Assert(err, IsNil)
- partsCopy = append(partsCopy, part)
- }
- // List
- lupr, err := s.bucket.ListUploadedParts(imurUpload)
- c.Assert(err, IsNil)
- testLogger.Println("lupr:", lupr)
- c.Assert(len(lupr.UploadedParts), Equals, len(chunks))
- lupr, err = s.bucket.ListUploadedParts(imurCopy)
- c.Assert(err, IsNil)
- testLogger.Println("lupr:", lupr)
- c.Assert(len(lupr.UploadedParts), Equals, len(chunks))
- lmur, err := s.bucket.ListMultipartUploads()
- c.Assert(err, IsNil)
- testLogger.Println("lmur:", lmur)
- c.Assert(len(lmur.Uploads), Equals, 2)
- // Abort
- err = s.bucket.AbortMultipartUpload(imurUpload)
- c.Assert(err, IsNil)
- err = s.bucket.AbortMultipartUpload(imurCopy)
- c.Assert(err, IsNil)
- lmur, err = s.bucket.ListMultipartUploads()
- c.Assert(err, IsNil)
- testLogger.Println("lmur:", lmur)
- c.Assert(len(lmur.Uploads), Equals, 0)
- // Download
- err = s.bucket.GetObjectToFile(objectDest, "newpic3.jpg")
- c.Assert(err, NotNil)
- err = s.bucket.GetObjectToFile(objectName, "newpic4.jpg")
- c.Assert(err, NotNil)
- }
- // TestUploadPartCopyWithConstraints
- func (s *OssBucketMultipartSuite) TestUploadPartCopyWithConstraints(c *C) {
- objectSrc := objectNamePrefix + RandStr(8) + "-src"
- objectDest := objectNamePrefix + RandStr(8) + "-dest"
- var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
- chunks, err := SplitFileByPartNum(fileName, 3)
- c.Assert(err, IsNil)
- testLogger.Println("chunks:", chunks)
- err = s.bucket.PutObjectFromFile(objectSrc, fileName)
- c.Assert(err, IsNil)
- imur, err := s.bucket.InitiateMultipartUpload(objectDest)
- var parts []UploadPart
- for _, chunk := range chunks {
- _, err = s.bucket.UploadPartCopy(imur, bucketName, objectSrc, chunk.Offset, chunk.Size, (int)(chunk.Number),
- CopySourceIfModifiedSince(futureDate))
- c.Assert(err, NotNil)
- }
- for _, chunk := range chunks {
- _, err = s.bucket.UploadPartCopy(imur, bucketName, objectSrc, chunk.Offset, chunk.Size, (int)(chunk.Number),
- CopySourceIfUnmodifiedSince(futureDate))
- c.Assert(err, IsNil)
- }
- meta, err := s.bucket.GetObjectDetailedMeta(objectSrc)
- c.Assert(err, IsNil)
- testLogger.Println("GetObjectDetailedMeta:", meta)
- for _, chunk := range chunks {
- _, err = s.bucket.UploadPartCopy(imur, bucketName, objectSrc, chunk.Offset, chunk.Size, (int)(chunk.Number),
- CopySourceIfNoneMatch(meta.Get("Etag")))
- c.Assert(err, NotNil)
- }
- for _, chunk := range chunks {
- part, err := s.bucket.UploadPartCopy(imur, bucketName, objectSrc, chunk.Offset, chunk.Size, (int)(chunk.Number),
- CopySourceIfMatch(meta.Get("Etag")))
- c.Assert(err, IsNil)
- parts = append(parts, part)
- }
- cmur, err := s.bucket.CompleteMultipartUpload(imur, parts)
- c.Assert(err, IsNil)
- testLogger.Println("cmur:", cmur)
- err = s.bucket.GetObjectToFile(objectDest, "newpic5.jpg")
- c.Assert(err, IsNil)
- err = s.bucket.DeleteObject(objectSrc)
- c.Assert(err, IsNil)
- err = s.bucket.DeleteObject(objectDest)
- c.Assert(err, IsNil)
- }
- // TestMultipartUploadFromFileOutofOrder
- func (s *OssBucketMultipartSuite) TestMultipartUploadFromFileOutofOrder(c *C) {
- objectName := objectNamePrefix + RandStr(8)
- var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
- chunks, err := SplitFileByPartSize(fileName, 1024*100)
- shuffleArray(chunks)
- c.Assert(err, IsNil)
- testLogger.Println("chunks:", chunks)
- imur, err := s.bucket.InitiateMultipartUpload(objectName)
- var parts []UploadPart
- for _, chunk := range chunks {
- _, err := s.bucket.UploadPartFromFile(imur, fileName, chunk.Offset, chunk.Size, (int)(chunk.Number))
- c.Assert(err, IsNil)
- }
- // Double upload
- for _, chunk := range chunks {
- part, err := s.bucket.UploadPartFromFile(imur, fileName, chunk.Offset, chunk.Size, (int)(chunk.Number))
- c.Assert(err, IsNil)
- parts = append(parts, part)
- }
- cmur, err := s.bucket.CompleteMultipartUpload(imur, parts)
- c.Assert(err, IsNil)
- testLogger.Println("cmur:", cmur)
- err = s.bucket.GetObjectToFile(objectName, "newpic6.jpg")
- c.Assert(err, IsNil)
- err = s.bucket.DeleteObject(objectName)
- c.Assert(err, IsNil)
- }
- // TestUploadPartCopyOutofOrder
- func (s *OssBucketMultipartSuite) TestUploadPartCopyOutofOrder(c *C) {
- objectSrc := objectNamePrefix + RandStr(8) + "-src"
- objectDest := objectNamePrefix + RandStr(8) + "-dest"
- var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
- chunks, err := SplitFileByPartSize(fileName, 1024*100)
- shuffleArray(chunks)
- c.Assert(err, IsNil)
- testLogger.Println("chunks:", chunks)
- err = s.bucket.PutObjectFromFile(objectSrc, fileName)
- c.Assert(err, IsNil)
- imur, err := s.bucket.InitiateMultipartUpload(objectDest)
- var parts []UploadPart
- for _, chunk := range chunks {
- _, err := s.bucket.UploadPartCopy(imur, bucketName, objectSrc, chunk.Offset, chunk.Size, (int)(chunk.Number))
- c.Assert(err, IsNil)
- }
- // Double copy
- for _, chunk := range chunks {
- part, err := s.bucket.UploadPartCopy(imur, bucketName, objectSrc, chunk.Offset, chunk.Size, (int)(chunk.Number))
- c.Assert(err, IsNil)
- parts = append(parts, part)
- }
- cmur, err := s.bucket.CompleteMultipartUpload(imur, parts)
- c.Assert(err, IsNil)
- testLogger.Println("cmur:", cmur)
- err = s.bucket.GetObjectToFile(objectDest, "newpic7.jpg")
- c.Assert(err, IsNil)
- err = s.bucket.DeleteObject(objectSrc)
- c.Assert(err, IsNil)
- err = s.bucket.DeleteObject(objectDest)
- c.Assert(err, IsNil)
- }
- // TestMultipartUploadFromFileType
- func (s *OssBucketMultipartSuite) TestMultipartUploadFromFileType(c *C) {
- objectName := objectNamePrefix + RandStr(8) + ".jpg"
- var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
- chunks, err := SplitFileByPartNum(fileName, 4)
- c.Assert(err, IsNil)
- testLogger.Println("chunks:", chunks)
- imur, err := s.bucket.InitiateMultipartUpload(objectName)
- var parts []UploadPart
- for _, chunk := range chunks {
- part, err := s.bucket.UploadPartFromFile(imur, fileName, chunk.Offset, chunk.Size, chunk.Number)
- c.Assert(err, IsNil)
- parts = append(parts, part)
- }
- testLogger.Println("parts:", parts)
- cmur, err := s.bucket.CompleteMultipartUpload(imur, parts)
- c.Assert(err, IsNil)
- testLogger.Println("cmur:", cmur)
- err = s.bucket.GetObjectToFile(objectName, "newpic8.jpg")
- c.Assert(err, IsNil)
- meta, err := s.bucket.GetObjectDetailedMeta(objectName)
- c.Assert(err, IsNil)
- c.Assert(meta.Get("Content-Type"), Equals, "image/jpeg")
- err = s.bucket.DeleteObject(objectName)
- c.Assert(err, IsNil)
- }
- func (s *OssBucketMultipartSuite) TestListMultipartUploads(c *C) {
- objectName := objectNamePrefix + RandStr(8)
- imurs := []InitiateMultipartUploadResult{}
- for i := 0; i < 20; i++ {
- imur, err := s.bucket.InitiateMultipartUpload(objectName + strconv.Itoa(i))
- c.Assert(err, IsNil)
- imurs = append(imurs, imur)
- }
- lmpu, err := s.bucket.ListMultipartUploads()
- c.Assert(err, IsNil)
- c.Assert(len(lmpu.Uploads), Equals, 20)
- lmpu, err = s.bucket.ListMultipartUploads(MaxUploads(3))
- c.Assert(err, IsNil)
- c.Assert(len(lmpu.Uploads), Equals, 3)
- lmpu, err = s.bucket.ListMultipartUploads(Prefix(objectName))
- c.Assert(err, IsNil)
- c.Assert(len(lmpu.Uploads), Equals, 20)
- lmpu, err = s.bucket.ListMultipartUploads(Prefix(objectName + "1"))
- c.Assert(err, IsNil)
- c.Assert(len(lmpu.Uploads), Equals, 11)
- lmpu, err = s.bucket.ListMultipartUploads(Prefix(objectName + "22"))
- c.Assert(err, IsNil)
- c.Assert(len(lmpu.Uploads), Equals, 0)
- lmpu, err = s.bucket.ListMultipartUploads(KeyMarker(objectName + "10"))
- c.Assert(err, IsNil)
- c.Assert(len(lmpu.Uploads), Equals, 17)
- lmpu, err = s.bucket.ListMultipartUploads(KeyMarker(objectName+"10"), MaxUploads(3))
- c.Assert(err, IsNil)
- c.Assert(len(lmpu.Uploads), Equals, 3)
- lmpu, err = s.bucket.ListMultipartUploads(Prefix(objectName), Delimiter("4"))
- c.Assert(err, IsNil)
- c.Assert(len(lmpu.Uploads), Equals, 18)
- c.Assert(len(lmpu.CommonPrefixes), Equals, 2)
- upLoadIDStr := RandStr(3)
- lmpu, err = s.bucket.ListMultipartUploads(KeyMarker(objectName+"12"), UploadIDMarker(upLoadIDStr))
- c.Assert(err, IsNil)
- checkNum := 15
- for _, im := range imurs {
- if im.Key == objectName+"12" && im.UploadID > upLoadIDStr {
- checkNum = 16
- break
- }
- }
- c.Assert(len(lmpu.Uploads), Equals, checkNum)
- //testLogger.Println("UploadIDMarker", lmpu.Uploads)
- for _, imur := range imurs {
- err = s.bucket.AbortMultipartUpload(imur)
- c.Assert(err, IsNil)
- }
- }
- func (s *OssBucketMultipartSuite) TestListMultipartUploadsEncodingKey(c *C) {
- prefix := objectNamePrefix + "让你任性让你狂" + RandStr(8)
- imurs := []InitiateMultipartUploadResult{}
- for i := 0; i < 3; i++ {
- imur, err := s.bucket.InitiateMultipartUpload(prefix + strconv.Itoa(i))
- c.Assert(err, IsNil)
- imurs = append(imurs, imur)
- }
- lmpu, err := s.bucket.ListMultipartUploads()
- c.Assert(err, IsNil)
- c.Assert(len(lmpu.Uploads), Equals, 3)
- lmpu, err = s.bucket.ListMultipartUploads(Prefix(prefix + "1"))
- c.Assert(err, IsNil)
- c.Assert(len(lmpu.Uploads), Equals, 1)
- lmpu, err = s.bucket.ListMultipartUploads(KeyMarker(prefix + "1"))
- c.Assert(err, IsNil)
- c.Assert(len(lmpu.Uploads), Equals, 1)
- lmpu, err = s.bucket.ListMultipartUploads(EncodingType("url"))
- c.Assert(err, IsNil)
- for i, upload := range lmpu.Uploads {
- c.Assert(upload.Key, Equals, prefix+strconv.Itoa(i))
- }
- for _, imur := range imurs {
- err = s.bucket.AbortMultipartUpload(imur)
- c.Assert(err, IsNil)
- }
- }
- func (s *OssBucketMultipartSuite) TestMultipartNegative(c *C) {
- objectName := objectNamePrefix + RandStr(8)
- // Key tool long
- data := make([]byte, 100*1024)
- imur, err := s.bucket.InitiateMultipartUpload(string(data))
- c.Assert(err, NotNil)
- // Invalid imur
- fileName := "../sample/BingWallpaper-2015-11-07.jpg"
- fd, err := os.Open(fileName)
- c.Assert(err, IsNil)
- defer fd.Close()
- _, err = s.bucket.UploadPart(imur, fd, 1024, 1)
- c.Assert(err, NotNil)
- _, err = s.bucket.UploadPartFromFile(imur, fileName, 0, 1024, 1)
- c.Assert(err, NotNil)
- _, err = s.bucket.UploadPartCopy(imur, bucketName, fileName, 0, 1024, 1)
- c.Assert(err, NotNil)
- err = s.bucket.AbortMultipartUpload(imur)
- c.Assert(err, NotNil)
- _, err = s.bucket.ListUploadedParts(imur)
- c.Assert(err, NotNil)
- // Invalid exist
- imur, err = s.bucket.InitiateMultipartUpload(objectName)
- c.Assert(err, IsNil)
- _, err = s.bucket.UploadPart(imur, fd, 1024, 1)
- c.Assert(err, IsNil)
- _, err = s.bucket.UploadPart(imur, fd, 102400, 10001)
- c.Assert(err, NotNil)
- // _, err = s.bucket.UploadPartFromFile(imur, fileName, 0, 1024, 1)
- // c.Assert(err, IsNil)
- _, err = s.bucket.UploadPartFromFile(imur, fileName, 0, 102400, 10001)
- c.Assert(err, NotNil)
- _, err = s.bucket.UploadPartCopy(imur, bucketName, fileName, 0, 1024, 1)
- c.Assert(err, NotNil)
- _, err = s.bucket.UploadPartCopy(imur, bucketName, fileName, 0, 1024, 1000)
- c.Assert(err, NotNil)
- err = s.bucket.AbortMultipartUpload(imur)
- c.Assert(err, IsNil)
- // Invalid option
- _, err = s.bucket.InitiateMultipartUpload(objectName, IfModifiedSince(futureDate))
- c.Assert(err, IsNil)
- }
- func (s *OssBucketMultipartSuite) TestMultipartUploadFromFileBigFile(c *C) {
- objectName := objectNamePrefix + RandStr(8)
- bigFile := "D:\\tmp\\bigfile.zip"
- newFile := "D:\\tmp\\newbigfile.zip"
- exist, err := isFileExist(bigFile)
- c.Assert(err, IsNil)
- if !exist {
- return
- }
- chunks, err := SplitFileByPartNum(bigFile, 64)
- c.Assert(err, IsNil)
- testLogger.Println("chunks:", chunks)
- imur, err := s.bucket.InitiateMultipartUpload(objectName)
- var parts []UploadPart
- start := GetNowSec()
- for _, chunk := range chunks {
- part, err := s.bucket.UploadPartFromFile(imur, bigFile, chunk.Offset, chunk.Size, (int)(chunk.Number))
- c.Assert(err, IsNil)
- parts = append(parts, part)
- }
- end := GetNowSec()
- testLogger.Println("Uplaod big file:", bigFile, "use sec:", end-start)
- testLogger.Println("parts:", parts)
- _, err = s.bucket.CompleteMultipartUpload(imur, parts)
- c.Assert(err, IsNil)
- start = GetNowSec()
- err = s.bucket.GetObjectToFile(objectName, newFile)
- c.Assert(err, IsNil)
- end = GetNowSec()
- testLogger.Println("Download big file:", bigFile, "use sec:", end-start)
- start = GetNowSec()
- eq, err := compareFiles(bigFile, newFile)
- c.Assert(err, IsNil)
- c.Assert(eq, Equals, true)
- end = GetNowSec()
- testLogger.Println("Compare big file:", bigFile, "use sec:", end-start)
- err = s.bucket.DeleteObject(objectName)
- c.Assert(err, IsNil)
- }
- // TestUploadFile
- func (s *OssBucketMultipartSuite) TestUploadFile(c *C) {
- objectName := objectNamePrefix + RandStr(8)
- var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
- newFile := RandStr(8) + ".jpg"
- // Upload with 100K part size
- err := s.bucket.UploadFile(objectName, fileName, 100*1024)
- c.Assert(err, IsNil)
- os.Remove(newFile)
- err = s.bucket.GetObjectToFile(objectName, newFile)
- c.Assert(err, IsNil)
- eq, err := compareFiles(fileName, newFile)
- c.Assert(err, IsNil)
- c.Assert(eq, Equals, true)
- err = s.bucket.DeleteObject(objectName)
- c.Assert(err, IsNil)
- // Upload with part size equals to 1/4 of the file size
- err = s.bucket.UploadFile(objectName, fileName, 482048/4)
- c.Assert(err, IsNil)
- os.Remove(newFile)
- err = s.bucket.GetObjectToFile(objectName, newFile)
- c.Assert(err, IsNil)
- eq, err = compareFiles(fileName, newFile)
- c.Assert(err, IsNil)
- c.Assert(eq, Equals, true)
- err = s.bucket.DeleteObject(objectName)
- c.Assert(err, IsNil)
- // Upload with part size equals to the file size
- err = s.bucket.UploadFile(objectName, fileName, 482048)
- c.Assert(err, IsNil)
- os.Remove(newFile)
- err = s.bucket.GetObjectToFile(objectName, newFile)
- c.Assert(err, IsNil)
- eq, err = compareFiles(fileName, newFile)
- c.Assert(err, IsNil)
- c.Assert(eq, Equals, true)
- err = s.bucket.DeleteObject(objectName)
- c.Assert(err, IsNil)
- // Upload with part size is bigger than the file size
- err = s.bucket.UploadFile(objectName, fileName, 482049)
- c.Assert(err, IsNil)
- os.Remove(newFile)
- err = s.bucket.GetObjectToFile(objectName, newFile)
- c.Assert(err, IsNil)
- eq, err = compareFiles(fileName, newFile)
- c.Assert(err, IsNil)
- c.Assert(eq, Equals, true)
- err = s.bucket.DeleteObject(objectName)
- c.Assert(err, IsNil)
- // Option
- options := []Option{
- Expires(futureDate),
- ObjectACL(ACLPublicRead),
- Meta("myprop", "mypropval")}
- err = s.bucket.UploadFile(objectName, fileName, 482049, options...)
- c.Assert(err, IsNil)
- // Check
- os.Remove(newFile)
- err = s.bucket.GetObjectToFile(objectName, newFile)
- c.Assert(err, IsNil)
- eq, err = compareFiles(fileName, newFile)
- c.Assert(err, IsNil)
- c.Assert(eq, Equals, true)
- acl, err := s.bucket.GetObjectACL(objectName)
- c.Assert(err, IsNil)
- testLogger.Println("GetObjectAcl:", acl)
- c.Assert(acl.ACL, Equals, "public-read")
- meta, err := s.bucket.GetObjectDetailedMeta(objectName)
- c.Assert(err, IsNil)
- testLogger.Println("GetObjectDetailedMeta:", meta)
- c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
- }
- func (s *OssBucketMultipartSuite) TestUploadFileNegative(c *C) {
- objectName := objectNamePrefix + RandStr(8)
- var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
- // Smaller than the required minimal part size (100KB)
- err := s.bucket.UploadFile(objectName, fileName, 100*1024-1)
- c.Assert(err, NotNil)
- // Bigger than the max part size (5G)
- err = s.bucket.UploadFile(objectName, fileName, 1024*1024*1024*5+1)
- c.Assert(err, NotNil)
- // File does not exist
- err = s.bucket.UploadFile(objectName, "/root1/123abc9874", 1024*1024*1024)
- c.Assert(err, NotNil)
- // Invalid key , key is empty.
- err = s.bucket.UploadFile("", fileName, 100*1024)
- c.Assert(err, NotNil)
- }
- // TestDownloadFile
- func (s *OssBucketMultipartSuite) TestDownloadFile(c *C) {
- objectName := objectNamePrefix + RandStr(8)
- var fileName = "../sample/BingWallpaper-2015-11-07.jpg"
- newFile := RandStr(8) + ".jpg"
- err := s.bucket.UploadFile(objectName, fileName, 100*1024)
- c.Assert(err, IsNil)
- // Download file with part size of 100K
- err = s.bucket.DownloadFile(objectName, newFile, 100*1024)
- c.Assert(err, IsNil)
- os.Remove(newFile)
- err = s.bucket.GetObjectToFile(objectName, newFile)
- c.Assert(err, IsNil)
- eq, err := compareFiles(fileName, newFile)
- c.Assert(err, IsNil)
- c.Assert(eq, Equals, true)
- // Download the file with part size equals to 1/4 of the file size
- err = s.bucket.DownloadFile(objectName, newFile, 482048/4)
- c.Assert(err, IsNil)
- os.Remove(newFile)
- err = s.bucket.GetObjectToFile(objectName, newFile)
- c.Assert(err, IsNil)
- eq, err = compareFiles(fileName, newFile)
- c.Assert(err, IsNil)
- c.Assert(eq, Equals, true)
- // Download the file with part size same as the file size
- err = s.bucket.DownloadFile(objectName, newFile, 482048)
- c.Assert(err, IsNil)
- os.Remove(newFile)
- err = s.bucket.GetObjectToFile(objectName, newFile)
- c.Assert(err, IsNil)
- eq, err = compareFiles(fileName, newFile)
- c.Assert(err, IsNil)
- c.Assert(eq, Equals, true)
- // Download the file with part size bigger than the file size
- err = s.bucket.DownloadFile(objectName, newFile, 482049)
- c.Assert(err, IsNil)
- os.Remove(newFile)
- err = s.bucket.GetObjectToFile(objectName, newFile)
- c.Assert(err, IsNil)
- eq, err = compareFiles(fileName, newFile)
- c.Assert(err, IsNil)
- c.Assert(eq, Equals, true)
- // Option
- meta, err := s.bucket.GetObjectDetailedMeta(objectName)
- c.Assert(err, IsNil)
- testLogger.Println("GetObjectDetailedMeta:", meta)
- // If-Match
- err = s.bucket.DownloadFile(objectName, newFile, 482048/4, IfMatch(meta.Get("Etag")))
- c.Assert(err, IsNil)
- os.Remove(newFile)
- err = s.bucket.GetObjectToFile(objectName, newFile)
- c.Assert(err, IsNil)
- eq, err = compareFiles(fileName, newFile)
- c.Assert(err, IsNil)
- c.Assert(eq, Equals, true)
- // If-None-Match
- err = s.bucket.DownloadFile(objectName, newFile, 482048, IfNoneMatch(meta.Get("Etag")))
- c.Assert(err, NotNil)
- os.Remove(newFile)
- err = s.bucket.DeleteObject(objectName)
- c.Assert(err, IsNil)
- }
- func (s *OssBucketMultipartSuite) TestDownloadFileNegative(c *C) {
- objectName := objectNamePrefix + RandStr(8)
- newFile := RandStr(8) + ".jpg"
- // Smaller than the required minimal part size (100KB)
- err := s.bucket.DownloadFile(objectName, newFile, 100*1024-1)
- c.Assert(err, NotNil)
- // Bigger than the required max part size (5G)
- err = s.bucket.DownloadFile(objectName, newFile, 1024*1024*1024+1)
- c.Assert(err, NotNil)
- // File does not exist
- err = s.bucket.DownloadFile(objectName, "/OSS/TEMP/ZIBI/QUQU/BALA", 1024*1024*1024+1)
- c.Assert(err, NotNil)
- // Key does not exist
- err = s.bucket.DownloadFile(objectName, newFile, 100*1024)
- c.Assert(err, NotNil)
- }
- // Private
- func shuffleArray(chunks []FileChunk) []FileChunk {
- for i := range chunks {
- j := rand.Intn(i + 1)
- chunks[i], chunks[j] = chunks[j], chunks[i]
- }
- return chunks
- }
|