Amazon S3 - 如何执行CRUD操作?

时间:2014-01-19 04:25:02

标签: php amazon-web-services amazon-s3

我正在为我的项目使用backbonejs。我的问题是,如何在亚马逊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";
    }

    ?>

提前多多感谢。

1 个答案:

答案 0 :(得分:0)

看一下php sdk http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html deleteMatching中有命令可以从存储桶中删除项目。

至于更新,再次调用putObject来覆盖文件。