Answers:
Amazon现在可以设置存储桶策略以自动使内容过期:
http://docs.amazonwebservices.com/AmazonS3/latest/UG/ObjectExpiration.html
同时,Amazon引入了S3生命周期(请参阅介绍性博客文章Amazon S3-Object Expiration),您可以在其中指定存储桶中对象的最长使用期限(以天为单位)-有关对象通过S3 API或AWS管理的使用情况的详细信息,请参阅对象过期。安慰。
使用s3cmd实用程序
源删除旧存储桶的shell脚本:http :
//shout.setfive.com/2011/12/05/deleting-files-older-than-specified-time-with-s3cmd-and-bash/
#!/bin/bash
# Usage: ./deleteOld "bucketname" "30 days"
s3cmd ls s3://$1 | while read -r line; do
createDate=`echo $line|awk {'print $1" "$2'}`
createDate=`date -d"$createDate" +%s`
olderThan=`date -d"-$2" +%s`
if [[ $createDate -lt $olderThan ]]
then
fileName=`echo $line|awk '{$1=$2=$3=""; print $0}' | sed 's/^[ \t]*//'`
echo $fileName
if [[ $fileName != "" ]]
then
s3cmd del "$fileName"
fi
fi
done;
Video 1280x720 (2)13201781136780000000.mp4
?
不,S3只是一个数据存储。您需要使用一些外部客户端来定期删除旧文件。
我发现使用AWS CLI的解决方案删除批处理速度更快
#!/usr/bin/env php
<?php
//remove files which were created 24 hrs ago
$fcmd = 'aws s3 ls s3://<bucket>/<prefix>/ | awk \'{$3=""; print $0}\'';//remove file size and handle file with spaces
exec($fcmd, $output, $return_var);
$seconds_24_hour = 24 * 60 * 60;
$file_deleted_count = 0;
if (!empty($output)) {
$deleted_keys = array();
foreach ($output as $file) {
$file_path = substr($file, 21);
$file_time_stamp = substr($file, 0, 19); //2017-09-19 07:59:41
if (time() - strtotime($file_time_stamp) > $seconds_24_hour) {
$deleted_keys[]["Key"] = "<prefix>/" . $file_path;
$file_deleted_count++;
}
}
if (!empty($deleted_keys)) {
$json_data_delete = array("Objects" => $deleted_keys);
echo $cmd = ("aws s3api delete-objects --bucket <bucket> --delete '" . json_encode($json_data_delete) . "'");
system($cmd);
}
echo "\n$file_deleted_count files deleted from content_media\n";
}
批量删除的参考/programming//a/41734090/1589444
使用100%通过案例处理具有空间的文件的参考/programming/36813327/how-to-display-only-files-from-aws-s3-ls-command