s3 = boto3clients3 bucket_name = harpocrates-files mongo_client = MongoClientlocalhost db = mongo_clientfiles_index
collection = db.files
list all files in S3 bucket
response = s3.list_objects_v2( Bucket=bucket_name )
iterate over files in S3 bucket
for obj in response['Contents']: # check if file already exists in MongoDB if not db.files.find_one({'key': obj['Key']}): # if file does not exist, add to MongoDB file_metadata = { 'key': obj['Key'], 'size': obj['Size'], 'last_modified': obj['LastModified'] } db.files.insert_one(file_metadata)
原文地址: https://www.cveoy.top/t/topic/bzRC 著作权归作者所有。请勿转载和采集!