如何将缩略图文件上传到亚马逊S3存储桶?

阿努

嗨,我是亚马逊的新手,几乎没有使用文件的想法。我正在尝试将缩略图上传到存储桶中。我可以在本地主机中上传映像,但不能在存储桶中上传。我可以将原始映像上传到存储桶,这是我的代码

<?php
//include the S3 class
            if (!class_exists('S3'))require_once('S3.php');

            //AWS access info
            if (!defined('awsAccessKey')) define('awsAccessKey', '');
            if (!defined('awsSecretKey')) define('awsSecretKey', '');

            //instantiate the class
            $s3 = new S3(awsAccessKey, awsSecretKey);


function Upload($field_name = '', $target_folder = '', $file_name = '', $thumb = FALSE, $thumb_folder = '', $thumb_width = '', $thumb_height = ''){
    //folder path setup
    $target_path = $target_folder;
    $thumb_path = $thumb_folder;

    //file name setup
    $filename_err = explode(".",$_FILES[$field_name]['name']);
    $filename_err_count = count($filename_err);
    $file_ext = $filename_err[$filename_err_count-1];
    if($file_name != '')
    {
        $fileName = $file_name.'.'.$file_ext;
    }
    else
    {
        $fileName = $_FILES[$field_name]['name'];
    }

    //upload image path
    $upload_image = $target_path.basename($fileName);

    //upload image
    if(move_uploaded_file($_FILES[$field_name]['tmp_name'],$upload_image))
    {
        //thumbnail creation
        if($thumb == TRUE)
        {
            $thumbnail = $thumb_path.$fileName;
            list($width,$height) = getimagesize($upload_image);
            $thumb_create = imagecreatetruecolor($thumb_width,$thumb_height);
            switch($file_ext){
                case 'jpg':
                    $source = imagecreatefromjpeg($upload_image);
                    break;
                case 'jpeg':
                    $source = imagecreatefromjpeg($upload_image);
                    break;
                case 'png':
                    $source = imagecreatefrompng($upload_image);
                    break;
                case 'gif':
                    $source = imagecreatefromgif($upload_image);
                    break;
                default:
                    $source = imagecreatefromjpeg($upload_image);
            }
            imagecopyresized($thumb_create,$source,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
            switch($file_ext){
                case 'jpg' || 'jpeg':
                    imagejpeg($thumb_create,$thumbnail,72);
                    break;
                case 'png':
                    imagepng($thumb_create,$thumbnail,50);
                    break;
                case 'gif':
                    imagegif($thumb_create,$thumbnail,50);
                    break;
                default:
                    imagejpeg($thumb_create,$thumbnail,50);
            }
        }

        return $fileName;
    }

}


if(!empty($_FILES['image']['name'])){

    $image_name = $_FILES['image']['name'];
        $tmp_name   = $_FILES['image']['tmp_name'];
        $size       = $_FILES['image']['size'];
        $type       = $_FILES['image']['type'];
        $error      = $_FILES['image']['error'];
    $target_dir = "products/";
    $target_file = $target_dir.$_FILES['image']['name'];
    $fileTempName = $_FILES['image']['tmp_name'];
    $fileName = $target_dir.$_FILES['image']['name'];

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

                //move the original file to bucket file
                if ($s3->putObjectFile($fileTempName, "bucketname", $fileName, S3::ACL_PUBLIC_READ)) {
                    echo "<strong>We successfully uploaded your file.</strong>";
                }else{
                    echo "<strong>Something went wrong while uploading your file... sorry.</strong>";
                }

    //call thumbnail creation function and store thumbnail name
    $upload_img = Upload('image','uploads/','',TRUE,'uploads/thumbs/','200','160');
    $upload_img1 = Upload('image','products/','',TRUE,'products/','200','160');

    $fileTempName12 = $_FILES[$upload_img1]['tmp_name'];
    //$fileName12 = 'products/'.$_FILES[$upload_img1]['name'];
    //full path of the thumbnail image
    $thumb_src = 'products/'.$upload_img1;
    $s3->putBucket("bucketname", S3::ACL_PUBLIC_READ);

                //move the thumbnail to bucket
                if ($s3->putObjectFile($fileTempName12, "bucketname", $thumb_src, S3::ACL_PUBLIC_READ)) {
                    echo "<strong>We successfully uploaded your file.</strong>";
                }else{
                    echo "<strong>Something went wrong while uploading your file... sorry.</strong>";
                }
    //set success and error messages
    $message = $upload_img?"<span style='color:#008000;'>Image thumbnail have been created successfully.</span>":"<span style='color:#F00000;'>Some error occurred, please try again.</span>";

}else{

    //if form is not submitted, below variable should be blank
    $thumb_src = '';
    $message = '';
}
?>
<?php
    // Get the contents of our bucket
    $contents = $s3->getBucket("bucketname");
    if(is_array($contents) || is_object($contents))
    {
    foreach ($contents as $file){

        $fname = $file['name'];
        $furl = "http://bucketname.s3.amazonaws.com/".$fname;

        //output a link to the file
        echo "<a href=\"$furl\">$fname</a><br />";
    }
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form method="post" enctype="multipart/form-data">
    <input type="file" name="image"/>
    <input type="submit" name="submit" value="Upload"/>
</form>
</body>
</html>

我的原始文件已成功上传,但是在上传缩略图时出现以下错误,

Warning: S3::putBucket(): [BucketAlreadyOwnedByYou] Your previous request to create the named bucket succeeded and you already own it. in /Applications/XAMPP/xamppfiles/htdocs/image_thumbnail_creation_php/S3.php on line 188
We successfully uploaded your file.upload google.jpgupload1 
Notice: Undefined index: in /Applications/XAMPP/xamppfiles/htdocs/image_thumbnail_creation_php/index.php on line 130

Notice: Undefined variable: putObjectFile in /Applications/XAMPP/xamppfiles/htdocs/image_thumbnail_creation_php/S3.php on line 187

Warning: S3::putBucket(): [BucketAlreadyOwnedByYou] Your previous request to create the named bucket succeeded and you already own it. in /Applications/XAMPP/xamppfiles/htdocs/image_thumbnail_creation_php/S3.php on line 188

Warning: S3::inputFile(): Unable to open input file: in /Applications/XAMPP/xamppfiles/htdocs/image_thumbnail_creation_php/S3.php on line 224
Something went wrong while uploading your file... sorry.

有人能帮我吗?提前致谢...

凯尔文·坎塔里亚

试试这个:这对我有用

<?php 
// Bucket Name
if(isset($_FILES['image']['name']))
{
    $bucket="buket_name";
    if (!class_exists('S3'))require_once('S3.php');
    if (!defined('awsAccessKey')) define('awsAccessKey', 'your key');
    if (!defined('awsSecretKey')) define('awsSecretKey', 'your secret key');
    $s3 = new S3(awsAccessKey, awsSecretKey);
    $s3->putBucket($bucket, S3::ACL_PUBLIC_READ);
    function make_thumb($src, $dest, $desired_width,$extension)
    {
        $extension=strtolower($extension);  
        if($extension == 'jpeg' ||  $extension == 'jpg' )
        {
            $source_image = imagecreatefromjpeg($src);
        }
        if($extension == 'png')
        {
            $source_image = imagecreatefrompng($src);
        }
        if($extension == 'gif')
        {
            $source_image = imagecreatefromgif($src);
        }
        $width = imagesx($source_image);
        $height = imagesy($source_image);
        $desired_height = floor($height * ($desired_width / $width));
        $virtual_image = imagecreatetruecolor($desired_width, $desired_height);
        imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
        if($extension == 'jpeg' ||  $extension == 'jpg')
        {
            imagejpeg($virtual_image, $dest);
        }
        if($extension == 'png' )
        {
            imagepng($virtual_image, $dest);
        }
        if($extension == 'gif' )
        {
            imagegif($virtual_image, $dest);
        }
    }

    $files=$_FILES;
    $form_name="image";
    $thumb_tmp_path="uploads/tmp/";
    $upload_path="user/";//s3 buket folder
    $name=$files[$form_name]['name'];
    $i_array=explode(".",$name);
    $ext=end($i_array);
    $size=$files[$form_name]['size'];
    $tmp=$files[$form_name]['tmp_name'];
    $names=time().$name;
    make_thumb($tmp,$thumb_tmp_path."thumb/".$names,512,$ext);
    $s3->putObjectFile($thumb_tmp_path."thumb/".$names,$bucket,$upload_path."thumb/".$names,S3::ACL_PUBLIC_READ);
    unlink($thumb_tmp_path."thumb/".$names);
    //if you upload full image than you remove belove comment
    //$s3->putObjectFile($tmp,$bucket,$upload_path.$names,S3::ACL_PUBLIC_READ);  
    echo "<img src='".$s3file='http://'.$bucket.'.s3.amazonaws.com/'.$upload_path."thumb/".$names."'/>";
}
?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
   <form method="post" action="" enctype="multipart/form-data">
    <input type="file" name="image">
    <input type="submit" value="upload">
   </form>
</body>
</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Boto将文件上传到S3存储桶中的目录

如何使用Boto将文件上传到S3存储桶中的目录

如何使用Java Apache HttpClient将文件上传到公共AWS S3存储桶

如何从React前端通过Spring后端将文件上传到S3存储桶?

如何将InMemoryUploadedFile上传到我的S3存储桶?

流星亚马逊S3图片上传缩略图

亚马逊s3,将文件上传到存储桶中的文件夹中

回送组件存储:如何在上传到Amazon S3之前将图像转换为缩略图

如何将图像上传到AWS S3并获取图像文件的S3存储桶URL并一次保存到dynamodb-Android

我的Amazon S3存储桶网址是什么?如何将许多文件上传到s3存储桶?

使用Lambda函数在Amazon S3存储桶上创建缩略图

使用Boto3将缩略图图像上传到S3

如何使用axios将文件上传到Amazon S3存储桶?

文件未上传到s3存储桶

如何将控制器中的生成pdf文件上传到S3存储桶

如何将文件上传到AWS S3存储桶?

生成缩略图并将其上传到节点中的s3存储桶的最佳方法是什么?

在使用multer重命名文件之前,如何将其上传到s3存储桶?

-AWS C#.Net Core-如何将.jpg图像上传到S3存储桶而不将其另存为文件

如何授予Lambda将文件上传到terraform中的s3存储桶的权限?

如何获取将图像上传到s3存储桶的文件名?

boto3如何将dict / json输出上传到s3存储桶?

将文件上传到S3存储桶

如何使用boto3将Github上的文件上传到AWS S3存储桶?

如何通过特定文件类型限制将S3文件上传到存储桶?

使用flask将文件上传到亚马逊s3

将文件上传到 s3 存储桶、sdk php 中的特定文件夹

尝试将文件从 lambda tmp 文件夹上传到 s3 存储桶

如何将文件从我的本地存储上传到 S3?