2011-08-14 4 views
0

저는 Rackspace API를 사용하여 파일을 RackSpace 클라우드에 업로드했습니다. 하지만이 방법은 느린 편 인 것 같습니다. 파일을 클라우드 (컬, http 어댑터 등)에 업로드하는 더 좋고 빠른 방법이 있습니까?더 빠른 Rackspace 클라우드 업로드

현재 PHP를 사용하여 업로드하고 제공된 API를 사용하고 있습니다.

을 나는 아래의 간단한 PHP 스크립트를 사용하는 경우에만 누락 된 파일을 업로드하고 있습니다 :

+0

당신 사이의 연결을 믿는 이유가 그들은 (상당히 극복 할 수 없습니다) 문제가되지 않습니다합니까? – ceejayoz

+0

네, 폼을 통해 파일을 업로드하지 않았기 때문에 저와 저 사이가 아닌 것으로 확신합니다. 현재 서버에있는 파일을 업로드하기 만하면 브라우저에서 단지 클릭 만하고있었습니다. –

+2

귀하와 다른 사람 사이의 네트워크 문제를 어떤 식으로도 배제하지 않습니다. – ceejayoz

답변

1

는 여기 빨리 만드는 방법을 내 솔루션입니다. 덕분에 나는 단지 클릭 한 번으로 그리고 단지 몇 초 안에에 있습니다.

PHP 소스 코드 :

function UploadMissingFilesToRackFileCDN($file_paths_to_upload, $b_force_upload = false) 
{ 
    include_once("cloudfiles.php"); 

    // Connect to Rackspace 
    $username = cloudfile_username; // username 
    echo "Connecting to CDN..." . date("H:i:s") . "<br>"; ob_flush(); 
    $key = cloudfile_api_key; // api key 
    $auth = new CF_Authentication($username, $key); 
    $auth->authenticate(); 
    $conn = new CF_Connection($auth); 
    echo "&nbsp;&nbsp;&nbsp;&nbsp;Connected!" . date("H:i:s") . "<br>"; ob_flush(); 

    // Get the container we want to use 
    $container_name = 'vladonai';//'test_container'; 
    echo "Obtaining container $container_name..." . date("H:i:s") . "<br>"; ob_flush(); 
    $container = $conn->get_container($container_name); 
    echo "&nbsp;&nbsp;&nbsp;&nbsp;The container is obtained." . date("H:i:s") . "<br>"; ob_flush(); 

    if (!$b_force_upload) 
    { 
     echo "Receiving container objects list..." . date("H:i:s") . "<br>"; ob_flush(); 
     $existing_object_names = $container->list_objects(); 
     $existing_files_count = count($existing_object_names); 
     echo "&nbsp;&nbsp;&nbsp;&nbsp;Objects list obtained: $existing_files_count." . date("H:i:s") . "<br>"; ob_flush(); 
     $existing_object_names_text .= "\r\n"; 
     foreach ($existing_object_names as $obj_name) 
     { 
      $existing_object_names_text .= $obj_name . "\r\n"; 
     } 
    } 

    // upload files to Rackspace 
    $uploaded_file_n = 0; 
    $skipped_file_n = 0; 
    $errors_count = 0; 
    foreach ($file_paths_to_upload as $localfile_path => $file_info) 
    { 
     $filename = basename($localfile_path); 

     if (!file_exists($localfile_path)) 
     { 
      echo "<font color=red>Error! File $localfile_path doesn't exists!</font>" . date("H:i:s") . "<br>"; ob_flush(); 
      $errors_count ++; 
     } else 
     if (is_dir($localfile_path)) 
     { 
      //simply skip it 
     } else 
     if (strpos($existing_object_names_text, "\r\n" . $filename . "\r\n") !== false) 
     { 
      //file is already uploaded to CDN (at least file name is present there). Would be good to have date/size checked, but CDN api has no such feature 
      //echo "<font color=gray>Skipped file $localfile_path - it already exists!</font><br>"; ob_flush(); 
      $skipped_file_n ++; 
     } else 
     { 
      echo "<font color=green>Uploading file $localfile_path (file #$uploaded_file_n)..." . date("H:i:s") . "</font><br>"; ob_flush(); 
      try 
      { 
       $object = $container->create_object($filename); 
       $object->load_from_filename($localfile_path); 
       $uploaded_file_n ++; 
      } 
      catch (Exception $e) 
      { 
       echo "<font color=red>Error! Caught exception: ", $e->getMessage(), " on uploading file <strong>$localfile_path</strong>!</font>" . date("H:i:s") . "<br>"; ob_flush(); 
       $errors_count ++; 
      } 
     } 

    // if ($uploaded_file_n >= 10) 
    //  break; 
    } 
    echo "Done! $uploaded_file_n files uploaded. Disconnecting :)" . date("H:i:s") . "<br>"; ob_flush(); 
    echo "Skipped files: $skipped_file_n<br>"; ob_flush(); 
    if ($errors_count > 0) 
     echo "<font color=red>Erorrs: $errors_count</font><br>"; ob_flush(); 
} 

