集成JqueryUpload插件与AWS PHP SDK来上传图片到S3 [英] Integrating JqueryUpload plugin with AWS php sdk to upload images to S3

查看:318
本文介绍了集成JqueryUpload插件与AWS PHP SDK来上传图片到S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jqueryfileupload插件与AWS.I的整合工作已成功完成上传,但现在我希望整合图像调整功能。
我使用插件codeI设置了使用最低$为例C $ C的是如下。

I am working on the integration of jqueryfileupload plugin with AWS.I have completed the upload section successfully,but now i am looking to integrate the image resize feature.
I am using this plugin code.I have set up an example using minimum code which is as below.

index.html的

 <!DOCTYPE HTML>
 <html>
 <head>
 <meta charset="utf-8">
 <title>jQuery File Upload Example</title>
 </head>
 <body>
 <input id="fileupload" type="file" name="files[]" data-url="aws/" multiple>
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
 <script src="js/vendor/jquery.ui.widget.js"></script>
 <script src="js/jquery.iframe-transport.js"></script>
 <script src="js/jquery.fileupload.js"></script>
 <script>
   $(function () {
     $('#fileupload').fileupload({
      dataType: 'json',
      done: function (e, data) {
        $.each(data.result.files, function (index, file) {
            $('<p/>').text(file.name).appendTo(document.body);
        });
      }
    });
  });
   </script>
  </body> 
 </html>

awssdk.php ---这就是我所说的被选中的图像后,该文件。

awssdk.php---This is the file I call after image is selected.

      <?php 
    $bucket = "my bucket name";
    $subFolder = "";  // leave blank for upload into the bucket directly
    if (!class_exists('S3'))require_once('S3.php');

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


    $options = array( 'image_versions' => array(
     'small' => array(
    'max_width' => 1920,
    'max_height' => 1200,
    'jpeg_quality' => 95
 ),

'medium' => array(
    'max_width' => 800,
    'max_height' => 600,
    'jpeg_quality' => 80
),

'thumbnail' => array(
    'max_width' => 80,
    'max_height' => 80
)
   ) 
  );

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

  function getFileInfo($bucket, $fileName) {
   global $s3;
   $fileArray = "";
   $size = $s3->getBucket($bucket);
   $furl = "http://" . $bucket . ".s3.amazonaws.com/".$fileName;
   $fileArray['name'] = $fileName;
   $fileArray['size'] = $size;
   $fileArray['url'] = $furl;
   $fileArray['thumbnail'] = $furl;
   $fileArray['delete_url'] = "server/php/index.php?file=".$fileName;
   $fileArray['delete_type'] = "DELETE";
   return $fileArray;
 }

  function uploadFiles($bucket, $prefix="") {
   global $s3;
   if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
     return "";
  }
  $upload = isset($_FILES['files']) ? $_FILES['files'] : null;
  $info = array();
  if ($upload && is_array($upload['tmp_name'])) {
foreach($upload['tmp_name'] as $index => $value) {
    $fileTempName = $upload['tmp_name'][$index];
    $fileName = (isset($_SERVER['HTTP_X_FILE_NAME']) ?      $_SERVER['HTTP_X_FILE_NAME'] : $upload['name'][$index]);
    $fileName = $prefix.str_replace(" ", "_", $fileName);
    // $response = $s3->create_object($bucket, $fileName, array('fileUpload' => $fileTempName, 'acl' => AmazonS3::ACL_PUBLIC, 'meta' => array('keywords' => 'example, test'),));
    $response = $s3->putObjectFile($fileTempName,$bucket,'images/'.$fileName,S3::ACL_PUBLIC_READ);
    //print_r($response);
    if ($response==1) {
        $info[] = getFileInfo($bucket, $fileName);
    } else {
             echo "<strong>Something went wrong while uploading your file... sorry.</strong>";
    }
}
} else {
    if ($upload || isset($_SERVER['HTTP_X_FILE_NAME'])) {
        $fileTempName = $upload['tmp_name'];
        $fileName = (isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : $upload['name']);
        $fileName =  $prefix.str_replace(" ", "_", $fileName);
        //$response = $s3->create_object($bucket, $fileName, array('fileUpload' => $fileTempName, 'acl' => AmazonS3::ACL_PUBLIC, 'meta' => array('keywords' => 'example, test'),));
        $response = $s3->putObjectFile($upload['tmp_name'],$bucket,$fileName,S3::ACL_PUBLIC_READ);
        if ($response->isOK()) {
            $info[] = getFileInfo($bucket, $fileName);
        } else {
                 echo "<strong>Something went wrong while uploading your file... sorry.</strong>";
        }
    }
}
header('Vary: Accept');
$json = json_encode($info);
$redirect = isset($_REQUEST['redirect']) ? stripslashes($_REQUEST['redirect']) : null;
if ($redirect) {
    header('Location: ' . sprintf($redirect, rawurlencode($json)));
    return;
}
if (isset($_SERVER['HTTP_ACCEPT']) && (strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false)) {
    header('Content-type: application/json');
} else {
    header('Content-type: text/plain');
}
 return $info;
 }
