作者 lyh

gx

... ... @@ -66,6 +66,7 @@ class AmazonS3Service
*/
public function syncImageFiles($files)
{
$file_link = $this->fetchRemoteImage($files);
$key = str_replace_url($files);
// try {
$context = stream_context_create([
... ... @@ -74,7 +75,7 @@ class AmazonS3Service
'verify_peer_name' => false
]
]);
$file_content = file_get_contents($files, false, $context);
$file_content = file_get_contents($file_link, false, $context);
$result = $this->s3->putObject([
'Bucket' => $this->bucket,
'Key' => $key,
... ... @@ -85,4 +86,33 @@ class AmazonS3Service
// return '上传文件到S3时发生错误:' . $e->getMessage();
// }
}
/**
* @remark :零时文件
* @name :fetchRemoteImage
* @author :lyh
* @method :post
* @time :2024/1/26 12:48
*/
public function fetchRemoteImage($url, $localPath = '/tmp/file.jpg') {
// 创建 cURL 句柄
$curl = curl_init();
// 设置 cURL 选项
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// 执行请求并获取内容
$response = curl_exec($curl);
// 检查请求是否成功
if ($response === false) {
$error = curl_error($curl);
// 处理错误
return 'cURL 错误:' . $error;
} else {
// 将内容保存到本地文件
file_put_contents($localPath, $response);
return $localPath;
}
// 关闭 cURL 句柄
curl_close($curl);
}
}
... ...