function UploadChangedImagesToRackFileCDN($b_force_upload = false) 
{ 
    $exclude = array 
    (
     '.', 
     '..', 
     '*.html', 
     '*.htm', 
     '*.php', 
     '*.csv', 
     '*.log', 
     '*.txt', 
     '*.cfg', 
     //'*sub/forum/files/*', 
    ); 
    $files_array_images = get_dirlist("/var/www/html/vladonai.com/images/", '*', $exclude, false); 
    $files_array = array_merge(get_dirlist("/var/www/html/vladonai.com/js/", '*', $exclude, false), $files_array_images); 

    UploadMissingFilesToRackFileCDN($files_array, $b_force_upload); 
} 


function get_dirlist($path, $match = '*', $exclude = array('.', '..'), $b_short_path = true) 
{ 
    $result = array(); 

    if (($handle = opendir($path))) 
    { 
     while (false !== ($fname = readdir($handle))) 
     { 
      $skip = false; 

      if (!empty($exclude)) 
      { 
       if (!is_array($exclude)) 
       { 
        $skip = fnmatch($exclude, $fname) || fnmatch($exclude, $path . $fname); 
       } else 
       { 
        foreach ($exclude as $ex) 
        { 
         if (fnmatch($ex, $fname) || fnmatch($ex, $path . $fname)) 
          $skip = true; 
        } 
       } 
      } 

      if (!$skip && (empty($match) || fnmatch($match, $fname))) 
      { 
       $file_full_path_and_name = $path . $fname; 
       //echo "$file_full_path_and_name<br>"; 
       $b_dir = is_dir($file_full_path_and_name); 
       $b_link = is_link($file_full_path_and_name); 
       $file_size = ($b_dir || $b_link) ? 0 : filesize($file_full_path_and_name); 
       $file_mod_time = ($b_dir || $b_link) ? 0 : filemtime($file_full_path_and_name); 

       $new_result_element = array(); 
       if ($b_short_path) 
        $file_name = str_replace("/var/www/html/vladonai.com/", "", $file_full_path_and_name);//'[' . str_replace("/var/www/html/vladonai.com/", "", $file_full_path_and_name) . ']'; 
       else 
        $file_name = $file_full_path_and_name; 
       $result[$file_name] = array(); 
       $result[$file_name]['size'] = $file_size; 
       $result[$file_name]['modtime'] = $file_mod_time; 

       if ($b_dir && !$b_link) 
       { 
        //recursively enumerate files in sub-directories 
        $result = array_merge(get_dirlist($file_full_path_and_name . "/", $match, $exclude, $b_short_path), $result); 
       } 
      } 
     } 

     closedir($handle); 
    } 

    return $result; 
} 
관련 문제