You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Amazon S3中列出含指定UUID的文件(忽略UNIX时间戳前缀)

如何列出以UNIX时间戳开头、包含指定UUID的S3文件?

S3的listObjectsV2接口不支持中间带通配符的Prefix匹配,你之前写的docker/*{$uuid}会被当成普通字符串处理,所以无法得到预期结果。这里提供可行的处理方式:

方法:先批量获取前缀下的所有文件,本地过滤

先获取docker/前缀下的全部文件(注意处理分页,避免因文件过多导致漏取),再在本地筛选出包含目标UUID的文件。示例代码如下:

// 先获取docker/前缀下的所有文件,处理分页逻辑
$allFiles = [];
$nextToken = null;
do {
    $response = $s3->listObjectsV2([
        'Bucket' => 'logs',
        'Prefix' => 'docker/',
        'ContinuationToken' => $nextToken
    ]);
    $allFiles = array_merge($allFiles, $response['Contents']);
    $nextToken = $response['NextContinuationToken'] ?? null;
} while ($nextToken);

// 替换为你的目标UUID列表
$uuidList = ['uuid-123', 'uuid-456', 'uuid-789'];
$targetFiles = [];

// 遍历筛选包含指定UUID的文件
foreach ($uuidList as $uuid) {
    foreach ($allFiles as $file) {
        if (str_contains($file['Key'], $uuid)) {
            $targetFiles[] = $file;
        }
    }
}

// $targetFiles即为需要清理的文件集合

注意:S3的Prefix仅支持匹配以指定字符串开头的对象键,不支持中间或后缀的模糊匹配,所以无法通过接口直接筛选包含UUID的文件,必须在本地完成过滤逻辑。

内容的提问来源于stack exchange,提问作者Meechew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 11:05:03