将文件上载到AWS S3存储不起作用

时间:2016-08-28 07:37:43

标签: php amazon-web-services amazon-s3

我有三个PHP文件和一个文本文件。 我尝试按照youtube中的教程(Amazon S3 with PHP:上传文件(1/6 - 4/6))。它没有像教程中讨论的那样正常工作。

文件夹和文件结构是

--var/www/html/tutorials/s3->upload.php
--var/www/html/tutorials/s3->files->uploadme.txt
--var/www/html/tutorials/s3->app->config.php, start.php

基本上我正在尝试将uploadme.txt上传到AWS S3存储。

是三个文件

的config.php     

 return [
    's3' => [
       'key' => '-- key --',
       'secret' => '-- secret --',
       'bucket' => '-- bucket --'
     ]
 ];

?>   

start.php     

use Aws\S3\S3Client;

require 'vendor/autoload.php';

$config = require('config.php');

//S3
$s3 = S3Client::factory([
   'key' => $config['s3']['key'],
   'secret' => $config['s3']['secret']
]);

?>

upload.php的

<?php
use Aws\S3\Exception\S3Exception;
require 'app/start.php';
if(isset($_FILES['file'])){ 
   $file = $_FILES['file'];

   //File details
   $name = $file['name'];
   $tmp_name = $file['tmp_name'];

   $extension = explode('.', $name);   
   $extension = strtolower(end($extension));

   //Tmp details
   $key = md5(uniqid());
   $tmp_file_name = "{$key}.{$extension}";
   $tmp_file_path = "files/{$tmp_file_name}";

   //Move the file
   move_uploaded_file($tmp_name, $tmp_file_path); 
   try{
      $s3->putObject([
      'Bucket' => $config['s3']['bucket'],
      'Key' => "uploads/{$name}",
      'Body' => fopen($tmp_file_path,'rb'),
      'ACL' => 'public-read'       
      ]);
      //remove the file
      unlink($tmp_file_path);
   }catch(S3Exception $e){
         die("There was an error uploading that file.");
   } 
}

?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Upload</title>
  </head>
  <body>
     <form action="upload.php" method="post" enctype="multipart/form-data">
       <input type="file" name="file">
       <input type="submit" value="Upload">
     </form>

  </body>
</html>

0 个答案:

没有答案
相关问题