繁体   English   中英

删除多个对象Amazon s3 PHP SDK

[英]Delete multiple objects Amazon s3 PHP SDK

我有问题一次删除多个对象..

使用这个库 - 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 URL隐藏):

$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

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

它实际上看起来你没有正确引用文件(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.jpghttp://s3.amazonaws.com/all-of-my-things/photos/2013/12/photo0005.jpg

水桶是all-of-my-things ,关键是photos/2013/12/photo0005.jpg

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

希望这会有所帮助。 当您直接使用SDK对象时,不要害怕与模块的问题跟踪器或直接在SDK的问题跟踪器上联系以解决此类问题。

要删除s3文件,请使用它

它对我有用 -

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

对于测试尝试:

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

如果发生异常,catch将仅返回“出现错误”,而不是deleteObjects()方法返回false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM