You can call the AppendObject operation to append content to existing appendable objects.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
To use append upload, you must have the
oss:PutObject
permission. For more information, see Attach a custom policy to a RAM user.If the object to which you want to append content does not exist, an appendable object is created when you call the AppendObject operation.
If the object to which you want to append content exists:
If the object is an appendable object and the specified position from which the append operation starts is equal to the current object size, the data is appended to the end of the object.
If the object is an appendable object and the specified position from which the append operation starts is not equal to the current object size, the PositionNotEqualToLength error is returned.
If the object is not an appendable object, the ObjectNotAppendable error is returned.
The CopyObject operation cannot be performed on appendable objects.
Upload strings by using append upload
The following sample code provides an example on how to use append upload to upload strings in sequence to the srcexampleobject.txt object in the examplebucket 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\OssClient;
use OSS\Core\OssException;
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$accessKeyId = getenv("OSS_ACCESS_KEY_ID");
$accessKeySecret = getenv("OSS_ACCESS_KEY_SECRET");
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. For more information about regions and endpoints, see the "Regions and endpoints" topic.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the name of the bucket. For more information about bucket naming, see the "Bucket naming conventions" topic.
$bucket= "examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path. For more information about object naming, see Terms.
$object = "srcexampleobject.txt";
// Specify the strings that you want to upload by using append upload. Specify that the object content obtained after the first append upload, second append upload, and third append upload is Hello OSS, Hi OSS, and OSS OK, respectively.
$content_array = array('Hello OSS', 'Hi OSS', 'OSS OK');
$options = array(
'headers' => array(
// Specify the caching behavior of the web page when the object is downloaded.
// 'Cache-Control' => 'no-cache',
//Specify the name of the object when the object is downloaded.
// 'Content-Disposition' => 'attachment;filename=oss_download.jpg',
// Specify the encoding format for the content of the object.
// 'Content-Encoding' => 'utf-8',
// Specify the expiration time.
// 'Expires' => 'Fri, 31 Dec 2021 16:57:01 GMT',
// Specify whether to overwrite the object with the same name in the append upload. In this example, this parameter is set to true. The value true indicates that the operation does not overwrite an object that has the same name.
// 'x-oss-forbid-overwrite' => 'true',
// Specify the server-side encryption method that you want to use to encrypt each part of the object.
// 'x-oss-server-side-encryption'=> 'AES256',
// Specify the algorithm that is used to encrypt the object.
// 'x-oss-server-side-data-encryption'=>'SM4',
// Specify the storage class of the object.
// 'x-oss-storage-class' => 'Standard',
// Specify the access control list (ACL) of the object.
// 'x-oss-object-acl' => 'private',
),
);
try{
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
// Perform the first append operation. The position for the first append upload is 0, and the position for the next append upload is included in the response. The position from which the next append upload starts is the current length of the object.
$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");
Upload a local file by using append upload
The following sample code provides an example on how to use append upload to upload the content of the examplefilea.txt, examplefileb.txt, and examplefilec.txt files in sequence to the exampleobject.txt object in the examplebucket 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\OssClient;
use OSS\Core\OssException;
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$accessKeyId = getenv("OSS_ACCESS_KEY_ID");
$accessKeySecret = getenv("OSS_ACCESS_KEY_SECRET");
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the name of the bucket.
$bucket= "examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path.
$object = "exampleobject.txt";
// Specify the full paths of the local files to upload. By default, if you do not specify the full path of a local file, the local file is uploaded from the path of the project to which the sample program belongs.
$filePath = "D:\\localpath\\examplefilea.txt";
$filePath1 = "D:\\localpath\\examplefileb.txt";
$filePath2 = "D:\\localpath\\examplefilec.txt";
try{
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
// Perform the first append operation. The position for the first append upload is 0, and the position for the next append upload is included in the response. The position from which the next append upload starts is the current length of the object.
$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");
References
For more information about the API operation that you can call to perform append upload, see AppendObject.