使用PHP SDK将外部文件上传到AWS S3 bucket

时间:2022-09-07 10:45:29

I want to upload a file from an external URL directly to an Amazon S3 bucket using the PHP SDK. I managed to do this with the following code:

我想使用PHP SDK将文件从外部URL直接上载到Amazon S3 bucket。我通过以下代码做到了这一点:

$s3 = new AmazonS3();
$response = $s3->create_object($bucket, $destination, array(
  'fileUpload' => $source,
  'length' => remote_filesize($source),
  'contentType' => 'image/jpeg'
)); 

Where the function remote_filesize is the following:

其中remote_filesize函数为:

function remote_filesize($url) {
  ob_start();
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_HEADER, 1);
  curl_setopt($ch, CURLOPT_NOBODY, 1);
  $ok = curl_exec($ch);
  curl_close($ch);
  $head = ob_get_contents();
  ob_end_clean();
  $regex = '/Content-Length:\s([0-9].+?)\s/';
  $count = preg_match($regex, $head, $matches);
  return isset($matches[1]) ? $matches[1] : "unknown";
}

However, it would be nice if I could skip setting the filesize when uploading to Amazon since this would save me a trip to my own server. But if I remove setting the 'length' property in the $s3->create_object function, I get an error saying that the 'The stream size for the streaming upload cannot be determined.' Any ideas how to solve this problem?

不过,如果我可以在上传至Amazon时跳过设置filesize,那就更好了,因为这样可以省去我访问自己服务器的时间。但是如果我在$s3->create_object函数中删除了“length”属性,我就会得到一个错误,它说“流上传的流大小不能确定”。“有什么办法解决这个问题吗?”

2 个解决方案

#1


2  

You can upload file from url directly to Amazon S3 like this (my example is about a jpg picture):

你可以直接从url上传文件到Amazon S3(我的例子是关于jpg图片):

1. Convert the content from url in binary

1。将内容从url转换为二进制

$binary = file_get_contents('http://the_url_of_my_image.....');

2. Create an S3 object with a body to pass the binary into

2。创建一个带有主体的S3对象,将二进制文件传递到其中

$s3 = new AmazonS3();
$response = $s3->create_object($bucket, $filename, array(
    'body' => $binary, // put the binary in the body
    'contentType' => 'image/jpeg'
));

That's all and it's very fast. Enjoy!

就这些,而且很快。享受吧!

#2


0  

Do you have any control over remote server/host?. If so you could set up a php server to query the file locally and pass the data to you.

你对远程服务器/主机有任何控制吗?如果是这样,您可以设置一个php服务器来本地查询文件并将数据传递给您。

If not, you could use something like curl to inspect the header like so;

如果没有,您可以使用curl之类的东西来检查header;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://sstatic.net/so/img/logo.png');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
var_dump($size);

This way, you are using a HEAD request, and not downloading the whole file -- still, you depend on the remote server send a correct Content-length header.

这样,您将使用HEAD请求,而不是下载整个文件——仍然依赖于远程服务器发送正确的内容长度头。

#1


2  

You can upload file from url directly to Amazon S3 like this (my example is about a jpg picture):

你可以直接从url上传文件到Amazon S3(我的例子是关于jpg图片):

1. Convert the content from url in binary

1。将内容从url转换为二进制

$binary = file_get_contents('http://the_url_of_my_image.....');

2. Create an S3 object with a body to pass the binary into

2。创建一个带有主体的S3对象,将二进制文件传递到其中

$s3 = new AmazonS3();
$response = $s3->create_object($bucket, $filename, array(
    'body' => $binary, // put the binary in the body
    'contentType' => 'image/jpeg'
));

That's all and it's very fast. Enjoy!

就这些,而且很快。享受吧!

#2


0  

Do you have any control over remote server/host?. If so you could set up a php server to query the file locally and pass the data to you.

你对远程服务器/主机有任何控制吗?如果是这样,您可以设置一个php服务器来本地查询文件并将数据传递给您。

If not, you could use something like curl to inspect the header like so;

如果没有,您可以使用curl之类的东西来检查header;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://sstatic.net/so/img/logo.png');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
var_dump($size);

This way, you are using a HEAD request, and not downloading the whole file -- still, you depend on the remote server send a correct Content-length header.

这样,您将使用HEAD请求,而不是下载整个文件——仍然依赖于远程服务器发送正确的内容长度头。