删除多个对象Amazon s3 PHP SDK [英] Delete multiple objects Amazon s3 PHP SDK

查看:86
本文介绍了删除多个对象Amazon s3 PHP SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having issues deleting multiple objects at once..

使用此库- https://github.com/aws/aws-sdk-php-laravel -使用该库的其他任何东西(放入,获取,删除单个对象等)都没有问题

Using this library- https://github.com/aws/aws-sdk-php-laravel - I have no issues with anything else using the library (putting, getting, deleting single objects, etc.)

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

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

我得到了返回"Success deleting files.";,但是文件没有被删除.

I get returned "Success deleting files.";, but the files are not being deleted.

我的存储桶名称是正确的,并且根据文档,$json['attachmentArray']的格式正确(同样,我在放置,获取,删除等其他区域也没有问题

My bucket name is correct, and the $json['attachmentArray'] is in the right format according to the docs (again, I have no issues with other areas like put, get, delete)

有人能指出我在搞砸吗? 谢谢.

Can anyone point out where I'm messing up? Thanks.

尝试提供更多信息:

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

I am POSTing info from an Angular function to Laravel endpoint.

这是Angular函数(Firebase URL隐藏):

Here is the Angular function (Firebase URL hidden):

$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);
           });
    } 
}

这是我试图创建deleteObjects的Laravel控制器(使用@Kevin Florida的更新代码:

And here is my Laravel controller that I am trying to make deleteObjects (with updated code from @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'];的格式不正确?不确定;/

Now this returns a 500 server error.. I think I'm formatting $json['attachmentArray']; incorrectly? Not sure ;/

角函数

$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控制器

Laravel controller

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;
}

然后我从亚马逊在控制台中记录以下响应:

And I log this response in the console from Amazon:

=====================
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

因此,它现在似乎可以正常工作..但是,这些对象没有被删除吗? 我还做错了什么?疯了!

So it seems to be working correctly now.. But the objects are not being deleted? What else am I doing wrong? Going crazy on this one!

推荐答案

实际上看起来您没有正确引用文件(S3对象).请求的格式很好,但是存储桶名称和键可能是错误的.

It actually looks like you are not referencing the files (S3 objects) correctly. The formatting of the request is fine, but the bucket name and keys may be wrong.

看着:

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

您的存储桶是"teamite/team1"还是"teamite"?请记住,S3具有平坦的结构.没有子桶".如果您将内容布置在嵌套文件结构中,则必须将路径或伪文件夹包含在密钥中.

Is your bucket called "teamite/team1" or just "teamite"? Keep in mind that S3 has a flat structure. There are no "subbuckets". If you have things laid out in a nested file structure, then the paths, or pseudo-folders, need to be included as part of the key.

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

For example, in this imaginary URL to an object in S3: http://s3.amazonaws.com/all-of-my-things/photos/2013/12/photo0005.jpg

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

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

The deleteObjects() call you are making is returning successfully because of the idempotent nature of S3 operations. If you delete an object multiple times, it will return successfully every time. What you are seeing here is that you are deleting objects using the wrong keys. Those keys don't exist, and therefore are already deleted.

希望这会有所帮助.不要害怕直接接触模块的问题跟踪器或直接接触在 SDK的问题跟踪器上查找当您直接使用SDK时出现的此类问题对象.

Hopefully this helps. Don't be afraid to reach out on the module's issue tracker or directly on the SDK's issue tracker for problems like this when you are working directly with the SDK objects.

这篇关于删除多个对象Amazon s3 PHP SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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