14 type OssDownloadSuite struct {
19 var _ = Suite(&OssDownloadSuite{})
21 // SetUpSuite runs once when the suite starts running
22 func (s *OssDownloadSuite) SetUpSuite(c *C) {
23 client, err := New(endpoint, accessID, accessKey)
27 s.client.CreateBucket(bucketName)
29 bucket, err := s.client.Bucket(bucketName)
33 testLogger.Println("test download started")
36 // TearDownSuite runs before each test or benchmark starts running
37 func (s *OssDownloadSuite) TearDownSuite(c *C) {
39 keyMarker := KeyMarker("")
40 uploadIDMarker := UploadIDMarker("")
42 lmur, err := s.bucket.ListMultipartUploads(keyMarker, uploadIDMarker)
44 for _, upload := range lmur.Uploads {
45 var imur = InitiateMultipartUploadResult{Bucket: s.bucket.BucketName,
46 Key: upload.Key, UploadID: upload.UploadID}
47 err = s.bucket.AbortMultipartUpload(imur)
50 keyMarker = KeyMarker(lmur.NextKeyMarker)
51 uploadIDMarker = UploadIDMarker(lmur.NextUploadIDMarker)
52 if !lmur.IsTruncated {
60 lor, err := s.bucket.ListObjects(marker)
62 for _, object := range lor.Objects {
63 err = s.bucket.DeleteObject(object.Key)
66 marker = Marker(lor.NextMarker)
73 err := s.client.DeleteBucket(s.bucket.BucketName)
76 testLogger.Println("test download completed")
79 // SetUpTest runs after each test or benchmark runs
80 func (s *OssDownloadSuite) SetUpTest(c *C) {
81 err := removeTempFiles("../oss", ".jpg")
85 // TearDownTest runs once after all tests or benchmarks have finished running
86 func (s *OssDownloadSuite) TearDownTest(c *C) {
87 err := removeTempFiles("../oss", ".jpg")
90 err = removeTempFiles("../oss", ".temp")
94 // TestDownloadRoutineWithoutRecovery multipart downloads without checkpoint
95 func (s *OssDownloadSuite) TestDownloadRoutineWithoutRecovery(c *C) {
96 objectName := objectNamePrefix + RandStr(8)
97 fileName := "../sample/BingWallpaper-2015-11-07.jpg"
98 newFile := RandStr(8) + ".jpg"
101 err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
104 // Download the file by default
105 err = s.bucket.DownloadFile(objectName, newFile, 100*1024)
109 eq, err := compareFiles(fileName, newFile)
111 c.Assert(eq, Equals, true)
113 // Use 2 coroutines to download the file and total parts count is 5
115 err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(2))
119 eq, err = compareFiles(fileName, newFile)
121 c.Assert(eq, Equals, true)
123 // Use 5 coroutines to download the file and the total parts count is 5.
125 err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(5))
129 eq, err = compareFiles(fileName, newFile)
131 c.Assert(eq, Equals, true)
133 // Use 10 coroutines to download the file and the total parts count is 5.
135 err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(10))
139 eq, err = compareFiles(fileName, newFile)
141 c.Assert(eq, Equals, true)
143 err = s.bucket.DeleteObject(objectName)
147 // DownErrorHooker requests hook by downloadPart
148 func DownErrorHooker(part downloadPart) error {
150 time.Sleep(time.Second)
151 return fmt.Errorf("ErrorHooker")
156 // TestDownloadRoutineWithRecovery multi-routine resumable download
157 func (s *OssDownloadSuite) TestDownloadRoutineWithRecovery(c *C) {
158 objectName := objectNamePrefix + RandStr(8)
159 fileName := "../sample/BingWallpaper-2015-11-07.jpg"
160 newFile := RandStr(8) + ".jpg"
163 err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
166 // Download a file with default checkpoint
167 downloadPartHooker = DownErrorHooker
168 err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, newFile+".cp"))
169 c.Assert(err, NotNil)
170 c.Assert(err.Error(), Equals, "ErrorHooker")
171 downloadPartHooker = defaultDownloadPartHook
174 dcp := downloadCheckpoint{}
175 err = dcp.load(newFile + ".cp")
177 c.Assert(dcp.Magic, Equals, downloadCpMagic)
178 c.Assert(len(dcp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
179 c.Assert(dcp.FilePath, Equals, newFile)
180 c.Assert(dcp.ObjStat.Size, Equals, int64(482048))
181 c.Assert(len(dcp.ObjStat.LastModified) > 0, Equals, true)
182 c.Assert(dcp.ObjStat.Etag, Equals, "\"2351E662233817A7AE974D8C5B0876DD-5\"")
183 c.Assert(dcp.Object, Equals, objectName)
184 c.Assert(len(dcp.Parts), Equals, 5)
185 c.Assert(len(dcp.todoParts()), Equals, 1)
187 err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, newFile+".cp"))
189 //download success, checkpoint file has been deleted
190 err = dcp.load(newFile + ".cp")
191 c.Assert(err, NotNil)
193 eq, err := compareFiles(fileName, newFile)
195 c.Assert(eq, Equals, true)
197 // Resumable download with empty checkpoint file path
198 downloadPartHooker = DownErrorHooker
199 err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, ""))
200 c.Assert(err, NotNil)
201 c.Assert(err.Error(), Equals, "ErrorHooker")
202 downloadPartHooker = defaultDownloadPartHook
204 dcp = downloadCheckpoint{}
205 err = dcp.load(newFile + ".cp")
206 c.Assert(err, NotNil)
208 // Resumable download with checkpoint dir
210 downloadPartHooker = DownErrorHooker
211 err = s.bucket.DownloadFile(objectName, newFile, 100*1024, CheckpointDir(true, "./"))
212 c.Assert(err, NotNil)
213 c.Assert(err.Error(), Equals, "ErrorHooker")
214 downloadPartHooker = defaultDownloadPartHook
217 dcp = downloadCheckpoint{}
218 cpConf := cpConfig{IsEnable: true, DirPath: "./"}
219 cpFilePath := getDownloadCpFilePath(&cpConf, s.bucket.BucketName, objectName, "",newFile)
220 err = dcp.load(cpFilePath)
222 c.Assert(dcp.Magic, Equals, downloadCpMagic)
223 c.Assert(len(dcp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
224 c.Assert(dcp.FilePath, Equals, newFile)
225 c.Assert(dcp.ObjStat.Size, Equals, int64(482048))
226 c.Assert(len(dcp.ObjStat.LastModified) > 0, Equals, true)
227 c.Assert(dcp.ObjStat.Etag, Equals, "\"2351E662233817A7AE974D8C5B0876DD-5\"")
228 c.Assert(dcp.Object, Equals, objectName)
229 c.Assert(len(dcp.Parts), Equals, 5)
230 c.Assert(len(dcp.todoParts()), Equals, 1)
232 err = s.bucket.DownloadFile(objectName, newFile, 100*1024, CheckpointDir(true, "./"))
234 //download success, checkpoint file has been deleted
235 err = dcp.load(cpFilePath)
236 c.Assert(err, NotNil)
238 eq, err = compareFiles(fileName, newFile)
240 c.Assert(eq, Equals, true)
242 // Resumable download with checkpoint at a time. No error is expected in the download procedure.
244 err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, newFile+".cp"))
247 err = dcp.load(newFile + ".cp")
248 c.Assert(err, NotNil)
250 eq, err = compareFiles(fileName, newFile)
252 c.Assert(eq, Equals, true)
254 // Resumable download with checkpoint at a time. No error is expected in the download procedure.
256 err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(10), Checkpoint(true, newFile+".cp"))
259 err = dcp.load(newFile + ".cp")
260 c.Assert(err, NotNil)
262 eq, err = compareFiles(fileName, newFile)
264 c.Assert(eq, Equals, true)
266 err = s.bucket.DeleteObject(objectName)
270 // TestDownloadOption options
271 func (s *OssDownloadSuite) TestDownloadOption(c *C) {
272 objectName := objectNamePrefix + RandStr(8)
273 fileName := "../sample/BingWallpaper-2015-11-07.jpg"
274 newFile := RandStr(8) + ".jpg"
277 err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
280 meta, err := s.bucket.GetObjectDetailedMeta(objectName)
285 err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), IfMatch(meta.Get("Etag")))
288 eq, err := compareFiles(fileName, newFile)
290 c.Assert(eq, Equals, true)
294 err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), IfNoneMatch(meta.Get("Etag")))
295 c.Assert(err, NotNil)
298 err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), IfMatch(meta.Get("Etag")))
301 eq, err = compareFiles(fileName, newFile)
303 c.Assert(eq, Equals, true)
306 err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), IfNoneMatch(meta.Get("Etag")))
307 c.Assert(err, NotNil)
310 // TestDownloadObjectChange tests the file is updated during the upload
311 func (s *OssDownloadSuite) TestDownloadObjectChange(c *C) {
312 objectName := objectNamePrefix + RandStr(8)
313 fileName := "../sample/BingWallpaper-2015-11-07.jpg"
314 newFile := RandStr(8) + ".jpg"
317 err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
320 // Download with default checkpoint
321 downloadPartHooker = DownErrorHooker
322 err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, newFile+".cp"))
323 c.Assert(err, NotNil)
324 c.Assert(err.Error(), Equals, "ErrorHooker")
325 downloadPartHooker = defaultDownloadPartHook
327 err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
330 err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, newFile+".cp"))
333 eq, err := compareFiles(fileName, newFile)
335 c.Assert(eq, Equals, true)
338 // TestDownloadNegative tests downloading negative
339 func (s *OssDownloadSuite) TestDownloadNegative(c *C) {
340 objectName := objectNamePrefix + RandStr(8)
341 fileName := "../sample/BingWallpaper-2015-11-07.jpg"
342 newFile := RandStr(8) + ".jpg"
345 err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
348 // Worker routine error
349 downloadPartHooker = DownErrorHooker
350 err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(2))
351 c.Assert(err, NotNil)
352 c.Assert(err.Error(), Equals, "ErrorHooker")
353 downloadPartHooker = defaultDownloadPartHook
355 // Local file does not exist
356 err = s.bucket.DownloadFile(objectName, "/tmp/", 100*1024, Routines(2))
357 c.Assert(err, NotNil)
360 err = s.bucket.DownloadFile(objectName, newFile, 0, Routines(2))
361 c.Assert(err, NotNil)
363 err = s.bucket.DownloadFile(objectName, newFile, 1024*1024*1024*100, Routines(2))
366 err = s.bucket.DeleteObject(objectName)
369 // Local file does not exist
370 err = s.bucket.DownloadFile(objectName, "/tmp/", 100*1024)
371 c.Assert(err, NotNil)
373 err = s.bucket.DownloadFile(objectName, "/tmp/", 100*1024, Routines(2))
374 c.Assert(err, NotNil)
377 err = s.bucket.DownloadFile(objectName, newFile, -1)
378 c.Assert(err, NotNil)
380 err = s.bucket.DownloadFile(objectName, newFile, 0, Routines(2))
381 c.Assert(err, NotNil)
383 err = s.bucket.DownloadFile(objectName, newFile, 1024*1024*1024*100)
384 c.Assert(err, NotNil)
386 err = s.bucket.DownloadFile(objectName, newFile, 1024*1024*1024*100, Routines(2))
387 c.Assert(err, NotNil)
390 // TestDownloadWithRange tests concurrent downloading with range specified and checkpoint enabled
391 func (s *OssDownloadSuite) TestDownloadWithRange(c *C) {
392 objectName := objectNamePrefix + RandStr(8)
393 fileName := "../sample/BingWallpaper-2015-11-07.jpg"
394 newFile := RandStr(8) + ".jpg"
395 newFileGet := RandStr(8) + "-.jpg"
398 err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
401 fileSize, err := getFileSize(fileName)
404 // Download with range, from 1024 to 4096
406 err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), Range(1024, 4095))
410 eq, err := compareFilesWithRange(fileName, 1024, newFile, 0, 3072)
412 c.Assert(eq, Equals, true)
414 os.Remove(newFileGet)
415 err = s.bucket.GetObjectToFile(objectName, newFileGet, Range(1024, 4095))
418 // Compare get and download
419 eq, err = compareFiles(newFile, newFileGet)
421 c.Assert(eq, Equals, true)
423 // Download with range, from 1024 to 4096
425 err = s.bucket.DownloadFile(objectName, newFile, 1024, Routines(3), NormalizedRange("1024-4095"))
429 eq, err = compareFilesWithRange(fileName, 1024, newFile, 0, 3072)
431 c.Assert(eq, Equals, true)
433 os.Remove(newFileGet)
434 err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("1024-4095"))
437 // Compare get and download
438 eq, err = compareFiles(newFile, newFileGet)
440 c.Assert(eq, Equals, true)
442 // Download with range, from 2048 to the end
444 err = s.bucket.DownloadFile(objectName, newFile, 1024*1024, Routines(3), NormalizedRange("2048-"))
448 eq, err = compareFilesWithRange(fileName, 2048, newFile, 0, fileSize-2048)
450 c.Assert(eq, Equals, true)
452 os.Remove(newFileGet)
453 err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("2048-"))
456 // Compare get and download
457 eq, err = compareFiles(newFile, newFileGet)
459 c.Assert(eq, Equals, true)
461 // Download with range, the last 4096
463 err = s.bucket.DownloadFile(objectName, newFile, 1024, Routines(3), NormalizedRange("-4096"))
467 eq, err = compareFilesWithRange(fileName, fileSize-4096, newFile, 0, 4096)
469 c.Assert(eq, Equals, true)
471 os.Remove(newFileGet)
472 err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("-4096"))
475 // Compare get and download
476 eq, err = compareFiles(newFile, newFileGet)
478 c.Assert(eq, Equals, true)
480 err = s.bucket.DeleteObject(objectName)
484 // TestDownloadWithCheckoutAndRange tests concurrent downloading with range specified and checkpoint enabled
485 func (s *OssDownloadSuite) TestDownloadWithCheckoutAndRange(c *C) {
486 objectName := objectNamePrefix + RandStr(8)
487 fileName := "../sample/BingWallpaper-2015-11-07.jpg"
488 newFile := RandStr(8) + ".jpg"
489 newFileGet := RandStr(8) + "-get.jpg"
492 err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), Checkpoint(true, fileName+".cp"))
495 fileSize, err := getFileSize(fileName)
498 // Download with range, from 1024 to 4096
500 err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), Checkpoint(true, newFile+".cp"), Range(1024, 4095))
504 eq, err := compareFilesWithRange(fileName, 1024, newFile, 0, 3072)
506 c.Assert(eq, Equals, true)
508 os.Remove(newFileGet)
509 err = s.bucket.GetObjectToFile(objectName, newFileGet, Range(1024, 4095))
512 // Compare get and download
513 eq, err = compareFiles(newFile, newFileGet)
515 c.Assert(eq, Equals, true)
517 // Download with range, from 1024 to 4096
519 err = s.bucket.DownloadFile(objectName, newFile, 1024, Routines(3), Checkpoint(true, newFile+".cp"), NormalizedRange("1024-4095"))
523 eq, err = compareFilesWithRange(fileName, 1024, newFile, 0, 3072)
525 c.Assert(eq, Equals, true)
527 os.Remove(newFileGet)
528 err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("1024-4095"))
531 // Compare get and download
532 eq, err = compareFiles(newFile, newFileGet)
534 c.Assert(eq, Equals, true)
536 // Download with range, from 2048 to the end
538 err = s.bucket.DownloadFile(objectName, newFile, 1024*1024, Routines(3), Checkpoint(true, newFile+".cp"), NormalizedRange("2048-"))
542 eq, err = compareFilesWithRange(fileName, 2048, newFile, 0, fileSize-2048)
544 c.Assert(eq, Equals, true)
546 os.Remove(newFileGet)
547 err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("2048-"))
550 // Compare get and download
551 eq, err = compareFiles(newFile, newFileGet)
553 c.Assert(eq, Equals, true)
555 // Download with range, the last 4096 bytes
557 err = s.bucket.DownloadFile(objectName, newFile, 1024, Routines(3), Checkpoint(true, newFile+".cp"), NormalizedRange("-4096"))
561 eq, err = compareFilesWithRange(fileName, fileSize-4096, newFile, 0, 4096)
563 c.Assert(eq, Equals, true)
565 os.Remove(newFileGet)
566 err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("-4096"))
569 // Compare get and download
570 eq, err = compareFiles(newFile, newFileGet)
572 c.Assert(eq, Equals, true)
574 err = s.bucket.DeleteObject(objectName)
578 // TestCombineCRCInDownloadParts tests combineCRCInParts
579 func (s *OssDownloadSuite) TestCombineCRCInDownloadParts(c *C) {
580 crc := combineCRCInParts(nil)
581 c.Assert(crc == 0, Equals, true)
583 crc = combineCRCInParts(make([]downloadPart, 0))
584 c.Assert(crc == 0, Equals, true)
586 parts := make([]downloadPart, 1)
587 parts[0].CRC64 = 10278880121275185425
588 crc = combineCRCInParts(parts)
589 c.Assert(crc == 10278880121275185425, Equals, true)
591 parts = make([]downloadPart, 2)
592 parts[0].CRC64 = 6748440630437108969
595 parts[1].CRC64 = 10278880121275185425
598 crc = combineCRCInParts(parts)
599 c.Assert(crc == 11051210869376104954, Equals, true)
602 func getFileSize(fileName string) (int64, error) {
603 file, err := os.Open(fileName)
609 stat, err := file.Stat()
614 return stat.Size(), nil
617 // compareFilesWithRange compares the content between fileL and fileR with specified range
618 func compareFilesWithRange(fileL string, offsetL int64, fileR string, offsetR int64, size int64) (bool, error) {
619 finL, err := os.Open(fileL)
624 finL.Seek(offsetL, os.SEEK_SET)
626 finR, err := os.Open(fileR)
631 finR.Seek(offsetR, os.SEEK_SET)
633 statL, err := finL.Stat()
638 statR, err := finR.Stat()
643 if (offsetL+size > statL.Size()) || (offsetR+size > statR.Size()) {
647 part := statL.Size() - offsetL
652 bufL := make([]byte, part)
653 bufR := make([]byte, part)
654 for readN := int64(0); readN < size; {
655 n, _ := finL.Read(bufL)
660 n, _ = finR.Read(bufR)
666 if tailer > size-readN {
667 tailer = size - readN
671 if !bytes.Equal(bufL[0:tailer], bufR[0:tailer]) {
679 func (s *OssDownloadSuite) TestVersioningDownloadWithoutCheckPoint(c *C) {
680 // create a bucket with default proprety
681 client, err := New(endpoint, accessID, accessKey)
684 bucketName := bucketNamePrefix + RandLowStr(6)
685 err = client.CreateBucket(bucketName)
688 bucket, err := client.Bucket(bucketName)
690 // put bucket version:enabled
691 var versioningConfig VersioningConfig
692 versioningConfig.Status = string(VersionEnabled)
693 err = client.SetBucketVersioning(bucketName, versioningConfig)
697 objectName := objectNamePrefix + RandStr(8)
698 fileName := "test-file-" + RandStr(8)
699 fileData := RandStr(500 * 1024)
700 CreateFile(fileName, fileData, c)
702 newFile := RandStr(8) + ".jpg"
703 newFileGet := RandStr(8) + "-.jpg"
706 var respHeader http.Header
707 options := []Option{Routines(3), GetResponseHeader(&respHeader)}
708 err = bucket.UploadFile(objectName, fileName, 100*1024, options...)
710 versionId := GetVersionId(respHeader)
711 c.Assert(len(versionId) > 0, Equals, true)
713 fileSize, err := getFileSize(fileName)
716 // overwrite emtpy object
717 err = bucket.PutObject(objectName, strings.NewReader(""))
720 // Download with range, from 1024 to 4096
722 options = []Option{Routines(3), Range(1024, 4095), VersionId(versionId)}
723 err = bucket.DownloadFile(objectName, newFile, 100*1024, options...)
727 eq, err := compareFilesWithRange(fileName, 1024, newFile, 0, 3072)
729 c.Assert(eq, Equals, true)
731 os.Remove(newFileGet)
732 options = []Option{Range(1024, 4095), VersionId(versionId)}
733 err = bucket.GetObjectToFile(objectName, newFileGet, options...)
736 // Compare get and download
737 eq, err = compareFiles(newFile, newFileGet)
739 c.Assert(eq, Equals, true)
741 // Download with range, from 1024 to 4096
743 options = []Option{Routines(3), NormalizedRange("1024-4095"), VersionId(versionId)}
744 err = bucket.DownloadFile(objectName, newFile, 1024, options...)
748 eq, err = compareFilesWithRange(fileName, 1024, newFile, 0, 3072)
750 c.Assert(eq, Equals, true)
752 os.Remove(newFileGet)
753 options = []Option{NormalizedRange("1024-4095"), VersionId(versionId)}
754 err = bucket.GetObjectToFile(objectName, newFileGet, options...)
757 // Compare get and download
758 eq, err = compareFiles(newFile, newFileGet)
760 c.Assert(eq, Equals, true)
762 // Download with range, from 2048 to the end
764 options = []Option{NormalizedRange("2048-"), VersionId(versionId)}
765 err = bucket.DownloadFile(objectName, newFile, 1024*1024, options...)
769 eq, err = compareFilesWithRange(fileName, 2048, newFile, 0, fileSize-2048)
771 c.Assert(eq, Equals, true)
773 os.Remove(newFileGet)
774 options = []Option{NormalizedRange("2048-"), VersionId(versionId)}
775 err = bucket.GetObjectToFile(objectName, newFileGet, options...)
778 // Compare get and download
779 eq, err = compareFiles(newFile, newFileGet)
781 c.Assert(eq, Equals, true)
783 // Download with range, the last 4096
785 options = []Option{Routines(3), NormalizedRange("-4096"), VersionId(versionId)}
786 err = bucket.DownloadFile(objectName, newFile, 1024, options...)
790 eq, err = compareFilesWithRange(fileName, fileSize-4096, newFile, 0, 4096)
792 c.Assert(eq, Equals, true)
794 os.Remove(newFileGet)
795 options = []Option{NormalizedRange("-4096"), VersionId(versionId)}
796 err = bucket.GetObjectToFile(objectName, newFileGet, options...)
799 // Compare get and download
800 eq, err = compareFiles(newFile, newFileGet)
802 c.Assert(eq, Equals, true)
804 // download whole file
805 os.Remove(newFileGet)
806 options = []Option{Routines(3), VersionId(versionId)}
807 err = bucket.GetObjectToFile(objectName, newFileGet, options...)
810 // Compare get and download
811 eq, err = compareFiles(fileName, newFileGet)
813 c.Assert(eq, Equals, true)
816 os.Remove(newFileGet)
817 err = bucket.DeleteObject(objectName)
819 ForceDeleteBucket(client, bucketName, c)
822 func (s *OssDownloadSuite) TestVersioningDownloadWithCheckPoint(c *C) {
823 // create a bucket with default proprety
824 client, err := New(endpoint, accessID, accessKey)
827 bucketName := bucketNamePrefix + RandLowStr(6)
828 err = client.CreateBucket(bucketName)
831 bucket, err := client.Bucket(bucketName)
833 // put bucket version:enabled
834 var versioningConfig VersioningConfig
835 versioningConfig.Status = string(VersionEnabled)
836 err = client.SetBucketVersioning(bucketName, versioningConfig)
840 objectName := objectNamePrefix + RandStr(8)
841 fileName := "test-file-" + RandStr(8)
842 fileData := RandStr(500 * 1024)
843 CreateFile(fileName, fileData, c)
844 newFile := RandStr(8) + ".jpg"
847 var respHeader http.Header
848 options := []Option{Routines(3), GetResponseHeader(&respHeader)}
849 err = bucket.UploadFile(objectName, fileName, 100*1024, options...)
851 versionId := GetVersionId(respHeader)
852 c.Assert(len(versionId) > 0, Equals, true)
854 // Resumable download with checkpoint dir
856 downloadPartHooker = DownErrorHooker
857 options = []Option{CheckpointDir(true, "./"), VersionId(versionId)}
859 strPayer := getPayer(options)
860 c.Assert(strPayer, Equals, "")
862 err = bucket.DownloadFile(objectName, newFile, 100*1024, options...)
863 c.Assert(err, NotNil)
864 c.Assert(err.Error(), Equals, "ErrorHooker")
867 downloadPartHooker = defaultDownloadPartHook
868 options = []Option{CheckpointDir(true, "./"), VersionId(versionId), GetResponseHeader(&respHeader)}
869 err = bucket.DownloadFile(objectName, newFile, 100*1024, options...)
871 c.Assert(GetVersionId(respHeader), Equals, versionId)
873 eq, err := compareFiles(fileName, newFile)
875 c.Assert(eq, Equals, true)
879 err = bucket.DeleteObject(objectName)
881 ForceDeleteBucket(client, bucketName, c)
884 func (s *OssDownloadSuite) TestdownloadFileChoiceOptions(c *C) {
885 // create a bucket with default proprety
886 client, err := New(endpoint, accessID, accessKey)
889 bucketName := bucketNamePrefix + RandLowStr(6)
890 err = client.CreateBucket(bucketName)
893 bucket, err := client.Bucket(bucketName)
896 objectName := objectNamePrefix + RandStr(8)
897 fileName := "test-file-" + RandStr(8)
898 fileData := RandStr(500 * 1024)
899 CreateFile(fileName, fileData, c)
900 newFile := RandStr(8) + ".jpg"
903 var respHeader http.Header
904 options := []Option{Routines(3), GetResponseHeader(&respHeader)}
905 err = bucket.UploadFile(objectName, fileName, 100*1024, options...)
908 // Resumable download with checkpoint dir
911 // downloadFile with properties
913 ObjectACL(ACLPublicRead),
914 RequestPayer(Requester),
915 TrafficLimitHeader(1024 * 1024 * 8),
918 err = bucket.DownloadFile(objectName, newFile, 100*1024, options...)
921 eq, err := compareFiles(fileName, newFile)
923 c.Assert(eq, Equals, true)
927 err = bucket.DeleteObject(objectName)
929 ForceDeleteBucket(client, bucketName, c)
932 func (s *OssDownloadSuite) TestdownloadFileWithCpChoiceOptions(c *C) {
933 // create a bucket with default proprety
934 client, err := New(endpoint, accessID, accessKey)
937 bucketName := bucketNamePrefix + RandLowStr(6)
938 err = client.CreateBucket(bucketName)
941 bucket, err := client.Bucket(bucketName)
944 objectName := objectNamePrefix + RandStr(8)
945 fileName := "test-file-" + RandStr(8)
946 fileData := RandStr(500 * 1024)
947 CreateFile(fileName, fileData, c)
948 newFile := RandStr(8) + ".jpg"
951 var respHeader http.Header
952 options := []Option{Routines(3), GetResponseHeader(&respHeader)}
953 err = bucket.UploadFile(objectName, fileName, 100*1024, options...)
956 // Resumable download with checkpoint dir
959 // DownloadFile with properties
961 ObjectACL(ACLPublicRead),
962 RequestPayer(Requester),
963 TrafficLimitHeader(1024 * 1024 * 8),
964 CheckpointDir(true, "./"),
967 err = bucket.DownloadFile(objectName, newFile, 100*1024, options...)
970 eq, err := compareFiles(fileName, newFile)
972 c.Assert(eq, Equals, true)
976 err = bucket.DeleteObject(objectName)
978 ForceDeleteBucket(client, bucketName, c)