将文件上载到Amazon AWS S3时出现致命错误

时间:2017-10-05 06:22:36

标签: php amazon-web-services amazon-s3 video-streaming aws-sdk

我正在开发一个视频流网站,我在将测试文件上传到S3存储桶时遇到致命错误。我已经包含了文档中提到的所有其他内容,包括Region和Version。我列出了每一个代码和细节。

浏览器屏幕上的

错误消息:

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Missing required client configuration options: region: (string) A "region" configuration value is required for the "s3" service (e.g., "us-west-2"). A list of available public regions and endpoints can be found at http://docs.aws.amazon.com/general/latest/gr/rande.html. version: (string) A "version" configuration value is required. Specifying a version constraint ensures that your code will not be affected by a breaking change made to the service. For example, when using Amazon S3, you can lock your API version to "2006-03-01". Your build of the SDK has the following version(s) of "s3": * "2006-03-01" You may provide "latest" to the "version" configuration value to utilize the most recent available API version that your client's API provider can find. Note: Using 'latest' in a production application is not recommended. A list of available API versions can be found on each client's API documentation page: http:/ in C:\xampp\htdocs\vdo_upload\vendor\aws\aws-sdk-php\src\ClientResolver.php on line 374

config_s3.php:

<?php

require 'vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\Exception\AwsException;


// Bucket Name
$bucket="comtrekwings2017";

//AWS access info
if (!defined('awsAccessKey')) define('awsAccessKey', 'AKIAIE6QJQ4B5Q');
if (!defined('awsSecretKey')) define('awsSecretKey', 'tFkQIuNDtmYOz6WoOjpWdMuJCk1w5srqT3');


$s3options = array(
        'services' => array(
            'default_settings' => array(
                'params' => array(
                    'key'    => 'AKIAIE6QJQ4B5Q',
                    'secret' => 'tFkQIuNDtmYOz6WoOjpWdMuJCk1w5srqT3',
                    'region' => 'ap-south-1'
                )
            )
        )
    );


$client = S3Client::factory($s3options);


try {
    $result = $client->getBucketCors([
        'Bucket' => $bucket, // REQUIRED
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    error_log($e->getMessage());
}
?>

composer.json:

{
    "require": {
        "aws/aws-sdk-php": "^3.36"
    }
}

上传文件的网页

<?php
    // Include the SDK using the Composer autoloader
     require 'vendor/autoload.php';
     use Aws\S3\S3Client;
     use Aws\S3\Exception\S3Exception;

include('image_validation.php'); // getExtension Method
$message='';
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$tmp = $_FILES['file']['tmp_name'];
$ext = getExtension($name);

if(strlen($name) > 0)
{
// File format validation
if(in_array($ext,$valid_formats))
{
// File size validation
        if($size<(1024*1024))
        {
        include('config_s3.php');
        //Rename image name.
            $image_name_actual = time().".".$ext;

                 try {
                        $client->putObject(array(
                             'Bucket'=>$bucket,
                             'Key' =>  $image_name_actual,
                             'SourceFile' => $tmp,
                             'StorageClass' => 'REDUCED_REDUNDANCY',
                             'region'  => 'eu-central-1',
                             'version' => '2006-03-01'
                        ));
                $message = "S3 Upload Successful.";
                $s3file='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name;
                echo "<img src='$s3file'/>";
                echo 'S3 File URL:'.$s3file;

                    } catch (S3Exception $e) {
                         // Catch an S3 specific exception.
                        echo $e->getMessage();
                    }
        }
        else
        $message = "Image size Max 1 MB";

        }
    else
    $message = "Invalid file, please upload image file.";

    }
else
$message = "Please select image file.";

}
?>


<form action="" method='post' enctype="multipart/form-data">
Upload image file here
<input type='file' name='file'/> <input type='submit' value='Upload Image'/>
<?php //echo $msg; ?>
</form>

由于我是电影流媒体的新手,我还想确定我是否正在创建视频流媒体网站,或者有其他更好的方式。

1 个答案:

答案 0 :(得分:0)

首先,您需要立即从您的邮件中删除您的API密钥。

其次,请尝试在 config_s3.php 中使用以下配置:

$s3options = array(
        'services' => array(
            'default_settings' => array(
                'params' => array(
                    'key'    => 'AKIAIE6QJQ4B5Q',
                    'secret' => 'tFkQIuNDtmYOz6WoOjpWdMuJCk1w5srqT3',
                    'region' => 'ap-south-1'
                    'version' => 'latest'
                )
            )
        )
    );

您还应该尝试在页面中从上传文件的配置行中更改以下内容,因为可能有不同的区域导致问题:

$client->putObject(array(
  'Bucket'=>$bucket,
  'Key' =>  $image_name_actual,
  'SourceFile' => $tmp,
  'StorageClass' => 'REDUCED_REDUNDANCY',
  'region'  => 'ap-south-1',
  'version' => 'latest'
));