使用Amazon S3上传图像

isi*_*atz 2 php amazon-s3 amazon-web-services

我需要使用Amazon S3上传给定的图像

我有这个PHP:

<?
$uploaddir = 'images/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "Data Uploaded Successfully";
} else {
    echo "ERROR";
}
?>
Run Code Online (Sandbox Code Playgroud)

但它给了我这个错误:

<?xml version="1.0" encoding="UTF-8" ?>
<Error>
    <Code>MethodNotAllowe</Code>
    <Message>The specified method is not allowed against this resource.</Message>
    <ResourceType>OBJECT</ResourceType>
    <Method>POST</Method>
    ....
    <AllowedMethod>PUT</AllowedMethod>
    ....
</Error>
Run Code Online (Sandbox Code Playgroud)

我怎样才能上传文件?

Mar*_*rth 6

您正在使用该POST方法(PHP默认值)来提交数据.大多数web应用程序在动词之间存在差异GET,PUT并且POST(参见Wb RFC on Verbs).

S3希望您<AllowedMethod>PUT</AllowedMethod>用作方法.move_uploaded_file无法做到这一点.在开始编写用于执行PUT请求的代码之前,您可能应该看看一些PHP S3库.


fun*_*eer 5

Have a try at Zend Framework, there is a great class (Zend_Service_Amazon_S3) to handle all your S3 hassle.

http://framework.zend.com

require_once 'Zend/Service/Amazon/S3.php';
$s3 = new Zend_Service_Amazon_S3($my_aws_key, $my_aws_secret_key);
$s3->createBucket("my-own-bucket");
$s3->putObject("my-own-bucket/myobject", $file);
Run Code Online (Sandbox Code Playgroud)