2013-09-24 49 views
0

我完全停留在我期望的簡單問題上。當$ filepath變量是要上載的文件的絕對路徑並在腳本中定義時,以下Amazon AWS SDK2腳本將文件成功上傳到AWS S3。PHP:將變量傳遞給AWS SDK2上傳器

但是,當我嘗試使用簡單的表單來選擇文件並將其作爲變量傳遞給AWS腳本時,我將自己束縛在節點上。例如試圖使用$ _FILES ['input1'] ['name'],$ _FILES ['input1'] ['tmp_name'],realpath(),file_get_contents()來訪問完整路徑。還嘗試使用JavaScript,但路徑然後由瀏覽器更改爲'假路徑'。我顯然不明白元素中的type =「file」。

所以,我的問題:如何讓用戶從本地磁盤選擇文件並(使用POST操作??)將文件路徑作爲變量傳遞給AWS SDK2上傳腳本?我的簡單測試表格也包含在下面。

簡單形式:

<body> 
<form id="form1" action="SDK2_script_process.php" method="post" enctype="multipart/form-data"> 
<input type="file" id="input1" name="input1" /> 
<input type="submit" name="submit" id="submit" value="Search" > 
</form> </body> 

過程腳本(SDK2_script_process.php):

<?php 
//CONNECTS TO AWS V2 SDK AND UPLOADS FILE 
//Literal path to aws.phar file 
require_once 'AWSSDKforPHP/aws.phar'; 

use Aws\S3\S3Client; 
use Guzzle\Http\EntityBody; 

// Instantiate the S3 client with AWS credentials and optional desired AWS region 
$client = S3Client::factory(array(
'key' => 'MYKEY', 
'secret' => MYSECRETKEY' 
)); 

//Name of bucket on S3 
$bucket = 'mybucket'; 
//Filename to be saved in S3 Bucket 
$filename = "/directoryA/directoryB/filename.extension"; 

//Literal filepath to file I want to save - THIS WORKS 
// $filepath = '../../directory1/directory2/directory3/filename.extension'; 

// Filepath from simple form - DOES NOT WORK 
$filepath = $_FILES['input1']['tmp_name']; 

$result = $client->putObject(array(
'Bucket' => $bucket, 
'Key' => $filename, 
'SourceFile' => $filepath, 
'Metadata' => array(
     'title' => 'This is the title metadata', 
     'artist' => 'This is the artist metadata' 
     ) 
)); 

// HEAD object confirms success 
$headers = $client->headObject(array(
    "Bucket" => $bucket, 
    "Key" => $filename 
)); 
//print_r($headers->toArray()); 

echo $result['ObjectURL']; 
echo $headers['Metadata']['artist']; 


?> 

欣賞任何幫助,或指針使用這種SDK2腳本的不同方式。

回答

0

找到了answer,我想。我應該使用PostObject,而不是putObject。

請參閱Aws \ S3 \ Model \ PostObject;在SDK docs