zoukankan      html  css  js  c++  java
  • php 关于使用七牛云存储

    1.首先注册七牛云存储账号 http://www.qiniu.com/

    2.获得密钥

    3.仔细查看文档

    http://developer.qiniu.com/docs/v6/sdk/php-sdk.html#io-put-flow

    4.下载sdk 可以使用composer,也可以直接使用压缩包进行下载

    5.在项目中引入七牛的类包

    6.获得token

    代码:

    <?php
    require_once 'autoload.php';

    use QiniuAuth;

    $accessKey = 'ntL5AciwhaAa35APXKCSlC4KoUKyN77KNPmbHW0K';
    $secretKey = 'x5W3KQikAzHTBYRdezWSMY9XGn0MLR0GQLXRd6X1';
    $auth = new Auth($accessKey, $secretKey);

    $bucket = 'bucket';
    $token = $auth->uploadToken($bucket);
    ?>

    结果:

    string

     'ntL5AciwhaAa35APXKCSlC4KoUKyN77KNPmbHW0K:H_W87vY-abWaHvOKpzVNGdwNUbc=:eyJzY29wZSI6ImJ1Y2tldCIsImRlYWRsaW5lIjoxNDM3OTc3ODAyfQ==' (length=126)

    7.上传字符串

    代码:

    <?php
    require_once 'autoload.php';

    use QiniuAuth;
    use QiniuStorageUploadManager;

    $accessKey = '你的accessKey';
    $secretKey = '你的secretKey';
    $auth = new Auth($accessKey, $secretKey);

    $bucket = 'bucket';

    // 设置put policy的其他参数, 上传回调
    //$opts = array(
    // 'callbackUrl' => 'http://www.callback.com/',
    // 'callbackBody' => 'name=$(fname)&hash=$(etag)'
    // );
    //$token = $auth->uploadToken($bucket, null, 3600, $opts);

    $token = $auth->uploadToken($bucket);
    $uploadMgr = new UploadManager();

    list($ret, $err) = $uploadMgr->put($token, null, 'content string');
    echo " ====> put result: ";
    if ($err !== null) {
    var_dump($err);
    } else {
    var_dump($ret);
    }
    ?>

    结果:

    ====> put result:

    array (size=2)
      'hash' => 

    string

     'FkRvouCaQN6HmCyPmMuBd0OnhiOi' (length=28)
      'key' => 

    string

     'FkRvouCaQN6HmCyPmMuBd0OnhiOi' (length=28)


    8.上传文件
    代码:

    <?php
    require_once 'autoload.php';

    
    

    use QiniuAuth;
    use QiniuStorageUploadManager;

    
    

    $accessKey = '你的accessKey';
    $secretKey = '你的secretKey';
    $auth = new Auth($accessKey, $secretKey);

    
    

    $bucket = 'bucket';

    
    

    // 设置put policy的其他参数, 上传回调
    //$opts = array(
    // 'callbackUrl' => 'http://www.callback.com/',
    // 'callbackBody' => 'name=$(fname)&hash=$(etag)'
    // );
    //$token = $auth->uploadToken($bucket, null, 3600, $opts);

    
    

    $token = $auth->uploadToken($bucket);
    $uploadMgr = new UploadManager();

    
    

    list($ret, $err) = $uploadMgr->putFile($token, null, "desert.jpg");
    echo " ====> putFile result: ";
    if ($err !== null) {
    var_dump($err);
    } else {
    var_dump($ret);
    }
    ?>

    结果:
    ====> putFile result:
    array (size=2)
      'hash' => 
    string
     'FjBCDRqa-yvLYDNYElaa9ENaWc4X' (length=28)
      'key' => 
    string
     'FjBCDRqa-yvLYDNYElaa9ENaWc4X' (length=28)
    9.下载图片(私有,如果公有的话,不用key值)

    <?php
    require_once 'autoload.php';

    use QiniuAuth;

    $accessKey = '你的accessKey';
    $secretKey = '你的secretKey';
    $auth = new Auth($accessKey, $secretKey);

    $baseUrl = 'http://7xkofd.com1.z0.glb.clouddn.com/FtmX4cN-3AWth9A2A-Mq1JXuLPzh';
    $authUrl = $auth->privateDownloadUrl($baseUrl);
    function download_remote_file_with_curl($file_url, $save_to)
    {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch,CURLOPT_URL,$file_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $file_content = curl_exec($ch);
    curl_close($ch);

    $downloaded_file = fopen($save_to, 'w');
    fwrite($downloaded_file, $file_content);
    fclose($downloaded_file);

    }
    download_remote_file_with_curl($authUrl, time().'file.jpg')
    ?>



  • 相关阅读:
    httpcontext in asp.net unit test
    initialize or clean up your unittest within .net unit test
    Load a script file in sencha, supports both asynchronous and synchronous approaches
    classes system in sencha touch
    ASP.NET MVC got 405 error on HTTP DELETE request
    how to run demo city bars using sencha architect
    sencha touch mvc
    sencha touch json store
    sencha touch jsonp
    51Nod 1344:走格子(贪心)
  • 原文地址:https://www.cnblogs.com/liuwenbohhh/p/4679791.html
Copyright © 2011-2022 走看看