我完全被我所期望的一个简单问题所困扰。当 $filepath 变量是要上传的文件的绝对路径并在脚本中定义时,以下 Amazon AWS SDK2 脚本会成功将文件上传到 AWS S3。
但是,当我尝试使用简单的表单来选择文件并将其作为变量传递给 AWS 脚本时,我陷入了困境。例如,已经尝试 $_FILES['input1']['name'], $_FILES['input1']['tmp_name'], realpath(), file_get_contents() 来访问完整路径。也尝试使用 javascript,但路径随后被浏览器更改为“fakepath”。我显然不了解元素中的 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 脚本的不同方式的指针。