1

I've been banging my head against the wall with this for a few hours now and I can't seem to figure out what's wrong.

Basically I have a S3 Bucket and I'm trying to upload a simple text (.txt) file to it from Unity3D. The C# script to download and PHP script are taken from here

So I put the PHP script in the bucket itself, get the URL of that php script and call it from the C# code:

PHP script:

<?php
   if(isset($_FILES['theFile']))
   {
      print("Success! ");
      print("tmpName: " . $_FILES['theFile']['tmp_name'] . " ");
      print("size: " . $_FILES['theFile']['size'] . " ");
      print("mime: " . $_FILES['theFile']['type'] . " ");
      print("name: " . $_FILES['theFile']['name'] . " ");


   move_uploaded_file($_FILES['theFile']['tmp_name'], "../images/" . $_FILES['theFile']['name']);

   } else
   {
      print("Failed!");
   }
?>

C#:

 WWWForm postForm = new WWWForm();
 postForm.AddBinaryData("theFile", bytes, "TestFile", "text/plain");
 WWW upload = new WWW(phpScriptURL, postForm);        
 yield return upload;
 if (upload.error == null)
     Debug.Log("upload done :" + upload.text);
 else
     Debug.Log("Error during upload: " + upload.error);

I get the error "Error during upload: 405 Method not allowed"

I tried changing permission settings, Region settings, etc but to no avail.

I read this answer and it mentioned to change the end point but I can't seem to do that there's no way to edit it.

If I try the same PHP/C# script on a different server it works fine... But just not in the S3 Bucket.

I've tried another PHP/C# script and got the same result, same error on the S3 bucket but success on a different server.

Here's the other PHP script I tried:

<?php
$data = file_get_contents('php://input');
try {
    $result = file_put_contents("save-bytes.txt", $data, FILE_APPEND);
} catch (Exception $e) {
    echo $e->getMessage();
    die();
}
if ($result !== false)
{
    echo "Saved " . $result . " bytes";
} else {
    echo "Data is not saved";
}

(The C# code used with this script does something similar to the previously posted C# code)

Thanks for any help in advance

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
ttsa
  • 11
  • 1
  • 2
  • I'm a little confused. You say the PHP is in S3, right? Does that mean you are trying to execute the PHP file INSIDE of S3? Where is your PHP server running from? – Chris Franklin Jan 14 '16 at 21:29
  • Yes I'm trying to run the script from the S3 bucket. This is my first time doing PHP stuff so excuse my ignorance if that's not what you're supposed to do. – ttsa Jan 14 '16 at 22:10
  • You have to start somewhere! S3 is a storage service intended to hold static files. PHP is a dynamic server side scripting language. In order to run PHP, you need a server set up that is running PHP and a Web Server (usually Apache for beginners). Look into the LAMP stack (Linux, Apache, MySQL and PHP) to start getting an idea of where to start. Also google ```installing php``` for even more info. Unfortunately, S3 can't execute code for you, so you need to learn how to set up a server to do it for you. – Chris Franklin Jan 14 '16 at 22:25
  • thanks for the tip. Is there any other way (other than setting up php) I could download files (via code) from a storage service like S3? – ttsa Jan 15 '16 at 00:48
  • Download from? Or upload to? Your question is about uploading, not downloading. – Michael - sqlbot Jan 15 '16 at 02:13
  • You mentioned C# so you could use the .NET AWS SDK to upload to an S3 bucket: https://aws.amazon.com/sdk-for-net/. If you can't use the SDK for some reason you can just use the POST API: http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPOST.html – Chris Franklin Jan 15 '16 at 03:02

1 Answers1

1

S3 buckets are locked down to private by default. You have to edit the access policy on the bucket if you want to put files into it. For information on S3 bucket policies check out the docs.

Chris Franklin
  • 3,864
  • 2
  • 16
  • 19
  • You mean go Right Click | Properties | Permissions? I added "Everyone" and gave full access just to test, didn't help. – ttsa Jan 14 '16 at 22:09
  • Read through the docs I linked. The 405 Method not allowed means you don't have permission to POST into that bucket. When you modify that section of the Permissions panel there is a button at the bottom that says ```Add bucket policy```. That is where you want to add your policy definition for what methods are allowed. All of that is in the docs linked above. – Chris Franklin Jan 14 '16 at 22:18