?>

这里是S3的I类现在用的。

Here is the S3 class I am using.

在JqueryUploadPLugin附带了一个服务器端的PHP类上传图片这是great.But因为我使用AWS我必须使用他们的API上传图片和插件code不会work.As我上面提到的我已经实现了上传部分,但需要帮助创建图像的缩略图和不同尺寸的图像之前,我upload.ie我希望图像上传3变化例如:原来,1024x768,100x100
UploadHandler.php 有一些功能创建缩放图像例如:保护功能create_scaled_image($ FILE_NAME,$版本,$选项)缩放等。我停留在整合这些功能,因为我是新来的面向对象的PHP和AWS。
任何一个做过类似的事情,可以给投入将是有益的
谢谢

The JqueryUploadPLugin ships with a server side PHP class to upload images which is great.But as I am using AWS I have to use their API to upload images and the plugin code won't work.As I mentioned above I have implemented the upload portion,but need help to create image thumbnails and different size images before I upload.i.e I want images to upload in 3 variations ex: original,1024x768,100x100.
The UploadHandler.php has few functions for creating scaled images example:protected function create_scaled_image($file_name, $version, $options) for scaling and others. I am stuck at integrating these functions as I am new to OO php and AWS.
Any one done something similar and can give inputs would be helpful
Thank you

推荐答案

看来你要使用从UploadHandler类的方法在你的code中的 awssdk.php

It seems you are trying to use the methods from UploadHandler class inside your code in awssdk.php.

我认为正确的方法让你去将通过定制UploadHandler类 - 更具体的 handle_file_upload 功能。这很可能是更有利于你,你可以访问UploadHandler类的所有优良特性这种方式。

I think the right way for you to go will be by customizing the UploadHandler class - more specifically the handle_file_upload function. This will be probably more beneficial for you as you get access to all the good features of UploadHandler class this way.

你可以把刚才下面几行你awssdk.php

And you can put just following lines in your awssdk.php

require('UploadHandler.php');
$upload_handler = new UploadHandler();

您可以看到目前的code在此功能存储在upload_dir选项设置的路径上传的文件。你只需要作 S3 类函数内的对象,并改变code到上传的文件存储到S3。

You can see currently the code in this function is storing the uploaded files in the path set in the "upload_dir" option. You just need to make an object of the S3 class inside that function and change the code to store uploaded file to S3.

我觉得行了,你将不得不在UploadHandler改变可能是703线。

I think the line you will have to change in UploadHandler is probably Line 703.

move_uploaded_file($uploaded_file, $file_path);

应该成为:

$s3 = new S3(awsAccessKey, awsSecretKey);
$response = $s3->putObjectFile($uploaded_file,$bucket,$file->name,S3::ACL_PUBLIC_READ);
if ($response->isOK()) {
    $info[] = getFileInfo($bucket, $fileName);
} else {
   $file->error = "<strong>Something went wrong while uploading your file... sorry.</strong>";
}

您可能还需要将相关的code - 例如getFileInfo功能 - 进UploadHandler类

You may also need to bring related code - for example the getFileInfo function - into the UploadHandler class.

行707-711似乎是通过PUT方法处理上传文件。为了处理这种情况下,你必须保持这些线路,让文件上传到服务器,然后再将该文件传送到S3,然后你就可以断开链接在你的服务器上的文件。但是,这也是安全的,只是注释掉那些如果允许POST方法而已。

Lines 707-711 appear to be for handling file uploads through PUT method. To handle this case you will have to keep those lines to let the file be uploaded to your server first and then transfer the file to S3, and then you can unlink the file in your server. But it is also safe to just comment out those lines if you are allowing POST method only.

行697-701似乎是用于处理拆分上传(我不知道)。你将不得不改变还,如果你想处理这种情况也是如此。

Lines 697-701 appear to be for handling split uploads(I'm not sure). You will have to change that also if you wish to handle that case as well.

这篇关于集成JqueryUpload插件与AWS PHP SDK来上传图片到S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