Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Basit yükleme (PHP SDK V1)

Son Güncelleme:Oct 01, 2026

Basit yükleme ile içeriği bir string olarak veya yerel bir dosyadan yükleyebilirsiniz. Bu konuda string yükleme ve dosya yükleme için kod örnekleri sunulur.

Kullanım notları

  • Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılır. Aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'e erişmek için dahili uç nokta kullanın. Desteklenen bölgeler ve uç noktalar hakkında ayrıntılı bilgi için bkz. Bölgeler ve uç noktalar.

  • Bu konuda bir OSS uç noktası ile OssClient örneğinin nasıl oluşturulacağı gösterilir. Özel etki alanı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. OssClient oluşturun.

  • Basit yükleme ile yüklenen bir nesnenin boyutu 5 GB'yi aşamaz.

İzinler

Varsayılan olarak Alibaba Cloud hesabının tam izinleri bulunur. Bir Alibaba Cloud hesabı altındaki RAM kullanıcıları veya RAM rolleri varsayılan olarak herhangi bir izne sahip değildir. Alibaba Cloud hesabı veya hesap yöneticisi, işlem izinlerini RAM ilkeleri veya Bucket Policy aracılığıyla vermelidir.

API

Eylem

Açıklama

PutObject

oss:PutObject

Bir nesne yükler.

oss:PutObjectTagging

Bir nesne yüklerken x-oss-tagging üstbilgisini kullanarak nesne etiketleri belirtiyorsanız gereklidir.

kms:GenerateDataKey

Bir nesne yüklerken X-Oss-Server-Side-Encryption: KMS üstbilgisi KMS olarak ayarlanmışsa gereklidir.

kms:Decrypt

String yükleme

Aşağıdaki kod örneği, examplebucket paketindeki exampledir/exampleobject.txt nesnesine bir string'in nasıl yükleneceğini 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;
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$provider = new EnvironmentVariableCredentialsProvider();
// Set yourEndpoint to the endpoint of your bucket's region. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "yourEndpoint";
// Set the bucket name. For example, examplebucket.
$bucket= "examplebucket";
// Set the full path of the object. The path must not contain the bucket name. For example, exampledir/exampleobject.txt.
$object = "exampledir/exampleobject.txt";
// The string to upload.
$content = "Hello OSS";

// You can specify headers when you upload the string. For example, set the ACL to private or add custom metadata.
$options = array(
    OssClient::OSS_HEADERS => array(
        'x-oss-object-acl' => 'private',
        'x-oss-meta-info' => 'yourinfo'
    ),
);
try{
    $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
    );
    $ossClient = new OssClient($config);

    $ossClient->putObject($bucket, $object, $content, $options);
} catch(OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}
print(__FUNCTION__ . "OK" . "\n");

Dosya yükleme

Aşağıdaki örnek kod, examplefile.txt adlı yerel bir dosyanın examplebucket paketinin exampledir dizinine nasıl yükleneceğini gösterir. Yerel dosya yüklendikten sonra exampledir dizinindeki nesnenin adı exampleobject.txt olur.

<?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;

// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$provider = new EnvironmentVariableCredentialsProvider();
// Set yourEndpoint to 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 = "yourEndpoint";
// Set the bucket name. For example, examplebucket.
$bucket= "examplebucket";
// Set the full path of the object. The path cannot contain the bucket name. For example, exampledir/exampleobject.txt.
$object = "exampledir/exampleobject.txt";
// Set the full path to the local file. For example, D:\\localpath\\examplefile.txt. If you omit the path, the file is uploaded from the project's root directory.
$filePath = "D:\\localpath\\examplefile.txt";

try{
    $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
    );
    $ossClient = new OssClient($config);

    $ossClient->uploadFile($bucket, $object, $filePath);
} catch(OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}
print(__FUNCTION__ . "OK" . "\n");