All Products
Search
Document Center

Object Storage Service:Simple upload (PHP SDK V1)

Last Updated:Mar 20, 2026

Simple upload lets you upload a string or a local file to an OSS bucket as an object. Objects uploaded this way are limited to 5 GB. For larger files, use [multipart upload]().

Prerequisites

Before you begin, ensure that you have:

  • An OSS bucket

  • The oss:PutObject permission on the target bucket (granted via RAM policies or bucket policies)

  • The OSS PHP SDK V1 installed with autoload configured

  • OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET set as environment variables

Permissions

By default, RAM users and RAM roles have no permissions. The Alibaba Cloud account owner or an administrator must grant the required actions.

APIRequired actionWhen it applies
PutObjectoss:PutObjectEvery upload
PutObjectoss:PutObjectTaggingUpload with object tags (x-oss-tagging)
PutObjectkms:GenerateDataKey, kms:DecryptUpload with server-side encryption using KMS (X-Oss-Server-Side-Encryption: KMS)

Upload a string

Use putObject() to upload an in-memory string as an object.

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

// Load credentials from environment variables (OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET).
$provider = new EnvironmentVariableCredentialsProvider();

// Replace with the endpoint for your bucket's region.
// Example: https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou).
$endpoint = "yourEndpoint";

$bucket = "examplebucket";
$object = "exampledir/exampleobject.txt";   // Object path — do not include the bucket name.
$content = "Hello OSS";                      // String content to upload.

// Optional: set object ACL or custom metadata headers.
$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");

Key parameters

ParameterDescriptionExample
$endpointEndpoint for your bucket's regionhttps://oss-cn-hangzhou.aliyuncs.com
$bucketBucket nameexamplebucket
$objectObject path in the bucket (no bucket name prefix)exampledir/exampleobject.txt
$contentString content to uploadHello OSS
$options(Optional) Additional headers: object ACL, custom metadata, tags, encryptionSee [PutObject]()
"region"Region ID matching your endpointcn-hangzhou

Upload a file

Use uploadFile() to upload a local file to a bucket.

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

// Load credentials from environment variables (OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET).
$provider = new EnvironmentVariableCredentialsProvider();

// Replace with the endpoint for your bucket's region.
$endpoint = "yourEndpoint";

$bucket   = "examplebucket";
$object   = "exampledir/exampleobject.txt";       // Destination object path in OSS.
$filePath = "D:\\localpath\\examplefile.txt";     // Full local file path.
                                                   // If omitted, the SDK looks in the project root.

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");

Key parameters

ParameterDescriptionExample
$endpointEndpoint for your bucket's regionhttps://oss-cn-hangzhou.aliyuncs.com
$bucketBucket nameexamplebucket
$objectDestination object path in OSSexampledir/exampleobject.txt
$filePathFull local file path. If blank, the SDK uses the project root directory.D:\localpath\examplefile.txt

Usage notes

  • Examples in this topic use the public endpoint for the China (Hangzhou) region (https://oss-cn-hangzhou.aliyuncs.com). If you are accessing OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. See Regions and endpoints.

  • These examples initialize OssClient with an OSS endpoint. For other initialization options — custom domain, Security Token Service (STS) credentials — see Create an OssClient instance.

  • Simple upload supports objects up to 5 GB. For larger objects, use multipart upload.

What's next

  • [Multipart upload]() — upload objects larger than 5 GB in parts

  • [Resumable upload]() — resume interrupted uploads automatically

  • [Object metadata]() — set Content-Type, custom metadata, and other headers at upload time