Append upload lets you add content to the end of an appendable object using the AppendObject operation. Use append upload when you need to write data incrementally to an object without knowing its final size in advance — for example, log files, real-time data streams, or incrementally built files.
AppendObject is designed for single-writer scenarios. If multiple clients append to the same object concurrently, PositionNotEqualToLength errors are likely.Prerequisites
Before you begin, make sure that you have:
An OSS bucket
The
oss:PutObjectpermission on the target object (granted via RAM Policy or bucket policy)The
oss:PutObjectTaggingpermission, if you plan to set object tags viax-oss-taggingThe OSS PHP SDK V1 installed and autoloaded in your project
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETset as environment variables
How it works
The first append must use position 0. Each subsequent append must use the position value returned by the previous call, which equals the object's length after that append.
| Object state | Result |
|---|---|
| Object does not exist | AppendObject creates a new appendable object and appends the content |
| Appendable object, position matches current length | Content is appended to the end of the object |
| Appendable object, position does not match current length | PositionNotEqualToLength exception |
| Non-appendable object (for example, uploaded via simple upload) | ObjectNotAppendable exception |
Use the public endpoint when accessing OSS from outside Alibaba Cloud. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For the full list of regions and endpoints, see Regions and endpoints.
Append a string
The following example appends three strings sequentially to srcexampleobject.txt in examplebucket. The first call uses position 0; each subsequent call passes the position returned by the previous one.
<?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;
// Load credentials from environment variables.
// Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
$provider = new EnvironmentVariableCredentialsProvider();
// Replace with the endpoint for your bucket's region.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
$bucket = "examplebucket";
$object = "srcexampleobject.txt";
// The three strings to append in sequence.
// After all three appends, the object contains: "Hello OSSHi OSSOSS OK"
$content_array = array('Hello OSS', 'Hi OSS', 'OSS OK');
$options = array(
'headers' => array(
// Uncomment to control caching behavior when the object is downloaded.
// 'Cache-Control' => 'no-cache',
// Uncomment to set the filename used when the object is downloaded.
// 'Content-Disposition' => 'attachment;filename=oss_download.jpg',
// Uncomment to set an expiration time for the object.
// 'Expires' => 'Fri, 31 Dec 2021 16:57:01 GMT',
// Uncomment to prevent overwriting an object with the same name.
// 'x-oss-forbid-overwrite' => 'true',
// Uncomment to enable server-side encryption.
// 'x-oss-server-side-encryption'=> 'AES256',
// 'x-oss-server-side-data-encryption'=>'SM4',
// Uncomment to set a storage class.
// 'x-oss-storage-class' => 'Standard',
// Uncomment to set object ACL.
// 'x-oss-object-acl' => 'private',
),
);
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
// First append: position must be 0.
// The return value is the byte offset for the next append.
$position = $ossClient->appendObject($bucket, $object, $content_array[0], 0, $options);
$position = $ossClient->appendObject($bucket, $object, $content_array[1], $position, $options);
$position = $ossClient->appendObject($bucket, $object, $content_array[2], $position, $options);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK\n");Always pass the position returned byappendObject()to the next call. If the position does not match the object's current length — for example, because another writer appended in between — OSS throwsPositionNotEqualToLength.
Optional parameters
| Parameter | Description |
|---|---|
Cache-Control | Caching behavior when the object is downloaded |
Content-Disposition | Filename used when the object is downloaded |
Expires | Expiration time for the object |
x-oss-forbid-overwrite | Set to true to prevent overwriting an object with the same name |
x-oss-server-side-encryption | Server-side encryption method for the object, for example AES256 |
x-oss-server-side-data-encryption | Encryption algorithm for the object data, for example SM4 |
x-oss-storage-class | Storage class of the object, for example Standard |
x-oss-object-acl | Access permissions for the object, for example private |
Append a local file
The following example appends three local files sequentially to exampleobject.txt in examplebucket. Use appendFile() instead of appendObject() when the source is a local file path.
<?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;
// Load credentials from environment variables.
$provider = new EnvironmentVariableCredentialsProvider();
// Replace with the endpoint for your bucket's region.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
$bucket = "examplebucket";
$object = "exampleobject.txt";
// Replace with the actual paths to the local files to append.
$filePath = "D:\\localpath\\examplefilea.txt";
$filePath1 = "D:\\localpath\\examplefileb.txt";
$filePath2 = "D:\\localpath\\examplefilec.txt";
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
// First append: position must be 0.
// Pass the returned position to each subsequent appendFile() call.
$position = $ossClient->appendFile($bucket, $object, $filePath, 0);
$position = $ossClient->appendFile($bucket, $object, $filePath1, $position);
$position = $ossClient->appendFile($bucket, $object, $filePath2, $position);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK\n");What's next
For the full AppendObject API reference, see AppendObject.
To configure access credentials, see Configure access credentials.
To create an OSSClient instance using a custom domain name or Security Token Service (STS), see Configuration examples for common scenarios.