1. Install the oss2 library
root@ubuntu:~# pip3 install oss2 Collecting oss2 Downloading oss2-2.18. (283 kB) |████████████████████████████████| 283 kB 6.9 MB/s Collecting aliyun-python-sdk-core>=2.13.12 Downloading aliyun-python-sdk-core-2.15. (443 kB) |████████████████████████████████| 443 kB 67.8 MB/s Collecting aliyun-python-sdk-kms>=2.4.1 Downloading aliyun_python_sdk_kms-2.16.3-py2. (98 kB) |████████████████████████████████| 98 kB 8.3 MB/s Collecting crcmod>=1.7 Downloading crcmod-1. (89 kB) |████████████████████████████████| 89 kB 14.3 MB/s Collecting pycryptodome>=3.4.7 Downloading pycryptodome-3.20.0-cp35-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB) |████████████████████████████████| 2.1 MB 7.0 MB/s Requirement already satisfied: requests!=2.9.0 in /usr/lib/python3/dist-packages (from oss2) (2.22.0) Requirement already satisfied: six in /usr/lib/python3/dist-packages (from oss2) (1.14.0) Requirement already satisfied: cryptography>=2.6.0 in /usr/lib/python3/dist-packages (from aliyun-python-sdk-core>=2.13.12->oss2) (2.8) Collecting jmespath<1.0.0,>=0.9.3 Downloading jmespath-0.10.0-py2. (24 kB) Building wheels for collected packages: oss2, aliyun-python-sdk-core, crcmod Building wheel for oss2 () ... done Created wheel for oss2: filename=oss2-2.18. size=118170 sha256=cd8f24bb98e8449af56d9df9e826cce691d0527e53420f9c2f14a5b85b059d0c Stored in directory: /root/.cache/pip/wheels/b6/af/6a/36f940ed11f11b5e1002f94160a82c3dc35e8a357cedc02bb7 Building wheel for aliyun-python-sdk-core () ... done Created wheel for aliyun-python-sdk-core: filename=aliyun_python_sdk_core-2.15. size=535318 sha256=83c40a77e365ad83d4b74b43e757c6cea78628e8054706e48ae424ed8ddf7b69 Stored in directory: /root/.cache/pip/wheels/64/af/7e/b3ec025852e53d69463404a2b281d4587d45ee710cd45d0f38 Building wheel for crcmod () ... done Created wheel for crcmod: filename=crcmod-1.7-cp38-cp38-linux_x86_64.whl size=35994 sha256=9fc029c468fe548c4862be3ab4783e1d17b2ba52b72aef537d520b42f1c0a0ad Stored in directory: /root/.cache/pip/wheels/ca/5a/02/f3acf982a026f3319fb3e798a8dca2d48fafee7761788562e9 Successfully built oss2 aliyun-python-sdk-core crcmod Installing collected packages: jmespath, aliyun-python-sdk-core, aliyun-python-sdk-kms, crcmod, pycryptodome, oss2 Successfully installed aliyun-python-sdk-core-2.15.1 aliyun-python-sdk-kms-2.16.3 crcmod-1.7 jmespath-0.10.0 oss2-2.18.5 pycryptodome-3.20.0
2. Write scripts
1) After changing, the file will be output more beautifully
Parameter indent=4: in formatting JSON data into an indented form to make it easier to read.
Parameter ensure_ascii=False: to ensure that Chinese characters can be output correctly
Script json format output
import json import oss2 def update_oss_json_value(access_key_id, access_key_secret, endpoint, bucket_name, file_path, key_value_map): # Create an OSS client auth = (access_key_id, access_key_secret) bucket = (auth, endpoint, bucket_name) # Read file content content = bucket.get_object(file_path).read() # parse JSON data = (content) # Modify the value of the specified key for key, value in key_value_map.items(): if key in data: data[key] = value # Format the modified JSON into a beautiful string new_content = (data, indent=4, ensure_ascii=False) # Upload the modified file contents bucket.put_object(object_key, new_content.encode('utf-8')) print("File content has been updated.") # The path to the OSS file to be modifiedfile_path = 'test/domain_bak.json' # Specify the correct endpointendpoint = '' # key-value map to be modifiedkey_value_map = { 'superSign1': '/c/gulorex6', 'superSign2': '/c/xqjqujc2' } # Call function to update the content of the OSS fileupdate_oss_json_value('XXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXX', endpoint, 'test', file_path, key_value_map)
After execution, the json file will be formatted and output. If there are not very standardized, it will be more beautiful.
2) Keep each key-value pair occupying one row and empty four boxes at the beginning
import json import oss2 def update_oss_json_value(access_key_id, access_key_secret, endpoint, bucket_name, file_path, key_value_map): # Create an OSS client auth = (access_key_id, access_key_secret) bucket = (auth, endpoint, bucket_name) # Read file content content = bucket.get_object(file_path).read() # parse JSON data = (content) # Modify the value of the specified key for key, value in key_value_map.items(): if key in data: data[key] = value # Convert the modified JSON to a string, keeping each key-value pair occupying one line, and starting with four empty spaces new_content = ',\n'.join([f' "{k}": "{v}"' for k, v in ()]) # Add braces to keep JSON format new_content = '{\n' + new_content + '\n}' # Upload the modified file contents bucket.put_object(object_key, new_content.encode('utf-8')) print("File content has been updated.") # The path to the OSS file to be modifiedfile_path = 'test/domain_bak.json' # Specify the correct endpointendpoint = '' # key-value map to be modifiedkey_value_map = { 'superSign1': '/c/gulorex6', 'superSign2': '/c/xqjqujc2' } # Call function to update the content of the OSS fileupdate_oss_json_value('XXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXX', endpoint, 'test', file_path, key_value_map)
3. Add new key-value pairs through positional parameters
import json import oss2 import argparse def update_oss_json_value(access_key_id, access_key_secret, endpoint, bucket_name, file_path, *superSigns): # Create an OSS client auth = (access_key_id, access_key_secret) bucket = (auth, endpoint, bucket_name) # Read file content content = bucket.get_object(file_path).read() # parse JSON data = (content) # Generate key-value map key_value_map = {} for i, superSign in enumerate(superSigns, start=1): key = f'superSign{i}' key_value_map[key] = superSign # Modify the value of the specified key for key, value in key_value_map.items(): #if key in data: data[key] = value print(key,value) # Convert the modified JSON to a string, keeping each key-value pair occupying one line, and starting with four empty spaces new_content = ',\n'.join([f' "{k}": "{v}"' for k, v in ()]) # Add braces to keep JSON format new_content = '{\n' + new_content + '\n}' # Print updated key-value pairs for key, value in (): print(f"{key}: {value}") # Upload the modified file contents bucket.put_object(file_path, new_content.encode('utf-8')) print("File content has been updated.") if __name__ == "__main__": # Create a parameter parser parser = (description="Update OSS JSON file") # Add command line parameters #parser.add_argument("access_key_id", type=str, help="Access Key ID") #parser.add_argument("access_key_secret", type=str, help="Access Key Secret") #parser.add_argument("endpoint", type=str, help="OSS Endpoint") parser.add_argument("bucket_name", type=str, help="Bucket Name") parser.add_argument("file_path", type=str, help="Object Key") parser.add_argument("superSigns", nargs='+', type=str, help="Values for superSigns") # parse command line parameters args = parser.parse_args() # Call function to update the content of the OSS file update_oss_json_value('XXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXX', '', args.bucket_name, args.file_path, *)
implement
root@ubuntu:~# python3 'test-prod' 'test/domain_bak.json' '/api/c/gulorex6' '/api/c/xqjqujc2' superSign1 /api/c/gulorex6 superSign2 /api/c/xqjqujc2 。。。。。textjson。。。。。 File content has been updated. root@ubuntu:~# python3 'test-prod' 'test/domain_bak.json' '/api/c/gulorex6' '/api/c/xqjqujc2' '/api/c/acrik5' superSign1 /api/c/gulorex6 superSign2 /api/c/xqjqujc2 superSign3 /api/c/acrik5 。。。。。textjson。。。。。 File content has been updated. root@ubuntu:~# python3 'test-prod' 'test/domain_bak.json' '/api/c/gulorex6' '/api/c/xqjqujc2' '/api/c/acrik5' '/api/c/acrik5' superSign1 /api/c/gulorex6 superSign2 /api/c/xqjqujc2 superSign3 /api/c/acrik5 superSign4 /api/c/acrik5 。。。。。textjson。。。。。 File content has been updated.
This is the article about the implementation example of python script editing oss file. For more related python editing oss file content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!