2016-11-28 195 views
0

我想通过Ajax将我的Wordpress应用程序上传到S3存储桶中的文件: 不知何故,我没有得到答案,并且脚本在应用'putObject ' 方法。上传到AWS S3失败

应用程序/ 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

如果您上传文件,你应该使用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

+0

只是去尝试,但仍然没有工作。 使用wp_die()进行测试我得到以下结果: $ bucket =>正确的存储桶名称, $ key =>正确的密钥名称, $ filepath =>'/ tmp/phpNKgE0e' –

+0

iI've刚刚检查过通过var_dump(get_class_methods($ s3))创建对象方法,并且没有putObject方法 –

+0

啊 - 请做两件事1)能否使用'ini_set()'和'error_reporting()'?打印所有错误。它一定会帮助你。 2)您的客户端连接似乎也是无效的,'key'和'secret'通常嵌套在'credentials'内。查看此文档,https://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/configuration.html,了解凭证部分。 – Chainat