作者 lyh

gx

@@ -66,6 +66,7 @@ class AmazonS3Service @@ -66,6 +66,7 @@ class AmazonS3Service
66 */ 66 */
67 public function syncImageFiles($files) 67 public function syncImageFiles($files)
68 { 68 {
  69 + $file_link = $this->fetchRemoteImage($files);
69 $key = str_replace_url($files); 70 $key = str_replace_url($files);
70 // try { 71 // try {
71 $context = stream_context_create([ 72 $context = stream_context_create([
@@ -74,7 +75,7 @@ class AmazonS3Service @@ -74,7 +75,7 @@ class AmazonS3Service
74 'verify_peer_name' => false 75 'verify_peer_name' => false
75 ] 76 ]
76 ]); 77 ]);
77 - $file_content = file_get_contents($files, false, $context); 78 + $file_content = file_get_contents($file_link, false, $context);
78 $result = $this->s3->putObject([ 79 $result = $this->s3->putObject([
79 'Bucket' => $this->bucket, 80 'Bucket' => $this->bucket,
80 'Key' => $key, 81 'Key' => $key,
@@ -85,4 +86,33 @@ class AmazonS3Service @@ -85,4 +86,33 @@ class AmazonS3Service
85 // return '上传文件到S3时发生错误:' . $e->getMessage(); 86 // return '上传文件到S3时发生错误:' . $e->getMessage();
86 // } 87 // }
87 } 88 }
  89 +
  90 + /**
  91 + * @remark :零时文件
  92 + * @name :fetchRemoteImage
  93 + * @author :lyh
  94 + * @method :post
  95 + * @time :2024/1/26 12:48
  96 + */
  97 + public function fetchRemoteImage($url, $localPath = '/tmp/file.jpg') {
  98 + // 创建 cURL 句柄
  99 + $curl = curl_init();
  100 + // 设置 cURL 选项
  101 + curl_setopt($curl, CURLOPT_URL, $url);
  102 + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  103 + // 执行请求并获取内容
  104 + $response = curl_exec($curl);
  105 + // 检查请求是否成功
  106 + if ($response === false) {
  107 + $error = curl_error($curl);
  108 + // 处理错误
  109 + return 'cURL 错误:' . $error;
  110 + } else {
  111 + // 将内容保存到本地文件
  112 + file_put_contents($localPath, $response);
  113 + return $localPath;
  114 + }
  115 + // 关闭 cURL 句柄
  116 + curl_close($curl);
  117 + }
88 } 118 }