douliedai4838 2016-11-28 10:17
浏览 488
已采纳

上传到AWS S3失败

I'm trying to upload a file from my Wordpress application to a S3 bucket by Ajax: Somehow, I don't get an answer and the script fails with a 500 error when applying the 'putObject' method.

app/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');
}

app/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',
]);

app/s3/aws

Latest version of the official AWS SDK for PHP

SOLUTION The credentials in app/start.php where not assigned correctly when initialising the $s3 object. That's how it must look like

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

1条回答 默认 最新

  • douyi3833 2016-11-28 10:30
    关注

    If you upload a file, you should use SourceFile instead of Body.

    Example code:

    $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'
            )
        ));
    

    More info from here - http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpPHP.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试