【发布时间】:2019-12-06 06:34:00
【问题描述】:
我想将图像文件直接上传到 S3,而不将它们存储在我的服务器上(出于安全考虑)。我如何使用PHP SDK from AWS S3 做到这一点?这是一个示例代码:
<?php
require_once '/var/www/site/vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$bucket = '<your bucket name>';
$keyname = 'sample';
$filepath = '/path/to/image.jpg';
// Instantiate the client.
$s3 = S3Client::factory(array(
'key' => 'your AWS access key',
'secret' => 'your AWS secret access key'
));
try {
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $filepath,
'ACL' => 'public-read'
));
echo 'Success';
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}
这是上传表格:
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" id="image">
<input type="submit" value="Upload Image" name="submit">
</form>
由于安全原因我不想将$filepath 存储在我的服务器上(我不希望它执行恶意代码和类似的东西),我会在 PHP 代码中添加什么?任何帮助将不胜感激!
【问题讨论】:
-
能贴出图片来源的form/html吗?
-
@atymic 是的,我在中添加了
标签: php amazon-web-services security amazon-s3 file-upload