AWS PHP SDK使用FORM上传到S3 Bucket



我有以下形式的

<form id="uploadFile" enctype="multipart/form-data" method="post" apaction="uptos3.php">
<input type="file" class="form-control" name="file" id="file"></input>
<br>
<button type="submit" name="uploadtoS3">Upload to S3</button>
</form>

我的代码与表单在同一页上,将S3对象加载到S3 Bucket中。

try {
// Upload data.
$result = $s3Client->putObject([
'Bucket' => 'mybucketS3/folderS3/',
'Key'    => $_FILES['file']['name'],
'Body'   => $_FILES['file']['tmp_name'],
'ACL'    => 'public-read'
]);
// Print the URL to the object.
echo $result['ObjectURL'] . PHP_EOL;
} catch (S3Exception $e) {
echo $e->getMessage() . PHP_EOL;
}

我怎么能做到,当用户提交文件时,该文件直接存储在S3上。

谢谢。

您需要将文件发布到PHP代码中,然后使用S3 PHP API将文件放入桶中。下面是PHP代码示例,它向您展示了如何将对象上传到bucket中。

https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/php/example_code/s3/PutObject.php

最新更新