JustK
JustK

Reputation: 13

Parse File Migration to AWS

Has anyone had any success migrating files from the Parse S3 Bucket to an S3 Bucket of their own? I have an app that contains many files (images) and I have them serving from both my own S3 Bucket and from the Parse Bucket using the S3 File Adapter but would like to migrate the physical files to my own Bucket on AWS where the app will now be hosted.

Thanks in advance!

Upvotes: 1

Views: 344

Answers (1)

Rafael Ruiz Muñoz
Rafael Ruiz Muñoz

Reputation: 5472

If you've configured your new Parse instance to host files with the S3 file adapter, you could write a PHP script that downloads files from Parse S3 Bucket and upload it to your own. In my example (using Parse-PHP-SDK):

  1. I do a loop through every entry.
  2. I download the binary of that file (hosted in Parse)
  3. I upload it as a new ParseFile (if your server is configured for S3, it will be uploaded to S3 bucket of your own).
  4. Apply that new ParseFile to your entry.
  5. Voilà

        <?php
    
                    require '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 = "AAA";
                    $rest_key = "BBB";
                    $master_key = "CCC";
    
                    ParseClient::initialize( $app_id, $rest_key, $master_key );
                    ParseClient::setServerURL('http://localhost:1338/','parse');
    
                    $query = new ParseQuery("YourClass");
                    $query->descending("createdAt"); // just because of my preference
                    $count = $query->count();
                    for ($i = 0; $i < $count; $i++) {
                            try {
                                    $query->skip($i);
                                    // get Entry
                                    $entryWithFile = $query->first();
                                    // get file
                                    $parseFile = $entryWithFile->get("file");
                                    // filename
                                    $fileName = $parseFile->getName();
                                    echo "\nFilename #".$i.": ". $fileName;
                                    echo "\nObjectId: ".$entryWithFile->getObjectId();
                                    // 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());
                                    // null by default, you don't need to specify if you don't want to.
                                    $fileType = "binary/octet-stream";
                                    $newFile = ParseFile::createFromData($binaryFile, $newFileName, $fileType);
    
                                    $entryWithFile->set("file", $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 ;)
                                    $i = $i - 1;
                                    sleep(10);
                                    continue;
                            }
                    }
    
                    echo "\n";
                    echo "¡FIN!";
    
        ?>
    

Upvotes: 2

Related Questions