MD5 verification checks data integrity during transmission by comparing checksums before and after upload. It is disabled by default because it has a negative impact on OSS performance — enable it only when data integrity validation is critical for your use case.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket in your target region
The OSS PHP SDK V1 installed and configured
The
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables set
Enable MD5 verification on upload
Pass OssClient::OSS_CHECK_MD5 => true in the options array when calling an upload method. The following example enables MD5 verification for a file upload.
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;
// Load credentials from environment variables.
// Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
$provider = new EnvironmentVariableCredentialsProvider();
// Replace with your actual endpoint. This example uses China (Hangzhou).
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
$bucket = "<your-bucket-name>";
$object = "<your-object-name>";
// Enable MD5 verification.
$options = array(OssClient::OSS_CHECK_MD5 => true);
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
$ossClient->uploadFile($bucket, $object, __FILE__, $options);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");Replace the following placeholders:
| Placeholder | Description | Example |
|---|---|---|
<your-bucket-name> | Name of your OSS bucket | my-bucket |
<your-object-name> | Object key (path) in the bucket | uploads/photo.jpg |
Supported operations
MD5 verification is supported for the following SDK operations:
| Operation | Description |
|---|---|
UploadFile | Simple upload |
PutObject | Put object |
AppendObject | Append to an existing object |
AppendFile | Append file content |
MultiuploadFile | Multipart upload |
Usage notes
The public endpoint for the China (Hangzhou) region is used in the example above. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For a full list of regions and endpoints, see Regions and endpoints.
To create an OSSClient instance using a custom domain name or Security Token Service (STS), see Create an OSSClient instance.