简体   繁体   English

多个 object 通过在 s3 中仅打开一次 json 从 json 文件中删除

[英]Multiple object delete from json file by opening json only once in s3

I have few json files in s3, now want to delete some objects from that json file by opening the file for one time and delete those objects at a time and also chaeck if the objects are present in the json file or not.我在 s3 中有几个 json 文件,现在想通过一次打开文件从 json 文件中删除一些对象,一次删除这些对象,并检查对象是否存在于 json 文件中。 and lastly want to update the file in the same location in s3.最后想更新 s3 中相同位置的文件。

img_list is the list of objects to be deleted. img_list是要删除的对象列表。

img_list = ['0000002.jpg','0000003.jpg','0000006.jpg']

edit_jsons_path = 'path where the json is located in s3'

# Contents like this:
# {
#     "abc": {
#         "0000001.jpg": {
#         },
#         "0000002.jpg": {
#         },
#         "0000003.jpg": {
#         },
#         "0000004.jpg": {
#         }
#     },
#     "xyz": {
#         "xx": 467,
#         "yy": 4
#     }
# }

s3 = boto3.client('s3')
   
def json_edit(img_list,edit_jsons_path):
    response = s3.get_object(Bucket = bucket, Key = edit_jsons_path)
    json_content = response["Body"].read().decode("utf-8")
    data = json.loads(json_content)
    
    for obj in img_list:
        del data['abc'][obj]

    s3.put_object(Bucket = bucket, Key = edit_jsons_path, Body = json.dumps(data))

Expected output:预计 output:

{
    "abc": {
        "0000001.jpg": {
        },
        "0000004.jpg": {
        }
    },
    "xyz": {
        "xx": 467,
        "yy": 4
    }
}

S3 is object/blob storage. S3 是对象/blob 存储。 There isn't a way to "edit" an S3 file like a traditional file on your local file system.没有办法像本地文件系统上的传统文件那样“编辑”S3 文件。 You will need to read the file, make the edits you desire, and push the update over the top of the existing file.您将需要阅读文件,进行所需的编辑,然后将更新推送到现有文件的顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM