【问题标题】:Migrating Parse files from Gridstore to S3将 Parse 文件从 Gridstore 迁移到 S3
【发布时间】:2017-01-24 19:48:10
【问题描述】:

我们将数据(不含文件)迁移到 mLab 和 Heroku。所以旧文件仍在Parse中。

从那时起,任何添加的新文件都会进入 Gridstore,这是 mLab 的默认文件存储。

现在我使用 sashido

将旧解析文件从 Parse 迁移到 S3 存储桶

文件已迁移并可使用 Heroku 中的 S3Adapter 访问。

但现在无法访问 Gridstore 上的文件。如何将它们迁移到同一个 S3 存储桶 并更改 mLab 中的引用?

【问题讨论】:

    标签: parse-platform


    【解决方案1】:

    也许您对我尝试过的解决方案感兴趣。这不是一个简单的操作,但是我用我的解析服务器配置成功迁移了3个数据库。

    它基于贯穿每个对象的 PHP 脚本(使用 Parse PHP SDK),它从 Parse.com 获取文件并在您自己的服务器中设置它(使用您的任何适配器配置)。

    脚本如下:

    <?php
        ini_set('display_errors', 1);
        ini_set('display_startup_errors', 1);
        error_reporting(E_ALL);
        date_default_timezone_set('America/New_York');
        
        $fileField = $argv[1];
        $class     = $argv[2];
        
        require_once 'vendor/autoload.php';
        use Parse\ParseObject;
        use Parse\ParseQuery;
        use Parse\ParseACL;
        use Parse\ParsePush;
        use Parse\ParseUser;
        use Parse\ParseInstallation;
        use Parse\ParseException;
        use Parse\ParseAnalytics;
        use Parse\ParseFile;
        use Parse\ParseCloud;
        use Parse\ParseClient;
        
        $app_id     = "******";
        $rest_key   = "******";
        $master_key = "******";
        
        ParseClient::initialize($app_id, $rest_key, $master_key);
        ParseClient::setServerURL('http://localhost:1338/', 'parse');
        
        $query = new ParseQuery($class);
        $query->ascending("createdAt"); // it's just my preference
        $query->exists($fileField);
        $query->limit(1);
        
        $count = $query->count();
        for ($i = 0; $i < $count; $i = $i + 1) {
            try {
                $query->skip($i);
                // get Entry
                $entryWithFile = $query->first();
                
                // get file
                $parseFile = $entryWithFile->get($fileField);
                // filename
                $fileName  = $parseFile->getName();
    
                // if the file is hosted in Parse, do the job, otherwise continue with the next one
                if (strpos($fileName, "tfss-") === false) {
                    echo "\nThis is already an internal file, skipping...";
                    continue;
                }
                
                $newFileName = str_replace("tfss-", "", $fileName);
                $binaryFile  = file_get_contents($parseFile->getURL());
                
                $newFile = ParseFile::createFromData($binaryFile, $newFileName);
                
                $entryWithFile->set($fileField, $newFile);
                $entryWithFile->save(true);
                
                echo "\nFile saved\n";
            }
            catch (Exception $e) {
                // The conection with mongo or the server could be off for some second, let's retry it ;)
                sleep(10);
                continue;
            }
        }
        
        echo "\n";
        echo "END!";
        
    ?>
    

    正确设置您的解析网址。

    假设您想从类 _User 迁移文件,字段为 imageProfile,因此请确保传递 $fileField = "imageProfile"; $class = "_User"。

    为每个类的任何字段运行该代码。

    我做了一个愚蠢的并行工作解决方案,例如跳过for循环中的步骤:

    <?php
        ini_set('display_errors', 1);
        ini_set('display_startup_errors', 1);
        error_reporting(E_ALL);
        date_default_timezone_set('America/New_York');
        
        $index     = $argv[1];
        $of        = $argv[2];
        $fileField = $argv[3];
        $class     = $argv[4];
        
        require_once 'vendor/autoload.php';
        use Parse\ParseObject;
        use Parse\ParseQuery;
        use Parse\ParseACL;
        use Parse\ParsePush;
        use Parse\ParseUser;
        use Parse\ParseInstallation;
        use Parse\ParseException;
        use Parse\ParseAnalytics;
        use Parse\ParseFile;
        use Parse\ParseCloud;
        use Parse\ParseClient;
        
        $app_id     = "********";
        $rest_key   = "********";
        $master_key = "********";
        
        ParseClient::initialize($app_id, $rest_key, $master_key);
        ParseClient::setServerURL('http://localhost:1338/', 'parse');
        
        $query = new ParseQuery($class);
        $query->ascending("createdAt");
        $query->exists($fileField);
        $query->limit(1);
        
        $count = $query->count();
        for ($i = $index; $i < $count; $i = $i + $of) {
            try {
                $query->skip($i);
                // get Entry
                $entryWithFile = $query->first();
                
                // get file
                $parseFile = $entryWithFile->get($fileField);
                // filename
                $fileName  = $parseFile->getName();
    
                // if the file is hosted in Parse, do the job, otherwise continue with the next one
                if (strpos($fileName, "tfss-") === false) {
                    echo "\nThis is already an internal file, skipping...";
                    continue;
                }
                
                $newFileName = str_replace("tfss-", "", $fileName);
                $binaryFile  = file_get_contents($parseFile->getURL());
                
                $newFile = ParseFile::createFromData($binaryFile, $newFileName);
                
                $entryWithFile->set($fileField, $newFile);
                $entryWithFile->save(true);
                
                echo "\nFile saved\n";
            }
            catch (Exception $e) {
                // The conection with mongo or the server could be off for some second, let's retry it ;)
                sleep(10);
                continue;
            }
        }
        
        echo "\n";
        echo "END!";
        
    ?>
    

    所以如果像以前一样配置$fileField和$class,就可以开3个线程运行:

    php migrator.php 0 3 "imageProfile" "_User"

    php migrator.php 1 3 "imageProfile" "_User"

    php migrator.php 2 3 "imageProfile" "_User"

    所以你会像这样运行循环:

    对象 0、3、6

    对象 1、4、7

    对象 2、5、8

    祝你好运,快点!过几天就关门了。

    【讨论】:

      猜你喜欢
      • 2016-05-25
      • 2020-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 2014-04-25
      • 2011-08-25
      相关资源
      最近更新 更多