2016-11-28 236 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