123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- package oss
- import (
- "fmt"
- "io"
- "os"
- "time"
- . "gopkg.in/check.v1"
- )
- type OssUploadSuite struct {
- client *Client
- bucket *Bucket
- }
- var _ = Suite(&OssUploadSuite{})
- // SetUpSuite runs once when the suite starts running
- func (s *OssUploadSuite) SetUpSuite(c *C) {
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- s.client = client
- s.client.CreateBucket(bucketName)
- time.Sleep(5 * time.Second)
- bucket, err := s.client.Bucket(bucketName)
- c.Assert(err, IsNil)
- s.bucket = bucket
- testLogger.Println("test upload started")
- }
- // TearDownSuite runs before each test or benchmark starts running
- func (s *OssUploadSuite) TearDownSuite(c *C) {
- // Delete part
- lmur, err := s.bucket.ListMultipartUploads()
- 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)
- }
- // Delete objects
- lor, err := s.bucket.ListObjects()
- c.Assert(err, IsNil)
- for _, object := range lor.Objects {
- err = s.bucket.DeleteObject(object.Key)
- c.Assert(err, IsNil)
- }
- testLogger.Println("test upload completed")
- }
- // SetUpTest runs after each test or benchmark runs
- func (s *OssUploadSuite) SetUpTest(c *C) {
- err := removeTempFiles("../oss", ".jpg")
- c.Assert(err, IsNil)
- }
- // TearDownTest runs once after all tests or benchmarks have finished running
- func (s *OssUploadSuite) TearDownTest(c *C) {
- err := removeTempFiles("../oss", ".jpg")
- c.Assert(err, IsNil)
- }
- // TestUploadRoutineWithoutRecovery tests multiroutineed upload without checkpoint
- func (s *OssUploadSuite) TestUploadRoutineWithoutRecovery(c *C) {
- objectName := objectNamePrefix + "turwr"
- fileName := "../sample/BingWallpaper-2015-11-07.jpg"
- newFile := "upload-new-file.jpg"
- // Routines is not specified, by default single routine
- 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)
- // Specify routine count as 1
- err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(1))
- 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)
- // Specify routine count as 3, which is smaller than parts count 5
- err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
- 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)
- // Specify routine count as 5, which is same as the part count 5
- err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(5))
- 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)
- // Specify routine count as 10, which is bigger than the part count 5.
- err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(10))
- 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)
- // Invalid routine count, it will use 1 automatically.
- err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(0))
- 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)
- // Invalid routine count, it will use 1 automatically
- err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(-1))
- 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
- err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), Meta("myprop", "mypropval"))
- meta, err := s.bucket.GetObjectDetailedMeta(objectName)
- c.Assert(err, IsNil)
- c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
- 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)
- }
- // ErrorHooker is a UploadPart hook---it will fail the 5th part's upload.
- func ErrorHooker(id int, chunk FileChunk) error {
- if chunk.Number == 5 {
- time.Sleep(time.Second)
- return fmt.Errorf("ErrorHooker")
- }
- return nil
- }
- // TestUploadRoutineWithoutRecoveryNegative is multiroutineed upload without checkpoint
- func (s *OssUploadSuite) TestUploadRoutineWithoutRecoveryNegative(c *C) {
- objectName := objectNamePrefix + "turwrn"
- fileName := "../sample/BingWallpaper-2015-11-07.jpg"
- uploadPartHooker = ErrorHooker
- // Worker routine error
- err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(2))
- c.Assert(err, NotNil)
- c.Assert(err.Error(), Equals, "ErrorHooker")
- uploadPartHooker = defaultUploadPart
- // Local file does not exist
- err = s.bucket.UploadFile(objectName, "NotExist", 100*1024, Routines(2))
- c.Assert(err, NotNil)
- // The part size is invalid
- err = s.bucket.UploadFile(objectName, fileName, 1024, Routines(2))
- c.Assert(err, NotNil)
- err = s.bucket.UploadFile(objectName, fileName, 1024*1024*1024*100, Routines(2))
- c.Assert(err, NotNil)
- }
- // TestUploadRoutineWithRecovery is multi-routine upload with resumable recovery
- func (s *OssUploadSuite) TestUploadRoutineWithRecovery(c *C) {
- objectName := objectNamePrefix + "turtr"
- fileName := "../sample/BingWallpaper-2015-11-07.jpg"
- newFile := "upload-new-file-2.jpg"
- // Use default routines and default CP file path (fileName+.cp)
- // First upload for 4 parts
- uploadPartHooker = ErrorHooker
- err := s.bucket.UploadFile(objectName, fileName, 100*1024, Checkpoint(true, fileName+".cp"))
- c.Assert(err, NotNil)
- c.Assert(err.Error(), Equals, "ErrorHooker")
- uploadPartHooker = defaultUploadPart
- // Check CP
- ucp := uploadCheckpoint{}
- err = ucp.load(fileName + ".cp")
- c.Assert(err, IsNil)
- c.Assert(ucp.Magic, Equals, uploadCpMagic)
- c.Assert(len(ucp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
- c.Assert(ucp.FilePath, Equals, fileName)
- c.Assert(ucp.FileStat.Size, Equals, int64(482048))
- c.Assert(len(ucp.FileStat.LastModified.String()) > 0, Equals, true)
- c.Assert(ucp.FileStat.MD5, Equals, "")
- c.Assert(ucp.ObjectKey, Equals, objectName)
- c.Assert(len(ucp.UploadID), Equals, len("3F79722737D1469980DACEDCA325BB52"))
- c.Assert(len(ucp.Parts), Equals, 5)
- c.Assert(len(ucp.todoParts()), Equals, 1)
- c.Assert(len(ucp.allParts()), Equals, 5)
- // Second upload, finish the remaining part
- err = s.bucket.UploadFile(objectName, fileName, 100*1024, Checkpoint(true, fileName+".cp"))
- 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)
- err = ucp.load(fileName + ".cp")
- c.Assert(err, NotNil)
- // Resumable upload with empty checkpoint path
- uploadPartHooker = ErrorHooker
- err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), CheckpointDir(true, ""))
- c.Assert(err, NotNil)
- c.Assert(err.Error(), Equals, "ErrorHooker")
- uploadPartHooker = defaultUploadPart
- ucp = uploadCheckpoint{}
- err = ucp.load(fileName + ".cp")
- c.Assert(err, NotNil)
- // Resumable upload with checkpoint dir
- uploadPartHooker = ErrorHooker
- err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), CheckpointDir(true, "./"))
- c.Assert(err, NotNil)
- c.Assert(err.Error(), Equals, "ErrorHooker")
- uploadPartHooker = defaultUploadPart
- // Check CP
- ucp = uploadCheckpoint{}
- cpConf := cpConfig{IsEnable: true, DirPath: "./"}
- cpFilePath := getUploadCpFilePath(&cpConf, fileName, s.bucket.BucketName, objectName)
- err = ucp.load(cpFilePath)
- c.Assert(err, IsNil)
- c.Assert(ucp.Magic, Equals, uploadCpMagic)
- c.Assert(len(ucp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
- c.Assert(ucp.FilePath, Equals, fileName)
- c.Assert(ucp.FileStat.Size, Equals, int64(482048))
- c.Assert(len(ucp.FileStat.LastModified.String()) > 0, Equals, true)
- c.Assert(ucp.FileStat.MD5, Equals, "")
- c.Assert(ucp.ObjectKey, Equals, objectName)
- c.Assert(len(ucp.UploadID), Equals, len("3F79722737D1469980DACEDCA325BB52"))
- c.Assert(len(ucp.Parts), Equals, 5)
- c.Assert(len(ucp.todoParts()), Equals, 1)
- c.Assert(len(ucp.allParts()), Equals, 5)
- err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), CheckpointDir(true, "./"))
- 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)
- err = ucp.load(cpFilePath)
- c.Assert(err, NotNil)
- // Upload all 5 parts without error
- err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), Checkpoint(true, objectName+".cp"))
- 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 all 5 parts with 10 routines without error
- err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(10), Checkpoint(true, objectName+".cp"))
- 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
- err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), Checkpoint(true, objectName+".cp"), Meta("myprop", "mypropval"))
- meta, err := s.bucket.GetObjectDetailedMeta(objectName)
- c.Assert(err, IsNil)
- c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
- 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)
- }
- // TestUploadRoutineWithRecoveryNegative is multiroutineed upload without checkpoint
- func (s *OssUploadSuite) TestUploadRoutineWithRecoveryNegative(c *C) {
- objectName := objectNamePrefix + "turrn"
- fileName := "../sample/BingWallpaper-2015-11-07.jpg"
- // The local file does not exist
- err := s.bucket.UploadFile(objectName, "NotExist", 100*1024, Checkpoint(true, "NotExist.cp"))
- c.Assert(err, NotNil)
- err = s.bucket.UploadFile(objectName, "NotExist", 100*1024, Routines(2), Checkpoint(true, "NotExist.cp"))
- c.Assert(err, NotNil)
- // Specified part size is invalid
- err = s.bucket.UploadFile(objectName, fileName, 1024, Checkpoint(true, fileName+".cp"))
- c.Assert(err, NotNil)
- err = s.bucket.UploadFile(objectName, fileName, 1024, Routines(2), Checkpoint(true, fileName+".cp"))
- c.Assert(err, NotNil)
- err = s.bucket.UploadFile(objectName, fileName, 1024*1024*1024*100, Checkpoint(true, fileName+".cp"))
- c.Assert(err, NotNil)
- err = s.bucket.UploadFile(objectName, fileName, 1024*1024*1024*100, Routines(2), Checkpoint(true, fileName+".cp"))
- c.Assert(err, NotNil)
- }
- // TestUploadLocalFileChange tests the file is updated while being uploaded
- func (s *OssUploadSuite) TestUploadLocalFileChange(c *C) {
- objectName := objectNamePrefix + "tulfc"
- fileName := "../sample/BingWallpaper-2015-11-07.jpg"
- localFile := "BingWallpaper-2015-11-07.jpg"
- newFile := "upload-new-file-3.jpg"
- os.Remove(localFile)
- err := copyFile(fileName, localFile)
- c.Assert(err, IsNil)
- // First upload for 4 parts
- uploadPartHooker = ErrorHooker
- err = s.bucket.UploadFile(objectName, localFile, 100*1024, Checkpoint(true, localFile+".cp"))
- c.Assert(err, NotNil)
- c.Assert(err.Error(), Equals, "ErrorHooker")
- uploadPartHooker = defaultUploadPart
- os.Remove(localFile)
- err = copyFile(fileName, localFile)
- c.Assert(err, IsNil)
- // Updating the file. The second upload will re-upload all 5 parts.
- err = s.bucket.UploadFile(objectName, localFile, 100*1024, Checkpoint(true, localFile+".cp"))
- 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)
- }
- func copyFile(src, dst string) error {
- srcFile, err := os.Open(src)
- if err != nil {
- return err
- }
- defer srcFile.Close()
- dstFile, err := os.Create(dst)
- if err != nil {
- return err
- }
- defer dstFile.Close()
- _, err = io.Copy(dstFile, srcFile)
- return err
- }
|