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:PutObjectpermission on the target bucket (granted via RAM policies or bucket policies)The OSS PHP SDK V1 installed with autoload configured
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETset 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.
| API | Required action | When it applies |
|---|---|---|
| PutObject | oss:PutObject | Every upload |
| PutObject | oss:PutObjectTagging | Upload with object tags (x-oss-tagging) |
| PutObject | kms:GenerateDataKey, kms:Decrypt | Upload 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
| Parameter | Description | Example |
|---|---|---|
$endpoint | Endpoint for your bucket's region | https://oss-cn-hangzhou.aliyuncs.com |
$bucket | Bucket name | examplebucket |
$object | Object path in the bucket (no bucket name prefix) | exampledir/exampleobject.txt |
$content | String content to upload | Hello OSS |
$options | (Optional) Additional headers: object ACL, custom metadata, tags, encryption | See [PutObject]() |
"region" | Region ID matching your endpoint | cn-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
| Parameter | Description | Example |
|---|---|---|
$endpoint | Endpoint for your bucket's region | https://oss-cn-hangzhou.aliyuncs.com |
$bucket | Bucket name | examplebucket |
$object | Destination object path in OSS | exampledir/exampleobject.txt |
$filePath | Full 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
OssClientwith 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