上传到AWS S3失败

时间:2016-11-28 10:17:41

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

我试图通过Ajax将文件从我的Wordpress应用程序上传到S3存储桶: 不知何故,我没有得到答案,并且在应用' putObject'时脚本失败并出现500错误。方法

应用/ ajax.php

require_once 's3/start.php'
//wp_die(var_dump($s3)); Seems to be fine
$upload = $s3->putObject([
    'Bucket' => $config['s3']['bucket'],
    'Key' => 'video,
    'Body'   => fopen( $_FILES['file']['tmp_name'], 'r' ),
    'ACL' => 'public-read',
]);
if ($upload) {
    wp_die('Uploaded');
} else {
    wp_die('Upload Error');
}

应用/ S3 / start.php

use Aws\S3\S3Client;
require 'aws/aws-autoloader.php';
$config = require('config.php');
$s3 = new S3Client([
    'key'       =>  $config['s3']['key'],
    'secret'    =>  $config['s3']['secret'],
    'region'    =>  $config['s3']['region'],
    'version'   =>  'latest',
]);

应用/ S3 / AWS

Latest version of the official AWS SDK for PHP

app / start.php中的凭据在初始化$ s3对象时未正确分配。这就是它的样子

$s3 = S3Client::factory([
'region'    =>  $config['s3']['region'],
'version'   =>  'latest',
'credentials' => [
    'key'    => $config['s3']['key'],
    'secret' => $config['s3']['secret']
]
]);

1 个答案:

答案 0 :(得分:1)

如果您上传文件,则应使用SourceFile代替Body

示例代码:

$result = $s3->putObject(array(
        'Bucket'       => $bucket,
        'Key'          => $keyname,
        'SourceFile'   => $filepath,
        'ContentType'  => 'text/plain',
        'ACL'          => 'public-read',
        'StorageClass' => 'REDUCED_REDUNDANCY',
        'Metadata'     => array(    
            'param1' => 'value 1',
            'param2' => 'value 2'
        )
    ));

来自此处的更多信息 - http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpPHP.html