All Products
Search
Document Center

Object Storage Service:Perform append upload by using OSS SDK for PHP

Last Updated:Mar 11, 2024

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 from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, 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 performing append upload

The following sample code provides an example on how to upload strings in sequence by performing append upload 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\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;

// 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. 
$provider = new EnvironmentVariableCredentialsProvider();
// 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 = "srcexampleobject.txt";
// Specify the strings that you want to upload in sequence. 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 downloaded object. 
        // 'Content-Disposition' => 'attachment;filename=oss_download.jpg',
        // Specify the content encoding format of the object. 
        // 'Content-Encoding' => 'utf-8',
        // Specify the validity period of the request. 
        // 'Expires' => 'Fri, 31 Dec 2021 16:57:01 GMT',
        // Specify whether to overwrite the object with the same name when you perform an append upload task. In this example, this parameter is set to true, which indicates that the existing object cannot be overwritten by the object with 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 you want to use 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{
    $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
    );
    $ossClient = new OssClient($config);

    // Perform the first append upload. 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 local files by performing append upload

The following sample code provides an example on how to upload the content of the examplefilea.txt, examplefileb.txt, and examplefilec.txt files in sequence by performing append upload 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\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;

// 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. 
$provider = new EnvironmentVariableCredentialsProvider();
// 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 that you want to upload. By default, if you do not specify the full paths of the local files, the local files are 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{
    $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
    );
    $ossClient = new OssClient($config);
    // Perform the first append upload. 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.