2017 © Pedro Peláez
 

library scs

PHP SDK for 新浪云存储

image

cloudmario/scs

PHP SDK for 新浪云存储

  • Friday, May 4, 2018
  • by CloudSide
  • Repository
  • 3 Watchers
  • 15 Stars
  • 224 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 10 Forks
  • 4 Open issues
  • 1 Versions
  • 45 % Grown

The README.md

SinaCloudStorage-SDK-PHP

PHP SDK for 新浪云存储, (*1)

Requirements

  • PHP >= 5.2.0
  • [PHP cURL]

Installation

  • 可以使用composer.phar进行安装, composer require cloudmario/scs:dev-master 详细信息:https://packagist.org/packages/cloudmario/scs
  • 也可以直接引用源码

Usage

OO method (e,g; $scs->getObject(...)):, (*2)

$scs = new SCS($scsAccessKey, $scsSecretKey);

Statically (e,g; SCS::getObject(...)):, (*3)

SCS::setAuth($scsAccessKey, $scsSecretKey);

Use exceptions, (*4)

SCS::setExceptions(true);

try
{
    $response = SCS::getObjectInfo($bucket, $uri);
    print_r($response);
}
catch(SCSException $e)
{
    echo $e->getMessage();
}

Object Operations

Uploading objects

Put an object from a file:, (*5)

SCS::putObject(SCS::inputFile($file, false), $bucketName, $uploadName, SCS::ACL_PUBLIC_READ);

Put an object from a string and set its Content-Type:, (*6)

SCS::putObject($string, $bucketName, $uploadName, SCS::ACL_PUBLIC_READ, array(), array('Content-Type' => 'text/plain'));

Put an object from a resource (buffer/file size is required - note: the resource will be fclose()'d automatically):, (*7)

SCS::putObject(SCS::inputResource(fopen($file, 'rb'), filesize($file)), $bucketName, $uploadName, SCS::ACL_PUBLIC_READ);

Also use:, (*8)

SCS::putObjectFile($file, $bucket, $uri);
SCS::putObjectString($string, $bucket, $uri);

Retrieving objects

Get an object:, (*9)

SCS::getObject($bucketName, $uploadName);

Get an object info (meta):, (*10)

SCS::getObjectInfo($bucket, $uri, $returnInfo = true);

You can also:, (*11)

SCS::getMeta($bucket, $uri);

Save an object to file:, (*12)

SCS::getObject($bucketName, $uploadName, $saveName);

Save an object to a resource of any type:, (*13)

SCS::getObject($bucketName, $uploadName, fopen('savefile.txt', 'wb'));

Copying and deleting objects

Copy an object:, (*14)

SCS::copyObject($srcBucket, $srcName, $bucketName, $saveName, $metaHeaders = array(), $requestHeaders = array());

Delete an object:, (*15)

SCS::deleteObject($bucketName, $uploadName);

Bucket Operations

Get a list of buckets:, (*16)

SCS::listBuckets();  // Simple bucket list
SCS::listBuckets(true);  // Detailed bucket list

Create a bucket:, (*17)

SCS::putBucket($bucketName);

Get the contents of a bucket (list objects):, (*18)

SCS::getBucket($bucketName);

Delete an empty bucket:, (*19)

SCS::deleteBucket($bucketName);

Get bucket meta:, (*20)

SCS::getMeta($bucketName);

ACL Operations

Get ACL, (*21)

SCS::getAccessControlPolicy($bucket); //for bucket
SCS::getAccessControlPolicy($bucket, $uri); //for object

Set ACL, (*22)

$acp = array(

    'GRPS000000ANONYMOUSE' => array('read'),
    'GRPS0000000CANONICAL' => array('read', 'write'),
    'SINA0000001001HBK300' => array('read', 'write', 'read_acp', 'write_acp')
);

SCS::setAccessControlPolicy($bucket, $uri, $acp); //for object
SCS::setAccessControlPolicy($bucket, '', $acp); //for bucket

Examples

基本示例:

  • 文件: examples/example.php

表单上传

  • 文件: examples/example-form.php

实现一个Wrapper

  • 文件: examples/example-wrapper.php
mkdir("scs://{$bucketName}");

file_put_contents("scs://{$bucketName}/test.txt", "http://weibo.com/smcz !");

file_get_contents("scs://{$bucketName}/test.txt")

foreach (new DirectoryIterator("scs://{$bucketName}") as $b) {

    echo "\t" . $b . "\n";
}

unlink("scs://{$bucketName}/test.txt");

rmdir("scs://{$bucketName}");

大文件分片上传

  • 文件: examples/example-multipart-upload.php
$fp = fopen($file, 'rb');
SCS::setExceptions(true);

try
{
    //初始化上传
    $info = SCS::initiateMultipartUpload($bucket, $object, SCS::ACL_PUBLIC_READ);
    $uploadId = $info['upload_id'];
    $fp = fopen($file, 'rb');
    $i = 1;
    $part_info = array();

    while (!feof($fp)) {

        //上传分片  
        $res = SCS::putObject(SCS::inputResourceMultipart($fp, 1024*512, $uploadId, $i), $bucket, $object); 
        if (isset($res['hash']))
        {   
            echo 'Part: ' . $i . " OK! \n";

            $part_info[] = array(

                'PartNumber' => $i,
                'ETag' => $res['hash'],
            );
        }
        $i++;
    }

    //列分片
    $parts = SCS::listParts($bucket, $object, $uploadId);
    //print_r($parts);
    //print_r($part_info);

    if (count($parts) > 0 && count($parts) == count($part_info)) {

        foreach ($parts as $part_number => $part) {

            //echo $part['etag'] . "\n";
            //echo $part_info[$k]['ETag'] . "\n";

            if ($part['etag'] != $part_info[$part_number-1]['ETag']) {

                exit('分片不匹配');
                break;
            }
        }

        //合并分片
        echo "开始合并\n";
        SCS::completeMultipartUpload($bucket, $object, $uploadId, $part_info);
        echo "上传完成\n";  
        fclose($fp);
    }
}
catch(SCSException $e)
{
    echo $e->getMessage();
}

The Versions

04/05 2018

dev-master

9999999-dev https://github.com/CloudSide/SinaCloudStorage-SDK-PHP

PHP SDK for 新浪云存储

  Sources   Download

MIT

The Requires

  • php >=5.2.0

 

sina scs sinastorage