Configuration
The OssClient can be configured in several ways depending on your environment and security requirements.
Constructor Signatures
The OssClient constructor supports two primary styles: Arguments style and Array style.
Arguments Style
Used for traditional AccessKey authentication.
public function __construct($accessKeyId, $accessKeySecret, $endpoint, $isCName = false, $securityToken = NULL, $requestProxy = NULL)
$isCName: Set totrueif you are using a custom domain bound to the bucket.$securityToken: Provide this if using temporary credentials from STS.$requestProxy: Formatproxy://user:pass@host:port.
Array Style (Recommended for Advanced Setup)
Introduced to support more flexible configuration, including V4 signatures.
$config = [
'endpoint' => 'oss-cn-hangzhou.aliyuncs.com',
'provider' => $credentialsProvider,
'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V4,
'region' => 'cn-hangzhou',
'checkObjectEncoding' => true,
];
$ossClient = new OssClient($config);
Credentials Providers
Instead of hardcoding AccessKeys, you can use CredentialsProviders.
Static Provider
use OSS\Credentials\StaticCredentialsProvider;
$provider = new StaticCredentialsProvider($id, $secret, $token);
Environment Variable Provider
This looks for OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET, and OSS_SESSION_TOKEN environment variables.
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
$provider = new EnvironmentVariableCredentialsProvider();
Signatures (V1 vs V4)
By default, the SDK uses V1 signatures. As of version 2.7.0, V4 signatures are supported and recommended for enhanced security. Note that V4 requires the region parameter.
$config = [
'endpoint' => 'oss-cn-hangzhou.aliyuncs.com',
'provider' => $provider,
'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V4,
'region' => 'cn-hangzhou',
];
Timeouts and Retries
You can adjust HTTP behavior after initialization:
$ossClient->setTimeout(3600); // Socket timeout in seconds
$ossClient->setConnectTimeout(10); // Connection timeout in seconds
$ossClient->setMaxTries(3); // Number of retries on 500 errors