如何在php中从s3下载文件?

时间:2015-10-14 17:49:07

标签: php file-upload amazon-s3

如何在php中从s3下载文件。以下代码对我不起作用...... 这是我的代码

上传文件应该正常

    try {
    $s3->putObject([
        'Bucket' => $config['s3']['bucket'],//'eliuserfiles',
        'Key'    => "uploads/{$name}",
        'Body'   =>  fopen($tmp_name, 'r+'),
        //'SourceFile' => $pathToFile,
        'ACL'    => 'public-read',
    ]);

下载给我发送错误

  $objInfo = $s3->get_object_headers($config['s3']['bucket'], "uploads/{$name}");
    $obj = $s3->get_object($config['s3']['bucket'], "uploads/{$name}");

    header('Content-type: ' . $objInfo->header['_info']['content_type']);
    echo $obj->body;

错误

 PHP Catchable fatal error:  Argument 2 passed to Aws\\AwsClient::getCommand() must be of the type array, string given, called in /php_workspace/s3_php/vendor/aws/aws-sdk-php/src/AwsClient.php on line 167 and defined in /php_workspace/s3_php/vendor/aws/aws-sdk-php/src/AwsClient.php on line 211, referer: http://localhost/upload.php

2 个答案:

答案 0 :(得分:1)

执行此操作的简单方法:

在您的项目中包含Amazon S3 PHP Class

实例化类:

<强> 1。 OO方法(例如,$ s3-&gt; getObject(...)):

$s3 = new S3($awsAccessKey, $awsSecretKey);

<强> 2。静态地(例如,S3 :: getObject(...)):

S3::setAuth($awsAccessKey, $awsSecretKey);

然后获取对象:

// Return an entire object buffer:
    $object = S3::getObject($bucket, $uri));
    var_dump($object);
  

通常,最有效的方法是将对象保存到文件或资源

<?php

    // To save it to a file (unbuffered write stream):
    if (($object = S3::getObject($bucket, $uri, "/tmp/savefile.txt")) !== false) {
        print_r($object);
    }

    // To write it to a resource (unbuffered write stream):
    $fp = fopen("/tmp/savefile.txt", "wb");
    if (($object = S3::getObject($bucket, $uri, $fp)) !== false) {
        print_r($object);
    }

?>

S3 Class -With Examples

S3 Class -Documentation

答案 1 :(得分:0)

你可以试试这个:

PythonInterpreter pythonInterpreter = new PythonInterpreter();