删除多个对象Amazon s3 PHP SDK

时间:2013-12-20 13:40:33

标签: javascript php amazon-s3 laravel amazon

我一次删除多个对象时遇到了问题..

使用这个图书馆 - https://github.com/aws/aws-sdk-php-laravel - 我使用该库(放置,获取,删除单个对象等)没有任何问题。

try {
        $s3 = AWS::get('s3');
        $s3->deleteObjects(array(
            'Bucket'  => $bucket,
            'Objects' => $json['attachmentArray']
        ));
        return "Success deleting files.";
    } catch (S3Exception $e) {
        return "There was an error.\n";
    }

deleteObjects Amazon Docs- http://docs.aws.amazon.com/AmazonS3/latest/dev/DeletingMultipleObjectsUsingPHPSDK.html

我返回"Success deleting files.";,但文件未被删除。

我的广告知识名称是正确的,$json['attachmentArray']的格式是正确的格式(再次,我对其他方面没有问题,比如put,get,delete)

有谁可以指出我搞砸了哪里? 感谢。

修改

尝试提供更多信息:

我正在从Angular函数向Laravel端点发布信息。

以下是Angular功能(Firebase网址隐藏):

$scope.deleteFile = function() {

  var r=confirm("Are you sure you want to delete this task & of it's all attachments?");
  if (r==true)
    {
      var attachmentArray = [];

      var atttachData = new Firebase('https://*.firebaseio.com/attachments');
        atttachData.on('value', function(dataSnapshot) {  
           dataSnapshot.forEach(function(childSnapshot) {
            var key   = childSnapshot.val().key;
            // attachmentArray.push(key); I tried this
               attachmentArray.push({Key:key});

            });
           });
                method: "POST",
                url: '/delete_task',
                data: {attachmentArray:attachmentArray},
                headers: {'Content-Type': 'application/json'},
           }).success(function(data){
              console.log(data);
           });
    } 
}

这是我的Laravel控制器,我试图制作deleteObjects(来自@Kevin Florida的更新代码:

 public function delete_task() {
    $request = Request::instance();
    $task = $request->getContent(); 
    $json = json_decode($task, TRUE); 
    $bucket ='bucketname/team1';

    // return $json['attachmentArray'];
    $s3 = AWS::get('s3');
    if(!$s3->deleteObjects(array(
                'Bucket'  => $bucket,
                'Objects' => $json['attachmentArray']
            ))) {
     return "error";
    } else {
     return "success";
    }
}

现在这会返回500服务器错误..我认为我格式错误$json['attachmentArray'];?不确定; /

EDIT2:

角度函数

$scope.deleteFile = function() {

  var r=confirm("Are you sure you want to delete this task & of it's all attachments?");
  if (r==true)
    {
      var attachmentArray = [];

      var atttachData = new Firebase('https://***/attachments');
        atttachData.on('value', function(dataSnapshot) {  
           dataSnapshot.forEach(function(childSnapshot) {
            var url   = childSnapshot.val().url;
            var name  = childSnapshot.val().name;
            var id    = childSnapshot.val().id;
            var key   = childSnapshot.val().key;              
            attachmentArray.push(key);
            });
           });
      $http({
                method: "POST",
                url: '/delete_task',
                data: {team:$scope.team,attachmentArray:attachmentArray,lastSegment:lastSegment},
                headers: {'Content-Type': 'application/json'},
           }).success(function(data){
              console.log(data);
           });          
    } 
}

Laravel控制器

public function delete_task() {
    $s3 = AWS::get('s3');
    $task = Input::all();
    $bucket ='teamite/team'.$task['team'];

    //make the array for Objects on this side
    foreach ($task['attachmentArray'] as $value) {
        $array[] = array('Key' => $value);
      }
     // return $task['attachmentArray'][0];

      $result = $s3->deleteObjects(array(
          'Bucket' => 'teamite/team1',
          'Objects' => $array
      ));

      return $result;
}

我在亚马逊的控制台中记录此响应:

=====================
Debug output of model
=====================

Model data
-----------

This data can be retrieved from the model object using the get() method of the model   (e.g. $model->get($key)) or accessing the model like an associative array (e.g. $model['key']).

[Deleted] => Array
    (
        [0] => Array
            (
                [Key] => 4700pzgds4i_ad.jpg
            )

        [1] => Array
            (
                [Key] => xxvu29ms4i_ad.jpg
            )

    )

[RequestId] => 477F0DA04101D82E

所以它现在似乎正常工作..但是对象没有被删除? 还有什么我做错了?对这个疯狂!

3 个答案:

答案 0 :(得分:4)

实际上看起来你没有正确引用文件(S3对象)。请求格式正常,但存储桶名称和密钥可能不正确。

看着:

$bucket = 'teamite/team'.$task['team'];

你的桶名为“teamite / team1”还是“teamite”?请记住,S3具有扁平结构。没有“subbuckets”。如果您在嵌套文件结构中布置了内容,则需要将路径或伪文件夹作为密钥的一部分包含在内。

例如,在S3中对象的虚构URL中:http://s3.amazonaws.com/all-of-my-things/photos/2013/12/photo0005.jpg

广告桶为all-of-my-things,密钥为photos/2013/12/photo0005.jpg

由于S3操作的幂等特性,您正在进行的deleteObjects()调用成功返回。如果多次删除对象,则每次都会成功返回。你在这里看到的是你使用错误的键删除对象。这些密钥不存在,因此已被删除。

希望这会有所帮助。当您直接使用SDK对象时,不要害怕在module's issue tracker上或直接在SDK's issue tracker上与这些问题联系。

答案 1 :(得分:2)

要删除s3文件,请使用

它对我有用 -

 $s3 = AWS::get('s3');
 $s3->deleteMatchingObjects('Your Bucket Name', 'File name to delete');

答案 2 :(得分:1)

测试试试:

if(!$s3->deleteObjects(array(
            'Bucket'  => $bucket,
            'Objects' => $json['attachmentArray']
        ))) {
 return "error";
} else {
 return "success";
}

catch只会在发生异常时返回“出现错误”,而不是deleteObjects()方法返回false