Java için OSS SDK, nesne yükleme, indirme ve kopyalama sırasında veri bütünlüğünü sağlamak üzere MD5 ve CRC-64 doğrulamasını kullanır.
Notlar
Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmıştır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'ye erişmek istiyorsanız dahili uç nokta kullanın. OSS bölgeleri ve uç noktaları hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.
Bu konuda erişim kimlik bilgileri ortam değişkenlerinden alınır. Erişim kimlik bilgilerini yapılandırma hakkında daha fazla bilgi için bkz. Python 1.0 için OSS SDK kullanarak erişim kimlik bilgilerini yapılandırma.
Bu konu, bir OSS uç noktası kullanarak bir OSSClient örneğinin nasıl oluşturulacağını gösterir. Özel alan adı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. Başlatma.
MD5 doğrulaması
Yükleme isteğine Content-MD5 eklediğinizde OSS, alınan verilerin MD5 hash değerini sağladığınız değerle karşılaştırır. Değerler eşleşmezse OSS bir InvalidDigest hatası döndürür; nesneyi yeniden yüklemeniz gerekir.
PutObject işleminde MD5 doğrulamasını yapılandırın:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the Endpoint for the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region that corresponds to the Endpoint, for example, cn-hangzhou. Note that this parameter is required for v4 signatures.
region = "cn-hangzhou"
# Replace examplebucket with the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Specify the full path of the object. The full path cannot include the bucket name. For example, exampledir/exampleobject.txt.
object_name = 'exampledir/exampleobject.txt'
# Specify the local path of the file to upload. The value of this variable is transferred to OSS as the content to upload. The file can be of any type, such as text, image, video, or audio.
with open('/Users/test/Desktop/demo.txt', 'rb') as file:
content = file.read()
# Calculate the MD5 hash of the content to be uploaded.
content_md5 = oss2.utils.content_md5(content)
print('content_md5', content_md5)
# Include the 'Content-MD5' header in the upload request. The server verifies the MD5 hash of the uploaded content to ensure its integrity and correctness.
headers = dict()
headers['Content-MD5'] = content_md5
bucket.put_object(object_name, content, headers=headers)
MD5 doğrulaması put_object, append_object, post_object ve upload_part işlemlerinde desteklenir.
CRC-64 doğrulaması
Veri doğrulaması için Döngüsel Yedeklilik Denetimi (CRC) kullanırken aşağıdaki hususlara dikkat edin:
CRC-64 doğrulaması put_object, get_object, append_object ve upload_part işlemlerinde desteklenir. Dosya yüklemelerinde CRC doğrulaması varsayılan olarak etkindir. İstemcinin hesapladığı CRC değeri sunucunun döndürdüğü CRC değeriyle eşleşmezse InconsistentError özel durumu oluşur.
Aralık indirmeleri CRC-64 doğrulamasını desteklemez.
CRC-64 doğrulaması CPU kaynaklarını tüketir ve yükleme ile indirme hızlarını etkileyebilir.
-
İndirmeler için CRC-64 doğrulaması
Aşağıdaki kod, dosya indirirken CRC-64 veri bütünlüğü doğrulamasının nasıl yapılacağını gösterir:
# -*- coding: utf-8 -*- import oss2 from oss2.credentials import EnvironmentVariableCredentialsProvider # Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider()) # Specify the Endpoint for the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. endpoint = "https://oss-cn-hangzhou.aliyuncs.com" # Specify the region that corresponds to the Endpoint, for example, cn-hangzhou. Note that this parameter is required for v4 signatures. region = "cn-hangzhou" # Replace examplebucket with the name of your bucket. bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region) # Specify the full path of the object. The full path cannot include the bucket name. object_name = 'yourObjectName' # Check whether CRC validation is enabled by default. print('bucket.enable-crc:', bucket.enable_crc) # The return value of bucket.get_object is a file-like object and is also iterable. object_stream = bucket.get_object(object_name) print(object_stream.read()) # Because the get_object operation returns a stream, you must call read() before you can calculate the CRC checksum of the returned object data. Therefore, perform CRC validation after you call this operation. if object_stream.client_crc != object_stream.server_crc: print("The CRC checksum between client and server is inconsistent!") -
Ekleme yüklemeleri için CRC-64 doğrulaması
Ekleme yüklemelerinde init_crc parametresini belirtirseniz CRC-64 doğrulaması varsayılan olarak etkinleşir.
# -*- coding: utf-8 -*- import oss2 from oss2.credentials import EnvironmentVariableCredentialsProvider # Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider()) # Specify the Endpoint for the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. endpoint = "https://oss-cn-hangzhou.aliyuncs.com" # Specify the region that corresponds to the Endpoint, for example, cn-hangzhou. Note that this parameter is required for v4 signatures. region = "cn-hangzhou" # Replace examplebucket with the name of your bucket. bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region) object_name = "yourAppendObjectName" first_content = "yourFirstContent" second_content = "yourSecondContent" # First append upload. # If init_crc is specified, the SDK performs CRC validation on the returned result by default. result = bucket.append_object(object_name, 0, first_content, init_crc=0) # Second append upload. # Set init_crc to the CRC value of the uploaded data. result = bucket.append_object(object_name, result.next_position, second_content, init_crc=result.crc)
İlgili konular
Veri doğrulamasına ilişkin tam örnek kod için bkz. GitHub örneği.