我使用php在s3中上传文件,我的代码正常工作但是当文件选择上传非常重,就像4 MB操作失败而且我有这个错误:
NoSuchKey
指定的密钥不存在.1469625998.pdf136D99DB27E5B57FE6UXAbj45afkoAlfysDm2 + EPhBqHOhwc64gaG1w / b1AXAsfhnF7qwlankwuflh + ZO1sf / JBe6tA =
但是当文件大约是1.3 MB时,上传成功。 以下是我的代码:
$name = $_FILES['file']['name'];
$tmp = $_FILES['file']['tmp_name'];
include('s3folder/s3_config.php');
$s3->putObjectFile($tmp, $bucket , $name, S3::ACL_PUBLIC_READ);
我想知道为什么无法上传任何尺寸的文件,因为我没有指定任何尺寸的文件,例如 if($ size<(1024 * 1024))!
答案 0 :(得分:0)
下载包含4个文件的脚本here
<强> s3_config.php 强>
<?php
// Bucket Name
$bucket="BucketName";
if (!class_exists('S3'))require_once('S3.php');
//AWS access info
if (!defined('awsAccessKey')) define('awsAccessKey', 'ACCESS_KEY');
if (!defined('awsSecretKey')) define('awsSecretKey', 'ACCESS_Secret_KEY');
$s3 = new S3(awsAccessKey, awsSecretKey);
$s3->putBucket($bucket, S3::ACL_PUBLIC_READ);
?>
<强>的index.php 强> 该文件包含用于提交表单的PHP和HTML代码。
<?php
include('image_check.php'); // getExtension Method
$msg='';
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$tmp = $_FILES['file']['tmp_name'];
$ext = getExtension($name);
if(strlen($name) > 0)
{
// File format validation
if(in_array($ext,$valid_formats))
{
// File size validation
if($size<(1024*1024))
{
include('s3_config.php');
//Rename image name.
$actual_image_name = time().".".$ext;
if($s3->putObjectFile($tmp, $bucket , $actual_image_name, S3::ACL_PUBLIC_READ) )
{
$msg = "S3 Upload Successful.";
$s3file='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name;
echo "<img src='$s3file'/>";
echo 'S3 File URL:'.$s3file;
}
else
$msg = "S3 Upload Fail.";
}
else
$msg = "Image size Max 1 MB";
}
else
$msg = "Invalid file, please upload image file.";
}
else
$msg = "Please select image file.";
}
?>
//HTML Code
<form action="" method='post' enctype="multipart/form-data">
Upload image file here
<input type='file' name='file'/> <input type='submit' value='Upload Image'/>
<?php echo $msg; ?>
</form>
创建存储桶并右键单击选择属性,添加权限选择Everyone enable check list框。
<强> image_check.php 强> 此文件可帮助您获取文件扩展名。
<?php
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
//Here you can add valid file extensions.
$valid_formats = array("jpg", "png", "gif","bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
?>