Project

General

Profile

Bug #11241 ยป test-case.php

Marcin Gibula, 03/26/2015 09:21 AM

 
<?php


$IMAGES_DIR = '/home/sh/upload/';

$BUCKET = 'bucketName';
$AWS_HOST = 'endpointurl';
$AWS_ACCESS_KEY = '';
$AWS_SECRET_ACCESS = '';


$AWS_URL = 'http://'.$BUCKET.'.'.$AWS_HOST;


function test_upload($key, $path) {
global $AWS_URL, $BUCKET, $AWS_ACCESS_KEY, $AWS_SECRET_ACCESS;

$expires = strftime('%Y-%m-%dT%H:%M:%SZ', time()+30);

$policy = [
"expiration" => $expires,
"conditions" => [
["bucket" => $BUCKET],
["starts-with", '$key', ""],
["acl" => "public-read"],
["starts-with", '$Content-Type', ""],
['success_action_status' => '201'],
]
];

$policy = base64_encode(json_encode($policy));
$signature = base64_encode(hash_hmac('sha1', $policy, $AWS_SECRET_ACCESS, true));

$data['Signature'] = $signature;
$data['AWSAccessKeyId'] = $AWS_ACCESS_KEY;
$data['Policy'] = $policy;
$data['acl'] = 'public-read';
$data['success_action_status'] = 201;
$data['Key'] = $key;
$data['file'] = "@/".realpath($path);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$AWS_URL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Expect: 100-continue'));
$verbose = fopen('php://temp', 'rw+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);

if(curl_exec ($ch)) {

rewind($verbose);
$verboseLog = stream_get_contents($verbose);
// echo $verboseLog;
// echo($output);
$etag = etag($key);
$md5 = md5_file(realpath($path));

echo $key.' => '.$etag.' = '.$md5.' => '.($etag == $md5? 'true': 'false')."\n";
}

}

function etag($key) {
global $AWS_URL;
$ch = curl_init();
$headers = array();
curl_setopt($ch, CURLOPT_URL,$AWS_URL.'/'.$key);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
if($output) {
$info = curl_getinfo($ch);
$headerSize=$info['header_size'];

$responseHeader = explode("\n",trim(mb_substr($output, 0, $headerSize)));
unset($responseHeader[0]);
$headers = array();
foreach($responseHeader as $line){
list($key,$val) = explode(':',$line,2);
$headers[strtolower($key)] = trim($val);
}
//print_r($aHeaders);
return substr($headers['etag'], 1, -1);
}
}



$dir = opendir($IMAGES_DIR);

while($file = readdir($dir)) {

if(!is_file($IMAGES_DIR.$file)) {
continue;
}

test_upload('testupload/'.$file, $IMAGES_DIR.$file);


}
    (1-1/1)