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



  • 相关阅读:
    IOS-UI- UIScrollView 滚动视图(1)
    git实用攻略(二)
    git实用攻略
    一些技术发展和职业规划的建议
    Spring Data JPA 事务
    配置Slf4j依赖,桥接各种多个日志组件(排除commons-logging依赖的影响)
    Apache Shiro去掉URL中的JSESSIONID
    浏览器缓存介绍之sessionStorage、localStorage、Cookie
    【转】大数据批处理框架 Spring Batch全面解析
    ssl和https协议详解
  • 原文地址:https://www.cnblogs.com/liuwenbohhh/p/4679791.html
Copyright © 2020-2023  润新知