通过使用通配符API PHP从S3中删除 [英] delete from S3 using api php using wildcard

查看:261
本文介绍了通过使用通配符API PHP从S3中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这方面的工作code从S3删除的文件和文件夹。 你会怎么删除使用通配符*?

  $ S3 =新AmazonS3();

$桶='mybucket';
$文件夹='myDirectory / *'; //这行不通

$响应= $ S3-> get_object_list($桶,阵列(
   'preFIX'=> $文件夹
));

的foreach($响应为$ V){
    $ S3-> delete_object($桶,$ V);
}
 

解决方案

presumably的使用通配符* 的表示要删除所有对象一次,而不是一次一个?

这通过<一个是可能的href="http://docs.amazonwebservices.com/AWSSDKforPHP/latest/#m=AmazonS3/delete_all_objects">delete_all_objects($bucket, $ PCRE),其中 $ PCRE 是一个可选的 Perl兼容的正防爆pression(PCRE)来筛选对(默认为 PCRE_ALL ,这是/.*/我),例如:

  $ S3 =新AmazonS3();
$响应= $ S3-&GT; delete_all_objects($桶,#myDirectory /.*#);
 

我选择了而不是通常的 / 的模式封闭分隔符,以避免逃脱(不是问题在这里的单斜线,但它得到的凌乱很快用于更复杂的情况下),请参见分隔符有关详细信息。


实现注意

请注意,删除多个对象没有可能过去在亚马逊S3 API级和模拟在 AWS SDK的PHP 与for循环中的 delete_all_objects()为好,也就是说,它使用的每个对象还是一个请求;幸运的是,亚马逊终于推出了亚马逊S3 - 多目标删除在2011年12月:

  

亚马逊S3的新多目标删除使您能够以   从S3斗与单个请求删除多达1000个对象。

支持的 S3多对象中删除的已加入的 AWS SDK的PHP 的此后不久,因此,看到的 AWS SDK的PHP 1.4.8的Zanarkand

  

该AmazonS3类现在,您可以删除多个亚马逊S3对象   使用一个HTTP请求。这已被公开为   delete_objects()方法,和delete_all_objects()和   delete_all_object_versions()方法已被重写,以利用这个新的亚马逊S3的功能。

有一个专用的多对象的示例删除(即不使用通配符)显示,以及:

  $ S3 =新AmazonS3();
$响应= $ S3-&GT; delete_objects($桶,阵列(
    '对象'=&GT;阵列(
        阵列('键'=&GT;'FILE1.TXT),
        阵列('键'=&GT;'FILE2.TXT),
    )
));
 


i have this working code to delete files and folders from s3. how would you delete using wildcard * ?

$s3 = new AmazonS3();

$bucket = 'mybucket';
$folder = 'myDirectory/*';// this doesnt work

$response = $s3->get_object_list($bucket, array(
   'prefix' => $folder
));

foreach ($response as $v) {
    $s3->delete_object($bucket, $v);
}

解决方案

Presumably using wildcard * means you want to delete all objects at once rather than one at a time?

This is possible via delete_all_objects($bucket, $pcre), where $pcreis an optional Perl-Compatible Regular Expression (PCRE) to filter the names against (default is PCRE_ALL, which is "/.*/i"), e.g.:

$s3 = new AmazonS3();
$response = $s3->delete_all_objects($bucket, "#myDirectory/.*#");

I've chosen # rather than the usual / as the pattern enclosing delimiter to avoid escaping (not a problem with the single slash here, but it get's messy soon for more complex cases), see Delimiters for details.


Implementation Note

Please note that deleting multiple objects had not been possible in the past at the Amazon S3 API level and simulated in the AWS SDK for PHP with a for loop within delete_all_objects() as well, i.e. it used one request per object still; fortunately, Amazon has finally introduced Amazon S3 - Multi-Object Delete in December 2011:

Amazon S3's new Multi-Object Delete gives you the ability to delete up to 1000 objects from an S3 bucket with a single request.

Support for S3 Multi Object Delete has been added to the AWS SDK for PHP shortly thereafter accordingly, see AWS SDK for PHP 1.4.8 "Zanarkand":

The AmazonS3 class now allows you to delete multiple Amazon S3 objects using a single HTTP request. This has been exposed as the delete_objects() method, and the delete_all_objects() and delete_all_object_versions() methods have been rewritten to leverage this new Amazon S3 feature.

An example for a dedicated multi-object delete (i.e. without wildcards) is shown as well:

$s3 = new AmazonS3();
$response = $s3->delete_objects($bucket, array(
    'objects' => array(
        array('Key' => 'file1.txt'),
        array('Key' => 'file2.txt'),
    )
));


这篇关于通过使用通配符API PHP从S3中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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