Upload Image Using Amazon S3

I need to upload a given image using Amazon S3

I have this 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";
}
?>

      

but it gives me this error:

<?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>

      

How do I upload a file?

+2


source to share


2 answers


You are using a method POST

(which is the default PHP standard) to send data. Most web applications differentiate between verbs GET

, PUT

and POST

(see the W3 RFC on verbs ).



S3 wants to use it <AllowedMethod>PUT</AllowedMethod>

as a method. move_uploaded_file cannot do this. Before you start writing code to make PUT requests, you might want to take a look at some PHP S3 libs .

+6


source


Try Zend Framework, there is a great class (Zend_Service_Amazon_S3) to handle all your S3 issues.

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);

      

+5


source







All Articles