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

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

问题描述

我有这个工作代码可以从 s3 中删除文件和文件夹.您将如何使用通配符 * 删除?

$s3 = new AmazonS3();$bucket = 'mybucket';$folder = 'myDirectory/*';//这不起作用$response = $s3->get_object_list($bucket, array('前缀' =>$文件夹));foreach ($response as $v) {$s3->delete_object($bucket, $v);}

解决方案

大概使用通配符 * 意味着您想一次删除所有对象而不是一次删除一个对象?

这可以通过 delete_all_objects($bucket, $pcre),其中 $pcre 是一个可选的 Perl 兼容正则表达式 (PCRE),用于过滤名称(默认为 PCRE_ALL,即 <代码>"/.*/i"),例如:

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

我选择了 # 而不是通常的 / 作为模式封闭分隔符以避免转义(这里的单斜线不是问题,但很快就会变得混乱对于更复杂的情况),请参阅分隔符了解详情.><小时>

实施说明

请注意,过去无法在 Amazon S3 API 级别删除多个对象,并在 中模拟适用于 PHP 的 AWS SDKdelete_all_objects() 中也有一个 for 循环,即它仍然使用每个对象一个请求;幸运的是,亚马逊终于在 2011 年 12 月推出了Amazon S3 - 多对象删除:

<块引用>

Amazon S3 新的多对象删除功能让您能够使用单个请求从 S3 存储桶中删除最多 1000 个对象.

S3 多对象删除的支持随即添加到 AWS SDK for PHP 中,相应地,请参阅适用于 PHP 1.4.8 "Zanarkand" 的 AWS 开发工具包:

<块引用>

AmazonS3 类现在允许您删除多个 Amazon S3 对象使用单个 HTTP 请求.这已被曝光为delete_objects() 方法,以及 delete_all_objects() 和delete_all_object_versions() 方法已被重写以利用这一新的 Amazon S3 功能.

还显示了专用多对象删除(即没有通配符)的示例:

$s3 = new AmazonS3();$response = $s3->delete_objects($bucket, array('对象' =>大批(array('Key' => 'file1.txt'),array('Key' => '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天全站免登陆