PHP:如何从非文件数据中下载部分内容.例如,存储在从AWS S3 Bucket提取的变量内部的数据



在下面的代码中:在safari中,它进行部分下载。我一直收到错误:

Failed to load resource: Plug-in handled load

我进入if (isset($_SERVER['HTTP_RANGE']))分支。Error_Log对此进行验证。我在谷歌chrome中检查了一下,以确保资源确实被占用了,而且确实被占用,谷歌chrome下载了前面提到的if语句的else语句中的整个文件。

我需要做些什么来更正标题由于"->读取(1024(";。

$s3 = S3Client::factory([
'version' => 'latest',
'region' => 'A Region',
'credentials' => [
'key' => "A KEY",
'secret' => "A SECRET",
]
]);
//echo "Step 4nn";

$bucket = "bucket";

$image_path = $_GET['video_path'];

try {

$result = $s3->getObject([
'Bucket' => $bucket,
'Key' => $image_path
]);

header("Content-Type: {$result['ContentType']}");
$fileSize = $result['ContentLength'];

if (isset($_SERVER['HTTP_RANGE'])){ // do it for any device that supports byte-ranges not only iPhone
// echo "partial";
error_log("Error message We are good on safari in HTTP_RANGEn", 3, "error_log");
// Read the body off of the underlying stream in chunks

header('HTTP/1.1 206 Partial Content');
header('Accept-Ranges: bytes');
$start = 0;
//$theLengthOfTheNextData = mb_strlen($data, '8bit');
header("Content-Range: bytes 0-1023/$fileSize");
header("Content-Length: 1024");
while ($data = $result['Body']->read(1024))
{
//$theLengthOfTheNextData = mb_strlen($data, '8bit');
//it starts at zero
//$end = $start + $theLengthOfTheNextData - 1;

//$theLengthOfTheNextData = mb_strlen(data);
//echo "Length: $theLengthOfTheNextDatann";
//header("Content-Range: bytes $start-$end/$fileSize");
//header("Content-Length: $theLengthOfTheNextData");
//$start = $start + $theLengthOfTheNextData;
// header("Content-Range: bytes $start-$end/$size");
set_time_limit(0);
echo $data;

// Flush the buffer immediately
//@ob_flush();
flush();
}
//header("Content-Length: $fileSize");
//rangeDownload($result['Body'],$fileSize);
}
else {
// echo "Just entire thing";
error_log("Error message ---- NOT good on safari in HTTP_RANGEn", 4, "error_log");
header("Content-Length: ".$fileSize);
echo $result['Body'];
}
//echo "Step 7nn";
//no need to cast as string
// Cast as a string
// $body = (string)$result->get('Body');
//below should actually work too
// $bodyAsString = (string) $result['Body'];
} catch (S3Exception $e) {
echo $e;
//echo "Step 6Bnn";
//grabbing the file was unsuccessful
$successfulMove = 0;
}

如果有人在寻找答案。您将需要使用";registerStreamWrapper";在S3功能中。

参考链路

您也需要向下滚动";tedchou12";议论它有效!!!

最新更新