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 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题