OssClient, Object Storage Service (OSS) için PHP istemcisidir. Paketler ve dosyalar gibi OSS kaynaklarını yönetmek için bu istemciyi kullanabilirsiniz. PHP yazılım geliştirme kitini (SDK) kullanarak bir OSS isteği göndermek üzere bir OssClient örneğini ilk kullanıma hazırlamanız ve gerekirse varsayılan yapılandırmalarını değiştirmeniz gerekir.
Bir OssClient Oluşturun
Signature V4 (Önerilir)
Daha güvenli olan Signature V4 algoritmasını kullanın. Bir istemciyi Signature V4 ile ilk kullanıma hazırlarken bir uç nokta ve genel amaçlı bir Alibaba Cloud bölge kimliği belirtmeniz gerekir. Bölge kimliği, isteğin kaynağını belirler. Örneğin, cn-hangzhou belirtin. Ayrıca OssClient::OSS_SIGNATURE_VERSION_V4 bildirimini yapmanız gerekir. OSS PHP SDK'nın 2.7.0 ve sonraki sürümleri Signature V4'ü destekler.
Aşağıdaki kod, bir OSS alan adı ve Signature V4 kullanarak bir OssClient oluşturmayı gösterir. Özel bir alan adı veya Security Token Service (STS) kullanarak OssClient oluşturma gibi diğer senaryolar için bu örnekteki kodu değiştirebilirsiniz.
<?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\Core\OssException;
try {
// Obtain access credentials from environment variables and save them to the provider. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint of 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";
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
// Specify the region that corresponds to the endpoint. For example, specify cn-hangzhou.
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
} catch (OssException $e) {
printf($e->getMessage() . "\n");
return;
}
Signature V1 (Önerilmez)
1 Mart 2025 tarihinden itibaren Alibaba Cloud Object Storage Service (OSS), yeni kullanıcılar için (yani yeni UID'ler için) V1 imzasını sunmamaktadır. 1 Eylül 2025'ten itibaren OSS, V1 imzasını güncellemeyi ve bakımını durduracak ve yeni nesne depolama paketleri için V1 imzasını sunmayacaktır. Hizmet kesintilerini önlemek için en kısa sürede V4 imzasına geçiş yapın. Daha fazla bilgi için bkz. Duyuru.
Bir OSS Alan Adı Kullanarak Bir OssClient Oluşturun
Aşağıdaki kod, bir OSS alan adı kullanarak bir OssClient oluşturmayı gösterir. Farklı bölgelerin OSS alan adları hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.
<?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;
// 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.
$provider = new EnvironmentVariableCredentialsProvider();
// The endpoint of the China (Hangzhou) region is used as an example. Specify an endpoint based on your actual region.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint
);
$ossClient = new OssClient($config);
} catch (OssException $e) {
print $e->getMessage();
}
Özel Bir Alan Adı Kullanarak Bir OssClient Oluşturun
Aşağıdaki kod, özel bir alan adı kullanarak bir OssClient oluşturmayı gösterir. OSS'e özel bir alan adı kullanarak erişme hakkında daha fazla bilgi için bkz. Özel bir alan adı kullanarak OSS'e erişin.
Özel bir alan adı ile listBuckets yöntemini kullanamazsınız.
<?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;
// 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.
$provider = new EnvironmentVariableCredentialsProvider();
// Specify a custom domain name. For example, http://example.com.
$endpoint = "http://example.com";
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"cname" => true
);
$ossClient = new OssClient($config);
} catch (OssException $e) {
print $e->getMessage();
}
STS Kullanarak Bir OssClient Oluşturun
Aşağıdaki kod, STS kullanarak bir OssClient oluşturmayı gösterir.
<?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\Core\OssException;
// Before you run this sample code, make sure that the AccessKey pair (AccessKey ID and AccessKey secret) and security token of STS are configured in the environment variables.
$provider = new EnvironmentVariableCredentialsProvider();
// The endpoint of the China (Hangzhou) region is used as an example. Specify an endpoint based on your actual region.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
);
$ossClient = new OssClient($config);
} catch (OssException $e) {
print $e->getMessage();
}
Örnek RAM Rolü Kullanarak Bir OssClient Oluşturun
Bir Elastic Compute Service (ECS) örneğinden örnek RAM rolü kullanarak OSS'e erişebilirsiniz. Örnek RAM rolü, bir rolü bir ECS örneğiyle ilişkilendirmenize olanak tanır. Bu sayede STS'den alınan geçici kimlik bilgilerini kullanarak örnek içinden OSS'e erişebilirsiniz. Sistem, geçici kimlik bilgilerini otomatik olarak oluşturur ve günceller. Uygulamanız, belirtilen örnek meta veri URL'sinden geçici kimlik bilgilerini alabilir.
Bir örnek RAM rolü kullanarak bir OssClient oluşturmadan önce, SDK'yı Composer ile yüklemek için aşağıdaki komutu çalıştırmanız gerekir.
composer require alibabacloud/credentials
Aşağıdaki kod, bir örnek RAM rolü kullanarak bir OssClient oluşturmayı gösterir.
<?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\CredentialsProvider;
use AlibabaCloud\Credentials\Credential;
use OSS\Credentials\StaticCredentialsProvider;
use OSS\Core\OssException;
use OSS\OssClient;
class AlibabaCloudCredentialsWrapper implements CredentialsProvider{
/**
* @var \OSS\Credentials\Credentials
*/
private $wrapper;
public function __construct($wrapper){
$this->wrapper = $wrapper;
}
public function getCredentials(){
$ak = $this->wrapper->getAccessKeyId();
$sk = $this->wrapper->getAccessKeySecret();
$token = $this->wrapper->getSecurityToken();
return new StaticCredentialsProvider($ak, $sk, $token);
}
}
$ecsRamRole = new Credential(array(
// Specify the credential type. The value is fixed to ecs_ram_role.
'type' => 'ecs_ram_role',
// Specify the role name.
'role_name' => 'EcsRamRoleOssTest',
));
$providerWarpper = new AlibabaCloudCredentialsWrapper($ecsRamRole);
$provider = $providerWarpper->getCredentials();
$config = array(
'provider' => $provider,
// The endpoint of the China (Hangzhou) region is used as an example. Set it to https://oss-cn-hangzhou.aliyuncs.com. For other regions, specify the actual endpoint.
'endpoint'=> 'https://oss-cn-hangzhou.aliyuncs.com'
);
try {
$ossClient = new OssClient($config);
} catch (OssException $e) {
print $e->getMessage();
}
STSAssumeRole Kullanarak Bir OssClient Oluşturun
STSAssumeRole kullanarak bir OssClient oluşturmadan önce, SDK'yı Composer ile yüklemek için aşağıdaki komutu çalıştırmanız gerekir.
composer require alibabacloud/credentials
Aşağıdaki kod, STSAssumeRole kullanarak bir OssClient oluşturmayı gösterir.
<?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\CredentialsProvider;
use AlibabaCloud\Credentials\Credential;
use OSS\Credentials\StaticCredentialsProvider;
use OSS\Core\OssException;
use OSS\OssClient;
class AlibabaCloudCredentialsWrapper implements CredentialsProvider{
/**
* @var \OSS\Credentials\Credentials
*/
private $wrapper;
public function __construct($wrapper){
$this->wrapper = $wrapper;
}
public function getCredentials(){
$ak = $this->wrapper->getAccessKeyId();
$sk = $this->wrapper->getAccessKeySecret();
$token = $this->wrapper->getSecurityToken();
return new StaticCredentialsProvider($ak, $sk, $token);
}
}
$ramRoleArn = new Credential(array(
// Specify the credential type. The value is fixed to ram_role_arn.
'type' => 'ram_role_arn',
// Before you run this sample code, make sure that you have used the AccessKey pair of a RAM user to set the YOUR_ACCESS_KEY_ID and YOUR_ACCESS_KEY_SECRET environment variables.
'access_key_id' => getenv('YOUR_ACCESS_KEY_ID'),
'access_key_secret' => getenv('YOUR_ACCESS_KEY_SECRET'),
// Specify the Alibaba Cloud Resource Name (ARN) of the role to assume. The format is acs:ram::$accountID:role/$roleName.
'role_arn' => 'acs:ram::17464958********:role/ossststest',
// Specify a custom role session name to distinguish different tokens.
'role_session_name' => 'yourRoleSessionName',
// Specify a custom policy.
'policy' => '',
));
$providerWarpper = new AlibabaCloudCredentialsWrapper($ramRoleArn);
$provider = $providerWarpper->getCredentials();
$config = array(
'provider' => $provider,
// The endpoint of the China (Hangzhou) region is used as an example. Set it to https://oss-cn-hangzhou.aliyuncs.com. For other regions, specify the actual endpoint.
'endpoint'=> 'https://oss-cn-hangzhou.aliyuncs.com'
);
try {
$ossClient = new OssClient($config);
var_dump($ossClient);
} catch (OssException $e) {
print $e->getMessage();
}
Bir OssClient Yapılandırın
Proxy sunucusu, bağlantı zaman aşımı ve maksimum bağlantı sayısı gibi parametreleri bir OssClient için yapılandırabilirsiniz.
|
Parametre |
Açıklama |
Yöntem |
|
timeout |
Soket katmanındaki veri iletimi için zaman aşımı süresi. Varsayılan değer 5184000'dir. Birim: saniye. |
$ossClient->setTimeout(60); |
|
connectTimeout |
Bağlantı kurmak için zaman aşımı süresi. Varsayılan değer 10'dur. Birim: saniye. |
$ossClient->setConnectTimeout(600); |
|
maxRetries |
Başarısız bir istek için maksimum yeniden deneme sayısı. Varsayılan değer 3'tür. |
$ossClient->setMaxTries(5); |
|
useSSL |
SSL sertifika doğrulamasının etkinleştirilip etkinleştirilmeyeceğini belirtir. Geçerli değerler:
|
$ossClient->setUseSSL(true); |
<?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;
// 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.
$provider = new EnvironmentVariableCredentialsProvider();
// The endpoint of the China (Hangzhou) region is used as an example. Specify an endpoint based on your actual region.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
// Set the connection timeout period.
$ossClient->setConnectTimeout(300);
// Set the maximum number of retries for failed requests.
$ossClient->setMaxTries(5);
// Set the timeout period for data transmission at the socket layer.
$ossClient->setTimeout(30);
// Specify whether to enable SSL certificate verification.
$ossClient->setUseSSL(true);
} catch (OssException $e) {
print $e->getMessage();
}
Bir Proxy Sunucusu Yapılandırın
PHP 5.3 ve sonraki sürümler proxy sunucu yapılandırmasını destekler.
<?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\Core\OssException;
// Before you run this sample code, make sure that the AccessKey pair (AccessKey ID and AccessKey secret) and security token of STS are configured in the environment variables.
$provider = new EnvironmentVariableCredentialsProvider();
// The endpoint of the China (Hangzhou) region is used as an example. Specify an endpoint based on your actual region.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Set the proxy server address. For example, http://<username>:<password>@<proxy_ip>:<proxy_port>.
$request_proxy = "yourRequestProxy"
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"request_proxy"=> $request_proxy,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
} catch (OssException $e) {
print $e->getMessage();
}