2014-01-19 41 views
0

我正在爲我的項目使用backbonejs。我的問題是,如何在亞馬遜S3中執行CRUD操作?到目前爲止,我可以上傳/創建文件到存儲桶並檢索文件,但我不知道如何更新和刪除文件。Amazon S3 - 如何執行CRUD操作?

的config.php

<?php 
//Bucket Name 
$bucket_name ="my_bucket"; 

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

//AWS access informations 
if (!defined('awsAccessKey')) define('awsAccessKey', 'access_key'); 
if (!defined('awsSecretKey')) define('awsSecretKey', 'secret_key'); 

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

//this is used to create a bucket in amazon S3 
$s3->putBucket($bucket_name, S3::ACL_PUBLIC_READ); 
?> 

upload_image.php

<?php 
header('Access-Control-Allow-Origin: *'); 
header('Access-Control-Allow-Methods: GET, POST'); 

    if(isset($_POST) && !empty($_FILES) && !empty($_FILES['uploadfile']['name']) && isset($_POST['productId'])) { 

     //upload file formats 
     $valid_file_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP"); 
     function get_file_extension($file_name) { 
      return substr(strrchr($file_name,'.'),1); 
     } 

     $filename = $_FILES['uploadfile']['name']; 
     $tmp_filename = $_FILES['uploadfile']['tmp_name']; 
     $filetype = strtolower($_FILES['uploadfile']['type']); 
     //get file extenstion to verify the format 
     $extension = get_file_extension($filename); 

     if(in_array($extension,$valid_file_formats)){ 
      //include config.php 
      include_once "config.php"; 
      //set content type in headers inorder to display image in browser 
      $header = array('Content-Type' => $filetype); 
      //change filename 
      $new_file_name = "w3_".time().".".$extension; 

      if($s3->putObject(S3::inputFile($tmp_filename), $bucket_name , $new_file_name, S3::ACL_PUBLIC_READ, array(), $header)){ 
       $img = "http://".$bucket_name.".s3.amazonaws.com/".$new_file_name; 
       // array for JSON response 
       $response = array(); 
       // check for required fields 

        $productId = $_POST['productId']; 
        $imageLink = $img; 
        // include db connect class 
        require_once __DIR__ . '/db_connect.php'; 
        // connecting to db 
        $db = new DB_CONNECT(); 
        // mysql inserting a new row 
        $result = mysql_query("INSERT INTO tbl_images(productId, imageLink,dateCreated) VALUES('$productId', '$imageLink',NOW())"); 
        // check if row inserted or not 

        if ($result) { 
         // successfully inserted into database 
         $response["success"] = 1; 
         $response["message"] = "Image successfully uploaded."; 
         // echoing JSON response 
         echo json_encode($response); 
        } else { 
         // failed to insert row 
         $response["success"] = 0; 
         $response["message"] = "Oops! An error occurred."; 
         // echoing JSON response 
         echo json_encode($response); 
        } 


       echo "1-".$img; 
      } else { 
       echo "2-Upload Failed"; 
      } 

     } else { 
      echo "3-Not a Valid Format"; 
     } 

    } else { 
     echo "4-Please Select a File"; 
    } 

    ?> 

非常感謝提前。

回答

相關問題