有没有一种用URL抓取图像并直接保存到亚马逊的方法?
可以使用PHP完成吗?
我的另一个选择是将文件保存在本地,然后使用S3 PHP类发送它。
可以使用PHP完成吗?
我的另一个选择是将文件保存在本地,然后使用S3 PHP类发送它。
function upload_image( $image_data )
{
//
// Write the image to S3
//
$s3 = new AmazonS3();
$bucket = ACTIVITY_S3_BUCKET;
$image_id = uniqid('myscript');
$path = ACTIVITY_S3_FOLDER.'/'.$image_id;
$response = $s3->create_object($bucket, $path, array(
'body' => $image_data,
'acl' => AmazonS3::ACL_PUBLIC
));
$image_url = 'https://s3.amazonaws.com/'.ACTIVITY_S3_BUCKET.'/'.$path;
return $image_id;
}
显然,这不是一个强大的脚本,但我想分享下我的经历。 我应该补充一点,因为在这种情况下您似乎最关心的是处理能力,请查看这篇有趣的帖子,在云中调整大小数十亿个图像。http://www.nslms.com/2010/11/01/how-to-resize-billions-of-images-in-the-cloud/
更新 根据这个答案How best to resize images off-server,“PHP / GD让您直接在HTTP响应中发送jpeg。”
使用Amazon S3 Stream Wrapper,您可以直接将远程文件保存到S3中。无需在服务器上保存临时副本。
// Register the stream wrapper from an S3Client object
$client->registerStreamWrapper();
// Copy a remote file to Amazon S3
copy('http://example.com/1.jpg', 's3://bucket/key');
Amazon S3提供了内置的PHP函数,如file_get_contents
、fopen
、copy
、rename
、unlink
、mkdir
和rmdir
等,方便您存储和检索数据。
<form action="https://s3-bucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value="uploads/${filename}">
<input type="hidden" name="AWSAccessKeyId" value="YOUR_AWS_ACCESS_KEY">
<input type="hidden" name="acl" value="private">
<input type="hidden" name="success_action_redirect" value="http://localhost/">
<input type="hidden" name="policy" value="YOUR_POLICY_DOCUMENT_BASE64_ENCODED">
<input type="hidden" name="signature" value="YOUR_CALCULATED_SIGNATURE">
<input type="hidden" name="Content-Type" value="image/jpeg">
File to upload to S3:
<input name="file" type="file">
<br>
<input type="submit" value="Upload File to S3">
</form>
您可以在官方文档中找到详细信息: http://aws.amazon.com/articles/1434