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')
    ?>



  • 相关阅读:
    Binding to a Service
    UML类图几种关系的总结
    阿里云调试
    Serif和Sans-serif字体的区别
    从Log4j迁移到LogBack的理由
    logback
    java 解析json格式数据(转)
    开源Web测试工具介绍
    GET乱码以及POST乱码的解决方法
    单元测试框架TestNg使用总结
  • 原文地址:https://www.cnblogs.com/liuwenbohhh/p/4679791.html
Copyright © 2011-2022 走看看