我有一個包含幾乎重復檔案的 S3 存盤桶:

如果我運行 AWS CLI,我會得到相同的檔案路徑,僅相差幾個位元組:
2021-09-23 16:36:36 134626 Original/53866358.xml
2021-09-23 16:36:36 134675 Original/53866358.xml
如果我查看單個物件頁面,它們都具有相同的鍵:


唯一的區別是
在其物件 URL 的末尾有(ASCII 回車)。據推測,這是更大的檔案。我的問題是:如何使用 AWS S3 CLI 獲得對其中每一個的唯一參考?我想洗掉結尾有回車的那些。
uj5u.com熱心網友回復:
這是一個有趣的問題,只是為了為我的解決方案如何提供幫助奠定基礎,我用一個簡單的 python 腳本重新創建了這個問題:
import boto3
s3 = boto3.client('s3')
s3.put_object(Bucket='example-bucket', Key='temp/key', Body=b'normal key')
s3.put_object(Bucket='example-bucket', Key='temp/key\r', Body=b'this is not the normal key')
從那里,您可以看到您描述的問題:
$ aws s3 ls s3://example-bucket/temp/
2021-12-03 20:14:45 10 key
2021-12-03 20:14:45 26 key
您可以使用 cli 列出具有更多詳細資訊的物件(已從此處的輸出中洗掉了一些詳細資訊):
$ aws s3api list-objects --bucket example-bucket --prefix temp/
{
"Contents": [
{
"Key": "temp/key",
"Size": 10
},
{
"Key": "temp/key\r",
"Size": 26
}
]
}
要洗掉鍵名中帶有 CR 的物件,腳本將是最簡單的,但您可以使用 CLI 洗掉它,只是語法有點笨拙:
## If you're using Unix or Mac
$ aws s3api delete-object --cli-input-json '{"Bucket": "example-bucket", "Key": "temp/key\r"}'
## If you're using Windows:
C:> aws s3api delete-object --cli-input-json "{""Bucket"": ""example-bucket"", ""
Key"": ""temp/key\r""}"
請注意參考 JSON 物件所需的語法,并在 Windows 上轉義引號。
從那里,很容易驗證這是否按預期作業:
$ aws s3 ls s3://example-bucket/temp/
2021-12-03 20:14:45 10 key
$ aws s3 cp s3://example-bucket/temp/key final_check.txt
download: s3://example-bucket/temp/key to ./final_check.txt
$ type final_check.txt
normal key
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/373688.html
