Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added S3 inventory to storage purge #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions admin/s3-add-inventory.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh
SOURCE_BUCKET="bucket1"
DESTINATION_BUCKET="bucket2"
INVENTORY_ID="inventory1"

aws s3api put-bucket-inventory-configuration \
--bucket $SOURCE_BUCKET \
--id $INVENTORY_ID \
--inventory-configuration \
'{
"Schedule": {
"Frequency": "Daily"
},
"IsEnabled": true,
"Destination": {
"S3BucketDestination": {
"Bucket": "arn:aws:s3:::'$DESTINATION_BUCKET'",
"Format": "CSV"
}
},
"OptionalFields": ["Size","LastModifiedDate","ETag"],
"IncludedObjectVersions": "Current",
"Id": "'$INVENTORY_ID'"
}'
2 changes: 1 addition & 1 deletion admin/storage_existing_files
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ foreach ($shardIDs as $shardID) {

$sql = "SELECT DISTINCT storageFileID FROM storageFileItems";
$ids = Zotero_DB::columnQuery($sql, false, $shardID);

if (!$ids) continue;
$inserted = 0;
$origInsertSQL = "INSERT IGNORE INTO storageFilesExisting VALUES ";

Expand Down
35 changes: 11 additions & 24 deletions admin/storage_purge
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ define('SET_NAME_DIFF', 'storage:keys:diff');
define('MAX_TIME', time() - (60 * 60 * 24 * 30 * 6)); // 6 months
define('DRY_RUN', false);

$startKey = empty($argv[1]) ? false : $argv[1];
// Gzipped CSV file
$csvFilename = empty($argv[1]) ? false : $argv[1];

$redis = Z_Redis::get();

Expand All @@ -26,28 +27,18 @@ if (!$redis->srandmember(SET_NAME_EXISTING)) {
$redis->del(SET_NAME_S3);
$redis->del(SET_NAME_DIFF);

echo "Listing objects on S3\n";

// Get iterator for all files in S3
$s3 = Z_Core::$AWS->get('s3');
$options = [
'Bucket' => Z_CONFIG::$S3_BUCKET
];
if ($startKey) {
$options['Marker'] = $startKey;
}
$iterator = $s3->getIterator('ListObjects', $options);
$input = gzopen($csvFilename, 'r');

$deleted = 0;
$ignoredS3 = 0;
$ignoredDB = 0;
$i = 0;
$arr = [SET_NAME_S3];

foreach ($iterator as $object) {
$key = $object['Key'];
$lastModified = $object['LastModified'];
while (($row = fgetcsv($input)) !== false) {
$key = $row[1];
$lastModified = $row[2];

$date = DateTime::createFromFormat('Y-m-d\TH:i:s.uP', $lastModified);
if (!$date) {
die("Invalid date '$lastModified'\n");
Expand Down Expand Up @@ -84,7 +75,7 @@ if (sizeOf($arr) > 1) {
}

function deleteFromS3() {
$s3 = Z_Core::$AWS->get('s3');
$s3 = Z_Core::$AWS->createS3();
$redis = Z_Redis::get();

$deleted = 0;
Expand All @@ -102,14 +93,10 @@ function deleteFromS3() {
// See if file was marked as used recently in the database. The modification time in S3
// isn't updated when an existing file is reused, so this is common.
$info = new Zotero_StorageFileInfo;
try {
$info->parseFromS3Key($key);
}
// Ignore other files ('bookmarklet_upload.html')
catch (Exception $e) {
continue;
}

$info->filename = $key;
//$info->zip = ?;

$info = Zotero_Storage::getLocalFileInfo($info);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a short window between getting file info and deleting it, where it can be updated by other user and result in a missing S3 file. One option would be to move this code in transaction.

if ($info) {
$lastAdded = DateTime::createFromFormat('Y-m-d H:i:s', $info['lastAdded']);
Expand Down