Using MD5 to Verify File Consistency (SDK for Python)
You can call the following APIs to verify file consistency using MD5 during object uploads:
- Uploading Objects - Text-Based (SDK for Python)
- Uploading an Object - File-Based (SDK for Python)
- Uploading an Object - Append (SDK for Python)
- Uploading a Part (SDK for Python)
Sample Code 1: Performing a Text-Based Upload
This example uses MD5 to verify file consistency during text upload.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import base64 import hashlib import os import traceback from obs import ObsClient from obs import PutObjectHeader # Obtain an AK/SK pair using environment variables (recommended) or import it in other ways. Using hard coding may result in leakage. # Obtain an AK and SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. # Before running the sample code, ensure that the environment variables AccessKeyID and SecretAccessKey have been configured. ak = os.getenv("AccessKeyID") sk = os.getenv("SecretAccessKey") # If you use a temporary AK/SK pair and a security token to access OBS, obtain them from environment variables. # security_token = os.getenv("SecurityToken") # Set server to the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use. server = "https://obs.ap-southeast-1.myhuaweicloud.com" # Create an obsClient instance. # If you use a temporary AK/SK pair and a security token to access OBS, you must specify security_token when creating an instance. obs_client = ObsClient(access_key_id=ak, secret_access_key=sk, server=server) try: bucket_name = "examplebucket" object_key = "objectname" # Specify the text to be uploaded. content = 'Hello OBS' # Specify the additional headers for object upload. headers = PutObjectHeader() # Obtain the MD5 value. md5 = hashlib.md5(content.encode('utf-8')).digest() # Encode the MD5 value using Base64. md5_base64 = base64.b64encode(md5).decode('utf-8') headers.md5 = md5_base64 # Upload the text. resp = obs_client.putContent(bucket_name, object_key, content, headers=headers) # If status code 2xx is returned, the API call succeeds. Otherwise, the API call fails. if resp.status < 300: print('Put Content Succeeded') print('requestId:', resp.requestId) print('etag:', resp.body.etag) else: print('Put Content Failed') print('requestId:', resp.requestId) print('errorCode:', resp.errorCode) print('errorMessage:', resp.errorMessage) except Exception: print('Put Content Failed') print(traceback.format_exc()) |
Sample Code 2: Performing a File-Based Upload
This example uses MD5 to verify file consistency during single file upload.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | import base64 import hashlib import os import traceback from obs import ObsClient from obs import PutObjectHeader # Obtain an AK/SK pair using environment variables (recommended) or import it in other ways. Using hard coding may result in leakage. # Obtain an AK and SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. # Before running the sample code, ensure that the environment variables AccessKeyID and SecretAccessKey have been configured. ak = os.getenv("AccessKeyID") sk = os.getenv("SecretAccessKey") # If you use a temporary AK/SK pair and a security token to access OBS, obtain them from environment variables. # security_token = os.getenv("SecurityToken") # Set server to the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use. server = "https://obs.ap-southeast-1.myhuaweicloud.com" # Create an obsClient instance. # If you use a temporary AK/SK pair and a security token to access OBS, you must specify security_token when creating an instance. obs_client = ObsClient(access_key_id=ak, secret_access_key=sk, server=server) try: bucket_name = "examplebucket" # Specify the object name (the name after the file is uploaded). object_key = "objectname" # Specify the additional headers for object upload. headers = PutObjectHeader() # Specify the full path of the file to be uploaded, for example, aa/bb.txt. file_path = 'localfile' # (Optional) Specify the MD5 value of the object to be uploaded. with open(file_path, 'rb') as f: md5_obj = hashlib.md5() while True: data = f.read() if not data: break md5_obj.update(data) # Obtain the MD5 value. md5 = md5_obj.digest() # Encrypt the MD5 value using Base64. md5_base64 = base64.b64encode(md5) headers.md5 = md5_base64 # Upload the file. resp = obs_client.putFile(bucket_name, object_key, file_path, headers=headers) # If status code 2xx is returned, the API call succeeds. Otherwise, the API call fails. if resp.status < 300: print('Put File Succeeded') print('requestId:', resp.requestId) print('etag:', resp.body.etag) print('versionId:', resp.body.versionId) print('storageClass:', resp.body.storageClass) else: print('Put File Failed') print('requestId:', resp.requestId) print('errorCode:', resp.errorCode) except Exception: print('Put File Failed') print(traceback.format_exc()) |
Sample Code 3: Performing an Append Upload
This example uses MD5 to verify file consistency during append upload.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import base64 import hashlib import os import traceback from obs import AppendObjectContent, AppendObjectHeader from obs import ObsClient # Obtain an AK/SK pair using environment variables (recommended) or import it in other ways. Using hard coding may result in leakage. # Obtain an AK and SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. ak = os.getenv("AccessKeyID") sk = os.getenv("SecretAccessKey") # If you use a temporary AK/SK pair and a security token to access OBS, obtain them from environment variables. # security_token = os.getenv("SecurityToken") # Set server to the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use. server = "https://obs.ap-southeast-1.myhuaweicloud.com" # Create an obsClient instance. # If you use a temporary AK/SK pair and a security token to access OBS, you must specify security_token when creating an instance. obs_client = ObsClient(access_key_id=ak, secret_access_key=sk, server=server) try: headers = AppendObjectHeader() # Specify the message body for append upload. content = AppendObjectContent() # Specify the content to be appended. content.content = 'Hello OBS' # Specify the starting position (byte 0 in this example) the content is appended to. content.position = 0 # If you upload an object for the first time using the append upload, an error will be reported (status code 409) if an ordinary object with the same name already exists. bucket_name = "examplebucket" # Specify the name of the object to append content to. object_key = "objectname" # Obtain the MD5 value. md5 = hashlib.md5(content.content.encode('utf-8')).digest() # Encode the MD5 value using Base64. md5_base64 = base64.b64encode(md5).decode('utf-8') headers.md5 = md5_base64 # Append content to the object. resp = obs_client.appendObject(bucket_name, object_key, content, headers=headers) # If status code 2xx is returned, the API call succeeds. Otherwise, the API call fails. if resp.status < 300: print('Append Object Succeeded') print('requestId:', resp.requestId) print('crc64:', resp.body.crc64) print('nextPosition:', resp.body.nextPosition) else: print('Append Object Failed') print('requestId:', resp.requestId) print('errorCode:', resp.errorCode) print('errorMessage:', resp.errorMessage) except Exception: print('Append Object Failed') print(traceback.format_exc()) |
Sample Code 4: Performing a Multipart Upload
This example uses MD5 to verify data consistency during part upload.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import os import traceback from obs import ObsClient # Obtain an AK/SK pair using environment variables (recommended) or import it in other ways. Using hard coding may result in leakage. # Obtain an AK and SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. ak = os.getenv("AccessKeyID") sk = os.getenv("SecretAccessKey") # If you use a temporary AK/SK pair and a security token to access OBS, obtain them from environment variables. # security_token = os.getenv("SecurityToken") # Set server to the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use. server = "https://obs.ap-southeast-1.myhuaweicloud.com" # Create an obsClient instance. # If you use a temporary AK/SK pair and a security token to access OBS, you must specify security_token when creating an instance. obs_client = ObsClient(access_key_id=ak, secret_access_key=sk, server=server) try: bucket_name = "examplebucket" # Specify the name of the object to be uploaded to the bucket. object_key = "objectname" # Specify the part number, which ranges from 1 to 10,000. part_number = "your part_number" # Specify the ID of the multipart upload. upload_id = "your uploadid" # Specify the content of the part to be uploaded as a string or readable object. object = 'Hello OBS' # Specify whether object indicates the file path. The default value is False. is_file = False # Specify the start offset (in bytes) of a part in the source file. The default value is 0. offset = 0 # Specify the size (in bytes) of a part in the source file. The default value is the file size minus the offset. part_size = 9 * 1024 * 1024 # Specify whether to automatically calculate the MD5 value of the data to be uploaded. The default value is False. is_attach_md5 = True # Upload the part to a specified bucket using the multipart upload ID. resp = obs_client.uploadPart(bucket_name, object_key, part_number, upload_id, object, is_file, part_size, offset, isAttachMd5 = is_attach_md5) # If status code 2xx is returned, the API call succeeds. Otherwise, the API call fails. if resp.status < 300: print('Upload Part Succeeded') print('requestId:', resp.requestId) print('etag:', resp.body.etag) else: print('Upload Part Failed') print('requestId:', resp.requestId) print('errorCode:', resp.errorCode) print('errorMessage:', resp.errorMessage) except Exception: print('Upload Part Failed') print(traceback.format_exc()) |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot