更快的 Rackspace 云上传

发布于 2024-11-29 09:52:44 字数 127 浏览 0 评论 0原文

我已经使用 Rackspace API 将文件上传到 RackSpace 云。但这个方法似乎有点慢。是否有更好或更快的方法将文件上传到云(curl、http 适配器等)?

我目前正在使用 PHP 并使用提供的 API 上传。

I have used to the Rackspace API to upload files to the RackSpace cloud. But this method seems to be a little on the slow side. Is there a better or faster way to upload a file to the cloud(curl, http adapters, etc)?

I am currently uploading with PHP and using the provided API.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你曾走过我的故事 2024-12-06 09:52:44

这是我的解决方案,如何使其快速:

我使用下面的简单 PHP 脚本仅上传丢失的文件。多亏了它,我只需一次点击几秒钟即可完成。

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 "    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 "    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 "    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;
}

Here is my solution how to make it fast:

I'm uploading only missing files using simple PHP script below. Thanks to it I do it in just one click and in just a few seconds.

PHP source code:

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 "    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 "    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 "    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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文