Softlayer PHP Client中的Chunked文件上传

时间:2016-06-13 08:24:53

标签: php ibm-cloud-infrastructure

我需要将大文件上传到Softlayer Object Storage。如何使用PHP客户端进行对象存储?

1 个答案:

答案 0 :(得分:0)

这是一个使用SoftLayer Object Storage PHP Client上传文件的php示例:

<?php
require_once ('lib/ObjectStorage/Util.php');

class ObjectStorageSL{

    var $objectStorage;
    public function __construct($host, $username, $password, $options) {

        $this -> objectStorage = new ObjectStorage($host, $username, $password, $options);

    }

    /**
     * This method shows token and url from an object storage
     * @var $objectStorage - Object Storage connection
     */
    function displayTokenUrl() {
        print("Token: " . $this -> objectStorage -> getAuthenticationData() -> authToken . "\n");
        print("Url: " . $this -> objectStorage -> getAuthenticationData() -> objectStorageUrl);
    }

    /**
     * This method uploads a file located in your local machine
     * @var $objectStorage - Object Storage connection
     * @var $containerName - The container's name where you want to upload the object
     * @var $objectName - The object's name that you wish to assign for the file uploaded
     * @var $path - The path where the file is located
     */
    function uploadFile($containerName, $objectName, $path) {
        try {
            $result = $this -> objectStorage -> with($containerName . "/" . $objectName) -> setLocalFile($path) -> create();
            print("\n".$result -> getUrl());
            print("\nThe file has been uploaded");
        } catch(Exception $e) {
            echo "\nError: " . $e -> getMessage();
        }
    }
}


/**
 * Declare Object Storage parameters
 */
$host = 'https://mil01.objectstorage.softlayer.net/auth/v1.0/';
// Declare your username and password for Object Storage connection
$username = 'set me';
$password = 'set me';
$options = array('adapter' => ObjectStorage_Http_Client::SOCKET, 'timeout' => 10);

/**
 * Create Object Storage Connection
 */
$objectStorage = new ObjectStorageSL($host, $username, $password, $options);

/**
 * Display Token and Url
 */
$objectStorage -> displayTokenUrl();
$path = "C:\Project\task.xml";
$objectStorage -> uploadFile("rcvTest", "task1.xml", $path);

我没有使用大于10 MB的文件测试它,但它应该可以工作,如果您遇到麻烦或任何问题请告诉我