亚马逊s3上传失败

时间:2012-03-14 00:51:56

标签: php amazon-s3 amazon

我一直在研究一些代码,用于从C#向PHP提交上传的图像,然后将图像存储在Amazon S3上,我已经将其上传到localhost,但我无法上传它到亚马逊S3。只是想知道你们能不能给我一点帮助?如果你不愿意,不用担心。

我正在使用amazon s3 PHP类找到here

这是我的代码。

<?php

if (!class_exists('S3'))require_once('S3.php');
if (!defined('awsAccessKey')) define('awsAccessKey', 'CHANGEME');
if (!defined('awsSecretKey')) define('awsSecretKey', 'CHANGEME');
$s3 = new S3(awsAccessKey, awsSecretKey);





$uploaddir = 'upload/'; // Relative Upload Location of data file
$random_digit=rand(0000,9999); // random 4 digit to add to our file name 
$nextWeek = time() + (7 * 24 * 60 * 60); //Gets system time.
$counter = 1;
if (is_uploaded_file($_FILES['file']['tmp_name']))
{
    if(strpos(basename($_FILES['file']['name']), ".")){
    $s1 = explode(".", basename($_FILES['file']['name']));
    $p1 = count($s1) - 2;
    $p2 = count($s1) - 1;

    $uploadfile = $uploaddir . $s1[$p1] . $random_digit . $nextWeek  . "." .    $s1[$p2];
    $s3->putObjectFile($uploadfile, "bucketname", $uploadfile, S3::ACL_PUBLIC_READ);
} 
else 
    {
    $uploadfile = $uploaddir . basename($_FILES['file']['name']) .  $random_digit . $nextWeek ;
    $s3->putObjectFile($uploadfile, "bucketname", $uploadfile, S3::ACL_PUBLIC_READ);
    }
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
{
    $url = explode("/", $uploadfile);
    echo $url[1];
}
else
    {
        print_r($_FILES);
    }

}

else {

echo "Upload Failed!!!";

print_r($_FILES);
}

?>

$uploadfile

开头的工作上传行中添加了两行代码

1 个答案:

答案 0 :(得分:2)

请找到下面的工作代码......

if (!class_exists('S3'))require_once('S3.php');
if (!defined('awsAccessKey')) define('awsAccessKey', 'CHANGEME');
if (!defined('awsSecretKey')) define('awsSecretKey', 'CHANGEME');
$s3 = new S3(awsAccessKey, awsSecretKey);

$fileName = $_FILES['theFile']['name'];
$fileTempName = $_FILES['theFile']['tmp_name'];

$s3->putBucket("Bucket Name", S3::ACL_PUBLIC_READ);

if ($s3->putObjectFile($fileTempName, "Bucket Name", $fileName, S3::ACL_PUBLIC_READ)) {
    echo "We successfully uploaded your file.";
}else{  
    echo "Something went wrong while uploading your file... sorry.";
}