php、shell 脚本for 七牛云离线下载

下载谷歌上面的android studio的程序包,速度实在太慢了。然后就花了一个晚上来搞了个七牛云离线下载的功能。。。
弄了个 for codeigniter 类库的,还有一个是 php+shell 的(把 curl 功能弄到了 shell 来处理)

** 原创于:https://www.skiy.net/201605084021.html **

upload.php

<?php
/**
    @author: Skiychan <dev@skiy.net>
    @link    : https://www.zzzzy.com
*/

function urlsafe_base64_encode($data) {
    $find = array('+', '/');
    $replace = array('-', '_');
    return str_replace($find, $replace, base64_encode($data));
}

function hmac_sha1($str, $key) {
    return hash_hmac("sha1", $str, $key, true);
}

// 原创于:https://www.zzzzy.com/201605084021.html 

//七牛云配置,请到七牛云上面申请帐号并填写密钥和bucket
define("AccessKey", "AccessKey_12345689"); 
define("SecretKey", "SecretKey_98764231");
define("BUCKET", "Your bucket");

$host = "http://iovip.qbox.me";

$uploadFormat = "%s/fetch/%s/to/%s";

$url = $argv[1];  //第二个参数是url
$encodeURI = urlsafe_base64_encode($url);
echo "url:{$url}, \r\nencodeURI:\r\n{$encodeURI}\r\n\r\n";

$entry = isset($argv[2]) ? BUCKET.':'.$argv[2] : BUCKET;

$encodedEntryURI = urlsafe_base64_encode($entry);
echo "entry:{$entry}, \r\nencodedEntryURI:\r\n{$encodedEntryURI}\r\n\r\n";

$uploadURL = sprintf($uploadFormat, $host, $encodeURI, $encodedEntryURI);

$parse = parse_url($uploadURL);
$path = $parse['path'];
$signingStr = isset($parse['query']) ? $path."?".$parse['query']."\n" : $path."\n";

$sign = hmac_sha1($signingStr, SecretKey);
$encodedSign = urlsafe_base64_encode($sign);

$accessToken = AccessKey.":{$encodedSign}";

echo "AccessToken:\r\n{$accessToken}\r\n\r\n";

echo "UploadURL:\r\n{$uploadURL}\r\n\r\n";

upload.sh

curl -i \
     -o - \
     -H 'Content-Type: application/x-www-form-urlencoded' \
     -H "Authorization: QBox $2" \
     -X POST \
     $1
#     "http://iovip.qbox.me/fetch/$1/to/$2"

上传操作:

# php upload.php 下载链接 保存到七牛后的名字
php upload.php http://dl.google.com/android/android-sdk_r24.4.1-linux.tgz android-sdk-linux.tgz
# 取得 accessToken 和 请求的 post 链接

# sh upload.sh 上面取得的 uploadURL 和 AccessToken
sh upload.sh $posturl $accesstoken
点赞