当前位置:网站首页>Batch package and download Alibaba OSS files

Batch package and download Alibaba OSS files

2022-06-13 08:19:00 Parthenocissus still works

Where there is a need, there is a way , Bulk download oss file , In the previous chapter, we talked about batch intermittent downloading from the front-end point of view , But there are some problems (1. Bad user experience ,2. The browser configuration needs to be modified for multiple downloads or pop ups , And sometimes there is a risk that the request will be cancelled , It is sometimes difficult for operators to find this problem if they do not verify the number of files , This has certain risks for statistical work ), This article is from the back-end interface , Batch request Ali oss Get the contents of the downloaded file , Package and download the acquired data at one time :

class TestOss extends BaseController
{
    // Preliminary survey 
	public function test()
	{
		$objectName = 'abc/2021/10/30/G20211030060001231.xlsx';
		$object = self::$oss->getObject($this->config['oss']['bucket'], $objectName);
  		$file = 'abc' . substr($objectName, strrpos($objectName, '.'));

		$fp = fopen ($file, 'w');
		fwrite($fp, $object);
		fclose($fp);
	}
    
    // Package download 
	public function testZip()
	{
		error_reporting(E_ALL);
		ini_set('display_errors', true);

		$zip = new ZipArchive;
		$tmpFileName = './tmpzip' . mt_rand(100, 999);
		$res = $zip->open($tmpFileName, ZipArchive::CREATE);
		var_dump(is_file($tmpFileName));

		$objectName = 'abc/1077/2021/10/30/G20211030060001231.xlsx';
		$object = self::$oss->getObject($this->config['oss']['bucket'], $objectName);
  		$file = 'abc' . substr($objectName, strrpos($objectName, '.'));

		if ($res === TRUE) {
		    $zip->addFromString($file, $object);
		    $zip->close();
		    $tarFile = 'abc.zip';
			var_dump(is_file($tmpFileName));
			$fileSize = filesize($tmpFileName);
			$fp = fopen($tmpFileName, "r");
			header("Content-type: application/octet-stream");
			header("Accept-Ranges: bytes");
			header("Accept-Length: " . $fileSize);
			header("Content-Disposition: attachment; filename=" . $tarFile); 
			$buffer = 1024;
			$fileCount = 0;
			while (!feof($fp) && $fileCount < $fileSize) {
				$fileCon = fread($fp, $buffer);
				$fileCount += $buffer;
				echo $fileCon;
			}
			fclose($fp);
			unlink($tmpFileName);
		} else {
		    echo 'failed';
		}
	}

}

(new TestOss())->testZip();

原网站

版权声明
本文为[Parthenocissus still works]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270544563836.html